clang -cc1 -cc1 -triple amd64-unknown-openbsd7.0 -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name Pragma.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model static -mframe-pointer=all -relaxed-aliasing -fno-rounding-math -mconstructor-aliases -munwind-tables -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fcoverage-compilation-dir=/usr/src/gnu/usr.bin/clang/libclangLex/obj -resource-dir /usr/local/lib/clang/13.0.0 -I /usr/src/gnu/usr.bin/clang/libclangLex/../../../llvm/clang/include -I /usr/src/gnu/usr.bin/clang/libclangLex/../../../llvm/llvm/include -I /usr/src/gnu/usr.bin/clang/libclangLex/../include -I /usr/src/gnu/usr.bin/clang/libclangLex/obj -I /usr/src/gnu/usr.bin/clang/libclangLex/obj/../include -D NDEBUG -D __STDC_LIMIT_MACROS -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D LLVM_PREFIX="/usr" -internal-isystem /usr/include/c++/v1 -internal-isystem /usr/local/lib/clang/13.0.0/include -internal-externc-isystem /usr/include -O2 -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-comment -std=c++14 -fdeprecated-macro -fdebug-compilation-dir=/usr/src/gnu/usr.bin/clang/libclangLex/obj -ferror-limit 19 -fvisibility-inlines-hidden -fwrapv -stack-protector 2 -fno-rtti -fgnuc-version=4.2.1 -vectorize-loops -vectorize-slp -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-valloc -fno-builtin-free -fno-builtin-strdup -fno-builtin-strndup -analyzer-output=html -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /home/ben/Projects/vmm/scan-build/2022-01-12-194120-40624-1 -x c++ /usr/src/gnu/usr.bin/clang/libclangLex/../../../llvm/clang/lib/Lex/Pragma.cpp
1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | |
14 | #include "clang/Lex/Pragma.h" |
15 | #include "clang/Basic/Diagnostic.h" |
16 | #include "clang/Basic/DiagnosticLex.h" |
17 | #include "clang/Basic/FileManager.h" |
18 | #include "clang/Basic/IdentifierTable.h" |
19 | #include "clang/Basic/LLVM.h" |
20 | #include "clang/Basic/LangOptions.h" |
21 | #include "clang/Basic/Module.h" |
22 | #include "clang/Basic/SourceLocation.h" |
23 | #include "clang/Basic/SourceManager.h" |
24 | #include "clang/Basic/TokenKinds.h" |
25 | #include "clang/Lex/HeaderSearch.h" |
26 | #include "clang/Lex/LexDiagnostic.h" |
27 | #include "clang/Lex/Lexer.h" |
28 | #include "clang/Lex/LiteralSupport.h" |
29 | #include "clang/Lex/MacroInfo.h" |
30 | #include "clang/Lex/ModuleLoader.h" |
31 | #include "clang/Lex/PPCallbacks.h" |
32 | #include "clang/Lex/Preprocessor.h" |
33 | #include "clang/Lex/PreprocessorLexer.h" |
34 | #include "clang/Lex/PreprocessorOptions.h" |
35 | #include "clang/Lex/Token.h" |
36 | #include "clang/Lex/TokenLexer.h" |
37 | #include "llvm/ADT/ArrayRef.h" |
38 | #include "llvm/ADT/DenseMap.h" |
39 | #include "llvm/ADT/Optional.h" |
40 | #include "llvm/ADT/STLExtras.h" |
41 | #include "llvm/ADT/SmallString.h" |
42 | #include "llvm/ADT/SmallVector.h" |
43 | #include "llvm/ADT/StringRef.h" |
44 | #include "llvm/ADT/StringSwitch.h" |
45 | #include "llvm/Support/Compiler.h" |
46 | #include "llvm/Support/ErrorHandling.h" |
47 | #include "llvm/Support/Timer.h" |
48 | #include <algorithm> |
49 | #include <cassert> |
50 | #include <cstddef> |
51 | #include <cstdint> |
52 | #include <limits> |
53 | #include <string> |
54 | #include <utility> |
55 | #include <vector> |
56 | |
57 | using namespace clang; |
58 | |
59 | |
60 | PragmaHandler::~PragmaHandler() = default; |
61 | |
62 | |
63 | |
64 | |
65 | |
66 | EmptyPragmaHandler::EmptyPragmaHandler(StringRef Name) : PragmaHandler(Name) {} |
67 | |
68 | void EmptyPragmaHandler::HandlePragma(Preprocessor &PP, |
69 | PragmaIntroducer Introducer, |
70 | Token &FirstToken) {} |
71 | |
72 | |
73 | |
74 | |
75 | |
76 | |
77 | |
78 | |
79 | |
80 | PragmaHandler *PragmaNamespace::FindHandler(StringRef Name, |
81 | bool IgnoreNull) const { |
82 | auto I = Handlers.find(Name); |
83 | if (I != Handlers.end()) |
84 | return I->getValue().get(); |
85 | if (IgnoreNull) |
86 | return nullptr; |
87 | I = Handlers.find(StringRef()); |
88 | if (I != Handlers.end()) |
89 | return I->getValue().get(); |
90 | return nullptr; |
91 | } |
92 | |
93 | void PragmaNamespace::AddPragma(PragmaHandler *Handler) { |
94 | assert(!Handlers.count(Handler->getName()) && |
95 | "A handler with this name is already registered in this namespace"); |
96 | Handlers[Handler->getName()].reset(Handler); |
97 | } |
98 | |
99 | void PragmaNamespace::RemovePragmaHandler(PragmaHandler *Handler) { |
100 | auto I = Handlers.find(Handler->getName()); |
101 | assert(I != Handlers.end() && |
102 | "Handler not registered in this namespace"); |
103 | |
104 | I->getValue().release(); |
105 | Handlers.erase(I); |
106 | } |
107 | |
108 | void PragmaNamespace::HandlePragma(Preprocessor &PP, |
109 | PragmaIntroducer Introducer, Token &Tok) { |
110 | |
111 | |
112 | PP.LexUnexpandedToken(Tok); |
113 | |
114 | |
115 | PragmaHandler *Handler |
116 | = FindHandler(Tok.getIdentifierInfo() ? Tok.getIdentifierInfo()->getName() |
117 | : StringRef(), |
118 | false); |
119 | if (!Handler) { |
120 | PP.Diag(Tok, diag::warn_pragma_ignored); |
121 | return; |
122 | } |
123 | |
124 | |
125 | Handler->HandlePragma(PP, Introducer, Tok); |
126 | } |
127 | |
128 | |
129 | |
130 | |
131 | |
132 | namespace { |
133 | |
134 | |
135 | |
136 | struct TokenCollector { |
137 | Preprocessor &Self; |
138 | bool Collect; |
139 | SmallVector<Token, 3> Tokens; |
140 | Token &Tok; |
141 | |
142 | void lex() { |
143 | if (Collect) |
144 | Tokens.push_back(Tok); |
145 | Self.Lex(Tok); |
146 | } |
147 | |
148 | void revert() { |
149 | assert(Collect && "did not collect tokens"); |
150 | assert(!Tokens.empty() && "collected unexpected number of tokens"); |
151 | |
152 | |
153 | auto Toks = std::make_unique<Token[]>(Tokens.size()); |
154 | std::copy(Tokens.begin() + 1, Tokens.end(), Toks.get()); |
155 | Toks[Tokens.size() - 1] = Tok; |
156 | Self.EnterTokenStream(std::move(Toks), Tokens.size(), |
157 | true, |
158 | true); |
159 | |
160 | |
161 | Tok = *Tokens.begin(); |
162 | } |
163 | }; |
164 | } |
165 | |
166 | |
167 | |
168 | void Preprocessor::HandlePragmaDirective(PragmaIntroducer Introducer) { |
169 | if (Callbacks) |
170 | Callbacks->PragmaDirective(Introducer.Loc, Introducer.Kind); |
171 | |
172 | if (!PragmasEnabled) |
173 | return; |
174 | |
175 | ++NumPragma; |
176 | |
177 | |
178 | Token Tok; |
179 | PragmaHandlers->HandlePragma(*this, Introducer, Tok); |
180 | |
181 | |
182 | if ((CurTokenLexer && CurTokenLexer->isParsingPreprocessorDirective()) |
183 | || (CurPPLexer && CurPPLexer->ParsingPreprocessorDirective)) |
184 | DiscardUntilEndOfDirective(); |
185 | } |
186 | |
187 | |
188 | |
189 | |
190 | void Preprocessor::Handle_Pragma(Token &Tok) { |
191 | |
192 | |
193 | |
194 | |
195 | |
196 | |
197 | |
198 | |
199 | |
200 | |
201 | |
202 | |
203 | |
204 | |
205 | |
206 | |
207 | |
208 | |
209 | |
210 | |
211 | TokenCollector Toks = {*this, InMacroArgPreExpansion, {}, Tok}; |
212 | |
213 | |
214 | SourceLocation PragmaLoc = Tok.getLocation(); |
215 | |
216 | |
217 | Toks.lex(); |
218 | if (Tok.isNot(tok::l_paren)) { |
219 | Diag(PragmaLoc, diag::err__Pragma_malformed); |
220 | return; |
221 | } |
222 | |
223 | |
224 | Toks.lex(); |
225 | if (!tok::isStringLiteral(Tok.getKind())) { |
226 | Diag(PragmaLoc, diag::err__Pragma_malformed); |
227 | |
228 | if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::eof)) |
229 | Lex(Tok); |
230 | while (Tok.isNot(tok::r_paren) && |
231 | !Tok.isAtStartOfLine() && |
232 | Tok.isNot(tok::eof)) |
233 | Lex(Tok); |
234 | if (Tok.is(tok::r_paren)) |
235 | Lex(Tok); |
236 | return; |
237 | } |
238 | |
239 | if (Tok.hasUDSuffix()) { |
240 | Diag(Tok, diag::err_invalid_string_udl); |
241 | |
242 | Lex(Tok); |
243 | if (Tok.is(tok::r_paren)) |
244 | Lex(Tok); |
245 | return; |
246 | } |
247 | |
248 | |
249 | Token StrTok = Tok; |
250 | |
251 | |
252 | Toks.lex(); |
253 | if (Tok.isNot(tok::r_paren)) { |
254 | Diag(PragmaLoc, diag::err__Pragma_malformed); |
255 | return; |
256 | } |
257 | |
258 | |
259 | if (InMacroArgPreExpansion) { |
260 | Toks.revert(); |
261 | return; |
262 | } |
263 | |
264 | SourceLocation RParenLoc = Tok.getLocation(); |
265 | std::string StrVal = getSpelling(StrTok); |
266 | |
267 | |
268 | |
269 | |
270 | |
271 | |
272 | if (StrVal[0] == 'L' || StrVal[0] == 'U' || |
273 | (StrVal[0] == 'u' && StrVal[1] != '8')) |
274 | StrVal.erase(StrVal.begin()); |
275 | else if (StrVal[0] == 'u') |
276 | StrVal.erase(StrVal.begin(), StrVal.begin() + 2); |
277 | |
278 | if (StrVal[0] == 'R') { |
279 | |
280 | |
281 | assert(StrVal[1] == '"' && StrVal[StrVal.size() - 1] == '"' && |
282 | "Invalid raw string token!"); |
283 | |
284 | |
285 | unsigned NumDChars = 0; |
286 | while (StrVal[2 + NumDChars] != '(') { |
287 | assert(NumDChars < (StrVal.size() - 5) / 2 && |
288 | "Invalid raw string token!"); |
289 | ++NumDChars; |
290 | } |
291 | assert(StrVal[StrVal.size() - 2 - NumDChars] == ')'); |
292 | |
293 | |
294 | |
295 | StrVal.erase(0, 2 + NumDChars); |
296 | StrVal.erase(StrVal.size() - 1 - NumDChars); |
297 | } else { |
298 | assert(StrVal[0] == '"' && StrVal[StrVal.size()-1] == '"' && |
299 | "Invalid string token!"); |
300 | |
301 | |
302 | unsigned ResultPos = 1; |
303 | for (size_t i = 1, e = StrVal.size() - 1; i != e; ++i) { |
304 | |
305 | if (StrVal[i] == '\\' && i + 1 < e && |
306 | (StrVal[i + 1] == '\\' || StrVal[i + 1] == '"')) |
307 | ++i; |
308 | StrVal[ResultPos++] = StrVal[i]; |
309 | } |
310 | StrVal.erase(StrVal.begin() + ResultPos, StrVal.end() - 1); |
311 | } |
312 | |
313 | |
314 | |
315 | StrVal[0] = ' '; |
316 | |
317 | |
318 | StrVal[StrVal.size()-1] = '\n'; |
319 | |
320 | |
321 | |
322 | Token TmpTok; |
323 | TmpTok.startToken(); |
324 | CreateString(StrVal, TmpTok); |
325 | SourceLocation TokLoc = TmpTok.getLocation(); |
326 | |
327 | |
328 | |
329 | Lexer *TL = Lexer::Create_PragmaLexer(TokLoc, PragmaLoc, RParenLoc, |
330 | StrVal.size(), *this); |
331 | |
332 | EnterSourceFileWithLexer(TL, nullptr); |
333 | |
334 | |
335 | HandlePragmaDirective({PIK__Pragma, PragmaLoc}); |
336 | |
337 | |
338 | return Lex(Tok); |
339 | } |
340 | |
341 | |
342 | |
343 | void Preprocessor::HandleMicrosoft__pragma(Token &Tok) { |
344 | |
345 | |
346 | TokenCollector Toks = {*this, InMacroArgPreExpansion, {}, Tok}; |
347 | |
348 | |
349 | SourceLocation PragmaLoc = Tok.getLocation(); |
350 | |
351 | |
352 | Toks.lex(); |
353 | if (Tok.isNot(tok::l_paren)) { |
354 | Diag(PragmaLoc, diag::err__Pragma_malformed); |
355 | return; |
356 | } |
357 | |
358 | |
359 | SmallVector<Token, 32> PragmaToks; |
360 | int NumParens = 0; |
361 | Toks.lex(); |
362 | while (Tok.isNot(tok::eof)) { |
363 | PragmaToks.push_back(Tok); |
364 | if (Tok.is(tok::l_paren)) |
365 | NumParens++; |
366 | else if (Tok.is(tok::r_paren) && NumParens-- == 0) |
367 | break; |
368 | Toks.lex(); |
369 | } |
370 | |
371 | if (Tok.is(tok::eof)) { |
372 | Diag(PragmaLoc, diag::err_unterminated___pragma); |
373 | return; |
374 | } |
375 | |
376 | |
377 | if (InMacroArgPreExpansion) { |
378 | Toks.revert(); |
379 | return; |
380 | } |
381 | |
382 | PragmaToks.front().setFlag(Token::LeadingSpace); |
383 | |
384 | |
385 | PragmaToks.back().setKind(tok::eod); |
386 | |
387 | Token *TokArray = new Token[PragmaToks.size()]; |
388 | std::copy(PragmaToks.begin(), PragmaToks.end(), TokArray); |
389 | |
390 | |
391 | EnterTokenStream(TokArray, PragmaToks.size(), true, true, |
392 | false); |
393 | |
394 | |
395 | HandlePragmaDirective({PIK___pragma, PragmaLoc}); |
396 | |
397 | |
398 | return Lex(Tok); |
399 | } |
400 | |
401 | |
402 | void Preprocessor::HandlePragmaOnce(Token &OnceTok) { |
403 | |
404 | |
405 | |
406 | |
407 | if (isInPrimaryFile() && TUKind != TU_Prefix && !getLangOpts().IsHeaderFile) { |
408 | Diag(OnceTok, diag::pp_pragma_once_in_main_file); |
409 | return; |
410 | } |
411 | |
412 | |
413 | |
414 | HeaderInfo.MarkFileIncludeOnce(getCurrentFileLexer()->getFileEntry()); |
415 | } |
416 | |
417 | void Preprocessor::HandlePragmaMark(Token &MarkTok) { |
418 | assert(CurPPLexer && "No current lexer?"); |
419 | |
420 | SmallString<64> Buffer; |
421 | CurLexer->ReadToEndOfLine(&Buffer); |
422 | if (Callbacks) |
423 | Callbacks->PragmaMark(MarkTok.getLocation(), Buffer); |
424 | } |
425 | |
426 | |
427 | void Preprocessor::HandlePragmaPoison() { |
428 | Token Tok; |
429 | |
430 | while (true) { |
431 | |
432 | |
433 | |
434 | |
435 | |
436 | if (CurPPLexer) CurPPLexer->LexingRawMode = true; |
437 | LexUnexpandedToken(Tok); |
438 | if (CurPPLexer) CurPPLexer->LexingRawMode = false; |
439 | |
440 | |
441 | if (Tok.is(tok::eod)) return; |
442 | |
443 | |
444 | if (Tok.isNot(tok::raw_identifier)) { |
445 | Diag(Tok, diag::err_pp_invalid_poison); |
446 | return; |
447 | } |
448 | |
449 | |
450 | |
451 | IdentifierInfo *II = LookUpIdentifierInfo(Tok); |
452 | |
453 | |
454 | if (II->isPoisoned()) continue; |
455 | |
456 | |
457 | if (isMacroDefined(II)) |
458 | Diag(Tok, diag::pp_poisoning_existing_macro); |
459 | |
460 | |
461 | II->setIsPoisoned(); |
462 | if (II->isFromAST()) |
463 | II->setChangedSinceDeserialization(); |
464 | } |
465 | } |
466 | |
467 | |
468 | |
469 | void Preprocessor::HandlePragmaSystemHeader(Token &SysHeaderTok) { |
470 | if (isInPrimaryFile()) { |
471 | Diag(SysHeaderTok, diag::pp_pragma_sysheader_in_main_file); |
472 | return; |
473 | } |
474 | |
475 | |
476 | PreprocessorLexer *TheLexer = getCurrentFileLexer(); |
477 | |
478 | |
479 | HeaderInfo.MarkFileSystemHeader(TheLexer->getFileEntry()); |
480 | |
481 | PresumedLoc PLoc = SourceMgr.getPresumedLoc(SysHeaderTok.getLocation()); |
482 | if (PLoc.isInvalid()) |
483 | return; |
484 | |
485 | unsigned FilenameID = SourceMgr.getLineTableFilenameID(PLoc.getFilename()); |
486 | |
487 | |
488 | if (Callbacks) |
489 | Callbacks->FileChanged(SysHeaderTok.getLocation(), |
490 | PPCallbacks::SystemHeaderPragma, SrcMgr::C_System); |
491 | |
492 | |
493 | |
494 | |
495 | SourceMgr.AddLineNote(SysHeaderTok.getLocation(), PLoc.getLine() + 1, |
496 | FilenameID, false, false, |
497 | SrcMgr::C_System); |
498 | } |
499 | |
500 | static llvm::Optional<Token> LexHeader(Preprocessor &PP, |
501 | Optional<FileEntryRef> &File, |
502 | bool SuppressIncludeNotFoundError) { |
503 | Token FilenameTok; |
504 | if (PP.LexHeaderName(FilenameTok, false)) |
505 | return llvm::None; |
506 | |
507 | |
508 | if (FilenameTok.isNot(tok::header_name)) { |
509 | PP.Diag(FilenameTok.getLocation(), diag::err_pp_expects_filename); |
510 | return llvm::None; |
511 | } |
512 | |
513 | |
514 | SmallString<128> FilenameBuffer; |
515 | bool Invalid = false; |
516 | StringRef Filename = PP.getSpelling(FilenameTok, FilenameBuffer, &Invalid); |
517 | if (Invalid) |
518 | return llvm::None; |
519 | |
520 | bool isAngled = |
521 | PP.GetIncludeFilenameSpelling(FilenameTok.getLocation(), Filename); |
522 | |
523 | |
524 | if (Filename.empty()) |
525 | return llvm::None; |
526 | |
527 | |
528 | const DirectoryLookup *CurDir; |
529 | File = PP.LookupFile(FilenameTok.getLocation(), Filename, isAngled, nullptr, |
530 | nullptr, CurDir, nullptr, nullptr, nullptr, nullptr, |
531 | nullptr); |
532 | if (!File) { |
533 | if (!SuppressIncludeNotFoundError) |
534 | PP.Diag(FilenameTok, diag::err_pp_file_not_found) << Filename; |
535 | return llvm::None; |
536 | } |
537 | |
538 | return FilenameTok; |
539 | } |
540 | |
541 | |
542 | void Preprocessor::HandlePragmaIncludeInstead(Token &Tok) { |
543 | |
544 | PreprocessorLexer *TheLexer = getCurrentFileLexer(); |
545 | |
546 | if (!SourceMgr.isInSystemHeader(Tok.getLocation())) { |
547 | Diag(Tok, diag::err_pragma_include_instead_not_sysheader); |
548 | return; |
549 | } |
550 | |
551 | Lex(Tok); |
552 | if (Tok.isNot(tok::l_paren)) { |
553 | Diag(Tok, diag::err_expected) << "("; |
554 | return; |
555 | } |
556 | |
557 | Optional<FileEntryRef> File; |
558 | llvm::Optional<Token> FilenameTok = |
559 | LexHeader(*this, File, SuppressIncludeNotFoundError); |
560 | if (!FilenameTok) |
561 | return; |
562 | |
563 | Lex(Tok); |
564 | if (Tok.isNot(tok::r_paren)) { |
565 | Diag(Tok, diag::err_expected) << ")"; |
566 | return; |
567 | } |
568 | |
569 | SmallString<128> FilenameBuffer; |
570 | StringRef Filename = getSpelling(*FilenameTok, FilenameBuffer); |
571 | HeaderInfo.AddFileAlias(TheLexer->getFileEntry(), Filename); |
572 | } |
573 | |
574 | |
575 | void Preprocessor::HandlePragmaDependency(Token &DependencyTok) { |
576 | Optional<FileEntryRef> File; |
577 | llvm::Optional<Token> FilenameTok = |
578 | LexHeader(*this, File, SuppressIncludeNotFoundError); |
579 | if (!FilenameTok) |
580 | return; |
581 | |
582 | const FileEntry *CurFile = getCurrentFileLexer()->getFileEntry(); |
583 | |
584 | |
585 | if (CurFile && CurFile->getModificationTime() < File->getModificationTime()) { |
586 | |
587 | std::string Message; |
588 | Lex(DependencyTok); |
589 | while (DependencyTok.isNot(tok::eod)) { |
590 | Message += getSpelling(DependencyTok) + " "; |
591 | Lex(DependencyTok); |
592 | } |
593 | |
594 | |
595 | if (!Message.empty()) |
596 | Message.erase(Message.end()-1); |
597 | Diag(*FilenameTok, diag::pp_out_of_date_dependency) << Message; |
598 | } |
599 | } |
600 | |
601 | |
602 | |
603 | IdentifierInfo *Preprocessor::ParsePragmaPushOrPopMacro(Token &Tok) { |
604 | |
605 | Token PragmaTok = Tok; |
606 | |
607 | |
608 | Lex(Tok); |
609 | if (Tok.isNot(tok::l_paren)) { |
610 | Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed) |
611 | << getSpelling(PragmaTok); |
612 | return nullptr; |
613 | } |
614 | |
615 | |
616 | Lex(Tok); |
617 | if (Tok.isNot(tok::string_literal)) { |
618 | Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed) |
619 | << getSpelling(PragmaTok); |
620 | return nullptr; |
621 | } |
622 | |
623 | if (Tok.hasUDSuffix()) { |
624 | Diag(Tok, diag::err_invalid_string_udl); |
625 | return nullptr; |
626 | } |
627 | |
628 | |
629 | std::string StrVal = getSpelling(Tok); |
630 | |
631 | |
632 | Lex(Tok); |
633 | if (Tok.isNot(tok::r_paren)) { |
634 | Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed) |
635 | << getSpelling(PragmaTok); |
636 | return nullptr; |
637 | } |
638 | |
639 | assert(StrVal[0] == '"' && StrVal[StrVal.size()-1] == '"' && |
640 | "Invalid string token!"); |
641 | |
642 | |
643 | Token MacroTok; |
644 | MacroTok.startToken(); |
645 | MacroTok.setKind(tok::raw_identifier); |
646 | CreateString(StringRef(&StrVal[1], StrVal.size() - 2), MacroTok); |
647 | |
648 | |
649 | return LookUpIdentifierInfo(MacroTok); |
650 | } |
651 | |
652 | |
653 | |
654 | |
655 | |
656 | |
657 | |
658 | void Preprocessor::HandlePragmaPushMacro(Token &PushMacroTok) { |
659 | |
660 | IdentifierInfo *IdentInfo = ParsePragmaPushOrPopMacro(PushMacroTok); |
661 | if (!IdentInfo) return; |
662 | |
663 | |
664 | MacroInfo *MI = getMacroInfo(IdentInfo); |
665 | |
666 | if (MI) { |
667 | |
668 | MI->setIsAllowRedefinitionsWithoutWarning(true); |
669 | } |
670 | |
671 | |
672 | PragmaPushMacroInfo[IdentInfo].push_back(MI); |
673 | } |
674 | |
675 | |
676 | |
677 | |
678 | |
679 | |
680 | |
681 | void Preprocessor::HandlePragmaPopMacro(Token &PopMacroTok) { |
682 | SourceLocation MessageLoc = PopMacroTok.getLocation(); |
683 | |
684 | |
685 | IdentifierInfo *IdentInfo = ParsePragmaPushOrPopMacro(PopMacroTok); |
686 | if (!IdentInfo) return; |
687 | |
688 | |
689 | llvm::DenseMap<IdentifierInfo *, std::vector<MacroInfo *>>::iterator iter = |
690 | PragmaPushMacroInfo.find(IdentInfo); |
691 | if (iter != PragmaPushMacroInfo.end()) { |
692 | |
693 | if (MacroInfo *MI = getMacroInfo(IdentInfo)) { |
694 | if (MI->isWarnIfUnused()) |
695 | WarnUnusedMacroLocs.erase(MI->getDefinitionLoc()); |
696 | appendMacroDirective(IdentInfo, AllocateUndefMacroDirective(MessageLoc)); |
697 | } |
698 | |
699 | |
700 | MacroInfo *MacroToReInstall = iter->second.back(); |
701 | |
702 | if (MacroToReInstall) |
703 | |
704 | appendDefMacroDirective(IdentInfo, MacroToReInstall, MessageLoc); |
705 | |
706 | |
707 | iter->second.pop_back(); |
708 | if (iter->second.empty()) |
709 | PragmaPushMacroInfo.erase(iter); |
710 | } else { |
711 | Diag(MessageLoc, diag::warn_pragma_pop_macro_no_push) |
712 | << IdentInfo->getName(); |
713 | } |
714 | } |
715 | |
716 | void Preprocessor::HandlePragmaIncludeAlias(Token &Tok) { |
717 | |
718 | |
719 | |
720 | |
721 | |
722 | |
723 | Lex(Tok); |
724 | if (Tok.isNot(tok::l_paren)) { |
725 | Diag(Tok, diag::warn_pragma_include_alias_expected) << "("; |
726 | return; |
727 | } |
728 | |
729 | |
730 | Token SourceFilenameTok; |
731 | if (LexHeaderName(SourceFilenameTok)) |
732 | return; |
733 | |
734 | StringRef SourceFileName; |
735 | SmallString<128> FileNameBuffer; |
736 | if (SourceFilenameTok.is(tok::header_name)) { |
737 | SourceFileName = getSpelling(SourceFilenameTok, FileNameBuffer); |
738 | } else { |
739 | Diag(Tok, diag::warn_pragma_include_alias_expected_filename); |
740 | return; |
741 | } |
742 | FileNameBuffer.clear(); |
743 | |
744 | |
745 | Lex(Tok); |
746 | if (Tok.isNot(tok::comma)) { |
747 | Diag(Tok, diag::warn_pragma_include_alias_expected) << ","; |
748 | return; |
749 | } |
750 | |
751 | Token ReplaceFilenameTok; |
752 | if (LexHeaderName(ReplaceFilenameTok)) |
753 | return; |
754 | |
755 | StringRef ReplaceFileName; |
756 | if (ReplaceFilenameTok.is(tok::header_name)) { |
757 | ReplaceFileName = getSpelling(ReplaceFilenameTok, FileNameBuffer); |
758 | } else { |
759 | Diag(Tok, diag::warn_pragma_include_alias_expected_filename); |
760 | return; |
761 | } |
762 | |
763 | |
764 | Lex(Tok); |
765 | if (Tok.isNot(tok::r_paren)) { |
766 | Diag(Tok, diag::warn_pragma_include_alias_expected) << ")"; |
767 | return; |
768 | } |
769 | |
770 | |
771 | |
772 | StringRef OriginalSource = SourceFileName; |
773 | |
774 | bool SourceIsAngled = |
775 | GetIncludeFilenameSpelling(SourceFilenameTok.getLocation(), |
776 | SourceFileName); |
777 | bool ReplaceIsAngled = |
778 | GetIncludeFilenameSpelling(ReplaceFilenameTok.getLocation(), |
779 | ReplaceFileName); |
780 | if (!SourceFileName.empty() && !ReplaceFileName.empty() && |
781 | (SourceIsAngled != ReplaceIsAngled)) { |
782 | unsigned int DiagID; |
783 | if (SourceIsAngled) |
784 | DiagID = diag::warn_pragma_include_alias_mismatch_angle; |
785 | else |
786 | DiagID = diag::warn_pragma_include_alias_mismatch_quote; |
787 | |
788 | Diag(SourceFilenameTok.getLocation(), DiagID) |
789 | << SourceFileName |
790 | << ReplaceFileName; |
791 | |
792 | return; |
793 | } |
794 | |
795 | |
796 | getHeaderSearchInfo().AddIncludeAlias(OriginalSource, ReplaceFileName); |
797 | } |
798 | |
799 | |
800 | |
801 | static bool LexModuleNameComponent( |
802 | Preprocessor &PP, Token &Tok, |
803 | std::pair<IdentifierInfo *, SourceLocation> &ModuleNameComponent, |
804 | bool First) { |
805 | PP.LexUnexpandedToken(Tok); |
806 | if (Tok.is(tok::string_literal) && !Tok.hasUDSuffix()) { |
807 | StringLiteralParser Literal(Tok, PP); |
808 | if (Literal.hadError) |
809 | return true; |
810 | ModuleNameComponent = std::make_pair( |
811 | PP.getIdentifierInfo(Literal.GetString()), Tok.getLocation()); |
812 | } else if (!Tok.isAnnotation() && Tok.getIdentifierInfo()) { |
813 | ModuleNameComponent = |
814 | std::make_pair(Tok.getIdentifierInfo(), Tok.getLocation()); |
815 | } else { |
816 | PP.Diag(Tok.getLocation(), diag::err_pp_expected_module_name) << First; |
817 | return true; |
818 | } |
819 | return false; |
820 | } |
821 | |
822 | static bool LexModuleName( |
823 | Preprocessor &PP, Token &Tok, |
824 | llvm::SmallVectorImpl<std::pair<IdentifierInfo *, SourceLocation>> |
825 | &ModuleName) { |
826 | while (true) { |
827 | std::pair<IdentifierInfo*, SourceLocation> NameComponent; |
828 | if (LexModuleNameComponent(PP, Tok, NameComponent, ModuleName.empty())) |
829 | return true; |
830 | ModuleName.push_back(NameComponent); |
831 | |
832 | PP.LexUnexpandedToken(Tok); |
833 | if (Tok.isNot(tok::period)) |
834 | return false; |
835 | } |
836 | } |
837 | |
838 | void Preprocessor::HandlePragmaModuleBuild(Token &Tok) { |
839 | SourceLocation Loc = Tok.getLocation(); |
840 | |
841 | std::pair<IdentifierInfo *, SourceLocation> ModuleNameLoc; |
842 | if (LexModuleNameComponent(*this, Tok, ModuleNameLoc, true)) |
843 | return; |
844 | IdentifierInfo *ModuleName = ModuleNameLoc.first; |
845 | |
846 | LexUnexpandedToken(Tok); |
847 | if (Tok.isNot(tok::eod)) { |
848 | Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma"; |
849 | DiscardUntilEndOfDirective(); |
850 | } |
851 | |
852 | CurLexer->LexingRawMode = true; |
853 | |
854 | auto TryConsumeIdentifier = [&](StringRef Ident) -> bool { |
855 | if (Tok.getKind() != tok::raw_identifier || |
856 | Tok.getRawIdentifier() != Ident) |
857 | return false; |
858 | CurLexer->Lex(Tok); |
859 | return true; |
860 | }; |
861 | |
862 | |
863 | const char *Start = CurLexer->getBufferLocation(); |
864 | const char *End = nullptr; |
865 | unsigned NestingLevel = 1; |
866 | while (true) { |
867 | End = CurLexer->getBufferLocation(); |
868 | CurLexer->Lex(Tok); |
869 | |
870 | if (Tok.is(tok::eof)) { |
871 | Diag(Loc, diag::err_pp_module_build_missing_end); |
872 | break; |
873 | } |
874 | |
875 | if (Tok.isNot(tok::hash) || !Tok.isAtStartOfLine()) { |
876 | |
877 | continue; |
878 | } |
879 | |
880 | |
881 | |
882 | CurLexer->ParsingPreprocessorDirective = true; |
883 | CurLexer->Lex(Tok); |
884 | if (TryConsumeIdentifier("pragma") && TryConsumeIdentifier("clang") && |
885 | TryConsumeIdentifier("module")) { |
886 | if (TryConsumeIdentifier("build")) |
887 | |
888 | ++NestingLevel; |
889 | else if (TryConsumeIdentifier("endbuild")) { |
890 | |
891 | if (--NestingLevel == 0) |
892 | break; |
893 | } |
894 | |
895 | |
896 | assert(Tok.getKind() != tok::eof && "missing EOD before EOF"); |
897 | } |
898 | } |
899 | |
900 | CurLexer->LexingRawMode = false; |
901 | |
902 | |
903 | assert(CurLexer->getBuffer().begin() <= Start && |
904 | Start <= CurLexer->getBuffer().end() && |
905 | CurLexer->getBuffer().begin() <= End && |
906 | End <= CurLexer->getBuffer().end() && |
907 | "module source range not contained within same file buffer"); |
908 | TheModuleLoader.createModuleFromSource(Loc, ModuleName->getName(), |
909 | StringRef(Start, End - Start)); |
910 | } |
911 | |
912 | void Preprocessor::HandlePragmaHdrstop(Token &Tok) { |
913 | Lex(Tok); |
914 | if (Tok.is(tok::l_paren)) { |
915 | Diag(Tok.getLocation(), diag::warn_pp_hdrstop_filename_ignored); |
916 | |
917 | std::string FileName; |
918 | if (!LexStringLiteral(Tok, FileName, "pragma hdrstop", false)) |
919 | return; |
920 | |
921 | if (Tok.isNot(tok::r_paren)) { |
922 | Diag(Tok, diag::err_expected) << tok::r_paren; |
923 | return; |
924 | } |
925 | Lex(Tok); |
926 | } |
927 | if (Tok.isNot(tok::eod)) |
928 | Diag(Tok.getLocation(), diag::ext_pp_extra_tokens_at_eol) |
929 | << "pragma hdrstop"; |
930 | |
931 | if (creatingPCHWithPragmaHdrStop() && |
932 | SourceMgr.isInMainFile(Tok.getLocation())) { |
933 | assert(CurLexer && "no lexer for #pragma hdrstop processing"); |
934 | Token &Result = Tok; |
935 | Result.startToken(); |
936 | CurLexer->FormTokenWithChars(Result, CurLexer->BufferEnd, tok::eof); |
937 | CurLexer->cutOffLexing(); |
938 | } |
939 | if (usingPCHWithPragmaHdrStop()) |
940 | SkippingUntilPragmaHdrStop = false; |
941 | } |
942 | |
943 | |
944 | |
945 | |
946 | void Preprocessor::AddPragmaHandler(StringRef Namespace, |
947 | PragmaHandler *Handler) { |
948 | PragmaNamespace *InsertNS = PragmaHandlers.get(); |
949 | |
950 | |
951 | if (!Namespace.empty()) { |
952 | |
953 | |
954 | |
955 | if (PragmaHandler *Existing = PragmaHandlers->FindHandler(Namespace)) { |
956 | InsertNS = Existing->getIfNamespace(); |
957 | assert(InsertNS != nullptr && "Cannot have a pragma namespace and pragma" |
958 | " handler with the same name!"); |
959 | } else { |
960 | |
961 | |
962 | InsertNS = new PragmaNamespace(Namespace); |
963 | PragmaHandlers->AddPragma(InsertNS); |
964 | } |
965 | } |
966 | |
967 | |
968 | assert(!InsertNS->FindHandler(Handler->getName()) && |
969 | "Pragma handler already exists for this identifier!"); |
970 | InsertNS->AddPragma(Handler); |
971 | } |
972 | |
973 | |
974 | |
975 | |
976 | |
977 | void Preprocessor::RemovePragmaHandler(StringRef Namespace, |
978 | PragmaHandler *Handler) { |
979 | PragmaNamespace *NS = PragmaHandlers.get(); |
980 | |
981 | |
982 | if (!Namespace.empty()) { |
983 | PragmaHandler *Existing = PragmaHandlers->FindHandler(Namespace); |
984 | assert(Existing && "Namespace containing handler does not exist!"); |
985 | |
986 | NS = Existing->getIfNamespace(); |
987 | assert(NS && "Invalid namespace, registered as a regular pragma handler!"); |
988 | } |
989 | |
990 | NS->RemovePragmaHandler(Handler); |
991 | |
992 | |
993 | if (NS != PragmaHandlers.get() && NS->IsEmpty()) { |
994 | PragmaHandlers->RemovePragmaHandler(NS); |
995 | delete NS; |
996 | } |
997 | } |
998 | |
999 | bool Preprocessor::LexOnOffSwitch(tok::OnOffSwitch &Result) { |
1000 | Token Tok; |
1001 | LexUnexpandedToken(Tok); |
1002 | |
1003 | if (Tok.isNot(tok::identifier)) { |
1004 | Diag(Tok, diag::ext_on_off_switch_syntax); |
1005 | return true; |
1006 | } |
1007 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
1008 | if (II->isStr("ON")) |
1009 | Result = tok::OOS_ON; |
1010 | else if (II->isStr("OFF")) |
1011 | Result = tok::OOS_OFF; |
1012 | else if (II->isStr("DEFAULT")) |
1013 | Result = tok::OOS_DEFAULT; |
1014 | else { |
1015 | Diag(Tok, diag::ext_on_off_switch_syntax); |
1016 | return true; |
1017 | } |
1018 | |
1019 | |
1020 | LexUnexpandedToken(Tok); |
1021 | if (Tok.isNot(tok::eod)) |
1022 | Diag(Tok, diag::ext_pragma_syntax_eod); |
1023 | return false; |
1024 | } |
1025 | |
1026 | namespace { |
1027 | |
1028 | |
1029 | struct PragmaOnceHandler : public PragmaHandler { |
1030 | PragmaOnceHandler() : PragmaHandler("once") {} |
1031 | |
1032 | void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer, |
1033 | Token &OnceTok) override { |
1034 | PP.CheckEndOfDirective("pragma once"); |
1035 | PP.HandlePragmaOnce(OnceTok); |
1036 | } |
1037 | }; |
1038 | |
1039 | |
1040 | |
1041 | struct PragmaMarkHandler : public PragmaHandler { |
1042 | PragmaMarkHandler() : PragmaHandler("mark") {} |
1043 | |
1044 | void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer, |
1045 | Token &MarkTok) override { |
1046 | PP.HandlePragmaMark(MarkTok); |
1047 | } |
1048 | }; |
1049 | |
1050 | |
1051 | struct PragmaPoisonHandler : public PragmaHandler { |
1052 | PragmaPoisonHandler() : PragmaHandler("poison") {} |
1053 | |
1054 | void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer, |
1055 | Token &PoisonTok) override { |
1056 | PP.HandlePragmaPoison(); |
1057 | } |
1058 | }; |
1059 | |
1060 | |
1061 | |
1062 | struct PragmaSystemHeaderHandler : public PragmaHandler { |
1063 | PragmaSystemHeaderHandler() : PragmaHandler("system_header") {} |
1064 | |
1065 | void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer, |
1066 | Token &SHToken) override { |
1067 | PP.HandlePragmaSystemHeader(SHToken); |
1068 | PP.CheckEndOfDirective("pragma"); |
1069 | } |
1070 | }; |
1071 | |
1072 | |
1073 | |
1074 | |
1075 | struct PragmaIncludeInsteadHandler : public PragmaHandler { |
1076 | PragmaIncludeInsteadHandler() : PragmaHandler("include_instead") {} |
1077 | |
1078 | void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer, |
1079 | Token &IIToken) override { |
1080 | PP.HandlePragmaIncludeInstead(IIToken); |
1081 | } |
1082 | }; |
1083 | |
1084 | struct PragmaDependencyHandler : public PragmaHandler { |
1085 | PragmaDependencyHandler() : PragmaHandler("dependency") {} |
1086 | |
1087 | void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer, |
1088 | Token &DepToken) override { |
1089 | PP.HandlePragmaDependency(DepToken); |
1090 | } |
1091 | }; |
1092 | |
1093 | struct PragmaDebugHandler : public PragmaHandler { |
1094 | PragmaDebugHandler() : PragmaHandler("__debug") {} |
1095 | |
1096 | void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer, |
1097 | Token &DebugToken) override { |
1098 | Token Tok; |
1099 | PP.LexUnexpandedToken(Tok); |
1100 | if (Tok.isNot(tok::identifier)) { |
| |
| 4 | | Returning from 'Token::isNot' | |
|
| |
1101 | PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid); |
1102 | return; |
1103 | } |
1104 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
1105 | |
1106 | if (II->isStr("assert")) { |
| 6 | | Calling 'IdentifierInfo::isStr' | |
|
| 9 | | Returning from 'IdentifierInfo::isStr' | |
|
| |
1107 | if (!PP.getPreprocessorOpts().DisablePragmaDebugCrash) |
1108 | llvm_unreachable("This is an assertion!"); |
1109 | } else if (II->isStr("crash")) { |
| 11 | | Calling 'IdentifierInfo::isStr' | |
|
| 14 | | Returning from 'IdentifierInfo::isStr' | |
|
| |
1110 | llvm::Timer T("crash", "pragma crash"); |
1111 | llvm::TimeRegion R(&T); |
1112 | if (!PP.getPreprocessorOpts().DisablePragmaDebugCrash) |
1113 | LLVM_BUILTIN_TRAP; |
1114 | } else if (II->isStr("parser_crash")) { |
| 16 | | Calling 'IdentifierInfo::isStr' | |
|
| 19 | | Returning from 'IdentifierInfo::isStr' | |
|
| |
1115 | if (!PP.getPreprocessorOpts().DisablePragmaDebugCrash) { |
1116 | Token Crasher; |
1117 | Crasher.startToken(); |
1118 | Crasher.setKind(tok::annot_pragma_parser_crash); |
1119 | Crasher.setAnnotationRange(SourceRange(Tok.getLocation())); |
1120 | PP.EnterToken(Crasher, false); |
1121 | } |
1122 | } else if (II->isStr("dump")) { |
| 21 | | Calling 'IdentifierInfo::isStr' | |
|
| 24 | | Returning from 'IdentifierInfo::isStr' | |
|
| |
1123 | Token Identifier; |
1124 | PP.LexUnexpandedToken(Identifier); |
1125 | if (auto *DumpII = Identifier.getIdentifierInfo()) { |
1126 | Token DumpAnnot; |
1127 | DumpAnnot.startToken(); |
1128 | DumpAnnot.setKind(tok::annot_pragma_dump); |
1129 | DumpAnnot.setAnnotationRange( |
1130 | SourceRange(Tok.getLocation(), Identifier.getLocation())); |
1131 | DumpAnnot.setAnnotationValue(DumpII); |
1132 | PP.DiscardUntilEndOfDirective(); |
1133 | PP.EnterToken(DumpAnnot, false); |
1134 | } else { |
1135 | PP.Diag(Identifier, diag::warn_pragma_debug_missing_argument) |
1136 | << II->getName(); |
1137 | } |
1138 | } else if (II->isStr("diag_mapping")) { |
| 26 | | Calling 'IdentifierInfo::isStr' | |
|
| 28 | | Returning from 'IdentifierInfo::isStr' | |
|
| |
1139 | Token DiagName; |
1140 | PP.LexUnexpandedToken(DiagName); |
1141 | if (DiagName.is(tok::eod)) |
1142 | PP.getDiagnostics().dump(); |
1143 | else if (DiagName.is(tok::string_literal) && !DiagName.hasUDSuffix()) { |
1144 | StringLiteralParser Literal(DiagName, PP); |
1145 | if (Literal.hadError) |
1146 | return; |
1147 | PP.getDiagnostics().dump(Literal.GetString()); |
1148 | } else { |
1149 | PP.Diag(DiagName, diag::warn_pragma_debug_missing_argument) |
1150 | << II->getName(); |
1151 | } |
1152 | } else if (II->isStr("llvm_fatal_error")) { |
| 30 | | Calling 'IdentifierInfo::isStr' | |
|
| 33 | | Returning from 'IdentifierInfo::isStr' | |
|
| |
1153 | if (!PP.getPreprocessorOpts().DisablePragmaDebugCrash) |
1154 | llvm::report_fatal_error("#pragma clang __debug llvm_fatal_error"); |
1155 | } else if (II->isStr("llvm_unreachable")) { |
| 35 | | Calling 'IdentifierInfo::isStr' | |
|
| 37 | | Returning from 'IdentifierInfo::isStr' | |
|
| |
1156 | if (!PP.getPreprocessorOpts().DisablePragmaDebugCrash) |
1157 | llvm_unreachable("#pragma clang __debug llvm_unreachable"); |
1158 | } else if (II->isStr("macro")) { |
| 39 | | Calling 'IdentifierInfo::isStr' | |
|
| 41 | | Returning from 'IdentifierInfo::isStr' | |
|
| |
1159 | Token MacroName; |
1160 | PP.LexUnexpandedToken(MacroName); |
1161 | auto *MacroII = MacroName.getIdentifierInfo(); |
1162 | if (MacroII) |
1163 | PP.dumpMacroInfo(MacroII); |
1164 | else |
1165 | PP.Diag(MacroName, diag::warn_pragma_debug_missing_argument) |
1166 | << II->getName(); |
1167 | } else if (II->isStr("module_map")) { |
| 43 | | Calling 'IdentifierInfo::isStr' | |
|
| 47 | | Returning from 'IdentifierInfo::isStr' | |
|
| |
1168 | llvm::SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 8> |
1169 | ModuleName; |
1170 | if (LexModuleName(PP, Tok, ModuleName)) |
| 49 | | Assuming the condition is false | |
|
| |
1171 | return; |
1172 | ModuleMap &MM = PP.getHeaderSearchInfo().getModuleMap(); |
1173 | Module *M = nullptr; |
| 51 | | 'M' initialized to a null pointer value | |
|
1174 | for (auto IIAndLoc : ModuleName) { |
| 52 | | Assuming '__begin11' is equal to '__end11' | |
|
1175 | M = MM.lookupModuleQualified(IIAndLoc.first->getName(), M); |
1176 | if (!M) { |
1177 | PP.Diag(IIAndLoc.second, diag::warn_pragma_debug_unknown_module) |
1178 | << IIAndLoc.first; |
1179 | return; |
1180 | } |
1181 | } |
1182 | M->dump(); |
| 53 | | Called C++ object pointer is null |
|
1183 | } else if (II->isStr("overflow_stack")) { |
1184 | if (!PP.getPreprocessorOpts().DisablePragmaDebugCrash) |
1185 | DebugOverflowStack(); |
1186 | } else if (II->isStr("captured")) { |
1187 | HandleCaptured(PP); |
1188 | } else if (II->isStr("modules")) { |
1189 | struct ModuleVisitor { |
1190 | Preprocessor &PP; |
1191 | void visit(Module *M, bool VisibleOnly) { |
1192 | SourceLocation ImportLoc = PP.getModuleImportLoc(M); |
1193 | if (!VisibleOnly || ImportLoc.isValid()) { |
1194 | llvm::errs() << M->getFullModuleName() << " "; |
1195 | if (ImportLoc.isValid()) { |
1196 | llvm::errs() << M << " visible "; |
1197 | ImportLoc.print(llvm::errs(), PP.getSourceManager()); |
1198 | } |
1199 | llvm::errs() << "\n"; |
1200 | } |
1201 | for (Module *Sub : M->submodules()) { |
1202 | if (!VisibleOnly || ImportLoc.isInvalid() || Sub->IsExplicit) |
1203 | visit(Sub, VisibleOnly); |
1204 | } |
1205 | } |
1206 | void visitAll(bool VisibleOnly) { |
1207 | for (auto &NameAndMod : |
1208 | PP.getHeaderSearchInfo().getModuleMap().modules()) |
1209 | visit(NameAndMod.second, VisibleOnly); |
1210 | } |
1211 | } Visitor{PP}; |
1212 | |
1213 | Token Kind; |
1214 | PP.LexUnexpandedToken(Kind); |
1215 | auto *DumpII = Kind.getIdentifierInfo(); |
1216 | if (!DumpII) { |
1217 | PP.Diag(Kind, diag::warn_pragma_debug_missing_argument) |
1218 | << II->getName(); |
1219 | } else if (DumpII->isStr("all")) { |
1220 | Visitor.visitAll(false); |
1221 | } else if (DumpII->isStr("visible")) { |
1222 | Visitor.visitAll(true); |
1223 | } else if (DumpII->isStr("building")) { |
1224 | for (auto &Building : PP.getBuildingSubmodules()) { |
1225 | llvm::errs() << "in " << Building.M->getFullModuleName(); |
1226 | if (Building.ImportLoc.isValid()) { |
1227 | llvm::errs() << " imported "; |
1228 | if (Building.IsPragma) |
1229 | llvm::errs() << "via pragma "; |
1230 | llvm::errs() << "at "; |
1231 | Building.ImportLoc.print(llvm::errs(), PP.getSourceManager()); |
1232 | llvm::errs() << "\n"; |
1233 | } |
1234 | } |
1235 | } else { |
1236 | PP.Diag(Tok, diag::warn_pragma_debug_unexpected_command) |
1237 | << DumpII->getName(); |
1238 | } |
1239 | } else { |
1240 | PP.Diag(Tok, diag::warn_pragma_debug_unexpected_command) |
1241 | << II->getName(); |
1242 | } |
1243 | |
1244 | PPCallbacks *Callbacks = PP.getPPCallbacks(); |
1245 | if (Callbacks) |
1246 | Callbacks->PragmaDebug(Tok.getLocation(), II->getName()); |
1247 | } |
1248 | |
1249 | void HandleCaptured(Preprocessor &PP) { |
1250 | Token Tok; |
1251 | PP.LexUnexpandedToken(Tok); |
1252 | |
1253 | if (Tok.isNot(tok::eod)) { |
1254 | PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) |
1255 | << "pragma clang __debug captured"; |
1256 | return; |
1257 | } |
1258 | |
1259 | SourceLocation NameLoc = Tok.getLocation(); |
1260 | MutableArrayRef<Token> Toks( |
1261 | PP.getPreprocessorAllocator().Allocate<Token>(1), 1); |
1262 | Toks[0].startToken(); |
1263 | Toks[0].setKind(tok::annot_pragma_captured); |
1264 | Toks[0].setLocation(NameLoc); |
1265 | |
1266 | PP.EnterTokenStream(Toks, true, |
1267 | false); |
1268 | } |
1269 | |
1270 | |
1271 | #ifdef _MSC_VER |
1272 | #pragma warning(disable : 4717) |
1273 | #endif |
1274 | static void DebugOverflowStack(void (*P)() = nullptr) { |
1275 | void (*volatile Self)(void(*P)()) = DebugOverflowStack; |
1276 | Self(reinterpret_cast<void(*)()>(Self)); |
1277 | } |
1278 | #ifdef _MSC_VER |
1279 | #pragma warning(default : 4717) |
1280 | #endif |
1281 | }; |
1282 | |
1283 | |
1284 | struct PragmaDiagnosticHandler : public PragmaHandler { |
1285 | private: |
1286 | const char *Namespace; |
1287 | |
1288 | public: |
1289 | explicit PragmaDiagnosticHandler(const char *NS) |
1290 | : PragmaHandler("diagnostic"), Namespace(NS) {} |
1291 | |
1292 | void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer, |
1293 | Token &DiagToken) override { |
1294 | SourceLocation DiagLoc = DiagToken.getLocation(); |
1295 | Token Tok; |
1296 | PP.LexUnexpandedToken(Tok); |
1297 | if (Tok.isNot(tok::identifier)) { |
1298 | PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid); |
1299 | return; |
1300 | } |
1301 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
1302 | PPCallbacks *Callbacks = PP.getPPCallbacks(); |
1303 | |
1304 | if (II->isStr("pop")) { |
1305 | if (!PP.getDiagnostics().popMappings(DiagLoc)) |
1306 | PP.Diag(Tok, diag::warn_pragma_diagnostic_cannot_pop); |
1307 | else if (Callbacks) |
1308 | Callbacks->PragmaDiagnosticPop(DiagLoc, Namespace); |
1309 | return; |
1310 | } else if (II->isStr("push")) { |
1311 | PP.getDiagnostics().pushMappings(DiagLoc); |
1312 | if (Callbacks) |
1313 | Callbacks->PragmaDiagnosticPush(DiagLoc, Namespace); |
1314 | return; |
1315 | } |
1316 | |
1317 | diag::Severity SV = llvm::StringSwitch<diag::Severity>(II->getName()) |
1318 | .Case("ignored", diag::Severity::Ignored) |
1319 | .Case("warning", diag::Severity::Warning) |
1320 | .Case("error", diag::Severity::Error) |
1321 | .Case("fatal", diag::Severity::Fatal) |
1322 | .Default(diag::Severity()); |
1323 | |
1324 | if (SV == diag::Severity()) { |
1325 | PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid); |
1326 | return; |
1327 | } |
1328 | |
1329 | PP.LexUnexpandedToken(Tok); |
1330 | SourceLocation StringLoc = Tok.getLocation(); |
1331 | |
1332 | std::string WarningName; |
1333 | if (!PP.FinishLexStringLiteral(Tok, WarningName, "pragma diagnostic", |
1334 | false)) |
1335 | return; |
1336 | |
1337 | if (Tok.isNot(tok::eod)) { |
1338 | PP.Diag(Tok.getLocation(), diag::warn_pragma_diagnostic_invalid_token); |
1339 | return; |
1340 | } |
1341 | |
1342 | if (WarningName.size() < 3 || WarningName[0] != '-' || |
1343 | (WarningName[1] != 'W' && WarningName[1] != 'R')) { |
1344 | PP.Diag(StringLoc, diag::warn_pragma_diagnostic_invalid_option); |
1345 | return; |
1346 | } |
1347 | |
1348 | diag::Flavor Flavor = WarningName[1] == 'W' ? diag::Flavor::WarningOrError |
1349 | : diag::Flavor::Remark; |
1350 | StringRef Group = StringRef(WarningName).substr(2); |
1351 | bool unknownDiag = false; |
1352 | if (Group == "everything") { |
1353 | |
1354 | |
1355 | |
1356 | PP.getDiagnostics().setSeverityForAll(Flavor, SV, DiagLoc); |
1357 | } else |
1358 | unknownDiag = PP.getDiagnostics().setSeverityForGroup(Flavor, Group, SV, |
1359 | DiagLoc); |
1360 | if (unknownDiag) |
1361 | PP.Diag(StringLoc, diag::warn_pragma_diagnostic_unknown_warning) |
1362 | << WarningName; |
1363 | else if (Callbacks) |
1364 | Callbacks->PragmaDiagnostic(DiagLoc, Namespace, SV, WarningName); |
1365 | } |
1366 | }; |
1367 | |
1368 | |
1369 | struct PragmaHdrstopHandler : public PragmaHandler { |
1370 | PragmaHdrstopHandler() : PragmaHandler("hdrstop") {} |
1371 | void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer, |
1372 | Token &DepToken) override { |
1373 | PP.HandlePragmaHdrstop(DepToken); |
1374 | } |
1375 | }; |
1376 | |
1377 | |
1378 | |
1379 | |
1380 | struct PragmaWarningHandler : public PragmaHandler { |
1381 | PragmaWarningHandler() : PragmaHandler("warning") {} |
1382 | |
1383 | void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer, |
1384 | Token &Tok) override { |
1385 | |
1386 | |
1387 | |
1388 | |
1389 | SourceLocation DiagLoc = Tok.getLocation(); |
1390 | PPCallbacks *Callbacks = PP.getPPCallbacks(); |
1391 | |
1392 | PP.Lex(Tok); |
1393 | if (Tok.isNot(tok::l_paren)) { |
1394 | PP.Diag(Tok, diag::warn_pragma_warning_expected) << "("; |
1395 | return; |
1396 | } |
1397 | |
1398 | PP.Lex(Tok); |
1399 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
1400 | |
1401 | if (II && II->isStr("push")) { |
1402 | |
1403 | int Level = -1; |
1404 | PP.Lex(Tok); |
1405 | if (Tok.is(tok::comma)) { |
1406 | PP.Lex(Tok); |
1407 | uint64_t Value; |
1408 | if (Tok.is(tok::numeric_constant) && |
1409 | PP.parseSimpleIntegerLiteral(Tok, Value)) |
1410 | Level = int(Value); |
1411 | if (Level < 0 || Level > 4) { |
1412 | PP.Diag(Tok, diag::warn_pragma_warning_push_level); |
1413 | return; |
1414 | } |
1415 | } |
1416 | if (Callbacks) |
1417 | Callbacks->PragmaWarningPush(DiagLoc, Level); |
1418 | } else if (II && II->isStr("pop")) { |
1419 | |
1420 | PP.Lex(Tok); |
1421 | if (Callbacks) |
1422 | Callbacks->PragmaWarningPop(DiagLoc); |
1423 | } else { |
1424 | |
1425 | |
1426 | while (true) { |
1427 | II = Tok.getIdentifierInfo(); |
1428 | if (!II && !Tok.is(tok::numeric_constant)) { |
1429 | PP.Diag(Tok, diag::warn_pragma_warning_spec_invalid); |
1430 | return; |
1431 | } |
1432 | |
1433 | |
1434 | bool SpecifierValid; |
1435 | StringRef Specifier; |
1436 | llvm::SmallString<1> SpecifierBuf; |
1437 | if (II) { |
1438 | Specifier = II->getName(); |
1439 | SpecifierValid = llvm::StringSwitch<bool>(Specifier) |
1440 | .Cases("default", "disable", "error", "once", |
1441 | "suppress", true) |
1442 | .Default(false); |
1443 | |
1444 | |
1445 | if (SpecifierValid) |
1446 | PP.Lex(Tok); |
1447 | } else { |
1448 | |
1449 | uint64_t Value; |
1450 | Specifier = PP.getSpelling(Tok, SpecifierBuf); |
1451 | if (PP.parseSimpleIntegerLiteral(Tok, Value)) { |
1452 | SpecifierValid = (Value >= 1) && (Value <= 4); |
1453 | } else |
1454 | SpecifierValid = false; |
1455 | |
1456 | } |
1457 | |
1458 | if (!SpecifierValid) { |
1459 | PP.Diag(Tok, diag::warn_pragma_warning_spec_invalid); |
1460 | return; |
1461 | } |
1462 | if (Tok.isNot(tok::colon)) { |
1463 | PP.Diag(Tok, diag::warn_pragma_warning_expected) << ":"; |
1464 | return; |
1465 | } |
1466 | |
1467 | |
1468 | SmallVector<int, 4> Ids; |
1469 | PP.Lex(Tok); |
1470 | while (Tok.is(tok::numeric_constant)) { |
1471 | uint64_t Value; |
1472 | if (!PP.parseSimpleIntegerLiteral(Tok, Value) || Value == 0 || |
1473 | Value > INT_MAX) { |
1474 | PP.Diag(Tok, diag::warn_pragma_warning_expected_number); |
1475 | return; |
1476 | } |
1477 | Ids.push_back(int(Value)); |
1478 | } |
1479 | if (Callbacks) |
1480 | Callbacks->PragmaWarning(DiagLoc, Specifier, Ids); |
1481 | |
1482 | |
1483 | if (Tok.isNot(tok::semi)) |
1484 | break; |
1485 | PP.Lex(Tok); |
1486 | } |
1487 | } |
1488 | |
1489 | if (Tok.isNot(tok::r_paren)) { |
1490 | PP.Diag(Tok, diag::warn_pragma_warning_expected) << ")"; |
1491 | return; |
1492 | } |
1493 | |
1494 | PP.Lex(Tok); |
1495 | if (Tok.isNot(tok::eod)) |
1496 | PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma warning"; |
1497 | } |
1498 | }; |
1499 | |
1500 | |
1501 | |
1502 | |
1503 | struct PragmaExecCharsetHandler : public PragmaHandler { |
1504 | PragmaExecCharsetHandler() : PragmaHandler("execution_character_set") {} |
1505 | |
1506 | void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer, |
1507 | Token &Tok) override { |
1508 | |
1509 | |
1510 | |
1511 | SourceLocation DiagLoc = Tok.getLocation(); |
1512 | PPCallbacks *Callbacks = PP.getPPCallbacks(); |
1513 | |
1514 | PP.Lex(Tok); |
1515 | if (Tok.isNot(tok::l_paren)) { |
1516 | PP.Diag(Tok, diag::warn_pragma_exec_charset_expected) << "("; |
1517 | return; |
1518 | } |
1519 | |
1520 | PP.Lex(Tok); |
1521 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
1522 | |
1523 | if (II && II->isStr("push")) { |
1524 | |
1525 | PP.Lex(Tok); |
1526 | if (Tok.is(tok::comma)) { |
1527 | PP.Lex(Tok); |
1528 | |
1529 | std::string ExecCharset; |
1530 | if (!PP.FinishLexStringLiteral(Tok, ExecCharset, |
1531 | "pragma execution_character_set", |
1532 | false)) |
1533 | return; |
1534 | |
1535 | |
1536 | if (ExecCharset != "UTF-8" && ExecCharset != "utf-8") { |
1537 | PP.Diag(Tok, diag::warn_pragma_exec_charset_push_invalid) << ExecCharset; |
1538 | return; |
1539 | } |
1540 | } |
1541 | if (Callbacks) |
1542 | Callbacks->PragmaExecCharsetPush(DiagLoc, "UTF-8"); |
1543 | } else if (II && II->isStr("pop")) { |
1544 | |
1545 | PP.Lex(Tok); |
1546 | if (Callbacks) |
1547 | Callbacks->PragmaExecCharsetPop(DiagLoc); |
1548 | } else { |
1549 | PP.Diag(Tok, diag::warn_pragma_exec_charset_spec_invalid); |
1550 | return; |
1551 | } |
1552 | |
1553 | if (Tok.isNot(tok::r_paren)) { |
1554 | PP.Diag(Tok, diag::warn_pragma_exec_charset_expected) << ")"; |
1555 | return; |
1556 | } |
1557 | |
1558 | PP.Lex(Tok); |
1559 | if (Tok.isNot(tok::eod)) |
1560 | PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma execution_character_set"; |
1561 | } |
1562 | }; |
1563 | |
1564 | |
1565 | struct PragmaIncludeAliasHandler : public PragmaHandler { |
1566 | PragmaIncludeAliasHandler() : PragmaHandler("include_alias") {} |
1567 | |
1568 | void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer, |
1569 | Token &IncludeAliasTok) override { |
1570 | PP.HandlePragmaIncludeAlias(IncludeAliasTok); |
1571 | } |
1572 | }; |
1573 | |
1574 | |
1575 | |
1576 | |
1577 | |
1578 | |
1579 | |
1580 | |
1581 | |
1582 | |
1583 | |
1584 | |
1585 | |
1586 | |
1587 | struct PragmaMessageHandler : public PragmaHandler { |
1588 | private: |
1589 | const PPCallbacks::PragmaMessageKind Kind; |
1590 | const StringRef Namespace; |
1591 | |
1592 | static const char* PragmaKind(PPCallbacks::PragmaMessageKind Kind, |
1593 | bool PragmaNameOnly = false) { |
1594 | switch (Kind) { |
1595 | case PPCallbacks::PMK_Message: |
1596 | return PragmaNameOnly ? "message" : "pragma message"; |
1597 | case PPCallbacks::PMK_Warning: |
1598 | return PragmaNameOnly ? "warning" : "pragma warning"; |
1599 | case PPCallbacks::PMK_Error: |
1600 | return PragmaNameOnly ? "error" : "pragma error"; |
1601 | } |
1602 | llvm_unreachable("Unknown PragmaMessageKind!"); |
1603 | } |
1604 | |
1605 | public: |
1606 | PragmaMessageHandler(PPCallbacks::PragmaMessageKind Kind, |
1607 | StringRef Namespace = StringRef()) |
1608 | : PragmaHandler(PragmaKind(Kind, true)), Kind(Kind), |
1609 | Namespace(Namespace) {} |
1610 | |
1611 | void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer, |
1612 | Token &Tok) override { |
1613 | SourceLocation MessageLoc = Tok.getLocation(); |
1614 | PP.Lex(Tok); |
1615 | bool ExpectClosingParen = false; |
1616 | switch (Tok.getKind()) { |
1617 | case tok::l_paren: |
1618 | |
1619 | ExpectClosingParen = true; |
1620 | |
1621 | PP.Lex(Tok); |
1622 | break; |
1623 | case tok::string_literal: |
1624 | |
1625 | break; |
1626 | default: |
1627 | PP.Diag(MessageLoc, diag::err_pragma_message_malformed) << Kind; |
1628 | return; |
1629 | } |
1630 | |
1631 | std::string MessageString; |
1632 | if (!PP.FinishLexStringLiteral(Tok, MessageString, PragmaKind(Kind), |
1633 | true)) |
1634 | return; |
1635 | |
1636 | if (ExpectClosingParen) { |
1637 | if (Tok.isNot(tok::r_paren)) { |
1638 | PP.Diag(Tok.getLocation(), diag::err_pragma_message_malformed) << Kind; |
1639 | return; |
1640 | } |
1641 | PP.Lex(Tok); |
1642 | } |
1643 | |
1644 | if (Tok.isNot(tok::eod)) { |
1645 | PP.Diag(Tok.getLocation(), diag::err_pragma_message_malformed) << Kind; |
1646 | return; |
1647 | } |
1648 | |
1649 | |
1650 | PP.Diag(MessageLoc, (Kind == PPCallbacks::PMK_Error) |
1651 | ? diag::err_pragma_message |
1652 | : diag::warn_pragma_message) << MessageString; |
1653 | |
1654 | |
1655 | if (PPCallbacks *Callbacks = PP.getPPCallbacks()) |
1656 | Callbacks->PragmaMessage(MessageLoc, Namespace, Kind, MessageString); |
1657 | } |
1658 | }; |
1659 | |
1660 | |
1661 | |
1662 | |
1663 | |
1664 | struct PragmaModuleImportHandler : public PragmaHandler { |
1665 | PragmaModuleImportHandler() : PragmaHandler("import") {} |
1666 | |
1667 | void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer, |
1668 | Token &Tok) override { |
1669 | SourceLocation ImportLoc = Tok.getLocation(); |
1670 | |
1671 | |
1672 | llvm::SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 8> |
1673 | ModuleName; |
1674 | if (LexModuleName(PP, Tok, ModuleName)) |
1675 | return; |
1676 | |
1677 | if (Tok.isNot(tok::eod)) |
1678 | PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma"; |
1679 | |
1680 | |
1681 | Module *Imported = |
1682 | PP.getModuleLoader().loadModule(ImportLoc, ModuleName, Module::Hidden, |
1683 | false); |
1684 | if (!Imported) |
1685 | return; |
1686 | |
1687 | PP.makeModuleVisible(Imported, ImportLoc); |
1688 | PP.EnterAnnotationToken(SourceRange(ImportLoc, ModuleName.back().second), |
1689 | tok::annot_module_include, Imported); |
1690 | if (auto *CB = PP.getPPCallbacks()) |
1691 | CB->moduleImport(ImportLoc, ModuleName, Imported); |
1692 | } |
1693 | }; |
1694 | |
1695 | |
1696 | |
1697 | |
1698 | |
1699 | |
1700 | |
1701 | struct PragmaModuleBeginHandler : public PragmaHandler { |
1702 | PragmaModuleBeginHandler() : PragmaHandler("begin") {} |
1703 | |
1704 | void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer, |
1705 | Token &Tok) override { |
1706 | SourceLocation BeginLoc = Tok.getLocation(); |
1707 | |
1708 | |
1709 | llvm::SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 8> |
1710 | ModuleName; |
1711 | if (LexModuleName(PP, Tok, ModuleName)) |
1712 | return; |
1713 | |
1714 | if (Tok.isNot(tok::eod)) |
1715 | PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma"; |
1716 | |
1717 | |
1718 | StringRef Current = PP.getLangOpts().CurrentModule; |
1719 | if (ModuleName.front().first->getName() != Current) { |
1720 | PP.Diag(ModuleName.front().second, diag::err_pp_module_begin_wrong_module) |
1721 | << ModuleName.front().first << (ModuleName.size() > 1) |
1722 | << Current.empty() << Current; |
1723 | return; |
1724 | } |
1725 | |
1726 | |
1727 | |
1728 | auto &HSI = PP.getHeaderSearchInfo(); |
1729 | Module *M = HSI.lookupModule(Current); |
1730 | if (!M) { |
1731 | PP.Diag(ModuleName.front().second, |
1732 | diag::err_pp_module_begin_no_module_map) << Current; |
1733 | return; |
1734 | } |
1735 | for (unsigned I = 1; I != ModuleName.size(); ++I) { |
1736 | auto *NewM = M->findOrInferSubmodule(ModuleName[I].first->getName()); |
1737 | if (!NewM) { |
1738 | PP.Diag(ModuleName[I].second, diag::err_pp_module_begin_no_submodule) |
1739 | << M->getFullModuleName() << ModuleName[I].first; |
1740 | return; |
1741 | } |
1742 | M = NewM; |
1743 | } |
1744 | |
1745 | |
1746 | if (Preprocessor::checkModuleIsAvailable( |
1747 | PP.getLangOpts(), PP.getTargetInfo(), PP.getDiagnostics(), M)) { |
1748 | PP.Diag(BeginLoc, diag::note_pp_module_begin_here) |
1749 | << M->getTopLevelModuleName(); |
1750 | return; |
1751 | } |
1752 | |
1753 | |
1754 | PP.EnterSubmodule(M, BeginLoc, true); |
1755 | PP.EnterAnnotationToken(SourceRange(BeginLoc, ModuleName.back().second), |
1756 | tok::annot_module_begin, M); |
1757 | } |
1758 | }; |
1759 | |
1760 | |
1761 | struct PragmaModuleEndHandler : public PragmaHandler { |
1762 | PragmaModuleEndHandler() : PragmaHandler("end") {} |
1763 | |
1764 | void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer, |
1765 | Token &Tok) override { |
1766 | SourceLocation Loc = Tok.getLocation(); |
1767 | |
1768 | PP.LexUnexpandedToken(Tok); |
1769 | if (Tok.isNot(tok::eod)) |
1770 | PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma"; |
1771 | |
1772 | Module *M = PP.LeaveSubmodule(true); |
1773 | if (M) |
1774 | PP.EnterAnnotationToken(SourceRange(Loc), tok::annot_module_end, M); |
1775 | else |
1776 | PP.Diag(Loc, diag::err_pp_module_end_without_module_begin); |
1777 | } |
1778 | }; |
1779 | |
1780 | |
1781 | struct PragmaModuleBuildHandler : public PragmaHandler { |
1782 | PragmaModuleBuildHandler() : PragmaHandler("build") {} |
1783 | |
1784 | void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer, |
1785 | Token &Tok) override { |
1786 | PP.HandlePragmaModuleBuild(Tok); |
1787 | } |
1788 | }; |
1789 | |
1790 | |
1791 | struct PragmaModuleLoadHandler : public PragmaHandler { |
1792 | PragmaModuleLoadHandler() : PragmaHandler("load") {} |
1793 | |
1794 | void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer, |
1795 | Token &Tok) override { |
1796 | SourceLocation Loc = Tok.getLocation(); |
1797 | |
1798 | |
1799 | llvm::SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 8> |
1800 | ModuleName; |
1801 | if (LexModuleName(PP, Tok, ModuleName)) |
1802 | return; |
1803 | |
1804 | if (Tok.isNot(tok::eod)) |
1805 | PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma"; |
1806 | |
1807 | |
1808 | PP.getModuleLoader().loadModule(Loc, ModuleName, Module::Hidden, |
1809 | false); |
1810 | } |
1811 | }; |
1812 | |
1813 | |
1814 | |
1815 | struct PragmaPushMacroHandler : public PragmaHandler { |
1816 | PragmaPushMacroHandler() : PragmaHandler("push_macro") {} |
1817 | |
1818 | void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer, |
1819 | Token &PushMacroTok) override { |
1820 | PP.HandlePragmaPushMacro(PushMacroTok); |
1821 | } |
1822 | }; |
1823 | |
1824 | |
1825 | |
1826 | struct PragmaPopMacroHandler : public PragmaHandler { |
1827 | PragmaPopMacroHandler() : PragmaHandler("pop_macro") {} |
1828 | |
1829 | void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer, |
1830 | Token &PopMacroTok) override { |
1831 | PP.HandlePragmaPopMacro(PopMacroTok); |
1832 | } |
1833 | }; |
1834 | |
1835 | |
1836 | |
1837 | struct PragmaARCCFCodeAuditedHandler : public PragmaHandler { |
1838 | PragmaARCCFCodeAuditedHandler() : PragmaHandler("arc_cf_code_audited") {} |
1839 | |
1840 | void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer, |
1841 | Token &NameTok) override { |
1842 | SourceLocation Loc = NameTok.getLocation(); |
1843 | bool IsBegin; |
1844 | |
1845 | Token Tok; |
1846 | |
1847 | |
1848 | PP.LexUnexpandedToken(Tok); |
1849 | const IdentifierInfo *BeginEnd = Tok.getIdentifierInfo(); |
1850 | if (BeginEnd && BeginEnd->isStr("begin")) { |
1851 | IsBegin = true; |
1852 | } else if (BeginEnd && BeginEnd->isStr("end")) { |
1853 | IsBegin = false; |
1854 | } else { |
1855 | PP.Diag(Tok.getLocation(), diag::err_pp_arc_cf_code_audited_syntax); |
1856 | return; |
1857 | } |
1858 | |
1859 | |
1860 | PP.LexUnexpandedToken(Tok); |
1861 | if (Tok.isNot(tok::eod)) |
1862 | PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma"; |
1863 | |
1864 | |
1865 | SourceLocation BeginLoc = PP.getPragmaARCCFCodeAuditedInfo().second; |
1866 | |
1867 | |
1868 | SourceLocation NewLoc; |
1869 | |
1870 | if (IsBegin) { |
1871 | |
1872 | if (BeginLoc.isValid()) { |
1873 | PP.Diag(Loc, diag::err_pp_double_begin_of_arc_cf_code_audited); |
1874 | PP.Diag(BeginLoc, diag::note_pragma_entered_here); |
1875 | } |
1876 | NewLoc = Loc; |
1877 | } else { |
1878 | |
1879 | if (!BeginLoc.isValid()) { |
1880 | PP.Diag(Loc, diag::err_pp_unmatched_end_of_arc_cf_code_audited); |
1881 | return; |
1882 | } |
1883 | NewLoc = SourceLocation(); |
1884 | } |
1885 | |
1886 | PP.setPragmaARCCFCodeAuditedInfo(NameTok.getIdentifierInfo(), NewLoc); |
1887 | } |
1888 | }; |
1889 | |
1890 | |
1891 | |
1892 | struct PragmaAssumeNonNullHandler : public PragmaHandler { |
1893 | PragmaAssumeNonNullHandler() : PragmaHandler("assume_nonnull") {} |
1894 | |
1895 | void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer, |
1896 | Token &NameTok) override { |
1897 | SourceLocation Loc = NameTok.getLocation(); |
1898 | bool IsBegin; |
1899 | |
1900 | Token Tok; |
1901 | |
1902 | |
1903 | PP.LexUnexpandedToken(Tok); |
1904 | const IdentifierInfo *BeginEnd = Tok.getIdentifierInfo(); |
1905 | if (BeginEnd && BeginEnd->isStr("begin")) { |
1906 | IsBegin = true; |
1907 | } else if (BeginEnd && BeginEnd->isStr("end")) { |
1908 | IsBegin = false; |
1909 | } else { |
1910 | PP.Diag(Tok.getLocation(), diag::err_pp_assume_nonnull_syntax); |
1911 | return; |
1912 | } |
1913 | |
1914 | |
1915 | PP.LexUnexpandedToken(Tok); |
1916 | if (Tok.isNot(tok::eod)) |
1917 | PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma"; |
1918 | |
1919 | |
1920 | SourceLocation BeginLoc = PP.getPragmaAssumeNonNullLoc(); |
1921 | |
1922 | |
1923 | SourceLocation NewLoc; |
1924 | PPCallbacks *Callbacks = PP.getPPCallbacks(); |
1925 | |
1926 | if (IsBegin) { |
1927 | |
1928 | if (BeginLoc.isValid()) { |
1929 | PP.Diag(Loc, diag::err_pp_double_begin_of_assume_nonnull); |
1930 | PP.Diag(BeginLoc, diag::note_pragma_entered_here); |
1931 | } |
1932 | NewLoc = Loc; |
1933 | if (Callbacks) |
1934 | Callbacks->PragmaAssumeNonNullBegin(NewLoc); |
1935 | } else { |
1936 | |
1937 | if (!BeginLoc.isValid()) { |
1938 | PP.Diag(Loc, diag::err_pp_unmatched_end_of_assume_nonnull); |
1939 | return; |
1940 | } |
1941 | NewLoc = SourceLocation(); |
1942 | if (Callbacks) |
1943 | Callbacks->PragmaAssumeNonNullEnd(NewLoc); |
1944 | } |
1945 | |
1946 | PP.setPragmaAssumeNonNullLoc(NewLoc); |
1947 | } |
1948 | }; |
1949 | |
1950 | |
1951 | |
1952 | |
1953 | |
1954 | |
1955 | |
1956 | |
1957 | |
1958 | |
1959 | |
1960 | |
1961 | struct PragmaRegionHandler : public PragmaHandler { |
1962 | PragmaRegionHandler(const char *pragma) : PragmaHandler(pragma) {} |
1963 | |
1964 | void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer, |
1965 | Token &NameTok) override { |
1966 | |
1967 | |
1968 | |
1969 | |
1970 | } |
1971 | }; |
1972 | |
1973 | } |
1974 | |
1975 | |
1976 | |
1977 | void Preprocessor::RegisterBuiltinPragmas() { |
1978 | AddPragmaHandler(new PragmaOnceHandler()); |
1979 | AddPragmaHandler(new PragmaMarkHandler()); |
1980 | AddPragmaHandler(new PragmaPushMacroHandler()); |
1981 | AddPragmaHandler(new PragmaPopMacroHandler()); |
1982 | AddPragmaHandler(new PragmaMessageHandler(PPCallbacks::PMK_Message)); |
1983 | |
1984 | |
1985 | AddPragmaHandler("GCC", new PragmaPoisonHandler()); |
1986 | AddPragmaHandler("GCC", new PragmaSystemHeaderHandler()); |
1987 | AddPragmaHandler("GCC", new PragmaDependencyHandler()); |
1988 | AddPragmaHandler("GCC", new PragmaDiagnosticHandler("GCC")); |
1989 | AddPragmaHandler("GCC", new PragmaMessageHandler(PPCallbacks::PMK_Warning, |
1990 | "GCC")); |
1991 | AddPragmaHandler("GCC", new PragmaMessageHandler(PPCallbacks::PMK_Error, |
1992 | "GCC")); |
1993 | |
1994 | AddPragmaHandler("clang", new PragmaPoisonHandler()); |
1995 | AddPragmaHandler("clang", new PragmaSystemHeaderHandler()); |
1996 | AddPragmaHandler("clang", new PragmaIncludeInsteadHandler()); |
1997 | AddPragmaHandler("clang", new PragmaDebugHandler()); |
1998 | AddPragmaHandler("clang", new PragmaDependencyHandler()); |
1999 | AddPragmaHandler("clang", new PragmaDiagnosticHandler("clang")); |
2000 | AddPragmaHandler("clang", new PragmaARCCFCodeAuditedHandler()); |
2001 | AddPragmaHandler("clang", new PragmaAssumeNonNullHandler()); |
2002 | |
2003 | |
2004 | auto *ModuleHandler = new PragmaNamespace("module"); |
2005 | AddPragmaHandler("clang", ModuleHandler); |
2006 | ModuleHandler->AddPragma(new PragmaModuleImportHandler()); |
2007 | ModuleHandler->AddPragma(new PragmaModuleBeginHandler()); |
2008 | ModuleHandler->AddPragma(new PragmaModuleEndHandler()); |
2009 | ModuleHandler->AddPragma(new PragmaModuleBuildHandler()); |
2010 | ModuleHandler->AddPragma(new PragmaModuleLoadHandler()); |
2011 | |
2012 | |
2013 | AddPragmaHandler(new PragmaRegionHandler("region")); |
2014 | AddPragmaHandler(new PragmaRegionHandler("endregion")); |
2015 | |
2016 | |
2017 | if (LangOpts.MicrosoftExt) { |
2018 | AddPragmaHandler(new PragmaWarningHandler()); |
2019 | AddPragmaHandler(new PragmaExecCharsetHandler()); |
2020 | AddPragmaHandler(new PragmaIncludeAliasHandler()); |
2021 | AddPragmaHandler(new PragmaHdrstopHandler()); |
2022 | AddPragmaHandler(new PragmaSystemHeaderHandler()); |
2023 | } |
2024 | |
2025 | |
2026 | for (const PragmaHandlerRegistry::entry &handler : |
2027 | PragmaHandlerRegistry::entries()) { |
2028 | AddPragmaHandler(handler.instantiate().release()); |
2029 | } |
2030 | } |
2031 | |
2032 | |
2033 | |
2034 | void Preprocessor::IgnorePragmas() { |
2035 | AddPragmaHandler(new EmptyPragmaHandler()); |
2036 | |
2037 | |
2038 | AddPragmaHandler("GCC", new EmptyPragmaHandler()); |
2039 | AddPragmaHandler("clang", new EmptyPragmaHandler()); |
2040 | } |
1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | |
14 | |
15 | #ifndef LLVM_CLANG_BASIC_IDENTIFIERTABLE_H |
16 | #define LLVM_CLANG_BASIC_IDENTIFIERTABLE_H |
17 | |
18 | #include "clang/Basic/LLVM.h" |
19 | #include "clang/Basic/TokenKinds.h" |
20 | #include "llvm/ADT/DenseMapInfo.h" |
21 | #include "llvm/ADT/SmallString.h" |
22 | #include "llvm/ADT/StringMap.h" |
23 | #include "llvm/ADT/StringRef.h" |
24 | #include "llvm/Support/Allocator.h" |
25 | #include "llvm/Support/PointerLikeTypeTraits.h" |
26 | #include "llvm/Support/type_traits.h" |
27 | #include <cassert> |
28 | #include <cstddef> |
29 | #include <cstdint> |
30 | #include <cstring> |
31 | #include <string> |
32 | #include <utility> |
33 | |
34 | namespace clang { |
35 | |
36 | class DeclarationName; |
37 | class DeclarationNameTable; |
38 | class IdentifierInfo; |
39 | class LangOptions; |
40 | class MultiKeywordSelector; |
41 | class SourceLocation; |
42 | |
43 | enum class ReservedIdentifierStatus { |
44 | NotReserved = 0, |
45 | StartsWithUnderscoreAtGlobalScope, |
46 | StartsWithDoubleUnderscore, |
47 | StartsWithUnderscoreFollowedByCapitalLetter, |
48 | ContainsDoubleUnderscore, |
49 | }; |
50 | |
51 | |
52 | using IdentifierLocPair = std::pair<IdentifierInfo *, SourceLocation>; |
53 | |
54 | |
55 | |
56 | |
57 | enum { IdentifierInfoAlignment = 8 }; |
58 | |
59 | static constexpr int ObjCOrBuiltinIDBits = 16; |
60 | |
61 | |
62 | |
63 | |
64 | |
65 | |
66 | |
67 | class alignas(IdentifierInfoAlignment) IdentifierInfo { |
68 | friend class IdentifierTable; |
69 | |
70 | |
71 | unsigned TokenID : 9; |
72 | |
73 | |
74 | |
75 | |
76 | unsigned ObjCOrBuiltinID : ObjCOrBuiltinIDBits; |
77 | |
78 | |
79 | unsigned HasMacro : 1; |
80 | |
81 | |
82 | unsigned HadMacro : 1; |
83 | |
84 | |
85 | unsigned IsExtension : 1; |
86 | |
87 | |
88 | unsigned IsFutureCompatKeyword : 1; |
89 | |
90 | |
91 | unsigned IsPoisoned : 1; |
92 | |
93 | |
94 | unsigned IsCPPOperatorKeyword : 1; |
95 | |
96 | |
97 | |
98 | unsigned NeedsHandleIdentifier : 1; |
99 | |
100 | |
101 | unsigned IsFromAST : 1; |
102 | |
103 | |
104 | |
105 | unsigned ChangedAfterLoad : 1; |
106 | |
107 | |
108 | |
109 | unsigned FEChangedAfterLoad : 1; |
110 | |
111 | |
112 | unsigned RevertedTokenID : 1; |
113 | |
114 | |
115 | |
116 | unsigned OutOfDate : 1; |
117 | |
118 | |
119 | unsigned IsModulesImport : 1; |
120 | |
121 | |
122 | unsigned IsMangledOpenMPVariantName : 1; |
123 | |
124 | |
125 | |
126 | |
127 | void *FETokenInfo = nullptr; |
128 | |
129 | llvm::StringMapEntry<IdentifierInfo *> *Entry = nullptr; |
130 | |
131 | IdentifierInfo() |
132 | : TokenID(tok::identifier), ObjCOrBuiltinID(0), HasMacro(false), |
133 | HadMacro(false), IsExtension(false), IsFutureCompatKeyword(false), |
134 | IsPoisoned(false), IsCPPOperatorKeyword(false), |
135 | NeedsHandleIdentifier(false), IsFromAST(false), ChangedAfterLoad(false), |
136 | FEChangedAfterLoad(false), RevertedTokenID(false), OutOfDate(false), |
137 | IsModulesImport(false), IsMangledOpenMPVariantName(false) {} |
138 | |
139 | public: |
140 | IdentifierInfo(const IdentifierInfo &) = delete; |
141 | IdentifierInfo &operator=(const IdentifierInfo &) = delete; |
142 | IdentifierInfo(IdentifierInfo &&) = delete; |
143 | IdentifierInfo &operator=(IdentifierInfo &&) = delete; |
144 | |
145 | |
146 | |
147 | |
148 | template <std::size_t StrLen> |
149 | bool isStr(const char (&Str)[StrLen]) const { |
150 | return getLength() == StrLen-1 && |
| 7 | | Assuming the condition is false | |
|
| 8 | | Returning zero, which participates in a condition later | |
|
| 12 | | Assuming the condition is false | |
|
| 13 | | Returning zero, which participates in a condition later | |
|
| 17 | | Assuming the condition is false | |
|
| 18 | | Returning zero, which participates in a condition later | |
|
| 22 | | Assuming the condition is false | |
|
| 23 | | Returning zero, which participates in a condition later | |
|
| 27 | | Returning zero, which participates in a condition later | |
|
| 31 | | Assuming the condition is false | |
|
| 32 | | Returning zero, which participates in a condition later | |
|
| 36 | | Returning zero, which participates in a condition later | |
|
| 40 | | Returning zero, which participates in a condition later | |
|
| 44 | | Assuming the condition is true | |
|
| 46 | | Returning the value 1, which participates in a condition later | |
|
151 | memcmp(getNameStart(), Str, StrLen-1) == 0; |
| 45 | | Assuming the condition is true | |
|
152 | } |
153 | |
154 | |
155 | bool isStr(llvm::StringRef Str) const { |
156 | llvm::StringRef ThisStr(getNameStart(), getLength()); |
157 | return ThisStr == Str; |
158 | } |
159 | |
160 | |
161 | |
162 | const char *getNameStart() const { return Entry->getKeyData(); } |
163 | |
164 | |
165 | unsigned getLength() const { return Entry->getKeyLength(); } |
166 | |
167 | |
168 | StringRef getName() const { |
169 | return StringRef(getNameStart(), getLength()); |
170 | } |
171 | |
172 | |
173 | |
174 | bool hasMacroDefinition() const { |
175 | return HasMacro; |
176 | } |
177 | void setHasMacroDefinition(bool Val) { |
178 | if (HasMacro == Val) return; |
179 | |
180 | HasMacro = Val; |
181 | if (Val) { |
182 | NeedsHandleIdentifier = true; |
183 | HadMacro = true; |
184 | } else { |
185 | RecomputeNeedsHandleIdentifier(); |
186 | } |
187 | } |
188 | |
189 | |
190 | |
191 | bool hadMacroDefinition() const { |
192 | return HadMacro; |
193 | } |
194 | |
195 | |
196 | |
197 | |
198 | tok::TokenKind getTokenID() const { return (tok::TokenKind)TokenID; } |
199 | |
200 | |
201 | bool hasRevertedTokenIDToIdentifier() const { return RevertedTokenID; } |
202 | |
203 | |
204 | |
205 | |
206 | |
207 | |
208 | |
209 | void revertTokenIDToIdentifier() { |
210 | assert(TokenID != tok::identifier && "Already at tok::identifier"); |
211 | TokenID = tok::identifier; |
212 | RevertedTokenID = true; |
213 | } |
214 | void revertIdentifierToTokenID(tok::TokenKind TK) { |
215 | assert(TokenID == tok::identifier && "Should be at tok::identifier"); |
216 | TokenID = TK; |
217 | RevertedTokenID = false; |
218 | } |
219 | |
220 | |
221 | |
222 | |
223 | tok::PPKeywordKind getPPKeywordID() const; |
224 | |
225 | |
226 | |
227 | |
228 | tok::ObjCKeywordKind getObjCKeywordID() const { |
229 | if (ObjCOrBuiltinID < tok::NUM_OBJC_KEYWORDS) |
230 | return tok::ObjCKeywordKind(ObjCOrBuiltinID); |
231 | else |
232 | return tok::objc_not_keyword; |
233 | } |
234 | void setObjCKeywordID(tok::ObjCKeywordKind ID) { ObjCOrBuiltinID = ID; } |
235 | |
236 | |
237 | |
238 | |
239 | unsigned getBuiltinID() const { |
240 | if (ObjCOrBuiltinID >= tok::NUM_OBJC_KEYWORDS) |
241 | return ObjCOrBuiltinID - tok::NUM_OBJC_KEYWORDS; |
242 | else |
243 | return 0; |
244 | } |
245 | void setBuiltinID(unsigned ID) { |
246 | ObjCOrBuiltinID = ID + tok::NUM_OBJC_KEYWORDS; |
247 | assert(ObjCOrBuiltinID - unsigned(tok::NUM_OBJC_KEYWORDS) == ID |
248 | && "ID too large for field!"); |
249 | } |
250 | |
251 | unsigned getObjCOrBuiltinID() const { return ObjCOrBuiltinID; } |
252 | void setObjCOrBuiltinID(unsigned ID) { ObjCOrBuiltinID = ID; } |
253 | |
254 | |
255 | |
256 | |
257 | bool isExtensionToken() const { return IsExtension; } |
258 | void setIsExtensionToken(bool Val) { |
259 | IsExtension = Val; |
260 | if (Val) |
261 | NeedsHandleIdentifier = true; |
262 | else |
263 | RecomputeNeedsHandleIdentifier(); |
264 | } |
265 | |
266 | |
267 | |
268 | |
269 | |
270 | |
271 | bool isFutureCompatKeyword() const { return IsFutureCompatKeyword; } |
272 | void setIsFutureCompatKeyword(bool Val) { |
273 | IsFutureCompatKeyword = Val; |
274 | if (Val) |
275 | NeedsHandleIdentifier = true; |
276 | else |
277 | RecomputeNeedsHandleIdentifier(); |
278 | } |
279 | |
280 | |
281 | |
282 | void setIsPoisoned(bool Value = true) { |
283 | IsPoisoned = Value; |
284 | if (Value) |
285 | NeedsHandleIdentifier = true; |
286 | else |
287 | RecomputeNeedsHandleIdentifier(); |
288 | } |
289 | |
290 | |
291 | bool isPoisoned() const { return IsPoisoned; } |
292 | |
293 | |
294 | |
295 | void setIsCPlusPlusOperatorKeyword(bool Val = true) { |
296 | IsCPPOperatorKeyword = Val; |
297 | } |
298 | bool isCPlusPlusOperatorKeyword() const { return IsCPPOperatorKeyword; } |
299 | |
300 | |
301 | bool isKeyword(const LangOptions &LangOpts) const; |
302 | |
303 | |
304 | |
305 | bool isCPlusPlusKeyword(const LangOptions &LangOpts) const; |
306 | |
307 | |
308 | |
309 | void *getFETokenInfo() const { return FETokenInfo; } |
310 | void setFETokenInfo(void *T) { FETokenInfo = T; } |
311 | |
312 | |
313 | |
314 | |
315 | |
316 | |
317 | bool isHandleIdentifierCase() const { return NeedsHandleIdentifier; } |
318 | |
319 | |
320 | |
321 | bool isFromAST() const { return IsFromAST; } |
322 | |
323 | void setIsFromAST() { IsFromAST = true; } |
324 | |
325 | |
326 | |
327 | bool hasChangedSinceDeserialization() const { |
328 | return ChangedAfterLoad; |
329 | } |
330 | |
331 | |
332 | |
333 | void setChangedSinceDeserialization() { |
334 | ChangedAfterLoad = true; |
335 | } |
336 | |
337 | |
338 | |
339 | bool hasFETokenInfoChangedSinceDeserialization() const { |
340 | return FEChangedAfterLoad; |
341 | } |
342 | |
343 | |
344 | |
345 | void setFETokenInfoChangedSinceDeserialization() { |
346 | FEChangedAfterLoad = true; |
347 | } |
348 | |
349 | |
350 | |
351 | bool isOutOfDate() const { return OutOfDate; } |
352 | |
353 | |
354 | |
355 | void setOutOfDate(bool OOD) { |
356 | OutOfDate = OOD; |
357 | if (OOD) |
358 | NeedsHandleIdentifier = true; |
359 | else |
360 | RecomputeNeedsHandleIdentifier(); |
361 | } |
362 | |
363 | |
364 | bool isModulesImport() const { return IsModulesImport; } |
365 | |
366 | |
367 | void setModulesImport(bool I) { |
368 | IsModulesImport = I; |
369 | if (I) |
370 | NeedsHandleIdentifier = true; |
371 | else |
372 | RecomputeNeedsHandleIdentifier(); |
373 | } |
374 | |
375 | |
376 | bool isMangledOpenMPVariantName() const { return IsMangledOpenMPVariantName; } |
377 | |
378 | |
379 | void setMangledOpenMPVariantName(bool I) { IsMangledOpenMPVariantName = I; } |
380 | |
381 | |
382 | |
383 | |
384 | |
385 | |
386 | |
387 | |
388 | |
389 | |
390 | bool isEditorPlaceholder() const { |
391 | return getName().startswith("<#") && getName().endswith("#>"); |
392 | } |
393 | |
394 | |
395 | |
396 | ReservedIdentifierStatus isReserved(const LangOptions &LangOpts) const; |
397 | |
398 | |
399 | bool operator<(const IdentifierInfo &RHS) const { |
400 | return getName() < RHS.getName(); |
401 | } |
402 | |
403 | private: |
404 | |
405 | |
406 | |
407 | |
408 | |
409 | |
410 | void RecomputeNeedsHandleIdentifier() { |
411 | NeedsHandleIdentifier = isPoisoned() || hasMacroDefinition() || |
412 | isExtensionToken() || isFutureCompatKeyword() || |
413 | isOutOfDate() || isModulesImport(); |
414 | } |
415 | }; |
416 | |
417 | |
418 | |
419 | |
420 | |
421 | class PoisonIdentifierRAIIObject { |
422 | IdentifierInfo *const II; |
423 | const bool OldValue; |
424 | |
425 | public: |
426 | PoisonIdentifierRAIIObject(IdentifierInfo *II, bool NewValue) |
427 | : II(II), OldValue(II ? II->isPoisoned() : false) { |
428 | if(II) |
429 | II->setIsPoisoned(NewValue); |
430 | } |
431 | |
432 | ~PoisonIdentifierRAIIObject() { |
433 | if(II) |
434 | II->setIsPoisoned(OldValue); |
435 | } |
436 | }; |
437 | |
438 | |
439 | |
440 | |
441 | |
442 | |
443 | |
444 | |
445 | |
446 | |
447 | |
448 | class IdentifierIterator { |
449 | protected: |
450 | IdentifierIterator() = default; |
451 | |
452 | public: |
453 | IdentifierIterator(const IdentifierIterator &) = delete; |
454 | IdentifierIterator &operator=(const IdentifierIterator &) = delete; |
455 | |
456 | virtual ~IdentifierIterator(); |
457 | |
458 | |
459 | |
460 | |
461 | |
462 | |
463 | virtual StringRef Next() = 0; |
464 | }; |
465 | |
466 | |
467 | class IdentifierInfoLookup { |
468 | public: |
469 | virtual ~IdentifierInfoLookup(); |
470 | |
471 | |
472 | |
473 | |
474 | |
475 | |
476 | virtual IdentifierInfo* get(StringRef Name) = 0; |
477 | |
478 | |
479 | |
480 | |
481 | |
482 | |
483 | |
484 | |
485 | |
486 | |
487 | |
488 | virtual IdentifierIterator *getIdentifiers(); |
489 | }; |
490 | |
491 | |
492 | |
493 | |
494 | |
495 | |
496 | class IdentifierTable { |
497 | |
498 | |
499 | using HashTableTy = llvm::StringMap<IdentifierInfo *, llvm::BumpPtrAllocator>; |
500 | HashTableTy HashTable; |
501 | |
502 | IdentifierInfoLookup* ExternalLookup; |
503 | |
504 | public: |
505 | |
506 | explicit IdentifierTable(IdentifierInfoLookup *ExternalLookup = nullptr); |
507 | |
508 | |
509 | |
510 | explicit IdentifierTable(const LangOptions &LangOpts, |
511 | IdentifierInfoLookup *ExternalLookup = nullptr); |
512 | |
513 | |
514 | void setExternalIdentifierLookup(IdentifierInfoLookup *IILookup) { |
515 | ExternalLookup = IILookup; |
516 | } |
517 | |
518 | |
519 | IdentifierInfoLookup *getExternalIdentifierLookup() const { |
520 | return ExternalLookup; |
521 | } |
522 | |
523 | llvm::BumpPtrAllocator& getAllocator() { |
524 | return HashTable.getAllocator(); |
525 | } |
526 | |
527 | |
528 | |
529 | IdentifierInfo &get(StringRef Name) { |
530 | auto &Entry = *HashTable.insert(std::make_pair(Name, nullptr)).first; |
531 | |
532 | IdentifierInfo *&II = Entry.second; |
533 | if (II) return *II; |
534 | |
535 | |
536 | if (ExternalLookup) { |
537 | II = ExternalLookup->get(Name); |
538 | if (II) |
539 | return *II; |
540 | } |
541 | |
542 | |
543 | void *Mem = getAllocator().Allocate<IdentifierInfo>(); |
544 | II = new (Mem) IdentifierInfo(); |
545 | |
546 | |
547 | |
548 | II->Entry = &Entry; |
549 | |
550 | return *II; |
551 | } |
552 | |
553 | IdentifierInfo &get(StringRef Name, tok::TokenKind TokenCode) { |
554 | IdentifierInfo &II = get(Name); |
555 | II.TokenID = TokenCode; |
556 | assert(II.TokenID == (unsigned) TokenCode && "TokenCode too large"); |
557 | return II; |
558 | } |
559 | |
560 | |
561 | |
562 | |
563 | |
564 | |
565 | |
566 | IdentifierInfo &getOwn(StringRef Name) { |
567 | auto &Entry = *HashTable.insert(std::make_pair(Name, nullptr)).first; |
568 | |
569 | IdentifierInfo *&II = Entry.second; |
570 | if (II) |
571 | return *II; |
572 | |
573 | |
574 | void *Mem = getAllocator().Allocate<IdentifierInfo>(); |
575 | II = new (Mem) IdentifierInfo(); |
576 | |
577 | |
578 | |
579 | II->Entry = &Entry; |
580 | |
581 | |
582 | if (Name.equals("import")) |
583 | II->setModulesImport(true); |
584 | |
585 | return *II; |
586 | } |
587 | |
588 | using iterator = HashTableTy::const_iterator; |
589 | using const_iterator = HashTableTy::const_iterator; |
590 | |
591 | iterator begin() const { return HashTable.begin(); } |
592 | iterator end() const { return HashTable.end(); } |
593 | unsigned size() const { return HashTable.size(); } |
594 | |
595 | iterator find(StringRef Name) const { return HashTable.find(Name); } |
596 | |
597 | |
598 | |
599 | void PrintStats() const; |
600 | |
601 | |
602 | |
603 | void AddKeywords(const LangOptions &LangOpts); |
604 | }; |
605 | |
606 | |
607 | |
608 | |
609 | |
610 | |
611 | |
612 | |
613 | |
614 | |
615 | |
616 | |
617 | |
618 | |
619 | |
620 | |
621 | |
622 | |
623 | enum ObjCMethodFamily { |
624 | |
625 | OMF_None, |
626 | |
627 | |
628 | |
629 | |
630 | |
631 | OMF_alloc, |
632 | OMF_copy, |
633 | OMF_init, |
634 | OMF_mutableCopy, |
635 | OMF_new, |
636 | |
637 | |
638 | |
639 | OMF_autorelease, |
640 | OMF_dealloc, |
641 | OMF_finalize, |
642 | OMF_release, |
643 | OMF_retain, |
644 | OMF_retainCount, |
645 | OMF_self, |
646 | OMF_initialize, |
647 | |
648 | |
649 | OMF_performSelector |
650 | }; |
651 | |
652 | |
653 | |
654 | enum { ObjCMethodFamilyBitWidth = 4 }; |
655 | |
656 | |
657 | enum { InvalidObjCMethodFamily = (1 << ObjCMethodFamilyBitWidth) - 1 }; |
658 | |
659 | |
660 | |
661 | |
662 | |
663 | enum ObjCInstanceTypeFamily { |
664 | OIT_None, |
665 | OIT_Array, |
666 | OIT_Dictionary, |
667 | OIT_Singleton, |
668 | OIT_Init, |
669 | OIT_ReturnsSelf |
670 | }; |
671 | |
672 | enum ObjCStringFormatFamily { |
673 | SFF_None, |
674 | SFF_NSString, |
675 | SFF_CFString |
676 | }; |
677 | |
678 | |
679 | |
680 | |
681 | |
682 | |
683 | |
684 | |
685 | class Selector { |
686 | friend class Diagnostic; |
687 | friend class SelectorTable; |
688 | friend class DeclarationName; |
689 | |
690 | enum IdentifierInfoFlag { |
691 | |
692 | |
693 | ZeroArg = 0x01, |
694 | OneArg = 0x02, |
695 | MultiArg = 0x07, |
696 | ArgFlags = 0x07 |
697 | }; |
698 | |
699 | |
700 | |
701 | |
702 | |
703 | uintptr_t InfoPtr = 0; |
704 | |
705 | Selector(IdentifierInfo *II, unsigned nArgs) { |
706 | InfoPtr = reinterpret_cast<uintptr_t>(II); |
707 | assert((InfoPtr & ArgFlags) == 0 &&"Insufficiently aligned IdentifierInfo"); |
708 | assert(nArgs < 2 && "nArgs not equal to 0/1"); |
709 | InfoPtr |= nArgs+1; |
710 | } |
711 | |
712 | Selector(MultiKeywordSelector *SI) { |
713 | InfoPtr = reinterpret_cast<uintptr_t>(SI); |
714 | assert((InfoPtr & ArgFlags) == 0 &&"Insufficiently aligned IdentifierInfo"); |
715 | InfoPtr |= MultiArg; |
716 | } |
717 | |
718 | IdentifierInfo *getAsIdentifierInfo() const { |
719 | if (getIdentifierInfoFlag() < MultiArg) |
720 | return reinterpret_cast<IdentifierInfo *>(InfoPtr & ~ArgFlags); |
721 | return nullptr; |
722 | } |
723 | |
724 | MultiKeywordSelector *getMultiKeywordSelector() const { |
725 | return reinterpret_cast<MultiKeywordSelector *>(InfoPtr & ~ArgFlags); |
726 | } |
727 | |
728 | unsigned getIdentifierInfoFlag() const { |
729 | return InfoPtr & ArgFlags; |
730 | } |
731 | |
732 | static ObjCMethodFamily getMethodFamilyImpl(Selector sel); |
733 | |
734 | static ObjCStringFormatFamily getStringFormatFamilyImpl(Selector sel); |
735 | |
736 | public: |
737 | |
738 | |
739 | Selector() = default; |
740 | explicit Selector(uintptr_t V) : InfoPtr(V) {} |
741 | |
742 | |
743 | bool operator==(Selector RHS) const { |
744 | return InfoPtr == RHS.InfoPtr; |
745 | } |
746 | bool operator!=(Selector RHS) const { |
747 | return InfoPtr != RHS.InfoPtr; |
748 | } |
749 | |
750 | void *getAsOpaquePtr() const { |
751 | return reinterpret_cast<void*>(InfoPtr); |
752 | } |
753 | |
754 | |
755 | bool isNull() const { return InfoPtr == 0; } |
756 | |
757 | |
758 | bool isKeywordSelector() const { |
759 | return getIdentifierInfoFlag() != ZeroArg; |
760 | } |
761 | |
762 | bool isUnarySelector() const { |
763 | return getIdentifierInfoFlag() == ZeroArg; |
764 | } |
765 | |
766 | |
767 | bool isKeywordSelector(ArrayRef<StringRef> Names) const; |
768 | |
769 | |
770 | bool isUnarySelector(StringRef Name) const; |
771 | |
772 | unsigned getNumArgs() const; |
773 | |
774 | |
775 | |
776 | |
777 | |
778 | |
779 | |
780 | |
781 | |
782 | |
783 | |
784 | |
785 | |
786 | |
787 | IdentifierInfo *getIdentifierInfoForSlot(unsigned argIndex) const; |
788 | |
789 | |
790 | |
791 | |
792 | |
793 | |
794 | |
795 | |
796 | |
797 | StringRef getNameForSlot(unsigned argIndex) const; |
798 | |
799 | |
800 | |
801 | std::string getAsString() const; |
802 | |
803 | |
804 | void print(llvm::raw_ostream &OS) const; |
805 | |
806 | void dump() const; |
807 | |
808 | |
809 | ObjCMethodFamily getMethodFamily() const { |
810 | return getMethodFamilyImpl(*this); |
811 | } |
812 | |
813 | ObjCStringFormatFamily getStringFormatFamily() const { |
814 | return getStringFormatFamilyImpl(*this); |
815 | } |
816 | |
817 | static Selector getEmptyMarker() { |
818 | return Selector(uintptr_t(-1)); |
819 | } |
820 | |
821 | static Selector getTombstoneMarker() { |
822 | return Selector(uintptr_t(-2)); |
823 | } |
824 | |
825 | static ObjCInstanceTypeFamily getInstTypeMethodFamily(Selector sel); |
826 | }; |
827 | |
828 | |
829 | |
830 | class SelectorTable { |
831 | |
832 | void *Impl; |
833 | |
834 | public: |
835 | SelectorTable(); |
836 | SelectorTable(const SelectorTable &) = delete; |
837 | SelectorTable &operator=(const SelectorTable &) = delete; |
838 | ~SelectorTable(); |
839 | |
840 | |
841 | |
842 | |
843 | |
844 | Selector getSelector(unsigned NumArgs, IdentifierInfo **IIV); |
845 | |
846 | Selector getUnarySelector(IdentifierInfo *ID) { |
847 | return Selector(ID, 1); |
848 | } |
849 | |
850 | Selector getNullarySelector(IdentifierInfo *ID) { |
851 | return Selector(ID, 0); |
852 | } |
853 | |
854 | |
855 | size_t getTotalMemory() const; |
856 | |
857 | |
858 | |
859 | |
860 | |
861 | static SmallString<64> constructSetterName(StringRef Name); |
862 | |
863 | |
864 | |
865 | |
866 | |
867 | static Selector constructSetterSelector(IdentifierTable &Idents, |
868 | SelectorTable &SelTable, |
869 | const IdentifierInfo *Name); |
870 | |
871 | |
872 | static std::string getPropertyNameFromSetterSelector(Selector Sel); |
873 | }; |
874 | |
875 | namespace detail { |
876 | |
877 | |
878 | |
879 | |
880 | |
881 | |
882 | |
883 | |
884 | |
885 | class alignas(IdentifierInfoAlignment) DeclarationNameExtra { |
886 | friend class clang::DeclarationName; |
887 | friend class clang::DeclarationNameTable; |
888 | |
889 | protected: |
890 | |
891 | |
892 | |
893 | |
894 | |
895 | enum ExtraKind { |
896 | CXXDeductionGuideName, |
897 | CXXLiteralOperatorName, |
898 | CXXUsingDirective, |
899 | ObjCMultiArgSelector |
900 | }; |
901 | |
902 | |
903 | |
904 | |
905 | |
906 | |
907 | |
908 | |
909 | |
910 | |
911 | |
912 | unsigned ExtraKindOrNumArgs; |
913 | |
914 | DeclarationNameExtra(ExtraKind Kind) : ExtraKindOrNumArgs(Kind) {} |
915 | DeclarationNameExtra(unsigned NumArgs) |
916 | : ExtraKindOrNumArgs(ObjCMultiArgSelector + NumArgs) {} |
917 | |
918 | |
919 | ExtraKind getKind() const { |
920 | return static_cast<ExtraKind>(ExtraKindOrNumArgs > |
921 | (unsigned)ObjCMultiArgSelector |
922 | ? (unsigned)ObjCMultiArgSelector |
923 | : ExtraKindOrNumArgs); |
924 | } |
925 | |
926 | |
927 | |
928 | unsigned getNumArgs() const { |
929 | assert(ExtraKindOrNumArgs >= (unsigned)ObjCMultiArgSelector && |
930 | "getNumArgs called but this is not an ObjC selector!"); |
931 | return ExtraKindOrNumArgs - (unsigned)ObjCMultiArgSelector; |
932 | } |
933 | }; |
934 | |
935 | } |
936 | |
937 | } |
938 | |
939 | namespace llvm { |
940 | |
941 | |
942 | |
943 | template <> |
944 | struct DenseMapInfo<clang::Selector> { |
945 | static clang::Selector getEmptyKey() { |
946 | return clang::Selector::getEmptyMarker(); |
947 | } |
948 | |
949 | static clang::Selector getTombstoneKey() { |
950 | return clang::Selector::getTombstoneMarker(); |
951 | } |
952 | |
953 | static unsigned getHashValue(clang::Selector S); |
954 | |
955 | static bool isEqual(clang::Selector LHS, clang::Selector RHS) { |
956 | return LHS == RHS; |
957 | } |
958 | }; |
959 | |
960 | template<> |
961 | struct PointerLikeTypeTraits<clang::Selector> { |
962 | static const void *getAsVoidPointer(clang::Selector P) { |
963 | return P.getAsOpaquePtr(); |
964 | } |
965 | |
966 | static clang::Selector getFromVoidPointer(const void *P) { |
967 | return clang::Selector(reinterpret_cast<uintptr_t>(P)); |
968 | } |
969 | |
970 | static constexpr int NumLowBitsAvailable = 0; |
971 | }; |
972 | |
973 | |
974 | |
975 | template<> |
976 | struct PointerLikeTypeTraits<clang::IdentifierInfo*> { |
977 | static void *getAsVoidPointer(clang::IdentifierInfo* P) { |
978 | return P; |
979 | } |
980 | |
981 | static clang::IdentifierInfo *getFromVoidPointer(void *P) { |
982 | return static_cast<clang::IdentifierInfo*>(P); |
983 | } |
984 | |
985 | static constexpr int NumLowBitsAvailable = 1; |
986 | }; |
987 | |
988 | template<> |
989 | struct PointerLikeTypeTraits<const clang::IdentifierInfo*> { |
990 | static const void *getAsVoidPointer(const clang::IdentifierInfo* P) { |
991 | return P; |
992 | } |
993 | |
994 | static const clang::IdentifierInfo *getFromVoidPointer(const void *P) { |
995 | return static_cast<const clang::IdentifierInfo*>(P); |
996 | } |
997 | |
998 | static constexpr int NumLowBitsAvailable = 1; |
999 | }; |
1000 | |
1001 | } |
1002 | |
1003 | #endif // LLVM_CLANG_BASIC_IDENTIFIERTABLE_H |