clang -cc1 -cc1 -triple amd64-unknown-openbsd7.0 -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name PPDirectives.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/PPDirectives.cpp
1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | |
14 | #include "clang/Basic/CharInfo.h" |
15 | #include "clang/Basic/FileManager.h" |
16 | #include "clang/Basic/IdentifierTable.h" |
17 | #include "clang/Basic/LangOptions.h" |
18 | #include "clang/Basic/Module.h" |
19 | #include "clang/Basic/SourceLocation.h" |
20 | #include "clang/Basic/SourceManager.h" |
21 | #include "clang/Basic/TokenKinds.h" |
22 | #include "clang/Lex/CodeCompletionHandler.h" |
23 | #include "clang/Lex/HeaderSearch.h" |
24 | #include "clang/Lex/LexDiagnostic.h" |
25 | #include "clang/Lex/LiteralSupport.h" |
26 | #include "clang/Lex/MacroInfo.h" |
27 | #include "clang/Lex/ModuleLoader.h" |
28 | #include "clang/Lex/ModuleMap.h" |
29 | #include "clang/Lex/PPCallbacks.h" |
30 | #include "clang/Lex/Pragma.h" |
31 | #include "clang/Lex/Preprocessor.h" |
32 | #include "clang/Lex/PreprocessorOptions.h" |
33 | #include "clang/Lex/Token.h" |
34 | #include "clang/Lex/VariadicMacroSupport.h" |
35 | #include "llvm/ADT/ArrayRef.h" |
36 | #include "llvm/ADT/ScopeExit.h" |
37 | #include "llvm/ADT/SmallString.h" |
38 | #include "llvm/ADT/SmallVector.h" |
39 | #include "llvm/ADT/STLExtras.h" |
40 | #include "llvm/ADT/StringSwitch.h" |
41 | #include "llvm/ADT/StringRef.h" |
42 | #include "llvm/Support/AlignOf.h" |
43 | #include "llvm/Support/ErrorHandling.h" |
44 | #include "llvm/Support/Path.h" |
45 | #include <algorithm> |
46 | #include <cassert> |
47 | #include <cstring> |
48 | #include <new> |
49 | #include <string> |
50 | #include <utility> |
51 | |
52 | using namespace clang; |
53 | |
54 | |
55 | |
56 | |
57 | |
58 | MacroInfo *Preprocessor::AllocateMacroInfo(SourceLocation L) { |
59 | auto *MIChain = new (BP) MacroInfoChain{L, MIChainHead}; |
| 17 | | Calling 'operator new<llvm::MallocAllocator, 4096UL, 4096UL, 128UL>' | |
|
60 | MIChainHead = MIChain; |
61 | return &MIChain->MI; |
62 | } |
63 | |
64 | DefMacroDirective *Preprocessor::AllocateDefMacroDirective(MacroInfo *MI, |
65 | SourceLocation Loc) { |
66 | return new (BP) DefMacroDirective(MI, Loc); |
67 | } |
68 | |
69 | UndefMacroDirective * |
70 | Preprocessor::AllocateUndefMacroDirective(SourceLocation UndefLoc) { |
71 | return new (BP) UndefMacroDirective(UndefLoc); |
72 | } |
73 | |
74 | VisibilityMacroDirective * |
75 | Preprocessor::AllocateVisibilityMacroDirective(SourceLocation Loc, |
76 | bool isPublic) { |
77 | return new (BP) VisibilityMacroDirective(Loc, isPublic); |
78 | } |
79 | |
80 | |
81 | |
82 | SourceRange Preprocessor::DiscardUntilEndOfDirective() { |
83 | Token Tmp; |
84 | SourceRange Res; |
85 | |
86 | LexUnexpandedToken(Tmp); |
87 | Res.setBegin(Tmp.getLocation()); |
88 | while (Tmp.isNot(tok::eod)) { |
89 | assert(Tmp.isNot(tok::eof) && "EOF seen while discarding directive tokens"); |
90 | LexUnexpandedToken(Tmp); |
91 | } |
92 | Res.setEnd(Tmp.getLocation()); |
93 | return Res; |
94 | } |
95 | |
96 | |
97 | enum MacroDiag { |
98 | MD_NoWarn, |
99 | MD_KeywordDef, |
100 | MD_ReservedMacro |
101 | }; |
102 | |
103 | |
104 | |
105 | enum PPElifDiag { |
106 | PED_Elif, |
107 | PED_Elifdef, |
108 | PED_Elifndef |
109 | }; |
110 | |
111 | |
112 | |
113 | |
114 | |
115 | |
116 | |
117 | static bool isForModuleBuilding(Module *M, StringRef CurrentModule, |
118 | StringRef ModuleName) { |
119 | StringRef TopLevelName = M->getTopLevelModuleName(); |
120 | |
121 | |
122 | |
123 | if (M->getTopLevelModule()->IsFramework && CurrentModule == ModuleName && |
124 | !CurrentModule.endswith("_Private") && TopLevelName.endswith("_Private")) |
125 | TopLevelName = TopLevelName.drop_back(8); |
126 | |
127 | return TopLevelName == CurrentModule; |
128 | } |
129 | |
130 | static MacroDiag shouldWarnOnMacroDef(Preprocessor &PP, IdentifierInfo *II) { |
131 | const LangOptions &Lang = PP.getLangOpts(); |
132 | if (II->isReserved(Lang) != ReservedIdentifierStatus::NotReserved) { |
133 | |
134 | |
135 | |
136 | |
137 | |
138 | static constexpr StringRef ReservedMacro[] = { |
139 | "_ATFILE_SOURCE", |
140 | "_BSD_SOURCE", |
141 | "_CRT_NONSTDC_NO_WARNINGS", |
142 | "_CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES", |
143 | "_CRT_SECURE_NO_WARNINGS", |
144 | "_FILE_OFFSET_BITS", |
145 | "_FORTIFY_SOURCE", |
146 | "_GLIBCXX_ASSERTIONS", |
147 | "_GLIBCXX_CONCEPT_CHECKS", |
148 | "_GLIBCXX_DEBUG", |
149 | "_GLIBCXX_DEBUG_PEDANTIC", |
150 | "_GLIBCXX_PARALLEL", |
151 | "_GLIBCXX_PARALLEL_ASSERTIONS", |
152 | "_GLIBCXX_SANITIZE_VECTOR", |
153 | "_GLIBCXX_USE_CXX11_ABI", |
154 | "_GLIBCXX_USE_DEPRECATED", |
155 | "_GNU_SOURCE", |
156 | "_ISOC11_SOURCE", |
157 | "_ISOC95_SOURCE", |
158 | "_ISOC99_SOURCE", |
159 | "_LARGEFILE64_SOURCE", |
160 | "_POSIX_C_SOURCE", |
161 | "_REENTRANT", |
162 | "_SVID_SOURCE", |
163 | "_THREAD_SAFE", |
164 | "_XOPEN_SOURCE", |
165 | "_XOPEN_SOURCE_EXTENDED", |
166 | "__STDCPP_WANT_MATH_SPEC_FUNCS__", |
167 | "__STDC_FORMAT_MACROS", |
168 | }; |
169 | if (std::binary_search(std::begin(ReservedMacro), std::end(ReservedMacro), |
170 | II->getName())) |
171 | return MD_NoWarn; |
172 | |
173 | return MD_ReservedMacro; |
174 | } |
175 | StringRef Text = II->getName(); |
176 | if (II->isKeyword(Lang)) |
177 | return MD_KeywordDef; |
178 | if (Lang.CPlusPlus11 && (Text.equals("override") || Text.equals("final"))) |
179 | return MD_KeywordDef; |
180 | return MD_NoWarn; |
181 | } |
182 | |
183 | static MacroDiag shouldWarnOnMacroUndef(Preprocessor &PP, IdentifierInfo *II) { |
184 | const LangOptions &Lang = PP.getLangOpts(); |
185 | |
186 | if (II->isReserved(Lang) != ReservedIdentifierStatus::NotReserved) |
187 | return MD_ReservedMacro; |
188 | return MD_NoWarn; |
189 | } |
190 | |
191 | |
192 | |
193 | |
194 | |
195 | |
196 | static bool warnByDefaultOnWrongCase(StringRef Include) { |
197 | |
198 | |
199 | if (::llvm::sys::path::begin(Include)->equals_insensitive("boost")) |
200 | return true; |
201 | |
202 | |
203 | |
204 | static const size_t MaxStdHeaderNameLen = 18u; |
205 | if (Include.size() > MaxStdHeaderNameLen) |
206 | return false; |
207 | |
208 | |
209 | SmallString<32> LowerInclude{Include}; |
210 | for (char &Ch : LowerInclude) { |
211 | |
212 | if (static_cast<unsigned char>(Ch) > 0x7f) |
213 | return false; |
214 | |
215 | if (Ch >= 'A' && Ch <= 'Z') |
216 | Ch += 'a' - 'A'; |
217 | |
218 | else if (::llvm::sys::path::is_separator(Ch)) |
219 | Ch = '/'; |
220 | } |
221 | |
222 | |
223 | return llvm::StringSwitch<bool>(LowerInclude) |
224 | |
225 | .Cases("assert.h", "complex.h", "ctype.h", "errno.h", "fenv.h", true) |
226 | .Cases("float.h", "inttypes.h", "iso646.h", "limits.h", "locale.h", true) |
227 | .Cases("math.h", "setjmp.h", "signal.h", "stdalign.h", "stdarg.h", true) |
228 | .Cases("stdatomic.h", "stdbool.h", "stddef.h", "stdint.h", "stdio.h", true) |
229 | .Cases("stdlib.h", "stdnoreturn.h", "string.h", "tgmath.h", "threads.h", true) |
230 | .Cases("time.h", "uchar.h", "wchar.h", "wctype.h", true) |
231 | |
232 | |
233 | .Cases("cassert", "ccomplex", "cctype", "cerrno", "cfenv", true) |
234 | .Cases("cfloat", "cinttypes", "ciso646", "climits", "clocale", true) |
235 | .Cases("cmath", "csetjmp", "csignal", "cstdalign", "cstdarg", true) |
236 | .Cases("cstdbool", "cstddef", "cstdint", "cstdio", "cstdlib", true) |
237 | .Cases("cstring", "ctgmath", "ctime", "cuchar", "cwchar", true) |
238 | .Case("cwctype", true) |
239 | |
240 | |
241 | .Cases("algorithm", "fstream", "list", "regex", "thread", true) |
242 | .Cases("array", "functional", "locale", "scoped_allocator", "tuple", true) |
243 | .Cases("atomic", "future", "map", "set", "type_traits", true) |
244 | .Cases("bitset", "initializer_list", "memory", "shared_mutex", "typeindex", true) |
245 | .Cases("chrono", "iomanip", "mutex", "sstream", "typeinfo", true) |
246 | .Cases("codecvt", "ios", "new", "stack", "unordered_map", true) |
247 | .Cases("complex", "iosfwd", "numeric", "stdexcept", "unordered_set", true) |
248 | .Cases("condition_variable", "iostream", "ostream", "streambuf", "utility", true) |
249 | .Cases("deque", "istream", "queue", "string", "valarray", true) |
250 | .Cases("exception", "iterator", "random", "strstream", "vector", true) |
251 | .Cases("forward_list", "limits", "ratio", "system_error", true) |
252 | |
253 | |
254 | .Cases("aio.h", "arpa/inet.h", "cpio.h", "dirent.h", "dlfcn.h", true) |
255 | .Cases("fcntl.h", "fmtmsg.h", "fnmatch.h", "ftw.h", "glob.h", true) |
256 | .Cases("grp.h", "iconv.h", "langinfo.h", "libgen.h", "monetary.h", true) |
257 | .Cases("mqueue.h", "ndbm.h", "net/if.h", "netdb.h", "netinet/in.h", true) |
258 | .Cases("netinet/tcp.h", "nl_types.h", "poll.h", "pthread.h", "pwd.h", true) |
259 | .Cases("regex.h", "sched.h", "search.h", "semaphore.h", "spawn.h", true) |
260 | .Cases("strings.h", "stropts.h", "sys/ipc.h", "sys/mman.h", "sys/msg.h", true) |
261 | .Cases("sys/resource.h", "sys/select.h", "sys/sem.h", "sys/shm.h", "sys/socket.h", true) |
262 | .Cases("sys/stat.h", "sys/statvfs.h", "sys/time.h", "sys/times.h", "sys/types.h", true) |
263 | .Cases("sys/uio.h", "sys/un.h", "sys/utsname.h", "sys/wait.h", "syslog.h", true) |
264 | .Cases("tar.h", "termios.h", "trace.h", "ulimit.h", true) |
265 | .Cases("unistd.h", "utime.h", "utmpx.h", "wordexp.h", true) |
266 | .Default(false); |
267 | } |
268 | |
269 | bool Preprocessor::CheckMacroName(Token &MacroNameTok, MacroUse isDefineUndef, |
270 | bool *ShadowFlag) { |
271 | |
272 | if (MacroNameTok.is(tok::eod)) |
273 | return Diag(MacroNameTok, diag::err_pp_missing_macro_name); |
274 | |
275 | IdentifierInfo *II = MacroNameTok.getIdentifierInfo(); |
276 | if (!II) |
277 | return Diag(MacroNameTok, diag::err_pp_macro_not_identifier); |
278 | |
279 | if (II->isCPlusPlusOperatorKeyword()) { |
280 | |
281 | |
282 | Diag(MacroNameTok, getLangOpts().MicrosoftExt |
283 | ? diag::ext_pp_operator_used_as_macro_name |
284 | : diag::err_pp_operator_used_as_macro_name) |
285 | << II << MacroNameTok.getKind(); |
286 | |
287 | |
288 | } |
289 | |
290 | if ((isDefineUndef != MU_Other) && II->getPPKeywordID() == tok::pp_defined) { |
291 | |
292 | return Diag(MacroNameTok, diag::err_defined_macro_name); |
293 | } |
294 | |
295 | if (isDefineUndef == MU_Undef) { |
296 | auto *MI = getMacroInfo(II); |
297 | if (MI && MI->isBuiltinMacro()) { |
298 | |
299 | |
300 | Diag(MacroNameTok, diag::ext_pp_undef_builtin_macro); |
301 | } |
302 | } |
303 | |
304 | |
305 | |
306 | SourceLocation MacroNameLoc = MacroNameTok.getLocation(); |
307 | if (ShadowFlag) |
308 | *ShadowFlag = false; |
309 | if (!SourceMgr.isInSystemHeader(MacroNameLoc) && |
310 | (SourceMgr.getBufferName(MacroNameLoc) != "<built-in>")) { |
311 | MacroDiag D = MD_NoWarn; |
312 | if (isDefineUndef == MU_Define) { |
313 | D = shouldWarnOnMacroDef(*this, II); |
314 | } |
315 | else if (isDefineUndef == MU_Undef) |
316 | D = shouldWarnOnMacroUndef(*this, II); |
317 | if (D == MD_KeywordDef) { |
318 | |
319 | |
320 | |
321 | if (ShadowFlag) |
322 | *ShadowFlag = true; |
323 | } |
324 | if (D == MD_ReservedMacro) |
325 | Diag(MacroNameTok, diag::warn_pp_macro_is_reserved_id); |
326 | } |
327 | |
328 | |
329 | return false; |
330 | } |
331 | |
332 | |
333 | |
334 | |
335 | |
336 | |
337 | |
338 | |
339 | |
340 | |
341 | void Preprocessor::ReadMacroName(Token &MacroNameTok, MacroUse isDefineUndef, |
342 | bool *ShadowFlag) { |
343 | |
344 | LexUnexpandedToken(MacroNameTok); |
345 | |
346 | if (MacroNameTok.is(tok::code_completion)) { |
347 | if (CodeComplete) |
348 | CodeComplete->CodeCompleteMacroName(isDefineUndef == MU_Define); |
349 | setCodeCompletionReached(); |
350 | LexUnexpandedToken(MacroNameTok); |
351 | } |
352 | |
353 | if (!CheckMacroName(MacroNameTok, isDefineUndef, ShadowFlag)) |
354 | return; |
355 | |
356 | |
357 | |
358 | if (MacroNameTok.isNot(tok::eod)) { |
359 | MacroNameTok.setKind(tok::eod); |
360 | DiscardUntilEndOfDirective(); |
361 | } |
362 | } |
363 | |
364 | |
365 | |
366 | |
367 | |
368 | |
369 | |
370 | SourceLocation Preprocessor::CheckEndOfDirective(const char *DirType, |
371 | bool EnableMacros) { |
372 | Token Tmp; |
373 | |
374 | |
375 | |
376 | if (EnableMacros) |
377 | Lex(Tmp); |
378 | else |
379 | LexUnexpandedToken(Tmp); |
380 | |
381 | |
382 | |
383 | while (Tmp.is(tok::comment)) |
384 | LexUnexpandedToken(Tmp); |
385 | |
386 | if (Tmp.is(tok::eod)) |
387 | return Tmp.getLocation(); |
388 | |
389 | |
390 | |
391 | |
392 | |
393 | FixItHint Hint; |
394 | if ((LangOpts.GNUMode || LangOpts.C99 || LangOpts.CPlusPlus) && |
395 | !CurTokenLexer) |
396 | Hint = FixItHint::CreateInsertion(Tmp.getLocation(),"//"); |
397 | Diag(Tmp, diag::ext_pp_extra_tokens_at_eol) << DirType << Hint; |
398 | return DiscardUntilEndOfDirective().getEnd(); |
399 | } |
400 | |
401 | Optional<unsigned> Preprocessor::getSkippedRangeForExcludedConditionalBlock( |
402 | SourceLocation HashLoc) { |
403 | if (!ExcludedConditionalDirectiveSkipMappings) |
404 | return None; |
405 | if (!HashLoc.isFileID()) |
406 | return None; |
407 | |
408 | std::pair<FileID, unsigned> HashFileOffset = |
409 | SourceMgr.getDecomposedLoc(HashLoc); |
410 | Optional<llvm::MemoryBufferRef> Buf = |
411 | SourceMgr.getBufferOrNone(HashFileOffset.first); |
412 | if (!Buf) |
413 | return None; |
414 | auto It = |
415 | ExcludedConditionalDirectiveSkipMappings->find(Buf->getBufferStart()); |
416 | if (It == ExcludedConditionalDirectiveSkipMappings->end()) |
417 | return None; |
418 | |
419 | const PreprocessorSkippedRangeMapping &SkippedRanges = *It->getSecond(); |
420 | |
421 | auto MappingIt = SkippedRanges.find(HashFileOffset.second); |
422 | if (MappingIt == SkippedRanges.end()) |
423 | return None; |
424 | |
425 | unsigned BytesToSkip = MappingIt->getSecond(); |
426 | unsigned CurLexerBufferOffset = CurLexer->getCurrentBufferOffset(); |
427 | assert(CurLexerBufferOffset >= HashFileOffset.second && |
428 | "lexer is before the hash?"); |
429 | |
430 | |
431 | unsigned LengthDiff = CurLexerBufferOffset - HashFileOffset.second; |
432 | assert(BytesToSkip >= LengthDiff && "lexer is after the skipped range?"); |
433 | return BytesToSkip - LengthDiff; |
434 | } |
435 | |
436 | |
437 | |
438 | |
439 | |
440 | |
441 | |
442 | |
443 | |
444 | void Preprocessor::SkipExcludedConditionalBlock(SourceLocation HashTokenLoc, |
445 | SourceLocation IfTokenLoc, |
446 | bool FoundNonSkipPortion, |
447 | bool FoundElse, |
448 | SourceLocation ElseLoc) { |
449 | ++NumSkipped; |
450 | assert(!CurTokenLexer && CurPPLexer && "Lexing a macro, not a file?"); |
451 | |
452 | if (PreambleConditionalStack.reachedEOFWhileSkipping()) |
453 | PreambleConditionalStack.clearSkipInfo(); |
454 | else |
455 | CurPPLexer->pushConditionalLevel(IfTokenLoc, false, |
456 | FoundNonSkipPortion, FoundElse); |
457 | |
458 | |
459 | |
460 | CurPPLexer->LexingRawMode = true; |
461 | Token Tok; |
462 | if (auto SkipLength = |
463 | getSkippedRangeForExcludedConditionalBlock(HashTokenLoc)) { |
464 | |
465 | CurLexer->skipOver(*SkipLength); |
466 | } |
467 | SourceLocation endLoc; |
468 | while (true) { |
469 | CurLexer->Lex(Tok); |
470 | |
471 | if (Tok.is(tok::code_completion)) { |
472 | setCodeCompletionReached(); |
473 | if (CodeComplete) |
474 | CodeComplete->CodeCompleteInConditionalExclusion(); |
475 | continue; |
476 | } |
477 | |
478 | |
479 | if (Tok.is(tok::eof)) { |
480 | |
481 | |
482 | |
483 | if (PreambleConditionalStack.isRecording()) |
484 | PreambleConditionalStack.SkipInfo.emplace( |
485 | HashTokenLoc, IfTokenLoc, FoundNonSkipPortion, FoundElse, ElseLoc); |
486 | break; |
487 | } |
488 | |
489 | |
490 | if (Tok.isNot(tok::hash) || !Tok.isAtStartOfLine()) |
491 | continue; |
492 | |
493 | |
494 | |
495 | |
496 | CurPPLexer->ParsingPreprocessorDirective = true; |
497 | if (CurLexer) CurLexer->SetKeepWhitespaceMode(false); |
498 | |
499 | |
500 | |
501 | LexUnexpandedToken(Tok); |
502 | |
503 | |
504 | |
505 | if (Tok.isNot(tok::raw_identifier)) { |
506 | CurPPLexer->ParsingPreprocessorDirective = false; |
507 | |
508 | if (CurLexer) CurLexer->resetExtendedTokenMode(); |
509 | continue; |
510 | } |
511 | |
512 | |
513 | |
514 | |
515 | |
516 | |
517 | StringRef RI = Tok.getRawIdentifier(); |
518 | |
519 | char FirstChar = RI[0]; |
520 | if (FirstChar >= 'a' && FirstChar <= 'z' && |
521 | FirstChar != 'i' && FirstChar != 'e') { |
522 | CurPPLexer->ParsingPreprocessorDirective = false; |
523 | |
524 | if (CurLexer) CurLexer->resetExtendedTokenMode(); |
525 | continue; |
526 | } |
527 | |
528 | |
529 | |
530 | |
531 | char DirectiveBuf[20]; |
532 | StringRef Directive; |
533 | if (!Tok.needsCleaning() && RI.size() < 20) { |
534 | Directive = RI; |
535 | } else { |
536 | std::string DirectiveStr = getSpelling(Tok); |
537 | size_t IdLen = DirectiveStr.size(); |
538 | if (IdLen >= 20) { |
539 | CurPPLexer->ParsingPreprocessorDirective = false; |
540 | |
541 | if (CurLexer) CurLexer->resetExtendedTokenMode(); |
542 | continue; |
543 | } |
544 | memcpy(DirectiveBuf, &DirectiveStr[0], IdLen); |
545 | Directive = StringRef(DirectiveBuf, IdLen); |
546 | } |
547 | |
548 | if (Directive.startswith("if")) { |
549 | StringRef Sub = Directive.substr(2); |
550 | if (Sub.empty() || |
551 | Sub == "def" || |
552 | Sub == "ndef") { |
553 | |
554 | |
555 | DiscardUntilEndOfDirective(); |
556 | CurPPLexer->pushConditionalLevel(Tok.getLocation(), true, |
557 | false, |
558 | false); |
559 | } |
560 | } else if (Directive[0] == 'e') { |
561 | StringRef Sub = Directive.substr(1); |
562 | if (Sub == "ndif") { |
563 | PPConditionalInfo CondInfo; |
564 | CondInfo.WasSkipping = true; |
565 | bool InCond = CurPPLexer->popConditionalLevel(CondInfo); |
566 | (void)InCond; |
567 | assert(!InCond && "Can't be skipping if not in a conditional!"); |
568 | |
569 | |
570 | if (!CondInfo.WasSkipping) { |
571 | |
572 | |
573 | CurPPLexer->LexingRawMode = false; |
574 | endLoc = CheckEndOfDirective("endif"); |
575 | CurPPLexer->LexingRawMode = true; |
576 | if (Callbacks) |
577 | Callbacks->Endif(Tok.getLocation(), CondInfo.IfLoc); |
578 | break; |
579 | } else { |
580 | DiscardUntilEndOfDirective(); |
581 | } |
582 | } else if (Sub == "lse") { |
583 | |
584 | |
585 | |
586 | PPConditionalInfo &CondInfo = CurPPLexer->peekConditionalLevel(); |
587 | |
588 | |
589 | if (CondInfo.FoundElse) |
590 | Diag(Tok, diag::pp_err_else_after_else); |
591 | |
592 | |
593 | CondInfo.FoundElse = true; |
594 | |
595 | |
596 | |
597 | if (!CondInfo.WasSkipping && !CondInfo.FoundNonSkip) { |
598 | CondInfo.FoundNonSkip = true; |
599 | |
600 | |
601 | CurPPLexer->LexingRawMode = false; |
602 | endLoc = CheckEndOfDirective("else"); |
603 | CurPPLexer->LexingRawMode = true; |
604 | if (Callbacks) |
605 | Callbacks->Else(Tok.getLocation(), CondInfo.IfLoc); |
606 | break; |
607 | } else { |
608 | DiscardUntilEndOfDirective(); |
609 | } |
610 | } else if (Sub == "lif") { |
611 | PPConditionalInfo &CondInfo = CurPPLexer->peekConditionalLevel(); |
612 | |
613 | |
614 | if (CondInfo.FoundElse) |
615 | Diag(Tok, diag::pp_err_elif_after_else) << PED_Elif; |
616 | |
617 | |
618 | |
619 | if (CondInfo.WasSkipping || CondInfo.FoundNonSkip) { |
620 | DiscardUntilEndOfDirective(); |
621 | } else { |
622 | |
623 | |
624 | assert(CurPPLexer->LexingRawMode && "We have to be skipping here!"); |
625 | CurPPLexer->LexingRawMode = false; |
626 | IdentifierInfo *IfNDefMacro = nullptr; |
627 | DirectiveEvalResult DER = EvaluateDirectiveExpression(IfNDefMacro); |
628 | |
629 | if (!CurPPLexer) |
630 | return; |
631 | const bool CondValue = DER.Conditional; |
632 | CurPPLexer->LexingRawMode = true; |
633 | if (Callbacks) { |
634 | Callbacks->Elif( |
635 | Tok.getLocation(), DER.ExprRange, |
636 | (CondValue ? PPCallbacks::CVK_True : PPCallbacks::CVK_False), |
637 | CondInfo.IfLoc); |
638 | } |
639 | |
640 | if (CondValue) { |
641 | CondInfo.FoundNonSkip = true; |
642 | break; |
643 | } |
644 | } |
645 | } else if (Sub == "lifdef" || |
646 | Sub == "lifndef") { |
647 | bool IsElifDef = Sub == "lifdef"; |
648 | PPConditionalInfo &CondInfo = CurPPLexer->peekConditionalLevel(); |
649 | Token DirectiveToken = Tok; |
650 | |
651 | |
652 | if (CondInfo.FoundElse) |
653 | Diag(Tok, diag::pp_err_elif_after_else) |
654 | << (IsElifDef ? PED_Elifdef : PED_Elifndef); |
655 | |
656 | |
657 | |
658 | if (CondInfo.WasSkipping || CondInfo.FoundNonSkip) { |
659 | DiscardUntilEndOfDirective(); |
660 | } else { |
661 | |
662 | |
663 | assert(CurPPLexer->LexingRawMode && "We have to be skipping here!"); |
664 | CurPPLexer->LexingRawMode = false; |
665 | Token MacroNameTok; |
666 | ReadMacroName(MacroNameTok); |
667 | CurPPLexer->LexingRawMode = true; |
668 | |
669 | |
670 | |
671 | if (MacroNameTok.is(tok::eod)) { |
672 | |
673 | |
674 | continue; |
675 | } |
676 | |
677 | CheckEndOfDirective(IsElifDef ? "elifdef" : "elifndef"); |
678 | |
679 | IdentifierInfo *MII = MacroNameTok.getIdentifierInfo(); |
680 | auto MD = getMacroDefinition(MII); |
681 | MacroInfo *MI = MD.getMacroInfo(); |
682 | |
683 | if (Callbacks) { |
684 | if (IsElifDef) { |
685 | Callbacks->Elifdef(DirectiveToken.getLocation(), MacroNameTok, |
686 | MD); |
687 | } else { |
688 | Callbacks->Elifndef(DirectiveToken.getLocation(), MacroNameTok, |
689 | MD); |
690 | } |
691 | } |
692 | |
693 | if (static_cast<bool>(MI) == IsElifDef) { |
694 | CondInfo.FoundNonSkip = true; |
695 | break; |
696 | } |
697 | } |
698 | } |
699 | } |
700 | |
701 | CurPPLexer->ParsingPreprocessorDirective = false; |
702 | |
703 | if (CurLexer) CurLexer->resetExtendedTokenMode(); |
704 | } |
705 | |
706 | |
707 | |
708 | |
709 | CurPPLexer->LexingRawMode = false; |
710 | |
711 | |
712 | |
713 | if (Callbacks && (Tok.isNot(tok::eof) || !isRecordingPreamble())) |
714 | Callbacks->SourceRangeSkipped( |
715 | SourceRange(HashTokenLoc, endLoc.isValid() |
716 | ? endLoc |
717 | : CurPPLexer->getSourceLocation()), |
718 | Tok.getLocation()); |
719 | } |
720 | |
721 | Module *Preprocessor::getModuleForLocation(SourceLocation Loc) { |
722 | if (!SourceMgr.isInMainFile(Loc)) { |
723 | |
724 | |
725 | FileID IDOfIncl = SourceMgr.getFileID(SourceMgr.getExpansionLoc(Loc)); |
726 | if (const FileEntry *EntryOfIncl = SourceMgr.getFileEntryForID(IDOfIncl)) { |
727 | |
728 | return HeaderInfo.getModuleMap() |
729 | .findModuleForHeader(EntryOfIncl) |
730 | .getModule(); |
731 | } |
732 | } |
733 | |
734 | |
735 | |
736 | return getLangOpts().CurrentModule.empty() |
737 | ? nullptr |
738 | : HeaderInfo.lookupModule(getLangOpts().CurrentModule); |
739 | } |
740 | |
741 | const FileEntry * |
742 | Preprocessor::getHeaderToIncludeForDiagnostics(SourceLocation IncLoc, |
743 | SourceLocation Loc) { |
744 | Module *IncM = getModuleForLocation(IncLoc); |
745 | |
746 | |
747 | |
748 | |
749 | |
750 | auto &SM = getSourceManager(); |
751 | while (!Loc.isInvalid() && !SM.isInMainFile(Loc)) { |
752 | auto ID = SM.getFileID(SM.getExpansionLoc(Loc)); |
753 | auto *FE = SM.getFileEntryForID(ID); |
754 | if (!FE) |
755 | break; |
756 | |
757 | |
758 | |
759 | HeaderInfo.hasModuleMap(FE->getName(), nullptr, |
760 | SourceMgr.isInSystemHeader(Loc)); |
761 | |
762 | bool InPrivateHeader = false; |
763 | for (auto Header : HeaderInfo.findAllModulesForHeader(FE)) { |
764 | if (!Header.isAccessibleFrom(IncM)) { |
765 | |
766 | |
767 | |
768 | |
769 | InPrivateHeader = true; |
770 | continue; |
771 | } |
772 | |
773 | |
774 | |
775 | if (Header.getRole() & ModuleMap::TextualHeader) |
776 | continue; |
777 | |
778 | |
779 | |
780 | |
781 | if (getLangOpts().ObjC || getLangOpts().CPlusPlusModules || |
782 | getLangOpts().ModulesTS) |
783 | return nullptr; |
784 | |
785 | |
786 | |
787 | |
788 | return FE; |
789 | } |
790 | |
791 | |
792 | |
793 | if (InPrivateHeader) |
794 | return nullptr; |
795 | |
796 | |
797 | |
798 | |
799 | if (getHeaderSearchInfo().isFileMultipleIncludeGuarded(FE)) |
800 | return FE; |
801 | |
802 | Loc = SM.getIncludeLoc(ID); |
803 | } |
804 | |
805 | return nullptr; |
806 | } |
807 | |
808 | Optional<FileEntryRef> Preprocessor::LookupFile( |
809 | SourceLocation FilenameLoc, StringRef Filename, bool isAngled, |
810 | const DirectoryLookup *FromDir, const FileEntry *FromFile, |
811 | const DirectoryLookup *&CurDir, SmallVectorImpl<char> *SearchPath, |
812 | SmallVectorImpl<char> *RelativePath, |
813 | ModuleMap::KnownHeader *SuggestedModule, bool *IsMapped, |
814 | bool *IsFrameworkFound, bool SkipCache) { |
815 | Module *RequestingModule = getModuleForLocation(FilenameLoc); |
816 | bool RequestingModuleIsModuleInterface = !SourceMgr.isInMainFile(FilenameLoc); |
817 | |
818 | |
819 | |
820 | SmallVector<std::pair<const FileEntry *, const DirectoryEntry *>, 16> |
821 | Includers; |
822 | bool BuildSystemModule = false; |
823 | if (!FromDir && !FromFile) { |
824 | FileID FID = getCurrentFileLexer()->getFileID(); |
825 | const FileEntry *FileEnt = SourceMgr.getFileEntryForID(FID); |
826 | |
827 | |
828 | |
829 | |
830 | |
831 | |
832 | |
833 | |
834 | |
835 | |
836 | |
837 | |
838 | |
839 | if (!FileEnt) { |
840 | if (FID == SourceMgr.getMainFileID() && MainFileDir) { |
841 | Includers.push_back(std::make_pair(nullptr, MainFileDir)); |
842 | BuildSystemModule = getCurrentModule()->IsSystem; |
843 | } else if ((FileEnt = |
844 | SourceMgr.getFileEntryForID(SourceMgr.getMainFileID()))) |
845 | Includers.push_back(std::make_pair(FileEnt, *FileMgr.getDirectory("."))); |
846 | } else { |
847 | Includers.push_back(std::make_pair(FileEnt, FileEnt->getDir())); |
848 | } |
849 | |
850 | |
851 | |
852 | |
853 | if (LangOpts.MSVCCompat && !isAngled) { |
854 | for (IncludeStackInfo &ISEntry : llvm::reverse(IncludeMacroStack)) { |
855 | if (IsFileLexer(ISEntry)) |
856 | if ((FileEnt = ISEntry.ThePPLexer->getFileEntry())) |
857 | Includers.push_back(std::make_pair(FileEnt, FileEnt->getDir())); |
858 | } |
859 | } |
860 | } |
861 | |
862 | CurDir = CurDirLookup; |
863 | |
864 | if (FromFile) { |
865 | |
866 | |
867 | const DirectoryLookup *TmpCurDir = CurDir; |
868 | const DirectoryLookup *TmpFromDir = nullptr; |
869 | while (Optional<FileEntryRef> FE = HeaderInfo.LookupFile( |
870 | Filename, FilenameLoc, isAngled, TmpFromDir, TmpCurDir, |
871 | Includers, SearchPath, RelativePath, RequestingModule, |
872 | SuggestedModule, nullptr, |
873 | nullptr, SkipCache)) { |
874 | |
875 | TmpFromDir = TmpCurDir; |
876 | ++TmpFromDir; |
877 | if (&FE->getFileEntry() == FromFile) { |
878 | |
879 | FromDir = TmpFromDir; |
880 | CurDir = TmpCurDir; |
881 | break; |
882 | } |
883 | } |
884 | } |
885 | |
886 | |
887 | Optional<FileEntryRef> FE = HeaderInfo.LookupFile( |
888 | Filename, FilenameLoc, isAngled, FromDir, CurDir, Includers, SearchPath, |
889 | RelativePath, RequestingModule, SuggestedModule, IsMapped, |
890 | IsFrameworkFound, SkipCache, BuildSystemModule); |
891 | if (FE) { |
892 | if (SuggestedModule && !LangOpts.AsmPreprocessor) |
893 | HeaderInfo.getModuleMap().diagnoseHeaderInclusion( |
894 | RequestingModule, RequestingModuleIsModuleInterface, FilenameLoc, |
895 | Filename, &FE->getFileEntry()); |
896 | return FE; |
897 | } |
898 | |
899 | const FileEntry *CurFileEnt; |
900 | |
901 | |
902 | |
903 | if (IsFileLexer()) { |
904 | if ((CurFileEnt = CurPPLexer->getFileEntry())) { |
905 | if (Optional<FileEntryRef> FE = HeaderInfo.LookupSubframeworkHeader( |
906 | Filename, CurFileEnt, SearchPath, RelativePath, RequestingModule, |
907 | SuggestedModule)) { |
908 | if (SuggestedModule && !LangOpts.AsmPreprocessor) |
909 | HeaderInfo.getModuleMap().diagnoseHeaderInclusion( |
910 | RequestingModule, RequestingModuleIsModuleInterface, FilenameLoc, |
911 | Filename, &FE->getFileEntry()); |
912 | return FE; |
913 | } |
914 | } |
915 | } |
916 | |
917 | for (IncludeStackInfo &ISEntry : llvm::reverse(IncludeMacroStack)) { |
918 | if (IsFileLexer(ISEntry)) { |
919 | if ((CurFileEnt = ISEntry.ThePPLexer->getFileEntry())) { |
920 | if (Optional<FileEntryRef> FE = HeaderInfo.LookupSubframeworkHeader( |
921 | Filename, CurFileEnt, SearchPath, RelativePath, |
922 | RequestingModule, SuggestedModule)) { |
923 | if (SuggestedModule && !LangOpts.AsmPreprocessor) |
924 | HeaderInfo.getModuleMap().diagnoseHeaderInclusion( |
925 | RequestingModule, RequestingModuleIsModuleInterface, |
926 | FilenameLoc, Filename, &FE->getFileEntry()); |
927 | return FE; |
928 | } |
929 | } |
930 | } |
931 | } |
932 | |
933 | |
934 | return None; |
935 | } |
936 | |
937 | |
938 | |
939 | |
940 | |
941 | class Preprocessor::ResetMacroExpansionHelper { |
942 | public: |
943 | ResetMacroExpansionHelper(Preprocessor *pp) |
944 | : PP(pp), save(pp->DisableMacroExpansion) { |
945 | if (pp->MacroExpansionInDirectivesOverride) |
946 | pp->DisableMacroExpansion = false; |
947 | } |
948 | |
949 | ~ResetMacroExpansionHelper() { |
950 | PP->DisableMacroExpansion = save; |
951 | } |
952 | |
953 | private: |
954 | Preprocessor *PP; |
955 | bool save; |
956 | }; |
957 | |
958 | |
959 | |
960 | |
961 | |
962 | |
963 | |
964 | void Preprocessor::HandleSkippedDirectiveWhileUsingPCH(Token &Result, |
965 | SourceLocation HashLoc) { |
966 | if (const IdentifierInfo *II = Result.getIdentifierInfo()) { |
| 8 | | Assuming 'II' is non-null | |
|
| |
967 | if (II->getPPKeywordID() == tok::pp_define) { |
| 10 | | Assuming the condition is true | |
|
| |
968 | return HandleDefineDirective(Result, |
| 12 | | Calling 'Preprocessor::HandleDefineDirective' | |
|
969 | false); |
970 | } |
971 | if (SkippingUntilPCHThroughHeader && |
972 | II->getPPKeywordID() == tok::pp_include) { |
973 | return HandleIncludeDirective(HashLoc, Result); |
974 | } |
975 | if (SkippingUntilPragmaHdrStop && II->getPPKeywordID() == tok::pp_pragma) { |
976 | Lex(Result); |
977 | auto *II = Result.getIdentifierInfo(); |
978 | if (II && II->getName() == "hdrstop") |
979 | return HandlePragmaHdrstop(Result); |
980 | } |
981 | } |
982 | DiscardUntilEndOfDirective(); |
983 | } |
984 | |
985 | |
986 | |
987 | |
988 | |
989 | void Preprocessor::HandleDirective(Token &Result) { |
990 | |
991 | |
992 | |
993 | |
994 | |
995 | CurPPLexer->ParsingPreprocessorDirective = true; |
996 | if (CurLexer) CurLexer->SetKeepWhitespaceMode(false); |
| |
997 | |
998 | bool ImmediatelyAfterTopLevelIfndef = |
999 | CurPPLexer->MIOpt.getImmediatelyAfterTopLevelIfndef(); |
1000 | CurPPLexer->MIOpt.resetImmediatelyAfterTopLevelIfndef(); |
1001 | |
1002 | ++NumDirectives; |
1003 | |
1004 | |
1005 | |
1006 | |
1007 | bool ReadAnyTokensBeforeDirective =CurPPLexer->MIOpt.getHasReadAnyTokensVal(); |
1008 | |
1009 | |
1010 | Token SavedHash = Result; |
1011 | |
1012 | |
1013 | |
1014 | LexUnexpandedToken(Result); |
1015 | |
1016 | |
1017 | |
1018 | |
1019 | |
1020 | |
1021 | |
1022 | |
1023 | |
1024 | if (InMacroArgs) { |
| 2 | | Assuming field 'InMacroArgs' is false | |
|
| |
1025 | if (IdentifierInfo *II = Result.getIdentifierInfo()) { |
1026 | switch (II->getPPKeywordID()) { |
1027 | case tok::pp_include: |
1028 | case tok::pp_import: |
1029 | case tok::pp_include_next: |
1030 | case tok::pp___include_macros: |
1031 | case tok::pp_pragma: |
1032 | Diag(Result, diag::err_embedded_directive) << II->getName(); |
1033 | Diag(*ArgMacro, diag::note_macro_expansion_here) |
1034 | << ArgMacro->getIdentifierInfo(); |
1035 | DiscardUntilEndOfDirective(); |
1036 | return; |
1037 | default: |
1038 | break; |
1039 | } |
1040 | } |
1041 | Diag(Result, diag::ext_embedded_directive); |
1042 | } |
1043 | |
1044 | |
1045 | |
1046 | ResetMacroExpansionHelper helper(this); |
1047 | |
1048 | if (SkippingUntilPCHThroughHeader || SkippingUntilPragmaHdrStop) |
| 4 | | Assuming field 'SkippingUntilPCHThroughHeader' is false | |
|
| 5 | | Assuming field 'SkippingUntilPragmaHdrStop' is true | |
|
| |
1049 | return HandleSkippedDirectiveWhileUsingPCH(Result, SavedHash.getLocation()); |
| 7 | | Calling 'Preprocessor::HandleSkippedDirectiveWhileUsingPCH' | |
|
1050 | |
1051 | switch (Result.getKind()) { |
1052 | case tok::eod: |
1053 | return; |
1054 | case tok::code_completion: |
1055 | setCodeCompletionReached(); |
1056 | if (CodeComplete) |
1057 | CodeComplete->CodeCompleteDirective( |
1058 | CurPPLexer->getConditionalStackDepth() > 0); |
1059 | return; |
1060 | case tok::numeric_constant: |
1061 | if (getLangOpts().AsmPreprocessor) |
1062 | break; |
1063 | return HandleDigitDirective(Result); |
1064 | default: |
1065 | IdentifierInfo *II = Result.getIdentifierInfo(); |
1066 | if (!II) break; |
1067 | |
1068 | |
1069 | switch (II->getPPKeywordID()) { |
1070 | default: break; |
1071 | |
1072 | case tok::pp_if: |
1073 | return HandleIfDirective(Result, SavedHash, ReadAnyTokensBeforeDirective); |
1074 | case tok::pp_ifdef: |
1075 | return HandleIfdefDirective(Result, SavedHash, false, |
1076 | true ); |
1077 | case tok::pp_ifndef: |
1078 | return HandleIfdefDirective(Result, SavedHash, true, |
1079 | ReadAnyTokensBeforeDirective); |
1080 | case tok::pp_elif: |
1081 | case tok::pp_elifdef: |
1082 | case tok::pp_elifndef: |
1083 | return HandleElifFamilyDirective(Result, SavedHash, II->getPPKeywordID()); |
1084 | |
1085 | case tok::pp_else: |
1086 | return HandleElseDirective(Result, SavedHash); |
1087 | case tok::pp_endif: |
1088 | return HandleEndifDirective(Result); |
1089 | |
1090 | |
1091 | case tok::pp_include: |
1092 | |
1093 | return HandleIncludeDirective(SavedHash.getLocation(), Result); |
1094 | case tok::pp___include_macros: |
1095 | |
1096 | return HandleIncludeMacrosDirective(SavedHash.getLocation(), Result); |
1097 | |
1098 | |
1099 | case tok::pp_define: |
1100 | return HandleDefineDirective(Result, ImmediatelyAfterTopLevelIfndef); |
1101 | case tok::pp_undef: |
1102 | return HandleUndefDirective(); |
1103 | |
1104 | |
1105 | case tok::pp_line: |
1106 | return HandleLineDirective(); |
1107 | |
1108 | |
1109 | case tok::pp_error: |
1110 | return HandleUserDiagnosticDirective(Result, false); |
1111 | |
1112 | |
1113 | case tok::pp_pragma: |
1114 | return HandlePragmaDirective({PIK_HashPragma, SavedHash.getLocation()}); |
1115 | |
1116 | |
1117 | case tok::pp_import: |
1118 | return HandleImportDirective(SavedHash.getLocation(), Result); |
1119 | case tok::pp_include_next: |
1120 | return HandleIncludeNextDirective(SavedHash.getLocation(), Result); |
1121 | |
1122 | case tok::pp_warning: |
1123 | Diag(Result, diag::ext_pp_warning_directive); |
1124 | return HandleUserDiagnosticDirective(Result, true); |
1125 | case tok::pp_ident: |
1126 | return HandleIdentSCCSDirective(Result); |
1127 | case tok::pp_sccs: |
1128 | return HandleIdentSCCSDirective(Result); |
1129 | case tok::pp_assert: |
1130 | |
1131 | break; |
1132 | case tok::pp_unassert: |
1133 | |
1134 | break; |
1135 | |
1136 | case tok::pp___public_macro: |
1137 | if (getLangOpts().Modules || getLangOpts().ModulesLocalVisibility) |
1138 | return HandleMacroPublicDirective(Result); |
1139 | break; |
1140 | |
1141 | case tok::pp___private_macro: |
1142 | if (getLangOpts().Modules || getLangOpts().ModulesLocalVisibility) |
1143 | return HandleMacroPrivateDirective(); |
1144 | break; |
1145 | } |
1146 | break; |
1147 | } |
1148 | |
1149 | |
1150 | |
1151 | |
1152 | |
1153 | if (getLangOpts().AsmPreprocessor) { |
1154 | auto Toks = std::make_unique<Token[]>(2); |
1155 | |
1156 | Toks[0] = SavedHash; |
1157 | Toks[1] = Result; |
1158 | |
1159 | |
1160 | |
1161 | if (Result.is(tok::hashhash)) |
1162 | Toks[1].setKind(tok::unknown); |
1163 | |
1164 | |
1165 | |
1166 | |
1167 | EnterTokenStream(std::move(Toks), 2, false, false); |
1168 | return; |
1169 | } |
1170 | |
1171 | |
1172 | Diag(Result, diag::err_pp_invalid_directive); |
1173 | |
1174 | |
1175 | DiscardUntilEndOfDirective(); |
1176 | |
1177 | |
1178 | } |
1179 | |
1180 | |
1181 | |
1182 | static bool GetLineValue(Token &DigitTok, unsigned &Val, |
1183 | unsigned DiagID, Preprocessor &PP, |
1184 | bool IsGNULineDirective=false) { |
1185 | if (DigitTok.isNot(tok::numeric_constant)) { |
1186 | PP.Diag(DigitTok, DiagID); |
1187 | |
1188 | if (DigitTok.isNot(tok::eod)) |
1189 | PP.DiscardUntilEndOfDirective(); |
1190 | return true; |
1191 | } |
1192 | |
1193 | SmallString<64> IntegerBuffer; |
1194 | IntegerBuffer.resize(DigitTok.getLength()); |
1195 | const char *DigitTokBegin = &IntegerBuffer[0]; |
1196 | bool Invalid = false; |
1197 | unsigned ActualLength = PP.getSpelling(DigitTok, DigitTokBegin, &Invalid); |
1198 | if (Invalid) |
1199 | return true; |
1200 | |
1201 | |
1202 | |
1203 | |
1204 | Val = 0; |
1205 | for (unsigned i = 0; i != ActualLength; ++i) { |
1206 | |
1207 | |
1208 | if (DigitTokBegin[i] == '\'') |
1209 | continue; |
1210 | |
1211 | if (!isDigit(DigitTokBegin[i])) { |
1212 | PP.Diag(PP.AdvanceToTokenCharacter(DigitTok.getLocation(), i), |
1213 | diag::err_pp_line_digit_sequence) << IsGNULineDirective; |
1214 | PP.DiscardUntilEndOfDirective(); |
1215 | return true; |
1216 | } |
1217 | |
1218 | unsigned NextVal = Val*10+(DigitTokBegin[i]-'0'); |
1219 | if (NextVal < Val) { |
1220 | PP.Diag(DigitTok, DiagID); |
1221 | PP.DiscardUntilEndOfDirective(); |
1222 | return true; |
1223 | } |
1224 | Val = NextVal; |
1225 | } |
1226 | |
1227 | if (DigitTokBegin[0] == '0' && Val) |
1228 | PP.Diag(DigitTok.getLocation(), diag::warn_pp_line_decimal) |
1229 | << IsGNULineDirective; |
1230 | |
1231 | return false; |
1232 | } |
1233 | |
1234 | |
1235 | |
1236 | |
1237 | |
1238 | |
1239 | |
1240 | |
1241 | void Preprocessor::HandleLineDirective() { |
1242 | |
1243 | |
1244 | Token DigitTok; |
1245 | Lex(DigitTok); |
1246 | |
1247 | |
1248 | unsigned LineNo; |
1249 | if (GetLineValue(DigitTok, LineNo, diag::err_pp_line_requires_integer,*this)) |
1250 | return; |
1251 | |
1252 | if (LineNo == 0) |
1253 | Diag(DigitTok, diag::ext_pp_line_zero); |
1254 | |
1255 | |
1256 | |
1257 | unsigned LineLimit = 32768U; |
1258 | if (LangOpts.C99 || LangOpts.CPlusPlus11) |
1259 | LineLimit = 2147483648U; |
1260 | if (LineNo >= LineLimit) |
1261 | Diag(DigitTok, diag::ext_pp_line_too_big) << LineLimit; |
1262 | else if (LangOpts.CPlusPlus11 && LineNo >= 32768U) |
1263 | Diag(DigitTok, diag::warn_cxx98_compat_pp_line_too_big); |
1264 | |
1265 | int FilenameID = -1; |
1266 | Token StrTok; |
1267 | Lex(StrTok); |
1268 | |
1269 | |
1270 | |
1271 | if (StrTok.is(tok::eod)) |
1272 | ; |
1273 | else if (StrTok.isNot(tok::string_literal)) { |
1274 | Diag(StrTok, diag::err_pp_line_invalid_filename); |
1275 | DiscardUntilEndOfDirective(); |
1276 | return; |
1277 | } else if (StrTok.hasUDSuffix()) { |
1278 | Diag(StrTok, diag::err_invalid_string_udl); |
1279 | DiscardUntilEndOfDirective(); |
1280 | return; |
1281 | } else { |
1282 | |
1283 | StringLiteralParser Literal(StrTok, *this); |
1284 | assert(Literal.isAscii() && "Didn't allow wide strings in"); |
1285 | if (Literal.hadError) { |
1286 | DiscardUntilEndOfDirective(); |
1287 | return; |
1288 | } |
1289 | if (Literal.Pascal) { |
1290 | Diag(StrTok, diag::err_pp_linemarker_invalid_filename); |
1291 | DiscardUntilEndOfDirective(); |
1292 | return; |
1293 | } |
1294 | FilenameID = SourceMgr.getLineTableFilenameID(Literal.GetString()); |
1295 | |
1296 | |
1297 | |
1298 | CheckEndOfDirective("line", true); |
1299 | } |
1300 | |
1301 | |
1302 | |
1303 | |
1304 | |
1305 | |
1306 | SrcMgr::CharacteristicKind FileKind = |
1307 | SourceMgr.getFileCharacteristic(DigitTok.getLocation()); |
1308 | |
1309 | SourceMgr.AddLineNote(DigitTok.getLocation(), LineNo, FilenameID, false, |
1310 | false, FileKind); |
1311 | |
1312 | if (Callbacks) |
1313 | Callbacks->FileChanged(CurPPLexer->getSourceLocation(), |
1314 | PPCallbacks::RenameFile, FileKind); |
1315 | } |
1316 | |
1317 | |
1318 | |
1319 | static bool ReadLineMarkerFlags(bool &IsFileEntry, bool &IsFileExit, |
1320 | SrcMgr::CharacteristicKind &FileKind, |
1321 | Preprocessor &PP) { |
1322 | unsigned FlagVal; |
1323 | Token FlagTok; |
1324 | PP.Lex(FlagTok); |
1325 | if (FlagTok.is(tok::eod)) return false; |
1326 | if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag, PP)) |
1327 | return true; |
1328 | |
1329 | if (FlagVal == 1) { |
1330 | IsFileEntry = true; |
1331 | |
1332 | PP.Lex(FlagTok); |
1333 | if (FlagTok.is(tok::eod)) return false; |
1334 | if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag,PP)) |
1335 | return true; |
1336 | } else if (FlagVal == 2) { |
1337 | IsFileExit = true; |
1338 | |
1339 | SourceManager &SM = PP.getSourceManager(); |
1340 | |
1341 | |
1342 | FileID CurFileID = |
1343 | SM.getDecomposedExpansionLoc(FlagTok.getLocation()).first; |
1344 | PresumedLoc PLoc = SM.getPresumedLoc(FlagTok.getLocation()); |
1345 | if (PLoc.isInvalid()) |
1346 | return true; |
1347 | |
1348 | |
1349 | |
1350 | SourceLocation IncLoc = PLoc.getIncludeLoc(); |
1351 | if (IncLoc.isInvalid() || |
1352 | SM.getDecomposedExpansionLoc(IncLoc).first != CurFileID) { |
1353 | PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_pop); |
1354 | PP.DiscardUntilEndOfDirective(); |
1355 | return true; |
1356 | } |
1357 | |
1358 | PP.Lex(FlagTok); |
1359 | if (FlagTok.is(tok::eod)) return false; |
1360 | if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag,PP)) |
1361 | return true; |
1362 | } |
1363 | |
1364 | |
1365 | if (FlagVal != 3) { |
1366 | PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag); |
1367 | PP.DiscardUntilEndOfDirective(); |
1368 | return true; |
1369 | } |
1370 | |
1371 | FileKind = SrcMgr::C_System; |
1372 | |
1373 | PP.Lex(FlagTok); |
1374 | if (FlagTok.is(tok::eod)) return false; |
1375 | if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag, PP)) |
1376 | return true; |
1377 | |
1378 | |
1379 | if (FlagVal != 4) { |
1380 | PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag); |
1381 | PP.DiscardUntilEndOfDirective(); |
1382 | return true; |
1383 | } |
1384 | |
1385 | FileKind = SrcMgr::C_ExternCSystem; |
1386 | |
1387 | PP.Lex(FlagTok); |
1388 | if (FlagTok.is(tok::eod)) return false; |
1389 | |
1390 | |
1391 | PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag); |
1392 | PP.DiscardUntilEndOfDirective(); |
1393 | return true; |
1394 | } |
1395 | |
1396 | |
1397 | |
1398 | |
1399 | |
1400 | |
1401 | |
1402 | |
1403 | void Preprocessor::HandleDigitDirective(Token &DigitTok) { |
1404 | |
1405 | |
1406 | unsigned LineNo; |
1407 | if (GetLineValue(DigitTok, LineNo, diag::err_pp_linemarker_requires_integer, |
1408 | *this, true)) |
1409 | return; |
1410 | |
1411 | Token StrTok; |
1412 | Lex(StrTok); |
1413 | |
1414 | bool IsFileEntry = false, IsFileExit = false; |
1415 | int FilenameID = -1; |
1416 | SrcMgr::CharacteristicKind FileKind = SrcMgr::C_User; |
1417 | |
1418 | |
1419 | |
1420 | if (StrTok.is(tok::eod)) { |
1421 | |
1422 | FileKind = SourceMgr.getFileCharacteristic(DigitTok.getLocation()); |
1423 | } else if (StrTok.isNot(tok::string_literal)) { |
1424 | Diag(StrTok, diag::err_pp_linemarker_invalid_filename); |
1425 | DiscardUntilEndOfDirective(); |
1426 | return; |
1427 | } else if (StrTok.hasUDSuffix()) { |
1428 | Diag(StrTok, diag::err_invalid_string_udl); |
1429 | DiscardUntilEndOfDirective(); |
1430 | return; |
1431 | } else { |
1432 | |
1433 | StringLiteralParser Literal(StrTok, *this); |
1434 | assert(Literal.isAscii() && "Didn't allow wide strings in"); |
1435 | if (Literal.hadError) { |
1436 | DiscardUntilEndOfDirective(); |
1437 | return; |
1438 | } |
1439 | if (Literal.Pascal) { |
1440 | Diag(StrTok, diag::err_pp_linemarker_invalid_filename); |
1441 | DiscardUntilEndOfDirective(); |
1442 | return; |
1443 | } |
1444 | FilenameID = SourceMgr.getLineTableFilenameID(Literal.GetString()); |
1445 | |
1446 | |
1447 | if (ReadLineMarkerFlags(IsFileEntry, IsFileExit, FileKind, *this)) |
1448 | return; |
1449 | } |
1450 | |
1451 | |
1452 | SourceMgr.AddLineNote(DigitTok.getLocation(), LineNo, FilenameID, IsFileEntry, |
1453 | IsFileExit, FileKind); |
1454 | |
1455 | |
1456 | |
1457 | |
1458 | if (Callbacks) { |
1459 | PPCallbacks::FileChangeReason Reason = PPCallbacks::RenameFile; |
1460 | if (IsFileEntry) |
1461 | Reason = PPCallbacks::EnterFile; |
1462 | else if (IsFileExit) |
1463 | Reason = PPCallbacks::ExitFile; |
1464 | |
1465 | Callbacks->FileChanged(CurPPLexer->getSourceLocation(), Reason, FileKind); |
1466 | } |
1467 | } |
1468 | |
1469 | |
1470 | |
1471 | void Preprocessor::HandleUserDiagnosticDirective(Token &Tok, |
1472 | bool isWarning) { |
1473 | |
1474 | |
1475 | |
1476 | |
1477 | |
1478 | SmallString<128> Message; |
1479 | CurLexer->ReadToEndOfLine(&Message); |
1480 | |
1481 | |
1482 | |
1483 | StringRef Msg = Message.str().ltrim(' '); |
1484 | |
1485 | if (isWarning) |
1486 | Diag(Tok, diag::pp_hash_warning) << Msg; |
1487 | else |
1488 | Diag(Tok, diag::err_pp_hash_error) << Msg; |
1489 | } |
1490 | |
1491 | |
1492 | |
1493 | void Preprocessor::HandleIdentSCCSDirective(Token &Tok) { |
1494 | |
1495 | Diag(Tok, diag::ext_pp_ident_directive); |
1496 | |
1497 | |
1498 | Token StrTok; |
1499 | Lex(StrTok); |
1500 | |
1501 | |
1502 | if (StrTok.isNot(tok::string_literal) && |
1503 | StrTok.isNot(tok::wide_string_literal)) { |
1504 | Diag(StrTok, diag::err_pp_malformed_ident); |
1505 | if (StrTok.isNot(tok::eod)) |
1506 | DiscardUntilEndOfDirective(); |
1507 | return; |
1508 | } |
1509 | |
1510 | if (StrTok.hasUDSuffix()) { |
1511 | Diag(StrTok, diag::err_invalid_string_udl); |
1512 | DiscardUntilEndOfDirective(); |
1513 | return; |
1514 | } |
1515 | |
1516 | |
1517 | CheckEndOfDirective("ident"); |
1518 | |
1519 | if (Callbacks) { |
1520 | bool Invalid = false; |
1521 | std::string Str = getSpelling(StrTok, &Invalid); |
1522 | if (!Invalid) |
1523 | Callbacks->Ident(Tok.getLocation(), Str); |
1524 | } |
1525 | } |
1526 | |
1527 | |
1528 | void Preprocessor::HandleMacroPublicDirective(Token &Tok) { |
1529 | Token MacroNameTok; |
1530 | ReadMacroName(MacroNameTok, MU_Undef); |
1531 | |
1532 | |
1533 | if (MacroNameTok.is(tok::eod)) |
1534 | return; |
1535 | |
1536 | |
1537 | CheckEndOfDirective("__public_macro"); |
1538 | |
1539 | IdentifierInfo *II = MacroNameTok.getIdentifierInfo(); |
1540 | |
1541 | MacroDirective *MD = getLocalMacroDirective(II); |
1542 | |
1543 | |
1544 | if (!MD) { |
1545 | Diag(MacroNameTok, diag::err_pp_visibility_non_macro) << II; |
1546 | return; |
1547 | } |
1548 | |
1549 | |
1550 | appendMacroDirective(II, AllocateVisibilityMacroDirective( |
1551 | MacroNameTok.getLocation(), true)); |
1552 | } |
1553 | |
1554 | |
1555 | void Preprocessor::HandleMacroPrivateDirective() { |
1556 | Token MacroNameTok; |
1557 | ReadMacroName(MacroNameTok, MU_Undef); |
1558 | |
1559 | |
1560 | if (MacroNameTok.is(tok::eod)) |
1561 | return; |
1562 | |
1563 | |
1564 | CheckEndOfDirective("__private_macro"); |
1565 | |
1566 | IdentifierInfo *II = MacroNameTok.getIdentifierInfo(); |
1567 | |
1568 | MacroDirective *MD = getLocalMacroDirective(II); |
1569 | |
1570 | |
1571 | if (!MD) { |
1572 | Diag(MacroNameTok, diag::err_pp_visibility_non_macro) << II; |
1573 | return; |
1574 | } |
1575 | |
1576 | |
1577 | appendMacroDirective(II, AllocateVisibilityMacroDirective( |
1578 | MacroNameTok.getLocation(), false)); |
1579 | } |
1580 | |
1581 | |
1582 | |
1583 | |
1584 | |
1585 | |
1586 | |
1587 | |
1588 | |
1589 | |
1590 | |
1591 | bool Preprocessor::GetIncludeFilenameSpelling(SourceLocation Loc, |
1592 | StringRef &Buffer) { |
1593 | |
1594 | assert(!Buffer.empty() && "Can't have tokens with empty spellings!"); |
1595 | |
1596 | |
1597 | |
1598 | |
1599 | |
1600 | |
1601 | |
1602 | |
1603 | |
1604 | |
1605 | bool isAngled; |
1606 | if (Buffer[0] == '<') { |
1607 | if (Buffer.back() != '>') { |
1608 | Diag(Loc, diag::err_pp_expects_filename); |
1609 | Buffer = StringRef(); |
1610 | return true; |
1611 | } |
1612 | isAngled = true; |
1613 | } else if (Buffer[0] == '"') { |
1614 | if (Buffer.back() != '"') { |
1615 | Diag(Loc, diag::err_pp_expects_filename); |
1616 | Buffer = StringRef(); |
1617 | return true; |
1618 | } |
1619 | isAngled = false; |
1620 | } else { |
1621 | Diag(Loc, diag::err_pp_expects_filename); |
1622 | Buffer = StringRef(); |
1623 | return true; |
1624 | } |
1625 | |
1626 | |
1627 | if (Buffer.size() <= 2) { |
1628 | Diag(Loc, diag::err_pp_empty_filename); |
1629 | Buffer = StringRef(); |
1630 | return true; |
1631 | } |
1632 | |
1633 | |
1634 | Buffer = Buffer.substr(1, Buffer.size()-2); |
1635 | return isAngled; |
1636 | } |
1637 | |
1638 | |
1639 | void Preprocessor::EnterAnnotationToken(SourceRange Range, |
1640 | tok::TokenKind Kind, |
1641 | void *AnnotationVal) { |
1642 | |
1643 | |
1644 | auto Tok = std::make_unique<Token[]>(1); |
1645 | Tok[0].startToken(); |
1646 | Tok[0].setKind(Kind); |
1647 | Tok[0].setLocation(Range.getBegin()); |
1648 | Tok[0].setAnnotationEndLoc(Range.getEnd()); |
1649 | Tok[0].setAnnotationValue(AnnotationVal); |
1650 | EnterTokenStream(std::move(Tok), 1, true, false); |
1651 | } |
1652 | |
1653 | |
1654 | |
1655 | static void diagnoseAutoModuleImport( |
1656 | Preprocessor &PP, SourceLocation HashLoc, Token &IncludeTok, |
1657 | ArrayRef<std::pair<IdentifierInfo *, SourceLocation>> Path, |
1658 | SourceLocation PathEnd) { |
1659 | StringRef ImportKeyword; |
1660 | if (PP.getLangOpts().ObjC) |
1661 | ImportKeyword = "@import"; |
1662 | else if (PP.getLangOpts().ModulesTS || PP.getLangOpts().CPlusPlusModules) |
1663 | ImportKeyword = "import"; |
1664 | else |
1665 | return; |
1666 | |
1667 | SmallString<128> PathString; |
1668 | for (size_t I = 0, N = Path.size(); I != N; ++I) { |
1669 | if (I) |
1670 | PathString += '.'; |
1671 | PathString += Path[I].first->getName(); |
1672 | } |
1673 | int IncludeKind = 0; |
1674 | |
1675 | switch (IncludeTok.getIdentifierInfo()->getPPKeywordID()) { |
1676 | case tok::pp_include: |
1677 | IncludeKind = 0; |
1678 | break; |
1679 | |
1680 | case tok::pp_import: |
1681 | IncludeKind = 1; |
1682 | break; |
1683 | |
1684 | case tok::pp_include_next: |
1685 | IncludeKind = 2; |
1686 | break; |
1687 | |
1688 | case tok::pp___include_macros: |
1689 | IncludeKind = 3; |
1690 | break; |
1691 | |
1692 | default: |
1693 | llvm_unreachable("unknown include directive kind"); |
1694 | } |
1695 | |
1696 | CharSourceRange ReplaceRange(SourceRange(HashLoc, PathEnd), |
1697 | false); |
1698 | PP.Diag(HashLoc, diag::warn_auto_module_import) |
1699 | << IncludeKind << PathString |
1700 | << FixItHint::CreateReplacement( |
1701 | ReplaceRange, (ImportKeyword + " " + PathString + ";").str()); |
1702 | } |
1703 | |
1704 | |
1705 | |
1706 | |
1707 | static bool trySimplifyPath(SmallVectorImpl<StringRef> &Components, |
1708 | StringRef RealPathName) { |
1709 | auto RealPathComponentIter = llvm::sys::path::rbegin(RealPathName); |
1710 | auto RealPathComponentEnd = llvm::sys::path::rend(RealPathName); |
1711 | int Cnt = 0; |
1712 | bool SuggestReplacement = false; |
1713 | |
1714 | |
1715 | for (auto &Component : llvm::reverse(Components)) { |
1716 | if ("." == Component) { |
1717 | } else if (".." == Component) { |
1718 | ++Cnt; |
1719 | } else if (Cnt) { |
1720 | --Cnt; |
1721 | } else if (RealPathComponentIter != RealPathComponentEnd) { |
1722 | if (Component != *RealPathComponentIter) { |
1723 | |
1724 | |
1725 | |
1726 | SuggestReplacement = |
1727 | RealPathComponentIter->equals_insensitive(Component); |
1728 | if (!SuggestReplacement) |
1729 | break; |
1730 | Component = *RealPathComponentIter; |
1731 | } |
1732 | ++RealPathComponentIter; |
1733 | } |
1734 | } |
1735 | return SuggestReplacement; |
1736 | } |
1737 | |
1738 | bool Preprocessor::checkModuleIsAvailable(const LangOptions &LangOpts, |
1739 | const TargetInfo &TargetInfo, |
1740 | DiagnosticsEngine &Diags, Module *M) { |
1741 | Module::Requirement Requirement; |
1742 | Module::UnresolvedHeaderDirective MissingHeader; |
1743 | Module *ShadowingModule = nullptr; |
1744 | if (M->isAvailable(LangOpts, TargetInfo, Requirement, MissingHeader, |
1745 | ShadowingModule)) |
1746 | return false; |
1747 | |
1748 | if (MissingHeader.FileNameLoc.isValid()) { |
1749 | Diags.Report(MissingHeader.FileNameLoc, diag::err_module_header_missing) |
1750 | << MissingHeader.IsUmbrella << MissingHeader.FileName; |
1751 | } else if (ShadowingModule) { |
1752 | Diags.Report(M->DefinitionLoc, diag::err_module_shadowed) << M->Name; |
1753 | Diags.Report(ShadowingModule->DefinitionLoc, |
1754 | diag::note_previous_definition); |
1755 | } else { |
1756 | |
1757 | |
1758 | Diags.Report(M->DefinitionLoc, diag::err_module_unavailable) |
1759 | << M->getFullModuleName() << Requirement.second << Requirement.first; |
1760 | } |
1761 | return true; |
1762 | } |
1763 | |
1764 | |
1765 | |
1766 | |
1767 | |
1768 | |
1769 | void Preprocessor::HandleIncludeDirective(SourceLocation HashLoc, |
1770 | Token &IncludeTok, |
1771 | const DirectoryLookup *LookupFrom, |
1772 | const FileEntry *LookupFromFile) { |
1773 | Token FilenameTok; |
1774 | if (LexHeaderName(FilenameTok)) |
1775 | return; |
1776 | |
1777 | if (FilenameTok.isNot(tok::header_name)) { |
1778 | Diag(FilenameTok.getLocation(), diag::err_pp_expects_filename); |
1779 | if (FilenameTok.isNot(tok::eod)) |
1780 | DiscardUntilEndOfDirective(); |
1781 | return; |
1782 | } |
1783 | |
1784 | |
1785 | |
1786 | |
1787 | |
1788 | SourceLocation EndLoc = |
1789 | CheckEndOfDirective(IncludeTok.getIdentifierInfo()->getNameStart(), true); |
1790 | |
1791 | auto Action = HandleHeaderIncludeOrImport(HashLoc, IncludeTok, FilenameTok, |
1792 | EndLoc, LookupFrom, LookupFromFile); |
1793 | switch (Action.Kind) { |
1794 | case ImportAction::None: |
1795 | case ImportAction::SkippedModuleImport: |
1796 | break; |
1797 | case ImportAction::ModuleBegin: |
1798 | EnterAnnotationToken(SourceRange(HashLoc, EndLoc), |
1799 | tok::annot_module_begin, Action.ModuleForHeader); |
1800 | break; |
1801 | case ImportAction::ModuleImport: |
1802 | EnterAnnotationToken(SourceRange(HashLoc, EndLoc), |
1803 | tok::annot_module_include, Action.ModuleForHeader); |
1804 | break; |
1805 | case ImportAction::Failure: |
1806 | assert(TheModuleLoader.HadFatalFailure && |
1807 | "This should be an early exit only to a fatal error"); |
1808 | TheModuleLoader.HadFatalFailure = true; |
1809 | IncludeTok.setKind(tok::eof); |
1810 | CurLexer->cutOffLexing(); |
1811 | return; |
1812 | } |
1813 | } |
1814 | |
1815 | Optional<FileEntryRef> Preprocessor::LookupHeaderIncludeOrImport( |
1816 | const DirectoryLookup *&CurDir, StringRef& Filename, |
1817 | SourceLocation FilenameLoc, CharSourceRange FilenameRange, |
1818 | const Token &FilenameTok, bool &IsFrameworkFound, bool IsImportDecl, |
1819 | bool &IsMapped, const DirectoryLookup *LookupFrom, |
1820 | const FileEntry *LookupFromFile, StringRef& LookupFilename, |
1821 | SmallVectorImpl<char> &RelativePath, SmallVectorImpl<char> &SearchPath, |
1822 | ModuleMap::KnownHeader &SuggestedModule, bool isAngled) { |
1823 | Optional<FileEntryRef> File = LookupFile( |
1824 | FilenameLoc, LookupFilename, |
1825 | isAngled, LookupFrom, LookupFromFile, CurDir, |
1826 | Callbacks ? &SearchPath : nullptr, Callbacks ? &RelativePath : nullptr, |
1827 | &SuggestedModule, &IsMapped, &IsFrameworkFound); |
1828 | if (File) |
1829 | return File; |
1830 | |
1831 | if (Callbacks) { |
1832 | |
1833 | SmallString<128> RecoveryPath; |
1834 | if (Callbacks->FileNotFound(Filename, RecoveryPath)) { |
1835 | if (auto DE = FileMgr.getOptionalDirectoryRef(RecoveryPath)) { |
1836 | |
1837 | DirectoryLookup DL(*DE, SrcMgr::C_User, false); |
1838 | HeaderInfo.AddSearchPath(DL, isAngled); |
1839 | |
1840 | |
1841 | Optional<FileEntryRef> File = LookupFile( |
1842 | FilenameLoc, |
1843 | LookupFilename, isAngled, |
1844 | LookupFrom, LookupFromFile, CurDir, nullptr, nullptr, |
1845 | &SuggestedModule, &IsMapped, nullptr, |
1846 | true); |
1847 | if (File) |
1848 | return File; |
1849 | } |
1850 | } |
1851 | } |
1852 | |
1853 | if (SuppressIncludeNotFoundError) |
1854 | return None; |
1855 | |
1856 | |
1857 | |
1858 | |
1859 | if (isAngled) { |
1860 | Optional<FileEntryRef> File = LookupFile( |
1861 | FilenameLoc, LookupFilename, |
1862 | false, LookupFrom, LookupFromFile, CurDir, |
1863 | Callbacks ? &SearchPath : nullptr, Callbacks ? &RelativePath : nullptr, |
1864 | &SuggestedModule, &IsMapped, |
1865 | nullptr); |
1866 | if (File) { |
1867 | Diag(FilenameTok, diag::err_pp_file_not_found_angled_include_not_fatal) |
1868 | << Filename << IsImportDecl |
1869 | << FixItHint::CreateReplacement(FilenameRange, |
1870 | "\"" + Filename.str() + "\""); |
1871 | return File; |
1872 | } |
1873 | } |
1874 | |
1875 | |
1876 | |
1877 | StringRef OriginalFilename = Filename; |
1878 | if (LangOpts.SpellChecking) { |
1879 | |
1880 | |
1881 | auto CorrectTypoFilename = [](llvm::StringRef Filename) { |
1882 | Filename = Filename.drop_until(isAlphanumeric); |
1883 | while (!Filename.empty() && !isAlphanumeric(Filename.back())) { |
1884 | Filename = Filename.drop_back(); |
1885 | } |
1886 | return Filename; |
1887 | }; |
1888 | StringRef TypoCorrectionName = CorrectTypoFilename(Filename); |
1889 | StringRef TypoCorrectionLookupName = CorrectTypoFilename(LookupFilename); |
1890 | |
1891 | Optional<FileEntryRef> File = LookupFile( |
1892 | FilenameLoc, TypoCorrectionLookupName, isAngled, LookupFrom, LookupFromFile, |
1893 | CurDir, Callbacks ? &SearchPath : nullptr, |
1894 | Callbacks ? &RelativePath : nullptr, &SuggestedModule, &IsMapped, |
1895 | nullptr); |
1896 | if (File) { |
1897 | auto Hint = |
1898 | isAngled ? FixItHint::CreateReplacement( |
1899 | FilenameRange, "<" + TypoCorrectionName.str() + ">") |
1900 | : FixItHint::CreateReplacement( |
1901 | FilenameRange, "\"" + TypoCorrectionName.str() + "\""); |
1902 | Diag(FilenameTok, diag::err_pp_file_not_found_typo_not_fatal) |
1903 | << OriginalFilename << TypoCorrectionName << Hint; |
1904 | |
1905 | |
1906 | Filename = TypoCorrectionName; |
1907 | LookupFilename = TypoCorrectionLookupName; |
1908 | return File; |
1909 | } |
1910 | } |
1911 | |
1912 | |
1913 | assert(!File.hasValue() && "expected missing file"); |
1914 | Diag(FilenameTok, diag::err_pp_file_not_found) |
1915 | << OriginalFilename << FilenameRange; |
1916 | if (IsFrameworkFound) { |
1917 | size_t SlashPos = OriginalFilename.find('/'); |
1918 | assert(SlashPos != StringRef::npos && |
1919 | "Include with framework name should have '/' in the filename"); |
1920 | StringRef FrameworkName = OriginalFilename.substr(0, SlashPos); |
1921 | FrameworkCacheEntry &CacheEntry = |
1922 | HeaderInfo.LookupFrameworkCache(FrameworkName); |
1923 | assert(CacheEntry.Directory && "Found framework should be in cache"); |
1924 | Diag(FilenameTok, diag::note_pp_framework_without_header) |
1925 | << OriginalFilename.substr(SlashPos + 1) << FrameworkName |
1926 | << CacheEntry.Directory->getName(); |
1927 | } |
1928 | |
1929 | return None; |
1930 | } |
1931 | |
1932 | |
1933 | |
1934 | |
1935 | |
1936 | |
1937 | |
1938 | |
1939 | |
1940 | |
1941 | |
1942 | |
1943 | |
1944 | Preprocessor::ImportAction Preprocessor::HandleHeaderIncludeOrImport( |
1945 | SourceLocation HashLoc, Token &IncludeTok, Token &FilenameTok, |
1946 | SourceLocation EndLoc, const DirectoryLookup *LookupFrom, |
1947 | const FileEntry *LookupFromFile) { |
1948 | SmallString<128> FilenameBuffer; |
1949 | StringRef Filename = getSpelling(FilenameTok, FilenameBuffer); |
1950 | SourceLocation CharEnd = FilenameTok.getEndLoc(); |
1951 | |
1952 | CharSourceRange FilenameRange |
1953 | = CharSourceRange::getCharRange(FilenameTok.getLocation(), CharEnd); |
1954 | StringRef OriginalFilename = Filename; |
1955 | bool isAngled = |
1956 | GetIncludeFilenameSpelling(FilenameTok.getLocation(), Filename); |
1957 | |
1958 | |
1959 | |
1960 | if (Filename.empty()) |
1961 | return {ImportAction::None}; |
1962 | |
1963 | bool IsImportDecl = HashLoc.isInvalid(); |
1964 | SourceLocation StartLoc = IsImportDecl ? IncludeTok.getLocation() : HashLoc; |
1965 | |
1966 | |
1967 | if (PragmaARCCFCodeAuditedInfo.second.isValid()) { |
1968 | Diag(StartLoc, diag::err_pp_include_in_arc_cf_code_audited) << IsImportDecl; |
1969 | Diag(PragmaARCCFCodeAuditedInfo.second, diag::note_pragma_entered_here); |
1970 | |
1971 | |
1972 | PragmaARCCFCodeAuditedInfo = {nullptr, SourceLocation()}; |
1973 | } |
1974 | |
1975 | |
1976 | if (PragmaAssumeNonNullLoc.isValid()) { |
1977 | Diag(StartLoc, diag::err_pp_include_in_assume_nonnull) << IsImportDecl; |
1978 | Diag(PragmaAssumeNonNullLoc, diag::note_pragma_entered_here); |
1979 | |
1980 | |
1981 | PragmaAssumeNonNullLoc = SourceLocation(); |
1982 | } |
1983 | |
1984 | if (HeaderInfo.HasIncludeAliasMap()) { |
1985 | |
1986 | |
1987 | |
1988 | StringRef NewName = HeaderInfo.MapHeaderToIncludeAlias(OriginalFilename); |
1989 | if (!NewName.empty()) |
1990 | Filename = NewName; |
1991 | } |
1992 | |
1993 | |
1994 | bool IsMapped = false; |
1995 | bool IsFrameworkFound = false; |
1996 | const DirectoryLookup *CurDir; |
1997 | SmallString<1024> SearchPath; |
1998 | SmallString<1024> RelativePath; |
1999 | |
2000 | |
2001 | ModuleMap::KnownHeader SuggestedModule; |
2002 | SourceLocation FilenameLoc = FilenameTok.getLocation(); |
2003 | StringRef LookupFilename = Filename; |
2004 | |
2005 | #ifdef _WIN32 |
2006 | llvm::sys::path::Style BackslashStyle = llvm::sys::path::Style::windows; |
2007 | #else |
2008 | |
2009 | |
2010 | SmallString<128> NormalizedPath; |
2011 | llvm::sys::path::Style BackslashStyle = llvm::sys::path::Style::posix; |
2012 | if (LangOpts.MicrosoftExt) { |
2013 | NormalizedPath = Filename.str(); |
2014 | llvm::sys::path::native(NormalizedPath); |
2015 | LookupFilename = NormalizedPath; |
2016 | BackslashStyle = llvm::sys::path::Style::windows; |
2017 | } |
2018 | #endif |
2019 | |
2020 | Optional<FileEntryRef> File = LookupHeaderIncludeOrImport( |
2021 | CurDir, Filename, FilenameLoc, FilenameRange, FilenameTok, |
2022 | IsFrameworkFound, IsImportDecl, IsMapped, LookupFrom, LookupFromFile, |
2023 | LookupFilename, RelativePath, SearchPath, SuggestedModule, isAngled); |
2024 | |
2025 | |
2026 | if (File) |
2027 | CurLexer->addInclude(OriginalFilename, File->getFileEntry(), FilenameLoc); |
2028 | |
2029 | if (usingPCHWithThroughHeader() && SkippingUntilPCHThroughHeader) { |
2030 | if (File && isPCHThroughHeader(&File->getFileEntry())) |
2031 | SkippingUntilPCHThroughHeader = false; |
2032 | return {ImportAction::None}; |
2033 | } |
2034 | |
2035 | |
2036 | |
2037 | |
2038 | |
2039 | enum { Enter, Import, Skip, IncludeLimitReached } Action = Enter; |
2040 | |
2041 | if (PPOpts->SingleFileParseMode) |
2042 | Action = IncludeLimitReached; |
2043 | |
2044 | |
2045 | |
2046 | |
2047 | if (Action == Enter && HasReachedMaxIncludeDepth && File && |
2048 | HeaderInfo.getFileInfo(&File->getFileEntry()).NumIncludes) |
2049 | Action = IncludeLimitReached; |
2050 | |
2051 | |
2052 | |
2053 | |
2054 | if (Action == Enter && File && SuggestedModule && getLangOpts().Modules && |
2055 | !isForModuleBuilding(SuggestedModule.getModule(), |
2056 | getLangOpts().CurrentModule, |
2057 | getLangOpts().ModuleName)) { |
2058 | |
2059 | |
2060 | |
2061 | |
2062 | if (checkModuleIsAvailable(getLangOpts(), getTargetInfo(), getDiagnostics(), |
2063 | SuggestedModule.getModule())) { |
2064 | Diag(FilenameTok.getLocation(), |
2065 | diag::note_implicit_top_level_module_import_here) |
2066 | << SuggestedModule.getModule()->getTopLevelModuleName(); |
2067 | return {ImportAction::None}; |
2068 | } |
2069 | |
2070 | |
2071 | |
2072 | |
2073 | SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 2> Path; |
2074 | for (Module *Mod = SuggestedModule.getModule(); Mod; Mod = Mod->Parent) |
2075 | Path.push_back(std::make_pair(getIdentifierInfo(Mod->Name), |
2076 | FilenameTok.getLocation())); |
2077 | std::reverse(Path.begin(), Path.end()); |
2078 | |
2079 | |
2080 | if (!IsImportDecl) |
2081 | diagnoseAutoModuleImport(*this, StartLoc, IncludeTok, Path, CharEnd); |
2082 | |
2083 | |
2084 | |
2085 | |
2086 | |
2087 | ModuleLoadResult Imported = TheModuleLoader.loadModule( |
2088 | IncludeTok.getLocation(), Path, Module::Hidden, |
2089 | true); |
2090 | assert((Imported == nullptr || Imported == SuggestedModule.getModule()) && |
2091 | "the imported module is different than the suggested one"); |
2092 | |
2093 | if (Imported) { |
2094 | Action = Import; |
2095 | } else if (Imported.isMissingExpected()) { |
2096 | |
2097 | |
2098 | |
2099 | |
2100 | SuggestedModule = ModuleMap::KnownHeader(); |
2101 | } else if (Imported.isConfigMismatch()) { |
2102 | |
2103 | |
2104 | } else { |
2105 | |
2106 | if (hadModuleLoaderFatalFailure()) { |
2107 | |
2108 | Token &Result = IncludeTok; |
2109 | assert(CurLexer && "#include but no current lexer set!"); |
2110 | Result.startToken(); |
2111 | CurLexer->FormTokenWithChars(Result, CurLexer->BufferEnd, tok::eof); |
2112 | CurLexer->cutOffLexing(); |
2113 | } |
2114 | return {ImportAction::None}; |
2115 | } |
2116 | } |
2117 | |
2118 | |
2119 | |
2120 | |
2121 | SrcMgr::CharacteristicKind FileCharacter = |
2122 | SourceMgr.getFileCharacteristic(FilenameTok.getLocation()); |
2123 | if (File) |
2124 | FileCharacter = std::max(HeaderInfo.getFileDirFlavor(&File->getFileEntry()), |
2125 | FileCharacter); |
2126 | |
2127 | |
2128 | |
2129 | |
2130 | |
2131 | |
2132 | bool EnterOnce = |
2133 | IsImportDecl || |
2134 | IncludeTok.getIdentifierInfo()->getPPKeywordID() == tok::pp_import; |
2135 | |
2136 | |
2137 | |
2138 | if (Action == Enter && File && |
2139 | !HeaderInfo.ShouldEnterIncludeFile(*this, &File->getFileEntry(), |
2140 | EnterOnce, getLangOpts().Modules, |
2141 | SuggestedModule.getModule())) { |
2142 | |
2143 | |
2144 | |
2145 | |
2146 | |
2147 | |
2148 | |
2149 | Action = (SuggestedModule && !getLangOpts().CompilingPCH) ? Import : Skip; |
2150 | } |
2151 | |
2152 | |
2153 | |
2154 | |
2155 | |
2156 | |
2157 | if (Action == Enter && File && PreambleConditionalStack.isRecording() && |
2158 | SourceMgr.isMainFile(File->getFileEntry())) { |
2159 | Diag(FilenameTok.getLocation(), |
2160 | diag::err_pp_including_mainfile_in_preamble); |
2161 | return {ImportAction::None}; |
2162 | } |
2163 | |
2164 | if (Callbacks && !IsImportDecl) { |
2165 | |
2166 | |
2167 | Callbacks->InclusionDirective( |
2168 | HashLoc, IncludeTok, LookupFilename, isAngled, FilenameRange, |
2169 | File ? &File->getFileEntry() : nullptr, SearchPath, RelativePath, |
2170 | Action == Import ? SuggestedModule.getModule() : nullptr, |
2171 | FileCharacter); |
2172 | if (Action == Skip && File) |
2173 | Callbacks->FileSkipped(*File, FilenameTok, FileCharacter); |
2174 | } |
2175 | |
2176 | if (!File) |
2177 | return {ImportAction::None}; |
2178 | |
2179 | |
2180 | |
2181 | if (IsImportDecl && !SuggestedModule) { |
2182 | Diag(FilenameTok, diag::err_header_import_not_header_unit) |
2183 | << OriginalFilename << File->getName(); |
2184 | return {ImportAction::None}; |
2185 | } |
2186 | |
2187 | |
2188 | |
2189 | const bool CheckIncludePathPortability = |
2190 | !IsMapped && !File->getFileEntry().tryGetRealPathName().empty(); |
2191 | |
2192 | if (CheckIncludePathPortability) { |
2193 | StringRef Name = LookupFilename; |
2194 | StringRef NameWithoriginalSlashes = Filename; |
2195 | #if defined(_WIN32) |
2196 | |
2197 | |
2198 | bool NameWasUNC = Name.consume_front("\\\\?\\"); |
2199 | NameWithoriginalSlashes.consume_front("\\\\?\\"); |
2200 | #endif |
2201 | StringRef RealPathName = File->getFileEntry().tryGetRealPathName(); |
2202 | SmallVector<StringRef, 16> Components(llvm::sys::path::begin(Name), |
2203 | llvm::sys::path::end(Name)); |
2204 | #if defined(_WIN32) |
2205 | |
2206 | |
2207 | |
2208 | |
2209 | |
2210 | |
2211 | |
2212 | |
2213 | |
2214 | |
2215 | SmallString<128> FixedDriveRealPath; |
2216 | if (llvm::sys::path::is_absolute(Name) && |
2217 | llvm::sys::path::is_absolute(RealPathName) && |
2218 | toLowercase(Name[0]) == toLowercase(RealPathName[0]) && |
2219 | isLowercase(Name[0]) != isLowercase(RealPathName[0])) { |
2220 | assert(Components.size() >= 3 && "should have drive, backslash, name"); |
2221 | assert(Components[0].size() == 2 && "should start with drive"); |
2222 | assert(Components[0][1] == ':' && "should have colon"); |
2223 | FixedDriveRealPath = (Name.substr(0, 1) + RealPathName.substr(1)).str(); |
2224 | RealPathName = FixedDriveRealPath; |
2225 | } |
2226 | #endif |
2227 | |
2228 | if (trySimplifyPath(Components, RealPathName)) { |
2229 | SmallString<128> Path; |
2230 | Path.reserve(Name.size()+2); |
2231 | Path.push_back(isAngled ? '<' : '"'); |
2232 | |
2233 | const auto IsSep = [BackslashStyle](char c) { |
2234 | return llvm::sys::path::is_separator(c, BackslashStyle); |
2235 | }; |
2236 | |
2237 | for (auto Component : Components) { |
2238 | |
2239 | |
2240 | |
2241 | |
2242 | |
2243 | |
2244 | |
2245 | |
2246 | |
2247 | |
2248 | |
2249 | if (!(Component.size() == 1 && IsSep(Component[0]))) |
2250 | Path.append(Component); |
2251 | else if (!Path.empty()) |
2252 | continue; |
2253 | |
2254 | |
2255 | if (Path.size() > NameWithoriginalSlashes.size()) { |
2256 | Path.push_back(isAngled ? '>' : '"'); |
2257 | continue; |
2258 | } |
2259 | assert(IsSep(NameWithoriginalSlashes[Path.size()-1])); |
2260 | do |
2261 | Path.push_back(NameWithoriginalSlashes[Path.size()-1]); |
2262 | while (Path.size() <= NameWithoriginalSlashes.size() && |
2263 | IsSep(NameWithoriginalSlashes[Path.size()-1])); |
2264 | } |
2265 | |
2266 | #if defined(_WIN32) |
2267 | |
2268 | if (NameWasUNC) |
2269 | Path = (Path.substr(0, 1) + "\\\\?\\" + Path.substr(1)).str(); |
2270 | #endif |
2271 | |
2272 | |
2273 | |
2274 | auto DiagId = |
2275 | (FileCharacter == SrcMgr::C_User || warnByDefaultOnWrongCase(Name)) |
2276 | ? diag::pp_nonportable_path |
2277 | : diag::pp_nonportable_system_path; |
2278 | Diag(FilenameTok, DiagId) << Path << |
2279 | FixItHint::CreateReplacement(FilenameRange, Path); |
2280 | } |
2281 | } |
2282 | |
2283 | switch (Action) { |
2284 | case Skip: |
2285 | |
2286 | if (Module *M = SuggestedModule.getModule()) |
2287 | return {ImportAction::SkippedModuleImport, M}; |
2288 | return {ImportAction::None}; |
2289 | |
2290 | case IncludeLimitReached: |
2291 | |
2292 | |
2293 | return {ImportAction::None}; |
2294 | |
2295 | case Import: { |
2296 | |
2297 | Module *M = SuggestedModule.getModule(); |
2298 | assert(M && "no module to import"); |
2299 | |
2300 | makeModuleVisible(M, EndLoc); |
2301 | |
2302 | if (IncludeTok.getIdentifierInfo()->getPPKeywordID() == |
2303 | tok::pp___include_macros) |
2304 | return {ImportAction::None}; |
2305 | |
2306 | return {ImportAction::ModuleImport, M}; |
2307 | } |
2308 | |
2309 | case Enter: |
2310 | break; |
2311 | } |
2312 | |
2313 | |
2314 | if (IncludeMacroStack.size() == MaxAllowedIncludeStackDepth-1) { |
2315 | Diag(FilenameTok, diag::err_pp_include_too_deep); |
2316 | HasReachedMaxIncludeDepth = true; |
2317 | return {ImportAction::None}; |
2318 | } |
2319 | |
2320 | |
2321 | SourceLocation IncludePos = FilenameTok.getLocation(); |
2322 | |
2323 | |
2324 | if (IncludePos.isMacroID()) |
2325 | IncludePos = SourceMgr.getExpansionRange(IncludePos).getEnd(); |
2326 | FileID FID = SourceMgr.createFileID(*File, IncludePos, FileCharacter); |
2327 | if (!FID.isValid()) { |
2328 | TheModuleLoader.HadFatalFailure = true; |
2329 | return ImportAction::Failure; |
2330 | } |
2331 | |
2332 | |
2333 | if (EnterSourceFile(FID, CurDir, FilenameTok.getLocation())) |
2334 | return {ImportAction::None}; |
2335 | |
2336 | |
2337 | if (auto *M = SuggestedModule.getModule()) { |
2338 | if (M->getTopLevelModule()->ShadowingModule) { |
2339 | |
2340 | |
2341 | Diag(M->DefinitionLoc, diag::err_module_build_shadowed_submodule) |
2342 | << M->getFullModuleName(); |
2343 | Diag(M->getTopLevelModule()->ShadowingModule->DefinitionLoc, |
2344 | diag::note_previous_definition); |
2345 | return {ImportAction::None}; |
2346 | } |
2347 | |
2348 | |
2349 | |
2350 | |
2351 | |
2352 | |
2353 | |
2354 | |
2355 | if (getLangOpts().CompilingPCH && |
2356 | isForModuleBuilding(M, getLangOpts().CurrentModule, |
2357 | getLangOpts().ModuleName)) |
2358 | return {ImportAction::None}; |
2359 | |
2360 | assert(!CurLexerSubmodule && "should not have marked this as a module yet"); |
2361 | CurLexerSubmodule = M; |
2362 | |
2363 | |
2364 | |
2365 | EnterSubmodule(M, EndLoc, false); |
2366 | |
2367 | |
2368 | |
2369 | |
2370 | |
2371 | return {ImportAction::ModuleBegin, M}; |
2372 | } |
2373 | |
2374 | assert(!IsImportDecl && "failed to diagnose missing module for import decl"); |
2375 | return {ImportAction::None}; |
2376 | } |
2377 | |
2378 | |
2379 | |
2380 | void Preprocessor::HandleIncludeNextDirective(SourceLocation HashLoc, |
2381 | Token &IncludeNextTok) { |
2382 | Diag(IncludeNextTok, diag::ext_pp_include_next_directive); |
2383 | |
2384 | |
2385 | |
2386 | |
2387 | const DirectoryLookup *Lookup = CurDirLookup; |
2388 | const FileEntry *LookupFromFile = nullptr; |
2389 | if (isInPrimaryFile() && LangOpts.IsHeaderFile) { |
2390 | |
2391 | |
2392 | |
2393 | } else if (isInPrimaryFile()) { |
2394 | Lookup = nullptr; |
2395 | Diag(IncludeNextTok, diag::pp_include_next_in_primary); |
2396 | } else if (CurLexerSubmodule) { |
2397 | |
2398 | |
2399 | assert(CurPPLexer && "#include_next directive in macro?"); |
2400 | LookupFromFile = CurPPLexer->getFileEntry(); |
2401 | Lookup = nullptr; |
2402 | } else if (!Lookup) { |
2403 | |
2404 | |
2405 | |
2406 | |
2407 | Diag(IncludeNextTok, diag::pp_include_next_absolute_path); |
2408 | } else { |
2409 | |
2410 | ++Lookup; |
2411 | } |
2412 | |
2413 | return HandleIncludeDirective(HashLoc, IncludeNextTok, Lookup, |
2414 | LookupFromFile); |
2415 | } |
2416 | |
2417 | |
2418 | void Preprocessor::HandleMicrosoftImportDirective(Token &Tok) { |
2419 | |
2420 | |
2421 | |
2422 | |
2423 | |
2424 | Diag(Tok, diag::err_pp_import_directive_ms ); |
2425 | |
2426 | |
2427 | |
2428 | DiscardUntilEndOfDirective(); |
2429 | } |
2430 | |
2431 | |
2432 | |
2433 | void Preprocessor::HandleImportDirective(SourceLocation HashLoc, |
2434 | Token &ImportTok) { |
2435 | if (!LangOpts.ObjC) { |
2436 | if (LangOpts.MSVCCompat) |
2437 | return HandleMicrosoftImportDirective(ImportTok); |
2438 | Diag(ImportTok, diag::ext_pp_import_directive); |
2439 | } |
2440 | return HandleIncludeDirective(HashLoc, ImportTok); |
2441 | } |
2442 | |
2443 | |
2444 | |
2445 | |
2446 | |
2447 | void Preprocessor::HandleIncludeMacrosDirective(SourceLocation HashLoc, |
2448 | Token &IncludeMacrosTok) { |
2449 | |
2450 | |
2451 | SourceLocation Loc = IncludeMacrosTok.getLocation(); |
2452 | if (SourceMgr.getBufferName(Loc) != "<built-in>") { |
2453 | Diag(IncludeMacrosTok.getLocation(), |
2454 | diag::pp_include_macros_out_of_predefines); |
2455 | DiscardUntilEndOfDirective(); |
2456 | return; |
2457 | } |
2458 | |
2459 | |
2460 | |
2461 | HandleIncludeDirective(HashLoc, IncludeMacrosTok); |
2462 | |
2463 | Token TmpTok; |
2464 | do { |
2465 | Lex(TmpTok); |
2466 | assert(TmpTok.isNot(tok::eof) && "Didn't find end of -imacros!"); |
2467 | } while (TmpTok.isNot(tok::hashhash)); |
2468 | } |
2469 | |
2470 | |
2471 | |
2472 | |
2473 | |
2474 | |
2475 | |
2476 | |
2477 | |
2478 | bool Preprocessor::ReadMacroParameterList(MacroInfo *MI, Token &Tok) { |
2479 | SmallVector<IdentifierInfo*, 32> Parameters; |
2480 | |
2481 | while (true) { |
2482 | LexUnexpandedToken(Tok); |
2483 | switch (Tok.getKind()) { |
2484 | case tok::r_paren: |
2485 | |
2486 | if (Parameters.empty()) |
2487 | return false; |
2488 | |
2489 | Diag(Tok, diag::err_pp_expected_ident_in_arg_list); |
2490 | return true; |
2491 | case tok::ellipsis: |
2492 | if (!LangOpts.C99) |
2493 | Diag(Tok, LangOpts.CPlusPlus11 ? |
2494 | diag::warn_cxx98_compat_variadic_macro : |
2495 | diag::ext_variadic_macro); |
2496 | |
2497 | |
2498 | if (LangOpts.OpenCL && !LangOpts.OpenCLCPlusPlus) { |
2499 | Diag(Tok, diag::ext_pp_opencl_variadic_macros); |
2500 | } |
2501 | |
2502 | |
2503 | LexUnexpandedToken(Tok); |
2504 | if (Tok.isNot(tok::r_paren)) { |
2505 | Diag(Tok, diag::err_pp_missing_rparen_in_macro_def); |
2506 | return true; |
2507 | } |
2508 | |
2509 | Parameters.push_back(Ident__VA_ARGS__); |
2510 | MI->setIsC99Varargs(); |
2511 | MI->setParameterList(Parameters, BP); |
2512 | return false; |
2513 | case tok::eod: |
2514 | Diag(Tok, diag::err_pp_missing_rparen_in_macro_def); |
2515 | return true; |
2516 | default: |
2517 | |
2518 | |
2519 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
2520 | if (!II) { |
2521 | |
2522 | Diag(Tok, diag::err_pp_invalid_tok_in_arg_list); |
2523 | return true; |
2524 | } |
2525 | |
2526 | |
2527 | |
2528 | if (llvm::find(Parameters, II) != Parameters.end()) { |
2529 | Diag(Tok, diag::err_pp_duplicate_name_in_arg_list) << II; |
2530 | return true; |
2531 | } |
2532 | |
2533 | |
2534 | Parameters.push_back(II); |
2535 | |
2536 | |
2537 | LexUnexpandedToken(Tok); |
2538 | |
2539 | switch (Tok.getKind()) { |
2540 | default: |
2541 | Diag(Tok, diag::err_pp_expected_comma_in_arg_list); |
2542 | return true; |
2543 | case tok::r_paren: |
2544 | MI->setParameterList(Parameters, BP); |
2545 | return false; |
2546 | case tok::comma: |
2547 | break; |
2548 | case tok::ellipsis: |
2549 | |
2550 | Diag(Tok, diag::ext_named_variadic_macro); |
2551 | |
2552 | |
2553 | LexUnexpandedToken(Tok); |
2554 | if (Tok.isNot(tok::r_paren)) { |
2555 | Diag(Tok, diag::err_pp_missing_rparen_in_macro_def); |
2556 | return true; |
2557 | } |
2558 | |
2559 | MI->setIsGNUVarargs(); |
2560 | MI->setParameterList(Parameters, BP); |
2561 | return false; |
2562 | } |
2563 | } |
2564 | } |
2565 | } |
2566 | |
2567 | static bool isConfigurationPattern(Token &MacroName, MacroInfo *MI, |
2568 | const LangOptions &LOptions) { |
2569 | if (MI->getNumTokens() == 1) { |
2570 | const Token &Value = MI->getReplacementToken(0); |
2571 | |
2572 | |
2573 | if (MacroName.getKind() == Value.getKind()) |
2574 | return true; |
2575 | |
2576 | |
2577 | |
2578 | |
2579 | |
2580 | |
2581 | StringRef MacroText = MacroName.getIdentifierInfo()->getName(); |
2582 | if (IdentifierInfo *II = Value.getIdentifierInfo()) { |
2583 | if (!II->isKeyword(LOptions)) |
2584 | return false; |
2585 | StringRef ValueText = II->getName(); |
2586 | StringRef TrimmedValue = ValueText; |
2587 | if (!ValueText.startswith("__")) { |
2588 | if (ValueText.startswith("_")) |
2589 | TrimmedValue = TrimmedValue.drop_front(1); |
2590 | else |
2591 | return false; |
2592 | } else { |
2593 | TrimmedValue = TrimmedValue.drop_front(2); |
2594 | if (TrimmedValue.endswith("__")) |
2595 | TrimmedValue = TrimmedValue.drop_back(2); |
2596 | } |
2597 | return TrimmedValue.equals(MacroText); |
2598 | } else { |
2599 | return false; |
2600 | } |
2601 | } |
2602 | |
2603 | |
2604 | return MacroName.isOneOf(tok::kw_extern, tok::kw_inline, tok::kw_static, |
2605 | tok::kw_const) && |
2606 | MI->getNumTokens() == 0; |
2607 | } |
2608 | |
2609 | |
2610 | |
2611 | |
2612 | |
2613 | |
2614 | |
2615 | |
2616 | |
2617 | MacroInfo *Preprocessor::ReadOptionalMacroParameterListAndBody( |
2618 | const Token &MacroNameTok, const bool ImmediatelyAfterHeaderGuard) { |
2619 | |
2620 | Token LastTok = MacroNameTok; |
2621 | |
2622 | MacroInfo *const MI = AllocateMacroInfo(MacroNameTok.getLocation()); |
| 16 | | Calling 'Preprocessor::AllocateMacroInfo' | |
|
2623 | |
2624 | Token Tok; |
2625 | LexUnexpandedToken(Tok); |
2626 | |
2627 | |
2628 | auto _ = llvm::make_scope_exit([&]() { |
2629 | |
2630 | if (CurLexer->ParsingPreprocessorDirective) |
2631 | DiscardUntilEndOfDirective(); |
2632 | }); |
2633 | |
2634 | |
2635 | |
2636 | VariadicMacroScopeGuard VariadicMacroScopeGuard(*this); |
2637 | |
2638 | |
2639 | |
2640 | |
2641 | if (Tok.is(tok::eod)) { |
2642 | if (ImmediatelyAfterHeaderGuard) { |
2643 | |
2644 | CurPPLexer->MIOpt.SetDefinedMacro(MacroNameTok.getIdentifierInfo(), |
2645 | MacroNameTok.getLocation()); |
2646 | } |
2647 | |
2648 | } else if (Tok.hasLeadingSpace()) { |
2649 | |
2650 | |
2651 | Tok.clearFlag(Token::LeadingSpace); |
2652 | } else if (Tok.is(tok::l_paren)) { |
2653 | |
2654 | MI->setIsFunctionLike(); |
2655 | if (ReadMacroParameterList(MI, LastTok)) |
2656 | return nullptr; |
2657 | |
2658 | |
2659 | |
2660 | |
2661 | |
2662 | |
2663 | if (MI->isC99Varargs()) { |
2664 | VariadicMacroScopeGuard.enterScope(); |
2665 | } |
2666 | |
2667 | |
2668 | LexUnexpandedToken(Tok); |
2669 | } else if (LangOpts.C99 || LangOpts.CPlusPlus11) { |
2670 | |
2671 | |
2672 | Diag(Tok, diag::ext_c99_whitespace_required_after_macro_name); |
2673 | } else { |
2674 | |
2675 | |
2676 | |
2677 | |
2678 | |
2679 | |
2680 | bool isInvalid = false; |
2681 | if (Tok.is(tok::at)) |
2682 | isInvalid = true; |
2683 | else if (Tok.is(tok::unknown)) { |
2684 | |
2685 | |
2686 | |
2687 | isInvalid = true; |
2688 | } |
2689 | if (isInvalid) |
2690 | Diag(Tok, diag::ext_missing_whitespace_after_macro_name); |
2691 | else |
2692 | Diag(Tok, diag::warn_missing_whitespace_after_macro_name); |
2693 | } |
2694 | |
2695 | if (!Tok.is(tok::eod)) |
2696 | LastTok = Tok; |
2697 | |
2698 | |
2699 | if (MI->isObjectLike()) { |
2700 | |
2701 | while (Tok.isNot(tok::eod)) { |
2702 | LastTok = Tok; |
2703 | MI->AddTokenToBody(Tok); |
2704 | |
2705 | LexUnexpandedToken(Tok); |
2706 | } |
2707 | } else { |
2708 | |
2709 | |
2710 | |
2711 | |
2712 | VAOptDefinitionContext VAOCtx(*this); |
2713 | |
2714 | while (Tok.isNot(tok::eod)) { |
2715 | LastTok = Tok; |
2716 | |
2717 | if (!Tok.isOneOf(tok::hash, tok::hashat, tok::hashhash)) { |
2718 | MI->AddTokenToBody(Tok); |
2719 | |
2720 | if (VAOCtx.isVAOptToken(Tok)) { |
2721 | |
2722 | if (VAOCtx.isInVAOpt()) { |
2723 | Diag(Tok, diag::err_pp_vaopt_nested_use); |
2724 | return nullptr; |
2725 | } |
2726 | |
2727 | LexUnexpandedToken(Tok); |
2728 | if (Tok.isNot(tok::l_paren)) { |
2729 | Diag(Tok, diag::err_pp_missing_lparen_in_vaopt_use); |
2730 | return nullptr; |
2731 | } |
2732 | MI->AddTokenToBody(Tok); |
2733 | VAOCtx.sawVAOptFollowedByOpeningParens(Tok.getLocation()); |
2734 | LexUnexpandedToken(Tok); |
2735 | if (Tok.is(tok::hashhash)) { |
2736 | Diag(Tok, diag::err_vaopt_paste_at_start); |
2737 | return nullptr; |
2738 | } |
2739 | continue; |
2740 | } else if (VAOCtx.isInVAOpt()) { |
2741 | if (Tok.is(tok::r_paren)) { |
2742 | if (VAOCtx.sawClosingParen()) { |
2743 | const unsigned NumTokens = MI->getNumTokens(); |
2744 | assert(NumTokens >= 3 && "Must have seen at least __VA_OPT__( " |
2745 | "and a subsequent tok::r_paren"); |
2746 | if (MI->getReplacementToken(NumTokens - 2).is(tok::hashhash)) { |
2747 | Diag(Tok, diag::err_vaopt_paste_at_end); |
2748 | return nullptr; |
2749 | } |
2750 | } |
2751 | } else if (Tok.is(tok::l_paren)) { |
2752 | VAOCtx.sawOpeningParen(Tok.getLocation()); |
2753 | } |
2754 | } |
2755 | |
2756 | LexUnexpandedToken(Tok); |
2757 | continue; |
2758 | } |
2759 | |
2760 | |
2761 | |
2762 | |
2763 | if (getLangOpts().TraditionalCPP) { |
2764 | Tok.setKind(tok::unknown); |
2765 | MI->AddTokenToBody(Tok); |
2766 | |
2767 | |
2768 | LexUnexpandedToken(Tok); |
2769 | continue; |
2770 | } |
2771 | |
2772 | if (Tok.is(tok::hashhash)) { |
2773 | |
2774 | |
2775 | |
2776 | |
2777 | |
2778 | LexUnexpandedToken(Tok); |
2779 | |
2780 | if (Tok.is(tok::eod)) { |
2781 | MI->AddTokenToBody(LastTok); |
2782 | break; |
2783 | } |
2784 | |
2785 | unsigned NumTokens = MI->getNumTokens(); |
2786 | if (NumTokens && Tok.getIdentifierInfo() == Ident__VA_ARGS__ && |
2787 | MI->getReplacementToken(NumTokens-1).is(tok::comma)) |
2788 | MI->setHasCommaPasting(); |
2789 | |
2790 | |
2791 | MI->AddTokenToBody(LastTok); |
2792 | continue; |
2793 | } |
2794 | |
2795 | |
2796 | |
2797 | LexUnexpandedToken(Tok); |
2798 | |
2799 | |
2800 | if (!VAOCtx.isVAOptToken(Tok) && |
2801 | (Tok.getIdentifierInfo() == nullptr || |
2802 | MI->getParameterNum(Tok.getIdentifierInfo()) == -1)) { |
2803 | |
2804 | |
2805 | |
2806 | |
2807 | |
2808 | if (getLangOpts().AsmPreprocessor && Tok.isNot(tok::eod)) { |
2809 | LastTok.setKind(tok::unknown); |
2810 | MI->AddTokenToBody(LastTok); |
2811 | continue; |
2812 | } else { |
2813 | Diag(Tok, diag::err_pp_stringize_not_parameter) |
2814 | << LastTok.is(tok::hashat); |
2815 | return nullptr; |
2816 | } |
2817 | } |
2818 | |
2819 | |
2820 | MI->AddTokenToBody(LastTok); |
2821 | |
2822 | |
2823 | |
2824 | |
2825 | if (!VAOCtx.isVAOptToken(Tok)) { |
2826 | MI->AddTokenToBody(Tok); |
2827 | LastTok = Tok; |
2828 | |
2829 | |
2830 | LexUnexpandedToken(Tok); |
2831 | } |
2832 | } |
2833 | if (VAOCtx.isInVAOpt()) { |
2834 | assert(Tok.is(tok::eod) && "Must be at End Of preprocessing Directive"); |
2835 | Diag(Tok, diag::err_pp_expected_after) |
2836 | << LastTok.getKind() << tok::r_paren; |
2837 | Diag(VAOCtx.getUnmatchedOpeningParenLoc(), diag::note_matching) << tok::l_paren; |
2838 | return nullptr; |
2839 | } |
2840 | } |
2841 | MI->setDefinitionEndLoc(LastTok.getLocation()); |
2842 | return MI; |
2843 | } |
2844 | |
2845 | |
2846 | void Preprocessor::HandleDefineDirective( |
2847 | Token &DefineTok, const bool ImmediatelyAfterHeaderGuard) { |
2848 | ++NumDefined; |
2849 | |
2850 | Token MacroNameTok; |
2851 | bool MacroShadowsKeyword; |
2852 | ReadMacroName(MacroNameTok, MU_Define, &MacroShadowsKeyword); |
2853 | |
2854 | |
2855 | if (MacroNameTok.is(tok::eod)) |
| |
2856 | return; |
2857 | |
2858 | |
2859 | |
2860 | if (CurLexer) CurLexer->SetCommentRetentionState(KeepMacroComments); |
| |
2861 | |
2862 | MacroInfo *const MI = ReadOptionalMacroParameterListAndBody( |
| 15 | | Calling 'Preprocessor::ReadOptionalMacroParameterListAndBody' | |
|
2863 | MacroNameTok, ImmediatelyAfterHeaderGuard); |
2864 | |
2865 | if (!MI) return; |
2866 | |
2867 | if (MacroShadowsKeyword && |
2868 | !isConfigurationPattern(MacroNameTok, MI, getLangOpts())) { |
2869 | Diag(MacroNameTok, diag::warn_pp_macro_hides_keyword); |
2870 | } |
2871 | |
2872 | |
2873 | unsigned NumTokens = MI->getNumTokens(); |
2874 | if (NumTokens != 0) { |
2875 | if (MI->getReplacementToken(0).is(tok::hashhash)) { |
2876 | Diag(MI->getReplacementToken(0), diag::err_paste_at_start); |
2877 | return; |
2878 | } |
2879 | if (MI->getReplacementToken(NumTokens-1).is(tok::hashhash)) { |
2880 | Diag(MI->getReplacementToken(NumTokens-1), diag::err_paste_at_end); |
2881 | return; |
2882 | } |
2883 | } |
2884 | |
2885 | |
2886 | if (SkippingUntilPCHThroughHeader) { |
2887 | const MacroInfo *OtherMI = getMacroInfo(MacroNameTok.getIdentifierInfo()); |
2888 | if (!OtherMI || !MI->isIdenticalTo(*OtherMI, *this, |
2889 | LangOpts.MicrosoftExt)) |
2890 | Diag(MI->getDefinitionLoc(), diag::warn_pp_macro_def_mismatch_with_pch) |
2891 | << MacroNameTok.getIdentifierInfo(); |
2892 | |
2893 | if (!LangOpts.MicrosoftExt) |
2894 | return; |
2895 | } |
2896 | |
2897 | |
2898 | |
2899 | if (const MacroInfo *OtherMI=getMacroInfo(MacroNameTok.getIdentifierInfo())) { |
2900 | |
2901 | |
2902 | |
2903 | auto isObjCProtectedMacro = [](const IdentifierInfo *II) -> bool { |
2904 | return II->isStr("__strong") || |
2905 | II->isStr("__weak") || |
2906 | II->isStr("__unsafe_unretained") || |
2907 | II->isStr("__autoreleasing"); |
2908 | }; |
2909 | if (getLangOpts().ObjC && |
2910 | SourceMgr.getFileID(OtherMI->getDefinitionLoc()) |
2911 | == getPredefinesFileID() && |
2912 | isObjCProtectedMacro(MacroNameTok.getIdentifierInfo())) { |
2913 | |
2914 | if ((!getDiagnostics().getSuppressSystemWarnings() || |
2915 | !SourceMgr.isInSystemHeader(DefineTok.getLocation())) && |
2916 | !MI->isIdenticalTo(*OtherMI, *this, |
2917 | LangOpts.MicrosoftExt)) { |
2918 | Diag(MI->getDefinitionLoc(), diag::warn_pp_objc_macro_redef_ignored); |
2919 | } |
2920 | assert(!OtherMI->isWarnIfUnused()); |
2921 | return; |
2922 | } |
2923 | |
2924 | |
2925 | |
2926 | |
2927 | if (!getDiagnostics().getSuppressSystemWarnings() || |
2928 | !SourceMgr.isInSystemHeader(DefineTok.getLocation())) { |
2929 | if (!OtherMI->isUsed() && OtherMI->isWarnIfUnused()) |
2930 | Diag(OtherMI->getDefinitionLoc(), diag::pp_macro_not_used); |
2931 | |
2932 | |
2933 | |
2934 | if (OtherMI->isBuiltinMacro()) |
2935 | Diag(MacroNameTok, diag::ext_pp_redef_builtin_macro); |
2936 | |
2937 | |
2938 | else if (!OtherMI->isAllowRedefinitionsWithoutWarning() && |
2939 | !MI->isIdenticalTo(*OtherMI, *this, LangOpts.MicrosoftExt)) { |
2940 | Diag(MI->getDefinitionLoc(), diag::ext_pp_macro_redef) |
2941 | << MacroNameTok.getIdentifierInfo(); |
2942 | Diag(OtherMI->getDefinitionLoc(), diag::note_previous_definition); |
2943 | } |
2944 | } |
2945 | if (OtherMI->isWarnIfUnused()) |
2946 | WarnUnusedMacroLocs.erase(OtherMI->getDefinitionLoc()); |
2947 | } |
2948 | |
2949 | DefMacroDirective *MD = |
2950 | appendDefMacroDirective(MacroNameTok.getIdentifierInfo(), MI); |
2951 | |
2952 | assert(!MI->isUsed()); |
2953 | |
2954 | |
2955 | if (getSourceManager().isInMainFile(MI->getDefinitionLoc()) && |
2956 | !Diags->isIgnored(diag::pp_macro_not_used, MI->getDefinitionLoc()) && |
2957 | !MacroExpansionInDirectivesOverride && |
2958 | getSourceManager().getFileID(MI->getDefinitionLoc()) != |
2959 | getPredefinesFileID()) { |
2960 | MI->setIsWarnIfUnused(true); |
2961 | WarnUnusedMacroLocs.insert(MI->getDefinitionLoc()); |
2962 | } |
2963 | |
2964 | |
2965 | if (Callbacks) |
2966 | Callbacks->MacroDefined(MacroNameTok, MD); |
2967 | |
2968 | |
2969 | |
2970 | |
2971 | |
2972 | if (!getLangOpts().CPlusPlus && getLangOpts().MSVCCompat && |
2973 | MacroNameTok.getIdentifierInfo()->isStr("assert") && |
2974 | !isMacroDefined("static_assert")) { |
2975 | MacroInfo *MI = AllocateMacroInfo(SourceLocation()); |
2976 | |
2977 | Token Tok; |
2978 | Tok.startToken(); |
2979 | Tok.setKind(tok::kw__Static_assert); |
2980 | Tok.setIdentifierInfo(getIdentifierInfo("_Static_assert")); |
2981 | MI->AddTokenToBody(Tok); |
2982 | (void)appendDefMacroDirective(getIdentifierInfo("static_assert"), MI); |
2983 | } |
2984 | } |
2985 | |
2986 | |
2987 | |
2988 | void Preprocessor::HandleUndefDirective() { |
2989 | ++NumUndefined; |
2990 | |
2991 | Token MacroNameTok; |
2992 | ReadMacroName(MacroNameTok, MU_Undef); |
2993 | |
2994 | |
2995 | if (MacroNameTok.is(tok::eod)) |
2996 | return; |
2997 | |
2998 | |
2999 | CheckEndOfDirective("undef"); |
3000 | |
3001 | |
3002 | auto *II = MacroNameTok.getIdentifierInfo(); |
3003 | auto MD = getMacroDefinition(II); |
3004 | UndefMacroDirective *Undef = nullptr; |
3005 | |
3006 | |
3007 | if (const MacroInfo *MI = MD.getMacroInfo()) { |
3008 | if (!MI->isUsed() && MI->isWarnIfUnused()) |
3009 | Diag(MI->getDefinitionLoc(), diag::pp_macro_not_used); |
3010 | |
3011 | if (MI->isWarnIfUnused()) |
3012 | WarnUnusedMacroLocs.erase(MI->getDefinitionLoc()); |
3013 | |
3014 | Undef = AllocateUndefMacroDirective(MacroNameTok.getLocation()); |
3015 | } |
3016 | |
3017 | |
3018 | |
3019 | if (Callbacks) |
3020 | Callbacks->MacroUndefined(MacroNameTok, MD, Undef); |
3021 | |
3022 | if (Undef) |
3023 | appendMacroDirective(II, Undef); |
3024 | } |
3025 | |
3026 | |
3027 | |
3028 | |
3029 | |
3030 | |
3031 | |
3032 | |
3033 | |
3034 | |
3035 | void Preprocessor::HandleIfdefDirective(Token &Result, |
3036 | const Token &HashToken, |
3037 | bool isIfndef, |
3038 | bool ReadAnyTokensBeforeDirective) { |
3039 | ++NumIf; |
3040 | Token DirectiveTok = Result; |
3041 | |
3042 | Token MacroNameTok; |
3043 | ReadMacroName(MacroNameTok); |
3044 | |
3045 | |
3046 | if (MacroNameTok.is(tok::eod)) { |
3047 | |
3048 | |
3049 | SkipExcludedConditionalBlock(HashToken.getLocation(), |
3050 | DirectiveTok.getLocation(), |
3051 | false, false); |
3052 | return; |
3053 | } |
3054 | |
3055 | |
3056 | CheckEndOfDirective(isIfndef ? "ifndef" : "ifdef"); |
3057 | |
3058 | IdentifierInfo *MII = MacroNameTok.getIdentifierInfo(); |
3059 | auto MD = getMacroDefinition(MII); |
3060 | MacroInfo *MI = MD.getMacroInfo(); |
3061 | |
3062 | if (CurPPLexer->getConditionalStackDepth() == 0) { |
3063 | |
3064 | |
3065 | |
3066 | |
3067 | if (!ReadAnyTokensBeforeDirective && !MI) { |
3068 | assert(isIfndef && "#ifdef shouldn't reach here"); |
3069 | CurPPLexer->MIOpt.EnterTopLevelIfndef(MII, MacroNameTok.getLocation()); |
3070 | } else |
3071 | CurPPLexer->MIOpt.EnterTopLevelConditional(); |
3072 | } |
3073 | |
3074 | |
3075 | if (MI) |
3076 | markMacroAsUsed(MI); |
3077 | |
3078 | if (Callbacks) { |
3079 | if (isIfndef) |
3080 | Callbacks->Ifndef(DirectiveTok.getLocation(), MacroNameTok, MD); |
3081 | else |
3082 | Callbacks->Ifdef(DirectiveTok.getLocation(), MacroNameTok, MD); |
3083 | } |
3084 | |
3085 | bool RetainExcludedCB = PPOpts->RetainExcludedConditionalBlocks && |
3086 | getSourceManager().isInMainFile(DirectiveTok.getLocation()); |
3087 | |
3088 | |
3089 | if (PPOpts->SingleFileParseMode && !MI) { |
3090 | |
3091 | |
3092 | CurPPLexer->pushConditionalLevel(DirectiveTok.getLocation(), |
3093 | false, false, |
3094 | false); |
3095 | } else if (!MI == isIfndef || RetainExcludedCB) { |
3096 | |
3097 | CurPPLexer->pushConditionalLevel(DirectiveTok.getLocation(), |
3098 | false, true, |
3099 | false); |
3100 | } else { |
3101 | |
3102 | SkipExcludedConditionalBlock(HashToken.getLocation(), |
3103 | DirectiveTok.getLocation(), |
3104 | false, |
3105 | false); |
3106 | } |
3107 | } |
3108 | |
3109 | |
3110 | |
3111 | void Preprocessor::HandleIfDirective(Token &IfToken, |
3112 | const Token &HashToken, |
3113 | bool ReadAnyTokensBeforeDirective) { |
3114 | ++NumIf; |
3115 | |
3116 | |
3117 | IdentifierInfo *IfNDefMacro = nullptr; |
3118 | const DirectiveEvalResult DER = EvaluateDirectiveExpression(IfNDefMacro); |
3119 | const bool ConditionalTrue = DER.Conditional; |
3120 | |
3121 | |
3122 | if (!CurPPLexer) |
3123 | return; |
3124 | |
3125 | |
3126 | |
3127 | if (CurPPLexer->getConditionalStackDepth() == 0) { |
3128 | if (!ReadAnyTokensBeforeDirective && IfNDefMacro && ConditionalTrue) |
3129 | |
3130 | CurPPLexer->MIOpt.EnterTopLevelIfndef(IfNDefMacro, IfToken.getLocation()); |
3131 | else |
3132 | CurPPLexer->MIOpt.EnterTopLevelConditional(); |
3133 | } |
3134 | |
3135 | if (Callbacks) |
3136 | Callbacks->If( |
3137 | IfToken.getLocation(), DER.ExprRange, |
3138 | (ConditionalTrue ? PPCallbacks::CVK_True : PPCallbacks::CVK_False)); |
3139 | |
3140 | bool RetainExcludedCB = PPOpts->RetainExcludedConditionalBlocks && |
3141 | getSourceManager().isInMainFile(IfToken.getLocation()); |
3142 | |
3143 | |
3144 | if (PPOpts->SingleFileParseMode && DER.IncludedUndefinedIds) { |
3145 | |
3146 | |
3147 | CurPPLexer->pushConditionalLevel(IfToken.getLocation(), false, |
3148 | false, false); |
3149 | } else if (ConditionalTrue || RetainExcludedCB) { |
3150 | |
3151 | CurPPLexer->pushConditionalLevel(IfToken.getLocation(), false, |
3152 | true, false); |
3153 | } else { |
3154 | |
3155 | SkipExcludedConditionalBlock(HashToken.getLocation(), IfToken.getLocation(), |
3156 | false, |
3157 | false); |
3158 | } |
3159 | } |
3160 | |
3161 | |
3162 | |
3163 | void Preprocessor::HandleEndifDirective(Token &EndifToken) { |
3164 | ++NumEndif; |
3165 | |
3166 | |
3167 | CheckEndOfDirective("endif"); |
3168 | |
3169 | PPConditionalInfo CondInfo; |
3170 | if (CurPPLexer->popConditionalLevel(CondInfo)) { |
3171 | |
3172 | Diag(EndifToken, diag::err_pp_endif_without_if); |
3173 | return; |
3174 | } |
3175 | |
3176 | |
3177 | if (CurPPLexer->getConditionalStackDepth() == 0) |
3178 | CurPPLexer->MIOpt.ExitTopLevelConditional(); |
3179 | |
3180 | assert(!CondInfo.WasSkipping && !CurPPLexer->LexingRawMode && |
3181 | "This code should only be reachable in the non-skipping case!"); |
3182 | |
3183 | if (Callbacks) |
3184 | Callbacks->Endif(EndifToken.getLocation(), CondInfo.IfLoc); |
3185 | } |
3186 | |
3187 | |
3188 | |
3189 | void Preprocessor::HandleElseDirective(Token &Result, const Token &HashToken) { |
3190 | ++NumElse; |
3191 | |
3192 | |
3193 | CheckEndOfDirective("else"); |
3194 | |
3195 | PPConditionalInfo CI; |
3196 | if (CurPPLexer->popConditionalLevel(CI)) { |
3197 | Diag(Result, diag::pp_err_else_without_if); |
3198 | return; |
3199 | } |
3200 | |
3201 | |
3202 | if (CurPPLexer->getConditionalStackDepth() == 0) |
3203 | CurPPLexer->MIOpt.EnterTopLevelConditional(); |
3204 | |
3205 | |
3206 | if (CI.FoundElse) Diag(Result, diag::pp_err_else_after_else); |
3207 | |
3208 | if (Callbacks) |
3209 | Callbacks->Else(Result.getLocation(), CI.IfLoc); |
3210 | |
3211 | bool RetainExcludedCB = PPOpts->RetainExcludedConditionalBlocks && |
3212 | getSourceManager().isInMainFile(Result.getLocation()); |
3213 | |
3214 | if ((PPOpts->SingleFileParseMode && !CI.FoundNonSkip) || RetainExcludedCB) { |
3215 | |
3216 | |
3217 | CurPPLexer->pushConditionalLevel(CI.IfLoc, false, |
3218 | false, true); |
3219 | return; |
3220 | } |
3221 | |
3222 | |
3223 | SkipExcludedConditionalBlock(HashToken.getLocation(), CI.IfLoc, |
3224 | true, |
3225 | true, Result.getLocation()); |
3226 | } |
3227 | |
3228 | |
3229 | void Preprocessor::HandleElifFamilyDirective(Token &ElifToken, |
3230 | const Token &HashToken, |
3231 | tok::PPKeywordKind Kind) { |
3232 | PPElifDiag DirKind = Kind == tok::pp_elif ? PED_Elif |
3233 | : Kind == tok::pp_elifdef ? PED_Elifdef |
3234 | : PED_Elifndef; |
3235 | ++NumElse; |
3236 | |
3237 | |
3238 | |
3239 | |
3240 | SourceRange ConditionRange = DiscardUntilEndOfDirective(); |
3241 | |
3242 | PPConditionalInfo CI; |
3243 | if (CurPPLexer->popConditionalLevel(CI)) { |
3244 | Diag(ElifToken, diag::pp_err_elif_without_if) << DirKind; |
3245 | return; |
3246 | } |
3247 | |
3248 | |
3249 | if (CurPPLexer->getConditionalStackDepth() == 0) |
3250 | CurPPLexer->MIOpt.EnterTopLevelConditional(); |
3251 | |
3252 | |
3253 | if (CI.FoundElse) |
3254 | Diag(ElifToken, diag::pp_err_elif_after_else) << DirKind; |
3255 | |
3256 | if (Callbacks) { |
3257 | switch (Kind) { |
3258 | case tok::pp_elif: |
3259 | Callbacks->Elif(ElifToken.getLocation(), ConditionRange, |
3260 | PPCallbacks::CVK_NotEvaluated, CI.IfLoc); |
3261 | break; |
3262 | case tok::pp_elifdef: |
3263 | Callbacks->Elifdef(ElifToken.getLocation(), ConditionRange, CI.IfLoc); |
3264 | break; |
3265 | case tok::pp_elifndef: |
3266 | Callbacks->Elifndef(ElifToken.getLocation(), ConditionRange, CI.IfLoc); |
3267 | break; |
3268 | default: |
3269 | assert(false && "unexpected directive kind"); |
3270 | break; |
3271 | } |
3272 | } |
3273 | |
3274 | bool RetainExcludedCB = PPOpts->RetainExcludedConditionalBlocks && |
3275 | getSourceManager().isInMainFile(ElifToken.getLocation()); |
3276 | |
3277 | if ((PPOpts->SingleFileParseMode && !CI.FoundNonSkip) || RetainExcludedCB) { |
3278 | |
3279 | |
3280 | CurPPLexer->pushConditionalLevel(ElifToken.getLocation(), false, |
3281 | false, false); |
3282 | return; |
3283 | } |
3284 | |
3285 | |
3286 | SkipExcludedConditionalBlock( |
3287 | HashToken.getLocation(), CI.IfLoc, true, |
3288 | CI.FoundElse, ElifToken.getLocation()); |
3289 | } |