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