1 //===--- PPDirectives.cpp - Directive Handling for Preprocessor -----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// Implements # directive processing for the Preprocessor.
13 //===----------------------------------------------------------------------===//
15 #include "clang/Basic/CharInfo.h"
16 #include "clang/Basic/FileManager.h"
17 #include "clang/Basic/IdentifierTable.h"
18 #include "clang/Basic/LangOptions.h"
19 #include "clang/Basic/Module.h"
20 #include "clang/Basic/SourceLocation.h"
21 #include "clang/Basic/SourceManager.h"
22 #include "clang/Basic/TokenKinds.h"
23 #include "clang/Lex/CodeCompletionHandler.h"
24 #include "clang/Lex/HeaderSearch.h"
25 #include "clang/Lex/LexDiagnostic.h"
26 #include "clang/Lex/LiteralSupport.h"
27 #include "clang/Lex/MacroInfo.h"
28 #include "clang/Lex/ModuleLoader.h"
29 #include "clang/Lex/ModuleMap.h"
30 #include "clang/Lex/PPCallbacks.h"
31 #include "clang/Lex/Pragma.h"
32 #include "clang/Lex/Preprocessor.h"
33 #include "clang/Lex/PreprocessorOptions.h"
34 #include "clang/Lex/PTHLexer.h"
35 #include "clang/Lex/Token.h"
36 #include "clang/Lex/VariadicMacroSupport.h"
37 #include "llvm/ADT/ArrayRef.h"
38 #include "llvm/ADT/SmallString.h"
39 #include "llvm/ADT/SmallVector.h"
40 #include "llvm/ADT/STLExtras.h"
41 #include "llvm/ADT/StringSwitch.h"
42 #include "llvm/ADT/StringRef.h"
43 #include "llvm/Support/AlignOf.h"
44 #include "llvm/Support/ErrorHandling.h"
45 #include "llvm/Support/Path.h"
53 using namespace clang;
55 //===----------------------------------------------------------------------===//
56 // Utility Methods for Preprocessor Directive Handling.
57 //===----------------------------------------------------------------------===//
59 MacroInfo *Preprocessor::AllocateMacroInfo(SourceLocation L) {
60 auto *MIChain = new (BP) MacroInfoChain{L, MIChainHead};
61 MIChainHead = MIChain;
65 DefMacroDirective *Preprocessor::AllocateDefMacroDirective(MacroInfo *MI,
67 return new (BP) DefMacroDirective(MI, Loc);
71 Preprocessor::AllocateUndefMacroDirective(SourceLocation UndefLoc) {
72 return new (BP) UndefMacroDirective(UndefLoc);
75 VisibilityMacroDirective *
76 Preprocessor::AllocateVisibilityMacroDirective(SourceLocation Loc,
78 return new (BP) VisibilityMacroDirective(Loc, isPublic);
81 /// Read and discard all tokens remaining on the current line until
82 /// the tok::eod token is found.
83 void Preprocessor::DiscardUntilEndOfDirective() {
86 LexUnexpandedToken(Tmp);
87 assert(Tmp.isNot(tok::eof) && "EOF seen while discarding directive tokens");
88 } while (Tmp.isNot(tok::eod));
91 /// Enumerates possible cases of #define/#undef a reserved identifier.
93 MD_NoWarn, //> Not a reserved identifier
94 MD_KeywordDef, //> Macro hides keyword, enabled by default
95 MD_ReservedMacro //> #define of #undef reserved id, disabled by default
98 /// Checks if the specified identifier is reserved in the specified
100 /// This function does not check if the identifier is a keyword.
101 static bool isReservedId(StringRef Text, const LangOptions &Lang) {
102 // C++ [macro.names], C11 7.1.3:
103 // All identifiers that begin with an underscore and either an uppercase
104 // letter or another underscore are always reserved for any use.
105 if (Text.size() >= 2 && Text[0] == '_' &&
106 (isUppercase(Text[1]) || Text[1] == '_'))
108 // C++ [global.names]
109 // Each name that contains a double underscore ... is reserved to the
110 // implementation for any use.
111 if (Lang.CPlusPlus) {
112 if (Text.find("__") != StringRef::npos)
118 // The -fmodule-name option tells the compiler to textually include headers in
119 // the specified module, meaning clang won't build the specified module. This is
120 // useful in a number of situations, for instance, when building a library that
121 // vends a module map, one might want to avoid hitting intermediate build
122 // products containig the the module map or avoid finding the system installed
123 // modulemap for that library.
124 static bool isForModuleBuilding(Module *M, StringRef CurrentModule,
125 StringRef ModuleName) {
126 StringRef TopLevelName = M->getTopLevelModuleName();
128 // When building framework Foo, we wanna make sure that Foo *and* Foo_Private
129 // are textually included and no modules are built for both.
130 if (M->getTopLevelModule()->IsFramework && CurrentModule == ModuleName &&
131 !CurrentModule.endswith("_Private") && TopLevelName.endswith("_Private"))
132 TopLevelName = TopLevelName.drop_back(8);
134 return TopLevelName == CurrentModule;
137 static MacroDiag shouldWarnOnMacroDef(Preprocessor &PP, IdentifierInfo *II) {
138 const LangOptions &Lang = PP.getLangOpts();
139 StringRef Text = II->getName();
140 if (isReservedId(Text, Lang))
141 return MD_ReservedMacro;
142 if (II->isKeyword(Lang))
143 return MD_KeywordDef;
144 if (Lang.CPlusPlus11 && (Text.equals("override") || Text.equals("final")))
145 return MD_KeywordDef;
149 static MacroDiag shouldWarnOnMacroUndef(Preprocessor &PP, IdentifierInfo *II) {
150 const LangOptions &Lang = PP.getLangOpts();
151 StringRef Text = II->getName();
152 // Do not warn on keyword undef. It is generally harmless and widely used.
153 if (isReservedId(Text, Lang))
154 return MD_ReservedMacro;
158 // Return true if we want to issue a diagnostic by default if we
159 // encounter this name in a #include with the wrong case. For now,
160 // this includes the standard C and C++ headers, Posix headers,
161 // and Boost headers. Improper case for these #includes is a
162 // potential portability issue.
163 static bool warnByDefaultOnWrongCase(StringRef Include) {
164 // If the first component of the path is "boost", treat this like a standard header
165 // for the purposes of diagnostics.
166 if (::llvm::sys::path::begin(Include)->equals_lower("boost"))
169 // "condition_variable" is the longest standard header name at 18 characters.
170 // If the include file name is longer than that, it can't be a standard header.
171 static const size_t MaxStdHeaderNameLen = 18u;
172 if (Include.size() > MaxStdHeaderNameLen)
175 // Lowercase and normalize the search string.
176 SmallString<32> LowerInclude{Include};
177 for (char &Ch : LowerInclude) {
178 // In the ASCII range?
179 if (static_cast<unsigned char>(Ch) > 0x7f)
180 return false; // Can't be a standard header
182 if (Ch >= 'A' && Ch <= 'Z')
184 // Normalize path separators for comparison purposes.
185 else if (::llvm::sys::path::is_separator(Ch))
189 // The standard C/C++ and Posix headers
190 return llvm::StringSwitch<bool>(LowerInclude)
192 .Cases("assert.h", "complex.h", "ctype.h", "errno.h", "fenv.h", true)
193 .Cases("float.h", "inttypes.h", "iso646.h", "limits.h", "locale.h", true)
194 .Cases("math.h", "setjmp.h", "signal.h", "stdalign.h", "stdarg.h", true)
195 .Cases("stdatomic.h", "stdbool.h", "stddef.h", "stdint.h", "stdio.h", true)
196 .Cases("stdlib.h", "stdnoreturn.h", "string.h", "tgmath.h", "threads.h", true)
197 .Cases("time.h", "uchar.h", "wchar.h", "wctype.h", true)
199 // C++ headers for C library facilities
200 .Cases("cassert", "ccomplex", "cctype", "cerrno", "cfenv", true)
201 .Cases("cfloat", "cinttypes", "ciso646", "climits", "clocale", true)
202 .Cases("cmath", "csetjmp", "csignal", "cstdalign", "cstdarg", true)
203 .Cases("cstdbool", "cstddef", "cstdint", "cstdio", "cstdlib", true)
204 .Cases("cstring", "ctgmath", "ctime", "cuchar", "cwchar", true)
205 .Case("cwctype", true)
207 // C++ library headers
208 .Cases("algorithm", "fstream", "list", "regex", "thread", true)
209 .Cases("array", "functional", "locale", "scoped_allocator", "tuple", true)
210 .Cases("atomic", "future", "map", "set", "type_traits", true)
211 .Cases("bitset", "initializer_list", "memory", "shared_mutex", "typeindex", true)
212 .Cases("chrono", "iomanip", "mutex", "sstream", "typeinfo", true)
213 .Cases("codecvt", "ios", "new", "stack", "unordered_map", true)
214 .Cases("complex", "iosfwd", "numeric", "stdexcept", "unordered_set", true)
215 .Cases("condition_variable", "iostream", "ostream", "streambuf", "utility", true)
216 .Cases("deque", "istream", "queue", "string", "valarray", true)
217 .Cases("exception", "iterator", "random", "strstream", "vector", true)
218 .Cases("forward_list", "limits", "ratio", "system_error", true)
220 // POSIX headers (which aren't also C headers)
221 .Cases("aio.h", "arpa/inet.h", "cpio.h", "dirent.h", "dlfcn.h", true)
222 .Cases("fcntl.h", "fmtmsg.h", "fnmatch.h", "ftw.h", "glob.h", true)
223 .Cases("grp.h", "iconv.h", "langinfo.h", "libgen.h", "monetary.h", true)
224 .Cases("mqueue.h", "ndbm.h", "net/if.h", "netdb.h", "netinet/in.h", true)
225 .Cases("netinet/tcp.h", "nl_types.h", "poll.h", "pthread.h", "pwd.h", true)
226 .Cases("regex.h", "sched.h", "search.h", "semaphore.h", "spawn.h", true)
227 .Cases("strings.h", "stropts.h", "sys/ipc.h", "sys/mman.h", "sys/msg.h", true)
228 .Cases("sys/resource.h", "sys/select.h", "sys/sem.h", "sys/shm.h", "sys/socket.h", true)
229 .Cases("sys/stat.h", "sys/statvfs.h", "sys/time.h", "sys/times.h", "sys/types.h", true)
230 .Cases("sys/uio.h", "sys/un.h", "sys/utsname.h", "sys/wait.h", "syslog.h", true)
231 .Cases("tar.h", "termios.h", "trace.h", "ulimit.h", true)
232 .Cases("unistd.h", "utime.h", "utmpx.h", "wordexp.h", true)
236 bool Preprocessor::CheckMacroName(Token &MacroNameTok, MacroUse isDefineUndef,
238 // Missing macro name?
239 if (MacroNameTok.is(tok::eod))
240 return Diag(MacroNameTok, diag::err_pp_missing_macro_name);
242 IdentifierInfo *II = MacroNameTok.getIdentifierInfo();
244 return Diag(MacroNameTok, diag::err_pp_macro_not_identifier);
246 if (II->isCPlusPlusOperatorKeyword()) {
247 // C++ 2.5p2: Alternative tokens behave the same as its primary token
248 // except for their spellings.
249 Diag(MacroNameTok, getLangOpts().MicrosoftExt
250 ? diag::ext_pp_operator_used_as_macro_name
251 : diag::err_pp_operator_used_as_macro_name)
252 << II << MacroNameTok.getKind();
253 // Allow #defining |and| and friends for Microsoft compatibility or
254 // recovery when legacy C headers are included in C++.
257 if ((isDefineUndef != MU_Other) && II->getPPKeywordID() == tok::pp_defined) {
258 // Error if defining "defined": C99 6.10.8/4, C++ [cpp.predefined]p4.
259 return Diag(MacroNameTok, diag::err_defined_macro_name);
262 if (isDefineUndef == MU_Undef) {
263 auto *MI = getMacroInfo(II);
264 if (MI && MI->isBuiltinMacro()) {
265 // Warn if undefining "__LINE__" and other builtins, per C99 6.10.8/4
266 // and C++ [cpp.predefined]p4], but allow it as an extension.
267 Diag(MacroNameTok, diag::ext_pp_undef_builtin_macro);
271 // If defining/undefining reserved identifier or a keyword, we need to issue
273 SourceLocation MacroNameLoc = MacroNameTok.getLocation();
276 if (!SourceMgr.isInSystemHeader(MacroNameLoc) &&
277 (SourceMgr.getBufferName(MacroNameLoc) != "<built-in>")) {
278 MacroDiag D = MD_NoWarn;
279 if (isDefineUndef == MU_Define) {
280 D = shouldWarnOnMacroDef(*this, II);
282 else if (isDefineUndef == MU_Undef)
283 D = shouldWarnOnMacroUndef(*this, II);
284 if (D == MD_KeywordDef) {
285 // We do not want to warn on some patterns widely used in configuration
286 // scripts. This requires analyzing next tokens, so do not issue warnings
287 // now, only inform caller.
291 if (D == MD_ReservedMacro)
292 Diag(MacroNameTok, diag::warn_pp_macro_is_reserved_id);
295 // Okay, we got a good identifier.
299 /// Lex and validate a macro name, which occurs after a
300 /// \#define or \#undef.
302 /// This sets the token kind to eod and discards the rest of the macro line if
303 /// the macro name is invalid.
305 /// \param MacroNameTok Token that is expected to be a macro name.
306 /// \param isDefineUndef Context in which macro is used.
307 /// \param ShadowFlag Points to a flag that is set if macro shadows a keyword.
308 void Preprocessor::ReadMacroName(Token &MacroNameTok, MacroUse isDefineUndef,
310 // Read the token, don't allow macro expansion on it.
311 LexUnexpandedToken(MacroNameTok);
313 if (MacroNameTok.is(tok::code_completion)) {
315 CodeComplete->CodeCompleteMacroName(isDefineUndef == MU_Define);
316 setCodeCompletionReached();
317 LexUnexpandedToken(MacroNameTok);
320 if (!CheckMacroName(MacroNameTok, isDefineUndef, ShadowFlag))
323 // Invalid macro name, read and discard the rest of the line and set the
324 // token kind to tok::eod if necessary.
325 if (MacroNameTok.isNot(tok::eod)) {
326 MacroNameTok.setKind(tok::eod);
327 DiscardUntilEndOfDirective();
331 /// Ensure that the next token is a tok::eod token.
333 /// If not, emit a diagnostic and consume up until the eod. If EnableMacros is
334 /// true, then we consider macros that expand to zero tokens as being ok.
335 void Preprocessor::CheckEndOfDirective(const char *DirType, bool EnableMacros) {
337 // Lex unexpanded tokens for most directives: macros might expand to zero
338 // tokens, causing us to miss diagnosing invalid lines. Some directives (like
339 // #line) allow empty macros.
343 LexUnexpandedToken(Tmp);
345 // There should be no tokens after the directive, but we allow them as an
347 while (Tmp.is(tok::comment)) // Skip comments in -C mode.
348 LexUnexpandedToken(Tmp);
350 if (Tmp.isNot(tok::eod)) {
351 // Add a fixit in GNU/C99/C++ mode. Don't offer a fixit for strict-C89,
352 // or if this is a macro-style preprocessing directive, because it is more
353 // trouble than it is worth to insert /**/ and check that there is no /**/
354 // in the range also.
356 if ((LangOpts.GNUMode || LangOpts.C99 || LangOpts.CPlusPlus) &&
358 Hint = FixItHint::CreateInsertion(Tmp.getLocation(),"//");
359 Diag(Tmp, diag::ext_pp_extra_tokens_at_eol) << DirType << Hint;
360 DiscardUntilEndOfDirective();
364 /// SkipExcludedConditionalBlock - We just read a \#if or related directive and
365 /// decided that the subsequent tokens are in the \#if'd out portion of the
366 /// file. Lex the rest of the file, until we see an \#endif. If
367 /// FoundNonSkipPortion is true, then we have already emitted code for part of
368 /// this \#if directive, so \#else/\#elif blocks should never be entered.
369 /// If ElseOk is true, then \#else directives are ok, if not, then we have
370 /// already seen one so a \#else directive is a duplicate. When this returns,
371 /// the caller can lex the first valid token.
372 void Preprocessor::SkipExcludedConditionalBlock(SourceLocation HashTokenLoc,
373 SourceLocation IfTokenLoc,
374 bool FoundNonSkipPortion,
376 SourceLocation ElseLoc) {
378 assert(!CurTokenLexer && CurPPLexer && "Lexing a macro, not a file?");
380 if (PreambleConditionalStack.reachedEOFWhileSkipping())
381 PreambleConditionalStack.clearSkipInfo();
383 CurPPLexer->pushConditionalLevel(IfTokenLoc, /*isSkipping*/ false,
384 FoundNonSkipPortion, FoundElse);
387 PTHSkipExcludedConditionalBlock();
391 // Enter raw mode to disable identifier lookup (and thus macro expansion),
392 // disabling warnings, etc.
393 CurPPLexer->LexingRawMode = true;
398 if (Tok.is(tok::code_completion)) {
400 CodeComplete->CodeCompleteInConditionalExclusion();
401 setCodeCompletionReached();
405 // If this is the end of the buffer, we have an error.
406 if (Tok.is(tok::eof)) {
407 // We don't emit errors for unterminated conditionals here,
408 // Lexer::LexEndOfFile can do that propertly.
409 // Just return and let the caller lex after this #include.
410 if (PreambleConditionalStack.isRecording())
411 PreambleConditionalStack.SkipInfo.emplace(
412 HashTokenLoc, IfTokenLoc, FoundNonSkipPortion, FoundElse, ElseLoc);
416 // If this token is not a preprocessor directive, just skip it.
417 if (Tok.isNot(tok::hash) || !Tok.isAtStartOfLine())
420 // We just parsed a # character at the start of a line, so we're in
421 // directive mode. Tell the lexer this so any newlines we see will be
422 // converted into an EOD token (this terminates the macro).
423 CurPPLexer->ParsingPreprocessorDirective = true;
424 if (CurLexer) CurLexer->SetKeepWhitespaceMode(false);
427 // Read the next token, the directive flavor.
428 LexUnexpandedToken(Tok);
430 // If this isn't an identifier directive (e.g. is "# 1\n" or "#\n", or
431 // something bogus), skip it.
432 if (Tok.isNot(tok::raw_identifier)) {
433 CurPPLexer->ParsingPreprocessorDirective = false;
434 // Restore comment saving mode.
435 if (CurLexer) CurLexer->resetExtendedTokenMode();
439 // If the first letter isn't i or e, it isn't intesting to us. We know that
440 // this is safe in the face of spelling differences, because there is no way
441 // to spell an i/e in a strange way that is another letter. Skipping this
442 // allows us to avoid looking up the identifier info for #define/#undef and
443 // other common directives.
444 StringRef RI = Tok.getRawIdentifier();
446 char FirstChar = RI[0];
447 if (FirstChar >= 'a' && FirstChar <= 'z' &&
448 FirstChar != 'i' && FirstChar != 'e') {
449 CurPPLexer->ParsingPreprocessorDirective = false;
450 // Restore comment saving mode.
451 if (CurLexer) CurLexer->resetExtendedTokenMode();
455 // Get the identifier name without trigraphs or embedded newlines. Note
456 // that we can't use Tok.getIdentifierInfo() because its lookup is disabled
458 char DirectiveBuf[20];
460 if (!Tok.needsCleaning() && RI.size() < 20) {
463 std::string DirectiveStr = getSpelling(Tok);
464 size_t IdLen = DirectiveStr.size();
466 CurPPLexer->ParsingPreprocessorDirective = false;
467 // Restore comment saving mode.
468 if (CurLexer) CurLexer->resetExtendedTokenMode();
471 memcpy(DirectiveBuf, &DirectiveStr[0], IdLen);
472 Directive = StringRef(DirectiveBuf, IdLen);
475 if (Directive.startswith("if")) {
476 StringRef Sub = Directive.substr(2);
477 if (Sub.empty() || // "if"
478 Sub == "def" || // "ifdef"
479 Sub == "ndef") { // "ifndef"
480 // We know the entire #if/#ifdef/#ifndef block will be skipped, don't
481 // bother parsing the condition.
482 DiscardUntilEndOfDirective();
483 CurPPLexer->pushConditionalLevel(Tok.getLocation(), /*wasskipping*/true,
484 /*foundnonskip*/false,
487 } else if (Directive[0] == 'e') {
488 StringRef Sub = Directive.substr(1);
489 if (Sub == "ndif") { // "endif"
490 PPConditionalInfo CondInfo;
491 CondInfo.WasSkipping = true; // Silence bogus warning.
492 bool InCond = CurPPLexer->popConditionalLevel(CondInfo);
493 (void)InCond; // Silence warning in no-asserts mode.
494 assert(!InCond && "Can't be skipping if not in a conditional!");
496 // If we popped the outermost skipping block, we're done skipping!
497 if (!CondInfo.WasSkipping) {
498 // Restore the value of LexingRawMode so that trailing comments
499 // are handled correctly, if we've reached the outermost block.
500 CurPPLexer->LexingRawMode = false;
501 CheckEndOfDirective("endif");
502 CurPPLexer->LexingRawMode = true;
504 Callbacks->Endif(Tok.getLocation(), CondInfo.IfLoc);
507 DiscardUntilEndOfDirective();
509 } else if (Sub == "lse") { // "else".
510 // #else directive in a skipping conditional. If not in some other
511 // skipping conditional, and if #else hasn't already been seen, enter it
512 // as a non-skipping conditional.
513 PPConditionalInfo &CondInfo = CurPPLexer->peekConditionalLevel();
515 // If this is a #else with a #else before it, report the error.
516 if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_else_after_else);
518 // Note that we've seen a #else in this conditional.
519 CondInfo.FoundElse = true;
521 // If the conditional is at the top level, and the #if block wasn't
522 // entered, enter the #else block now.
523 if (!CondInfo.WasSkipping && !CondInfo.FoundNonSkip) {
524 CondInfo.FoundNonSkip = true;
525 // Restore the value of LexingRawMode so that trailing comments
526 // are handled correctly.
527 CurPPLexer->LexingRawMode = false;
528 CheckEndOfDirective("else");
529 CurPPLexer->LexingRawMode = true;
531 Callbacks->Else(Tok.getLocation(), CondInfo.IfLoc);
534 DiscardUntilEndOfDirective(); // C99 6.10p4.
536 } else if (Sub == "lif") { // "elif".
537 PPConditionalInfo &CondInfo = CurPPLexer->peekConditionalLevel();
539 // If this is a #elif with a #else before it, report the error.
540 if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_elif_after_else);
542 // If this is in a skipping block or if we're already handled this #if
543 // block, don't bother parsing the condition.
544 if (CondInfo.WasSkipping || CondInfo.FoundNonSkip) {
545 DiscardUntilEndOfDirective();
547 const SourceLocation CondBegin = CurPPLexer->getSourceLocation();
548 // Restore the value of LexingRawMode so that identifiers are
549 // looked up, etc, inside the #elif expression.
550 assert(CurPPLexer->LexingRawMode && "We have to be skipping here!");
551 CurPPLexer->LexingRawMode = false;
552 IdentifierInfo *IfNDefMacro = nullptr;
553 const bool CondValue = EvaluateDirectiveExpression(IfNDefMacro).Conditional;
554 CurPPLexer->LexingRawMode = true;
556 const SourceLocation CondEnd = CurPPLexer->getSourceLocation();
557 Callbacks->Elif(Tok.getLocation(),
558 SourceRange(CondBegin, CondEnd),
559 (CondValue ? PPCallbacks::CVK_True : PPCallbacks::CVK_False), CondInfo.IfLoc);
561 // If this condition is true, enter it!
563 CondInfo.FoundNonSkip = true;
570 CurPPLexer->ParsingPreprocessorDirective = false;
571 // Restore comment saving mode.
572 if (CurLexer) CurLexer->resetExtendedTokenMode();
575 // Finally, if we are out of the conditional (saw an #endif or ran off the end
576 // of the file, just stop skipping and return to lexing whatever came after
578 CurPPLexer->LexingRawMode = false;
580 // The last skipped range isn't actually skipped yet if it's truncated
581 // by the end of the preamble; we'll resume parsing after the preamble.
582 if (Callbacks && (Tok.isNot(tok::eof) || !isRecordingPreamble()))
583 Callbacks->SourceRangeSkipped(
584 SourceRange(HashTokenLoc, CurPPLexer->getSourceLocation()),
588 void Preprocessor::PTHSkipExcludedConditionalBlock() {
591 assert(CurPTHLexer->LexingRawMode == false);
593 // Skip to the next '#else', '#elif', or #endif.
594 if (CurPTHLexer->SkipBlock()) {
595 // We have reached an #endif. Both the '#' and 'endif' tokens
596 // have been consumed by the PTHLexer. Just pop off the condition level.
597 PPConditionalInfo CondInfo;
598 bool InCond = CurPTHLexer->popConditionalLevel(CondInfo);
599 (void)InCond; // Silence warning in no-asserts mode.
600 assert(!InCond && "Can't be skipping if not in a conditional!");
604 // We have reached a '#else' or '#elif'. Lex the next token to get
605 // the directive flavor.
607 LexUnexpandedToken(Tok);
609 // We can actually look up the IdentifierInfo here since we aren't in
611 tok::PPKeywordKind K = Tok.getIdentifierInfo()->getPPKeywordID();
613 if (K == tok::pp_else) {
614 // #else: Enter the else condition. We aren't in a nested condition
615 // since we skip those. We're always in the one matching the last
616 // blocked we skipped.
617 PPConditionalInfo &CondInfo = CurPTHLexer->peekConditionalLevel();
618 // Note that we've seen a #else in this conditional.
619 CondInfo.FoundElse = true;
621 // If the #if block wasn't entered then enter the #else block now.
622 if (!CondInfo.FoundNonSkip) {
623 CondInfo.FoundNonSkip = true;
625 // Scan until the eod token.
626 CurPTHLexer->ParsingPreprocessorDirective = true;
627 DiscardUntilEndOfDirective();
628 CurPTHLexer->ParsingPreprocessorDirective = false;
633 // Otherwise skip this block.
637 assert(K == tok::pp_elif);
638 PPConditionalInfo &CondInfo = CurPTHLexer->peekConditionalLevel();
640 // If this is a #elif with a #else before it, report the error.
641 if (CondInfo.FoundElse)
642 Diag(Tok, diag::pp_err_elif_after_else);
644 // If this is in a skipping block or if we're already handled this #if
645 // block, don't bother parsing the condition. We just skip this block.
646 if (CondInfo.FoundNonSkip)
649 // Evaluate the condition of the #elif.
650 IdentifierInfo *IfNDefMacro = nullptr;
651 CurPTHLexer->ParsingPreprocessorDirective = true;
652 bool ShouldEnter = EvaluateDirectiveExpression(IfNDefMacro).Conditional;
653 CurPTHLexer->ParsingPreprocessorDirective = false;
655 // If this condition is true, enter it!
657 CondInfo.FoundNonSkip = true;
661 // Otherwise, skip this block and go to the next one.
665 Module *Preprocessor::getModuleForLocation(SourceLocation Loc) {
666 if (!SourceMgr.isInMainFile(Loc)) {
667 // Try to determine the module of the include directive.
668 // FIXME: Look into directly passing the FileEntry from LookupFile instead.
669 FileID IDOfIncl = SourceMgr.getFileID(SourceMgr.getExpansionLoc(Loc));
670 if (const FileEntry *EntryOfIncl = SourceMgr.getFileEntryForID(IDOfIncl)) {
671 // The include comes from an included file.
672 return HeaderInfo.getModuleMap()
673 .findModuleForHeader(EntryOfIncl)
678 // This is either in the main file or not in a file at all. It belongs
679 // to the current module, if there is one.
680 return getLangOpts().CurrentModule.empty()
682 : HeaderInfo.lookupModule(getLangOpts().CurrentModule);
686 Preprocessor::getModuleHeaderToIncludeForDiagnostics(SourceLocation IncLoc,
688 SourceLocation Loc) {
689 assert(M && "no module to include");
691 // If we have a module import syntax, we shouldn't include a header to
692 // make a particular module visible.
693 if (getLangOpts().ObjC2)
696 Module *TopM = M->getTopLevelModule();
697 Module *IncM = getModuleForLocation(IncLoc);
699 // Walk up through the include stack, looking through textual headers of M
700 // until we hit a non-textual header that we can #include. (We assume textual
701 // headers of a module with non-textual headers aren't meant to be used to
702 // import entities from the module.)
703 auto &SM = getSourceManager();
704 while (!Loc.isInvalid() && !SM.isInMainFile(Loc)) {
705 auto ID = SM.getFileID(SM.getExpansionLoc(Loc));
706 auto *FE = SM.getFileEntryForID(ID);
710 bool InTextualHeader = false;
711 for (auto Header : HeaderInfo.getModuleMap().findAllModulesForHeader(FE)) {
712 if (!Header.getModule()->isSubModuleOf(TopM))
715 if (!(Header.getRole() & ModuleMap::TextualHeader)) {
716 // If this is an accessible, non-textual header of M's top-level module
717 // that transitively includes the given location and makes the
718 // corresponding module visible, this is the thing to #include.
719 if (Header.isAccessibleFrom(IncM))
722 // It's in a private header; we can't #include it.
723 // FIXME: If there's a public header in some module that re-exports it,
724 // then we could suggest including that, but it's not clear that's the
725 // expected way to make this entity visible.
729 InTextualHeader = true;
732 if (!InTextualHeader)
735 Loc = SM.getIncludeLoc(ID);
741 const FileEntry *Preprocessor::LookupFile(
742 SourceLocation FilenameLoc, StringRef Filename, bool isAngled,
743 const DirectoryLookup *FromDir, const FileEntry *FromFile,
744 const DirectoryLookup *&CurDir, SmallVectorImpl<char> *SearchPath,
745 SmallVectorImpl<char> *RelativePath,
746 ModuleMap::KnownHeader *SuggestedModule, bool *IsMapped, bool SkipCache) {
747 Module *RequestingModule = getModuleForLocation(FilenameLoc);
748 bool RequestingModuleIsModuleInterface = !SourceMgr.isInMainFile(FilenameLoc);
750 // If the header lookup mechanism may be relative to the current inclusion
751 // stack, record the parent #includes.
752 SmallVector<std::pair<const FileEntry *, const DirectoryEntry *>, 16>
754 bool BuildSystemModule = false;
755 if (!FromDir && !FromFile) {
756 FileID FID = getCurrentFileLexer()->getFileID();
757 const FileEntry *FileEnt = SourceMgr.getFileEntryForID(FID);
759 // If there is no file entry associated with this file, it must be the
760 // predefines buffer or the module includes buffer. Any other file is not
761 // lexed with a normal lexer, so it won't be scanned for preprocessor
764 // If we have the predefines buffer, resolve #include references (which come
765 // from the -include command line argument) from the current working
766 // directory instead of relative to the main file.
768 // If we have the module includes buffer, resolve #include references (which
769 // come from header declarations in the module map) relative to the module
772 if (FID == SourceMgr.getMainFileID() && MainFileDir) {
773 Includers.push_back(std::make_pair(nullptr, MainFileDir));
774 BuildSystemModule = getCurrentModule()->IsSystem;
775 } else if ((FileEnt =
776 SourceMgr.getFileEntryForID(SourceMgr.getMainFileID())))
777 Includers.push_back(std::make_pair(FileEnt, FileMgr.getDirectory(".")));
779 Includers.push_back(std::make_pair(FileEnt, FileEnt->getDir()));
782 // MSVC searches the current include stack from top to bottom for
783 // headers included by quoted include directives.
784 // See: http://msdn.microsoft.com/en-us/library/36k2cdd4.aspx
785 if (LangOpts.MSVCCompat && !isAngled) {
786 for (IncludeStackInfo &ISEntry : llvm::reverse(IncludeMacroStack)) {
787 if (IsFileLexer(ISEntry))
788 if ((FileEnt = ISEntry.ThePPLexer->getFileEntry()))
789 Includers.push_back(std::make_pair(FileEnt, FileEnt->getDir()));
794 CurDir = CurDirLookup;
797 // We're supposed to start looking from after a particular file. Search
798 // the include path until we find that file or run out of files.
799 const DirectoryLookup *TmpCurDir = CurDir;
800 const DirectoryLookup *TmpFromDir = nullptr;
801 while (const FileEntry *FE = HeaderInfo.LookupFile(
802 Filename, FilenameLoc, isAngled, TmpFromDir, TmpCurDir,
803 Includers, SearchPath, RelativePath, RequestingModule,
804 SuggestedModule, /*IsMapped=*/nullptr, SkipCache)) {
805 // Keep looking as if this file did a #include_next.
806 TmpFromDir = TmpCurDir;
808 if (FE == FromFile) {
810 FromDir = TmpFromDir;
817 // Do a standard file entry lookup.
818 const FileEntry *FE = HeaderInfo.LookupFile(
819 Filename, FilenameLoc, isAngled, FromDir, CurDir, Includers, SearchPath,
820 RelativePath, RequestingModule, SuggestedModule, IsMapped, SkipCache,
823 if (SuggestedModule && !LangOpts.AsmPreprocessor)
824 HeaderInfo.getModuleMap().diagnoseHeaderInclusion(
825 RequestingModule, RequestingModuleIsModuleInterface, FilenameLoc,
830 const FileEntry *CurFileEnt;
831 // Otherwise, see if this is a subframework header. If so, this is relative
832 // to one of the headers on the #include stack. Walk the list of the current
833 // headers on the #include stack and pass them to HeaderInfo.
835 if ((CurFileEnt = CurPPLexer->getFileEntry())) {
836 if ((FE = HeaderInfo.LookupSubframeworkHeader(Filename, CurFileEnt,
837 SearchPath, RelativePath,
840 if (SuggestedModule && !LangOpts.AsmPreprocessor)
841 HeaderInfo.getModuleMap().diagnoseHeaderInclusion(
842 RequestingModule, RequestingModuleIsModuleInterface, FilenameLoc,
849 for (IncludeStackInfo &ISEntry : llvm::reverse(IncludeMacroStack)) {
850 if (IsFileLexer(ISEntry)) {
851 if ((CurFileEnt = ISEntry.ThePPLexer->getFileEntry())) {
852 if ((FE = HeaderInfo.LookupSubframeworkHeader(
853 Filename, CurFileEnt, SearchPath, RelativePath,
854 RequestingModule, SuggestedModule))) {
855 if (SuggestedModule && !LangOpts.AsmPreprocessor)
856 HeaderInfo.getModuleMap().diagnoseHeaderInclusion(
857 RequestingModule, RequestingModuleIsModuleInterface,
858 FilenameLoc, Filename, FE);
865 // Otherwise, we really couldn't find the file.
869 //===----------------------------------------------------------------------===//
870 // Preprocessor Directive Handling.
871 //===----------------------------------------------------------------------===//
873 class Preprocessor::ResetMacroExpansionHelper {
875 ResetMacroExpansionHelper(Preprocessor *pp)
876 : PP(pp), save(pp->DisableMacroExpansion) {
877 if (pp->MacroExpansionInDirectivesOverride)
878 pp->DisableMacroExpansion = false;
881 ~ResetMacroExpansionHelper() {
882 PP->DisableMacroExpansion = save;
890 /// Process a directive while looking for the through header.
891 /// Only #include (to check if it is the through header) and #define (to warn
892 /// about macros that don't match the PCH) are handled. All other directives
893 /// are completely discarded.
894 void Preprocessor::HandleSkippedThroughHeaderDirective(Token &Result,
895 SourceLocation HashLoc) {
896 if (const IdentifierInfo *II = Result.getIdentifierInfo()) {
897 if (II->getPPKeywordID() == tok::pp_include)
898 return HandleIncludeDirective(HashLoc, Result);
899 if (II->getPPKeywordID() == tok::pp_define)
900 return HandleDefineDirective(Result,
901 /*ImmediatelyAfterHeaderGuard=*/false);
903 DiscardUntilEndOfDirective();
906 /// HandleDirective - This callback is invoked when the lexer sees a # token
907 /// at the start of a line. This consumes the directive, modifies the
908 /// lexer/preprocessor state, and advances the lexer(s) so that the next token
909 /// read is the correct one.
910 void Preprocessor::HandleDirective(Token &Result) {
911 // FIXME: Traditional: # with whitespace before it not recognized by K&R?
913 // We just parsed a # character at the start of a line, so we're in directive
914 // mode. Tell the lexer this so any newlines we see will be converted into an
915 // EOD token (which terminates the directive).
916 CurPPLexer->ParsingPreprocessorDirective = true;
917 if (CurLexer) CurLexer->SetKeepWhitespaceMode(false);
919 bool ImmediatelyAfterTopLevelIfndef =
920 CurPPLexer->MIOpt.getImmediatelyAfterTopLevelIfndef();
921 CurPPLexer->MIOpt.resetImmediatelyAfterTopLevelIfndef();
925 // We are about to read a token. For the multiple-include optimization FA to
926 // work, we have to remember if we had read any tokens *before* this
928 bool ReadAnyTokensBeforeDirective =CurPPLexer->MIOpt.getHasReadAnyTokensVal();
930 // Save the '#' token in case we need to return it later.
931 Token SavedHash = Result;
933 // Read the next token, the directive flavor. This isn't expanded due to
935 LexUnexpandedToken(Result);
937 // C99 6.10.3p11: Is this preprocessor directive in macro invocation? e.g.:
942 // If so, the user is relying on undefined behavior, emit a diagnostic. Do
943 // not support this for #include-like directives, since that can result in
944 // terrible diagnostics, and does not work in GCC.
946 if (IdentifierInfo *II = Result.getIdentifierInfo()) {
947 switch (II->getPPKeywordID()) {
948 case tok::pp_include:
950 case tok::pp_include_next:
951 case tok::pp___include_macros:
953 Diag(Result, diag::err_embedded_directive) << II->getName();
954 DiscardUntilEndOfDirective();
960 Diag(Result, diag::ext_embedded_directive);
963 // Temporarily enable macro expansion if set so
964 // and reset to previous state when returning from this function.
965 ResetMacroExpansionHelper helper(this);
967 if (SkippingUntilPCHThroughHeader)
968 return HandleSkippedThroughHeaderDirective(Result, SavedHash.getLocation());
970 switch (Result.getKind()) {
972 return; // null directive.
973 case tok::code_completion:
975 CodeComplete->CodeCompleteDirective(
976 CurPPLexer->getConditionalStackDepth() > 0);
977 setCodeCompletionReached();
979 case tok::numeric_constant: // # 7 GNU line marker directive.
980 if (getLangOpts().AsmPreprocessor)
981 break; // # 4 is not a preprocessor directive in .S files.
982 return HandleDigitDirective(Result);
984 IdentifierInfo *II = Result.getIdentifierInfo();
985 if (!II) break; // Not an identifier.
987 // Ask what the preprocessor keyword ID is.
988 switch (II->getPPKeywordID()) {
990 // C99 6.10.1 - Conditional Inclusion.
992 return HandleIfDirective(Result, SavedHash, ReadAnyTokensBeforeDirective);
994 return HandleIfdefDirective(Result, SavedHash, false,
995 true /*not valid for miopt*/);
997 return HandleIfdefDirective(Result, SavedHash, true,
998 ReadAnyTokensBeforeDirective);
1000 return HandleElifDirective(Result, SavedHash);
1002 return HandleElseDirective(Result, SavedHash);
1004 return HandleEndifDirective(Result);
1006 // C99 6.10.2 - Source File Inclusion.
1007 case tok::pp_include:
1009 return HandleIncludeDirective(SavedHash.getLocation(), Result);
1010 case tok::pp___include_macros:
1012 return HandleIncludeMacrosDirective(SavedHash.getLocation(), Result);
1014 // C99 6.10.3 - Macro Replacement.
1015 case tok::pp_define:
1016 return HandleDefineDirective(Result, ImmediatelyAfterTopLevelIfndef);
1018 return HandleUndefDirective();
1020 // C99 6.10.4 - Line Control.
1022 return HandleLineDirective();
1024 // C99 6.10.5 - Error Directive.
1026 return HandleUserDiagnosticDirective(Result, false);
1028 // C99 6.10.6 - Pragma Directive.
1029 case tok::pp_pragma:
1030 return HandlePragmaDirective(SavedHash.getLocation(), PIK_HashPragma);
1033 case tok::pp_import:
1034 return HandleImportDirective(SavedHash.getLocation(), Result);
1035 case tok::pp_include_next:
1036 return HandleIncludeNextDirective(SavedHash.getLocation(), Result);
1038 case tok::pp_warning:
1039 Diag(Result, diag::ext_pp_warning_directive);
1040 return HandleUserDiagnosticDirective(Result, true);
1042 return HandleIdentSCCSDirective(Result);
1044 return HandleIdentSCCSDirective(Result);
1045 case tok::pp_assert:
1046 //isExtension = true; // FIXME: implement #assert
1048 case tok::pp_unassert:
1049 //isExtension = true; // FIXME: implement #unassert
1052 case tok::pp___public_macro:
1053 if (getLangOpts().Modules)
1054 return HandleMacroPublicDirective(Result);
1057 case tok::pp___private_macro:
1058 if (getLangOpts().Modules)
1059 return HandleMacroPrivateDirective();
1065 // If this is a .S file, treat unknown # directives as non-preprocessor
1066 // directives. This is important because # may be a comment or introduce
1067 // various pseudo-ops. Just return the # token and push back the following
1068 // token to be lexed next time.
1069 if (getLangOpts().AsmPreprocessor) {
1070 auto Toks = llvm::make_unique<Token[]>(2);
1071 // Return the # and the token after it.
1072 Toks[0] = SavedHash;
1075 // If the second token is a hashhash token, then we need to translate it to
1076 // unknown so the token lexer doesn't try to perform token pasting.
1077 if (Result.is(tok::hashhash))
1078 Toks[1].setKind(tok::unknown);
1080 // Enter this token stream so that we re-lex the tokens. Make sure to
1081 // enable macro expansion, in case the token after the # is an identifier
1082 // that is expanded.
1083 EnterTokenStream(std::move(Toks), 2, false);
1087 // If we reached here, the preprocessing token is not valid!
1088 Diag(Result, diag::err_pp_invalid_directive);
1090 // Read the rest of the PP line.
1091 DiscardUntilEndOfDirective();
1093 // Okay, we're done parsing the directive.
1096 /// GetLineValue - Convert a numeric token into an unsigned value, emitting
1097 /// Diagnostic DiagID if it is invalid, and returning the value in Val.
1098 static bool GetLineValue(Token &DigitTok, unsigned &Val,
1099 unsigned DiagID, Preprocessor &PP,
1100 bool IsGNULineDirective=false) {
1101 if (DigitTok.isNot(tok::numeric_constant)) {
1102 PP.Diag(DigitTok, DiagID);
1104 if (DigitTok.isNot(tok::eod))
1105 PP.DiscardUntilEndOfDirective();
1109 SmallString<64> IntegerBuffer;
1110 IntegerBuffer.resize(DigitTok.getLength());
1111 const char *DigitTokBegin = &IntegerBuffer[0];
1112 bool Invalid = false;
1113 unsigned ActualLength = PP.getSpelling(DigitTok, DigitTokBegin, &Invalid);
1117 // Verify that we have a simple digit-sequence, and compute the value. This
1118 // is always a simple digit string computed in decimal, so we do this manually
1121 for (unsigned i = 0; i != ActualLength; ++i) {
1122 // C++1y [lex.fcon]p1:
1123 // Optional separating single quotes in a digit-sequence are ignored
1124 if (DigitTokBegin[i] == '\'')
1127 if (!isDigit(DigitTokBegin[i])) {
1128 PP.Diag(PP.AdvanceToTokenCharacter(DigitTok.getLocation(), i),
1129 diag::err_pp_line_digit_sequence) << IsGNULineDirective;
1130 PP.DiscardUntilEndOfDirective();
1134 unsigned NextVal = Val*10+(DigitTokBegin[i]-'0');
1135 if (NextVal < Val) { // overflow.
1136 PP.Diag(DigitTok, DiagID);
1137 PP.DiscardUntilEndOfDirective();
1143 if (DigitTokBegin[0] == '0' && Val)
1144 PP.Diag(DigitTok.getLocation(), diag::warn_pp_line_decimal)
1145 << IsGNULineDirective;
1150 /// Handle a \#line directive: C99 6.10.4.
1152 /// The two acceptable forms are:
1154 /// # line digit-sequence
1155 /// # line digit-sequence "s-char-sequence"
1157 void Preprocessor::HandleLineDirective() {
1158 // Read the line # and string argument. Per C99 6.10.4p5, these tokens are
1163 // Validate the number and convert it to an unsigned.
1165 if (GetLineValue(DigitTok, LineNo, diag::err_pp_line_requires_integer,*this))
1169 Diag(DigitTok, diag::ext_pp_line_zero);
1171 // Enforce C99 6.10.4p3: "The digit sequence shall not specify ... a
1172 // number greater than 2147483647". C90 requires that the line # be <= 32767.
1173 unsigned LineLimit = 32768U;
1174 if (LangOpts.C99 || LangOpts.CPlusPlus11)
1175 LineLimit = 2147483648U;
1176 if (LineNo >= LineLimit)
1177 Diag(DigitTok, diag::ext_pp_line_too_big) << LineLimit;
1178 else if (LangOpts.CPlusPlus11 && LineNo >= 32768U)
1179 Diag(DigitTok, diag::warn_cxx98_compat_pp_line_too_big);
1181 int FilenameID = -1;
1185 // If the StrTok is "eod", then it wasn't present. Otherwise, it must be a
1186 // string followed by eod.
1187 if (StrTok.is(tok::eod))
1189 else if (StrTok.isNot(tok::string_literal)) {
1190 Diag(StrTok, diag::err_pp_line_invalid_filename);
1191 return DiscardUntilEndOfDirective();
1192 } else if (StrTok.hasUDSuffix()) {
1193 Diag(StrTok, diag::err_invalid_string_udl);
1194 return DiscardUntilEndOfDirective();
1196 // Parse and validate the string, converting it into a unique ID.
1197 StringLiteralParser Literal(StrTok, *this);
1198 assert(Literal.isAscii() && "Didn't allow wide strings in");
1199 if (Literal.hadError)
1200 return DiscardUntilEndOfDirective();
1201 if (Literal.Pascal) {
1202 Diag(StrTok, diag::err_pp_linemarker_invalid_filename);
1203 return DiscardUntilEndOfDirective();
1205 FilenameID = SourceMgr.getLineTableFilenameID(Literal.GetString());
1207 // Verify that there is nothing after the string, other than EOD. Because
1208 // of C99 6.10.4p5, macros that expand to empty tokens are ok.
1209 CheckEndOfDirective("line", true);
1212 // Take the file kind of the file containing the #line directive. #line
1213 // directives are often used for generated sources from the same codebase, so
1214 // the new file should generally be classified the same way as the current
1215 // file. This is visible in GCC's pre-processed output, which rewrites #line
1216 // to GNU line markers.
1217 SrcMgr::CharacteristicKind FileKind =
1218 SourceMgr.getFileCharacteristic(DigitTok.getLocation());
1220 SourceMgr.AddLineNote(DigitTok.getLocation(), LineNo, FilenameID, false,
1224 Callbacks->FileChanged(CurPPLexer->getSourceLocation(),
1225 PPCallbacks::RenameFile, FileKind);
1228 /// ReadLineMarkerFlags - Parse and validate any flags at the end of a GNU line
1229 /// marker directive.
1230 static bool ReadLineMarkerFlags(bool &IsFileEntry, bool &IsFileExit,
1231 SrcMgr::CharacteristicKind &FileKind,
1236 if (FlagTok.is(tok::eod)) return false;
1237 if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag, PP))
1244 if (FlagTok.is(tok::eod)) return false;
1245 if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag,PP))
1247 } else if (FlagVal == 2) {
1250 SourceManager &SM = PP.getSourceManager();
1251 // If we are leaving the current presumed file, check to make sure the
1252 // presumed include stack isn't empty!
1254 SM.getDecomposedExpansionLoc(FlagTok.getLocation()).first;
1255 PresumedLoc PLoc = SM.getPresumedLoc(FlagTok.getLocation());
1256 if (PLoc.isInvalid())
1259 // If there is no include loc (main file) or if the include loc is in a
1260 // different physical file, then we aren't in a "1" line marker flag region.
1261 SourceLocation IncLoc = PLoc.getIncludeLoc();
1262 if (IncLoc.isInvalid() ||
1263 SM.getDecomposedExpansionLoc(IncLoc).first != CurFileID) {
1264 PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_pop);
1265 PP.DiscardUntilEndOfDirective();
1270 if (FlagTok.is(tok::eod)) return false;
1271 if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag,PP))
1275 // We must have 3 if there are still flags.
1277 PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag);
1278 PP.DiscardUntilEndOfDirective();
1282 FileKind = SrcMgr::C_System;
1285 if (FlagTok.is(tok::eod)) return false;
1286 if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag, PP))
1289 // We must have 4 if there is yet another flag.
1291 PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag);
1292 PP.DiscardUntilEndOfDirective();
1296 FileKind = SrcMgr::C_ExternCSystem;
1299 if (FlagTok.is(tok::eod)) return false;
1301 // There are no more valid flags here.
1302 PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag);
1303 PP.DiscardUntilEndOfDirective();
1307 /// HandleDigitDirective - Handle a GNU line marker directive, whose syntax is
1308 /// one of the following forms:
1311 /// # 42 "file" ('1' | '2')?
1312 /// # 42 "file" ('1' | '2')? '3' '4'?
1314 void Preprocessor::HandleDigitDirective(Token &DigitTok) {
1315 // Validate the number and convert it to an unsigned. GNU does not have a
1316 // line # limit other than it fit in 32-bits.
1318 if (GetLineValue(DigitTok, LineNo, diag::err_pp_linemarker_requires_integer,
1325 bool IsFileEntry = false, IsFileExit = false;
1326 int FilenameID = -1;
1327 SrcMgr::CharacteristicKind FileKind = SrcMgr::C_User;
1329 // If the StrTok is "eod", then it wasn't present. Otherwise, it must be a
1330 // string followed by eod.
1331 if (StrTok.is(tok::eod)) {
1332 // Treat this like "#line NN", which doesn't change file characteristics.
1333 FileKind = SourceMgr.getFileCharacteristic(DigitTok.getLocation());
1334 } else if (StrTok.isNot(tok::string_literal)) {
1335 Diag(StrTok, diag::err_pp_linemarker_invalid_filename);
1336 return DiscardUntilEndOfDirective();
1337 } else if (StrTok.hasUDSuffix()) {
1338 Diag(StrTok, diag::err_invalid_string_udl);
1339 return DiscardUntilEndOfDirective();
1341 // Parse and validate the string, converting it into a unique ID.
1342 StringLiteralParser Literal(StrTok, *this);
1343 assert(Literal.isAscii() && "Didn't allow wide strings in");
1344 if (Literal.hadError)
1345 return DiscardUntilEndOfDirective();
1346 if (Literal.Pascal) {
1347 Diag(StrTok, diag::err_pp_linemarker_invalid_filename);
1348 return DiscardUntilEndOfDirective();
1350 FilenameID = SourceMgr.getLineTableFilenameID(Literal.GetString());
1352 // If a filename was present, read any flags that are present.
1353 if (ReadLineMarkerFlags(IsFileEntry, IsFileExit, FileKind, *this))
1357 // Create a line note with this information.
1358 SourceMgr.AddLineNote(DigitTok.getLocation(), LineNo, FilenameID, IsFileEntry,
1359 IsFileExit, FileKind);
1361 // If the preprocessor has callbacks installed, notify them of the #line
1362 // change. This is used so that the line marker comes out in -E mode for
1365 PPCallbacks::FileChangeReason Reason = PPCallbacks::RenameFile;
1367 Reason = PPCallbacks::EnterFile;
1368 else if (IsFileExit)
1369 Reason = PPCallbacks::ExitFile;
1371 Callbacks->FileChanged(CurPPLexer->getSourceLocation(), Reason, FileKind);
1375 /// HandleUserDiagnosticDirective - Handle a #warning or #error directive.
1377 void Preprocessor::HandleUserDiagnosticDirective(Token &Tok,
1379 // PTH doesn't emit #warning or #error directives.
1381 return CurPTHLexer->DiscardToEndOfLine();
1383 // Read the rest of the line raw. We do this because we don't want macros
1384 // to be expanded and we don't require that the tokens be valid preprocessing
1385 // tokens. For example, this is allowed: "#warning ` 'foo". GCC does
1386 // collapse multiple consecutive white space between tokens, but this isn't
1387 // specified by the standard.
1388 SmallString<128> Message;
1389 CurLexer->ReadToEndOfLine(&Message);
1391 // Find the first non-whitespace character, so that we can make the
1392 // diagnostic more succinct.
1393 StringRef Msg = StringRef(Message).ltrim(' ');
1396 Diag(Tok, diag::pp_hash_warning) << Msg;
1398 Diag(Tok, diag::err_pp_hash_error) << Msg;
1401 /// HandleIdentSCCSDirective - Handle a #ident/#sccs directive.
1403 void Preprocessor::HandleIdentSCCSDirective(Token &Tok) {
1404 // Yes, this directive is an extension.
1405 Diag(Tok, diag::ext_pp_ident_directive);
1407 // Read the string argument.
1411 // If the token kind isn't a string, it's a malformed directive.
1412 if (StrTok.isNot(tok::string_literal) &&
1413 StrTok.isNot(tok::wide_string_literal)) {
1414 Diag(StrTok, diag::err_pp_malformed_ident);
1415 if (StrTok.isNot(tok::eod))
1416 DiscardUntilEndOfDirective();
1420 if (StrTok.hasUDSuffix()) {
1421 Diag(StrTok, diag::err_invalid_string_udl);
1422 return DiscardUntilEndOfDirective();
1425 // Verify that there is nothing after the string, other than EOD.
1426 CheckEndOfDirective("ident");
1429 bool Invalid = false;
1430 std::string Str = getSpelling(StrTok, &Invalid);
1432 Callbacks->Ident(Tok.getLocation(), Str);
1436 /// Handle a #public directive.
1437 void Preprocessor::HandleMacroPublicDirective(Token &Tok) {
1439 ReadMacroName(MacroNameTok, MU_Undef);
1441 // Error reading macro name? If so, diagnostic already issued.
1442 if (MacroNameTok.is(tok::eod))
1445 // Check to see if this is the last token on the #__public_macro line.
1446 CheckEndOfDirective("__public_macro");
1448 IdentifierInfo *II = MacroNameTok.getIdentifierInfo();
1449 // Okay, we finally have a valid identifier to undef.
1450 MacroDirective *MD = getLocalMacroDirective(II);
1452 // If the macro is not defined, this is an error.
1454 Diag(MacroNameTok, diag::err_pp_visibility_non_macro) << II;
1458 // Note that this macro has now been exported.
1459 appendMacroDirective(II, AllocateVisibilityMacroDirective(
1460 MacroNameTok.getLocation(), /*IsPublic=*/true));
1463 /// Handle a #private directive.
1464 void Preprocessor::HandleMacroPrivateDirective() {
1466 ReadMacroName(MacroNameTok, MU_Undef);
1468 // Error reading macro name? If so, diagnostic already issued.
1469 if (MacroNameTok.is(tok::eod))
1472 // Check to see if this is the last token on the #__private_macro line.
1473 CheckEndOfDirective("__private_macro");
1475 IdentifierInfo *II = MacroNameTok.getIdentifierInfo();
1476 // Okay, we finally have a valid identifier to undef.
1477 MacroDirective *MD = getLocalMacroDirective(II);
1479 // If the macro is not defined, this is an error.
1481 Diag(MacroNameTok, diag::err_pp_visibility_non_macro) << II;
1485 // Note that this macro has now been marked private.
1486 appendMacroDirective(II, AllocateVisibilityMacroDirective(
1487 MacroNameTok.getLocation(), /*IsPublic=*/false));
1490 //===----------------------------------------------------------------------===//
1491 // Preprocessor Include Directive Handling.
1492 //===----------------------------------------------------------------------===//
1494 /// GetIncludeFilenameSpelling - Turn the specified lexer token into a fully
1495 /// checked and spelled filename, e.g. as an operand of \#include. This returns
1496 /// true if the input filename was in <>'s or false if it were in ""'s. The
1497 /// caller is expected to provide a buffer that is large enough to hold the
1498 /// spelling of the filename, but is also expected to handle the case when
1499 /// this method decides to use a different buffer.
1500 bool Preprocessor::GetIncludeFilenameSpelling(SourceLocation Loc,
1501 StringRef &Buffer) {
1502 // Get the text form of the filename.
1503 assert(!Buffer.empty() && "Can't have tokens with empty spellings!");
1505 // Make sure the filename is <x> or "x".
1507 if (Buffer[0] == '<') {
1508 if (Buffer.back() != '>') {
1509 Diag(Loc, diag::err_pp_expects_filename);
1510 Buffer = StringRef();
1514 } else if (Buffer[0] == '"') {
1515 if (Buffer.back() != '"') {
1516 Diag(Loc, diag::err_pp_expects_filename);
1517 Buffer = StringRef();
1522 Diag(Loc, diag::err_pp_expects_filename);
1523 Buffer = StringRef();
1527 // Diagnose #include "" as invalid.
1528 if (Buffer.size() <= 2) {
1529 Diag(Loc, diag::err_pp_empty_filename);
1530 Buffer = StringRef();
1534 // Skip the brackets.
1535 Buffer = Buffer.substr(1, Buffer.size()-2);
1539 // Handle cases where the \#include name is expanded from a macro
1540 // as multiple tokens, which need to be glued together.
1542 // This occurs for code like:
1544 // \#define FOO <a/b.h>
1547 // because in this case, "<a/b.h>" is returned as 7 tokens, not one.
1549 // This code concatenates and consumes tokens up to the '>' token. It returns
1550 // false if the > was found, otherwise it returns true if it finds and consumes
1552 bool Preprocessor::ConcatenateIncludeName(SmallString<128> &FilenameBuffer,
1553 SourceLocation &End) {
1557 while (CurTok.isNot(tok::eod)) {
1558 End = CurTok.getLocation();
1560 // FIXME: Provide code completion for #includes.
1561 if (CurTok.is(tok::code_completion)) {
1562 setCodeCompletionReached();
1567 // Append the spelling of this token to the buffer. If there was a space
1568 // before it, add it now.
1569 if (CurTok.hasLeadingSpace())
1570 FilenameBuffer.push_back(' ');
1572 // Get the spelling of the token, directly into FilenameBuffer if possible.
1573 size_t PreAppendSize = FilenameBuffer.size();
1574 FilenameBuffer.resize(PreAppendSize+CurTok.getLength());
1576 const char *BufPtr = &FilenameBuffer[PreAppendSize];
1577 unsigned ActualLen = getSpelling(CurTok, BufPtr);
1579 // If the token was spelled somewhere else, copy it into FilenameBuffer.
1580 if (BufPtr != &FilenameBuffer[PreAppendSize])
1581 memcpy(&FilenameBuffer[PreAppendSize], BufPtr, ActualLen);
1583 // Resize FilenameBuffer to the correct size.
1584 if (CurTok.getLength() != ActualLen)
1585 FilenameBuffer.resize(PreAppendSize+ActualLen);
1587 // If we found the '>' marker, return success.
1588 if (CurTok.is(tok::greater))
1594 // If we hit the eod marker, emit an error and return true so that the caller
1595 // knows the EOD has been read.
1596 Diag(CurTok.getLocation(), diag::err_pp_expects_filename);
1600 /// Push a token onto the token stream containing an annotation.
1601 void Preprocessor::EnterAnnotationToken(SourceRange Range,
1602 tok::TokenKind Kind,
1603 void *AnnotationVal) {
1604 // FIXME: Produce this as the current token directly, rather than
1605 // allocating a new token for it.
1606 auto Tok = llvm::make_unique<Token[]>(1);
1607 Tok[0].startToken();
1608 Tok[0].setKind(Kind);
1609 Tok[0].setLocation(Range.getBegin());
1610 Tok[0].setAnnotationEndLoc(Range.getEnd());
1611 Tok[0].setAnnotationValue(AnnotationVal);
1612 EnterTokenStream(std::move(Tok), 1, true);
1615 /// Produce a diagnostic informing the user that a #include or similar
1616 /// was implicitly treated as a module import.
1617 static void diagnoseAutoModuleImport(
1618 Preprocessor &PP, SourceLocation HashLoc, Token &IncludeTok,
1619 ArrayRef<std::pair<IdentifierInfo *, SourceLocation>> Path,
1620 SourceLocation PathEnd) {
1621 assert(PP.getLangOpts().ObjC2 && "no import syntax available");
1623 SmallString<128> PathString;
1624 for (size_t I = 0, N = Path.size(); I != N; ++I) {
1627 PathString += Path[I].first->getName();
1629 int IncludeKind = 0;
1631 switch (IncludeTok.getIdentifierInfo()->getPPKeywordID()) {
1632 case tok::pp_include:
1636 case tok::pp_import:
1640 case tok::pp_include_next:
1644 case tok::pp___include_macros:
1649 llvm_unreachable("unknown include directive kind");
1652 CharSourceRange ReplaceRange(SourceRange(HashLoc, PathEnd),
1653 /*IsTokenRange=*/false);
1654 PP.Diag(HashLoc, diag::warn_auto_module_import)
1655 << IncludeKind << PathString
1656 << FixItHint::CreateReplacement(ReplaceRange,
1657 ("@import " + PathString + ";").str());
1660 // Given a vector of path components and a string containing the real
1661 // path to the file, build a properly-cased replacement in the vector,
1662 // and return true if the replacement should be suggested.
1663 static bool trySimplifyPath(SmallVectorImpl<StringRef> &Components,
1664 StringRef RealPathName) {
1665 auto RealPathComponentIter = llvm::sys::path::rbegin(RealPathName);
1666 auto RealPathComponentEnd = llvm::sys::path::rend(RealPathName);
1668 bool SuggestReplacement = false;
1669 // Below is a best-effort to handle ".." in paths. It is admittedly
1670 // not 100% correct in the presence of symlinks.
1671 for (auto &Component : llvm::reverse(Components)) {
1672 if ("." == Component) {
1673 } else if (".." == Component) {
1677 } else if (RealPathComponentIter != RealPathComponentEnd) {
1678 if (Component != *RealPathComponentIter) {
1679 // If these path components differ by more than just case, then we
1680 // may be looking at symlinked paths. Bail on this diagnostic to avoid
1681 // noisy false positives.
1682 SuggestReplacement = RealPathComponentIter->equals_lower(Component);
1683 if (!SuggestReplacement)
1685 Component = *RealPathComponentIter;
1687 ++RealPathComponentIter;
1690 return SuggestReplacement;
1693 bool Preprocessor::checkModuleIsAvailable(const LangOptions &LangOpts,
1694 const TargetInfo &TargetInfo,
1695 DiagnosticsEngine &Diags, Module *M) {
1696 Module::Requirement Requirement;
1697 Module::UnresolvedHeaderDirective MissingHeader;
1698 Module *ShadowingModule = nullptr;
1699 if (M->isAvailable(LangOpts, TargetInfo, Requirement, MissingHeader,
1703 if (MissingHeader.FileNameLoc.isValid()) {
1704 Diags.Report(MissingHeader.FileNameLoc, diag::err_module_header_missing)
1705 << MissingHeader.IsUmbrella << MissingHeader.FileName;
1706 } else if (ShadowingModule) {
1707 Diags.Report(M->DefinitionLoc, diag::err_module_shadowed) << M->Name;
1708 Diags.Report(ShadowingModule->DefinitionLoc,
1709 diag::note_previous_definition);
1711 // FIXME: Track the location at which the requirement was specified, and
1713 Diags.Report(M->DefinitionLoc, diag::err_module_unavailable)
1714 << M->getFullModuleName() << Requirement.second << Requirement.first;
1719 /// HandleIncludeDirective - The "\#include" tokens have just been read, read
1720 /// the file to be included from the lexer, then include it! This is a common
1721 /// routine with functionality shared between \#include, \#include_next and
1722 /// \#import. LookupFrom is set when this is a \#include_next directive, it
1723 /// specifies the file to start searching from.
1724 void Preprocessor::HandleIncludeDirective(SourceLocation HashLoc,
1726 const DirectoryLookup *LookupFrom,
1727 const FileEntry *LookupFromFile,
1730 CurPPLexer->LexIncludeFilename(FilenameTok);
1732 // Reserve a buffer to get the spelling.
1733 SmallString<128> FilenameBuffer;
1736 SourceLocation CharEnd; // the end of this directive, in characters
1738 switch (FilenameTok.getKind()) {
1740 // If the token kind is EOD, the error has already been diagnosed.
1743 case tok::angle_string_literal:
1744 case tok::string_literal:
1745 Filename = getSpelling(FilenameTok, FilenameBuffer);
1746 End = FilenameTok.getLocation();
1747 CharEnd = End.getLocWithOffset(FilenameTok.getLength());
1751 // This could be a <foo/bar.h> file coming from a macro expansion. In this
1752 // case, glue the tokens together into FilenameBuffer and interpret those.
1753 FilenameBuffer.push_back('<');
1754 if (ConcatenateIncludeName(FilenameBuffer, End))
1755 return; // Found <eod> but no ">"? Diagnostic already emitted.
1756 Filename = FilenameBuffer;
1757 CharEnd = End.getLocWithOffset(1);
1760 Diag(FilenameTok.getLocation(), diag::err_pp_expects_filename);
1761 DiscardUntilEndOfDirective();
1765 CharSourceRange FilenameRange
1766 = CharSourceRange::getCharRange(FilenameTok.getLocation(), CharEnd);
1767 StringRef OriginalFilename = Filename;
1769 GetIncludeFilenameSpelling(FilenameTok.getLocation(), Filename);
1770 // If GetIncludeFilenameSpelling set the start ptr to null, there was an
1772 if (Filename.empty()) {
1773 DiscardUntilEndOfDirective();
1777 // Verify that there is nothing after the filename, other than EOD. Note that
1778 // we allow macros that expand to nothing after the filename, because this
1779 // falls into the category of "#include pp-tokens new-line" specified in
1781 CheckEndOfDirective(IncludeTok.getIdentifierInfo()->getNameStart(), true);
1783 // Check that we don't have infinite #include recursion.
1784 if (IncludeMacroStack.size() == MaxAllowedIncludeStackDepth-1) {
1785 Diag(FilenameTok, diag::err_pp_include_too_deep);
1789 // Complain about attempts to #include files in an audit pragma.
1790 if (PragmaARCCFCodeAuditedLoc.isValid()) {
1791 Diag(HashLoc, diag::err_pp_include_in_arc_cf_code_audited);
1792 Diag(PragmaARCCFCodeAuditedLoc, diag::note_pragma_entered_here);
1794 // Immediately leave the pragma.
1795 PragmaARCCFCodeAuditedLoc = SourceLocation();
1798 // Complain about attempts to #include files in an assume-nonnull pragma.
1799 if (PragmaAssumeNonNullLoc.isValid()) {
1800 Diag(HashLoc, diag::err_pp_include_in_assume_nonnull);
1801 Diag(PragmaAssumeNonNullLoc, diag::note_pragma_entered_here);
1803 // Immediately leave the pragma.
1804 PragmaAssumeNonNullLoc = SourceLocation();
1807 if (HeaderInfo.HasIncludeAliasMap()) {
1808 // Map the filename with the brackets still attached. If the name doesn't
1809 // map to anything, fall back on the filename we've already gotten the
1811 StringRef NewName = HeaderInfo.MapHeaderToIncludeAlias(OriginalFilename);
1812 if (!NewName.empty())
1816 // Search include directories.
1817 bool IsMapped = false;
1818 const DirectoryLookup *CurDir;
1819 SmallString<1024> SearchPath;
1820 SmallString<1024> RelativePath;
1821 // We get the raw path only if we have 'Callbacks' to which we later pass
1823 ModuleMap::KnownHeader SuggestedModule;
1824 SourceLocation FilenameLoc = FilenameTok.getLocation();
1825 SmallString<128> NormalizedPath;
1826 if (LangOpts.MSVCCompat) {
1827 NormalizedPath = Filename.str();
1829 llvm::sys::path::native(NormalizedPath);
1832 const FileEntry *File = LookupFile(
1833 FilenameLoc, LangOpts.MSVCCompat ? NormalizedPath.c_str() : Filename,
1834 isAngled, LookupFrom, LookupFromFile, CurDir,
1835 Callbacks ? &SearchPath : nullptr, Callbacks ? &RelativePath : nullptr,
1836 &SuggestedModule, &IsMapped);
1840 // Give the clients a chance to recover.
1841 SmallString<128> RecoveryPath;
1842 if (Callbacks->FileNotFound(Filename, RecoveryPath)) {
1843 if (const DirectoryEntry *DE = FileMgr.getDirectory(RecoveryPath)) {
1844 // Add the recovery path to the list of search paths.
1845 DirectoryLookup DL(DE, SrcMgr::C_User, false);
1846 HeaderInfo.AddSearchPath(DL, isAngled);
1848 // Try the lookup again, skipping the cache.
1851 LangOpts.MSVCCompat ? NormalizedPath.c_str() : Filename, isAngled,
1852 LookupFrom, LookupFromFile, CurDir, nullptr, nullptr,
1853 &SuggestedModule, &IsMapped, /*SkipCache*/ true);
1858 if (!SuppressIncludeNotFoundError) {
1859 // If the file could not be located and it was included via angle
1860 // brackets, we can attempt a lookup as though it were a quoted path to
1861 // provide the user with a possible fixit.
1865 LangOpts.MSVCCompat ? NormalizedPath.c_str() : Filename, false,
1866 LookupFrom, LookupFromFile, CurDir,
1867 Callbacks ? &SearchPath : nullptr,
1868 Callbacks ? &RelativePath : nullptr, &SuggestedModule, &IsMapped);
1870 SourceRange Range(FilenameTok.getLocation(), CharEnd);
1871 Diag(FilenameTok, diag::err_pp_file_not_found_not_fatal) <<
1873 FixItHint::CreateReplacement(Range, "\"" + Filename.str() + "\"");
1877 // If the file is still not found, just go with the vanilla diagnostic
1879 Diag(FilenameTok, diag::err_pp_file_not_found) << Filename
1884 if (usingPCHWithThroughHeader() && SkippingUntilPCHThroughHeader) {
1885 if (isPCHThroughHeader(File))
1886 SkippingUntilPCHThroughHeader = false;
1890 // Should we enter the source file? Set to false if either the source file is
1891 // known to have no effect beyond its effect on module visibility -- that is,
1892 // if it's got an include guard that is already defined or is a modular header
1893 // we've imported or already built.
1894 bool ShouldEnter = true;
1896 if (PPOpts->SingleFileParseMode)
1897 ShouldEnter = false;
1899 // Any diagnostics after the fatal error will not be visible. As the
1900 // compilation failed already and errors in subsequently included files won't
1901 // be visible, avoid preprocessing those files.
1902 if (ShouldEnter && Diags->hasFatalErrorOccurred())
1903 ShouldEnter = false;
1905 // Determine whether we should try to import the module for this #include, if
1906 // there is one. Don't do so if precompiled module support is disabled or we
1907 // are processing this module textually (because we're building the module).
1908 if (ShouldEnter && File && SuggestedModule && getLangOpts().Modules &&
1909 !isForModuleBuilding(SuggestedModule.getModule(),
1910 getLangOpts().CurrentModule,
1911 getLangOpts().ModuleName)) {
1912 // If this include corresponds to a module but that module is
1913 // unavailable, diagnose the situation and bail out.
1914 // FIXME: Remove this; loadModule does the same check (but produces
1915 // slightly worse diagnostics).
1916 if (checkModuleIsAvailable(getLangOpts(), getTargetInfo(), getDiagnostics(),
1917 SuggestedModule.getModule())) {
1918 Diag(FilenameTok.getLocation(),
1919 diag::note_implicit_top_level_module_import_here)
1920 << SuggestedModule.getModule()->getTopLevelModuleName();
1924 // Compute the module access path corresponding to this module.
1925 // FIXME: Should we have a second loadModule() overload to avoid this
1926 // extra lookup step?
1927 SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 2> Path;
1928 for (Module *Mod = SuggestedModule.getModule(); Mod; Mod = Mod->Parent)
1929 Path.push_back(std::make_pair(getIdentifierInfo(Mod->Name),
1930 FilenameTok.getLocation()));
1931 std::reverse(Path.begin(), Path.end());
1933 // Warn that we're replacing the include/import with a module import.
1934 // We only do this in Objective-C, where we have a module-import syntax.
1935 if (getLangOpts().ObjC2)
1936 diagnoseAutoModuleImport(*this, HashLoc, IncludeTok, Path, CharEnd);
1938 // Load the module to import its macros. We'll make the declarations
1939 // visible when the parser gets here.
1940 // FIXME: Pass SuggestedModule in here rather than converting it to a path
1941 // and making the module loader convert it back again.
1942 ModuleLoadResult Imported = TheModuleLoader.loadModule(
1943 IncludeTok.getLocation(), Path, Module::Hidden,
1944 /*IsIncludeDirective=*/true);
1945 assert((Imported == nullptr || Imported == SuggestedModule.getModule()) &&
1946 "the imported module is different than the suggested one");
1949 ShouldEnter = false;
1950 else if (Imported.isMissingExpected()) {
1951 // We failed to find a submodule that we assumed would exist (because it
1952 // was in the directory of an umbrella header, for instance), but no
1953 // actual module containing it exists (because the umbrella header is
1954 // incomplete). Treat this as a textual inclusion.
1955 SuggestedModule = ModuleMap::KnownHeader();
1956 } else if (Imported.isConfigMismatch()) {
1957 // On a configuration mismatch, enter the header textually. We still know
1958 // that it's part of the corresponding module.
1960 // We hit an error processing the import. Bail out.
1961 if (hadModuleLoaderFatalFailure()) {
1962 // With a fatal failure in the module loader, we abort parsing.
1963 Token &Result = IncludeTok;
1965 Result.startToken();
1966 CurLexer->FormTokenWithChars(Result, CurLexer->BufferEnd, tok::eof);
1967 CurLexer->cutOffLexing();
1969 assert(CurPTHLexer && "#include but no current lexer set!");
1970 CurPTHLexer->getEOF(Result);
1977 // The #included file will be considered to be a system header if either it is
1978 // in a system include directory, or if the #includer is a system include
1980 SrcMgr::CharacteristicKind FileCharacter =
1981 SourceMgr.getFileCharacteristic(FilenameTok.getLocation());
1983 FileCharacter = std::max(HeaderInfo.getFileDirFlavor(File), FileCharacter);
1985 // Ask HeaderInfo if we should enter this #include file. If not, #including
1986 // this file will have no effect.
1987 bool SkipHeader = false;
1988 if (ShouldEnter && File &&
1989 !HeaderInfo.ShouldEnterIncludeFile(*this, File, isImport,
1990 getLangOpts().Modules,
1991 SuggestedModule.getModule())) {
1992 ShouldEnter = false;
1997 // Notify the callback object that we've seen an inclusion directive.
1998 Callbacks->InclusionDirective(
1999 HashLoc, IncludeTok,
2000 LangOpts.MSVCCompat ? NormalizedPath.c_str() : Filename, isAngled,
2001 FilenameRange, File, SearchPath, RelativePath,
2002 ShouldEnter ? nullptr : SuggestedModule.getModule(), FileCharacter);
2003 if (SkipHeader && !SuggestedModule.getModule())
2004 Callbacks->FileSkipped(*File, FilenameTok, FileCharacter);
2010 // FIXME: If we have a suggested module, and we've already visited this file,
2011 // don't bother entering it again. We know it has no further effect.
2013 // Issue a diagnostic if the name of the file on disk has a different case
2014 // than the one we're about to open.
2015 const bool CheckIncludePathPortability =
2016 !IsMapped && File && !File->tryGetRealPathName().empty();
2018 if (CheckIncludePathPortability) {
2019 StringRef Name = LangOpts.MSVCCompat ? NormalizedPath.str() : Filename;
2020 StringRef RealPathName = File->tryGetRealPathName();
2021 SmallVector<StringRef, 16> Components(llvm::sys::path::begin(Name),
2022 llvm::sys::path::end(Name));
2024 if (trySimplifyPath(Components, RealPathName)) {
2025 SmallString<128> Path;
2026 Path.reserve(Name.size()+2);
2027 Path.push_back(isAngled ? '<' : '"');
2028 bool isLeadingSeparator = llvm::sys::path::is_absolute(Name);
2029 for (auto Component : Components) {
2030 if (isLeadingSeparator)
2031 isLeadingSeparator = false;
2033 Path.append(Component);
2034 // Append the separator the user used, or the close quote
2036 Path.size() <= Filename.size() ? Filename[Path.size()-1] :
2037 (isAngled ? '>' : '"'));
2039 // For user files and known standard headers, by default we issue a diagnostic.
2040 // For other system headers, we don't. They can be controlled separately.
2041 auto DiagId = (FileCharacter == SrcMgr::C_User || warnByDefaultOnWrongCase(Name)) ?
2042 diag::pp_nonportable_path : diag::pp_nonportable_system_path;
2043 SourceRange Range(FilenameTok.getLocation(), CharEnd);
2044 Diag(FilenameTok, DiagId) << Path <<
2045 FixItHint::CreateReplacement(Range, Path);
2049 // If we don't need to enter the file, stop now.
2051 // If this is a module import, make it visible if needed.
2052 if (auto *M = SuggestedModule.getModule()) {
2053 // When building a pch, -fmodule-name tells the compiler to textually
2054 // include headers in the specified module. But it is possible that
2055 // ShouldEnter is false because we are skipping the header. In that
2056 // case, We are not importing the specified module.
2057 if (SkipHeader && getLangOpts().CompilingPCH &&
2058 isForModuleBuilding(M, getLangOpts().CurrentModule,
2059 getLangOpts().ModuleName))
2062 makeModuleVisible(M, HashLoc);
2064 if (IncludeTok.getIdentifierInfo()->getPPKeywordID() !=
2065 tok::pp___include_macros)
2066 EnterAnnotationToken(SourceRange(HashLoc, End),
2067 tok::annot_module_include, M);
2072 // Look up the file, create a File ID for it.
2073 SourceLocation IncludePos = End;
2074 // If the filename string was the result of macro expansions, set the include
2075 // position on the file where it will be included and after the expansions.
2076 if (IncludePos.isMacroID())
2077 IncludePos = SourceMgr.getExpansionRange(IncludePos).getEnd();
2078 FileID FID = SourceMgr.createFileID(File, IncludePos, FileCharacter);
2079 assert(FID.isValid() && "Expected valid file ID");
2081 // If all is good, enter the new file!
2082 if (EnterSourceFile(FID, CurDir, FilenameTok.getLocation()))
2085 // Determine if we're switching to building a new submodule, and which one.
2086 if (auto *M = SuggestedModule.getModule()) {
2087 if (M->getTopLevelModule()->ShadowingModule) {
2088 // We are building a submodule that belongs to a shadowed module. This
2089 // means we find header files in the shadowed module.
2090 Diag(M->DefinitionLoc, diag::err_module_build_shadowed_submodule)
2091 << M->getFullModuleName();
2092 Diag(M->getTopLevelModule()->ShadowingModule->DefinitionLoc,
2093 diag::note_previous_definition);
2096 // When building a pch, -fmodule-name tells the compiler to textually
2097 // include headers in the specified module. We are not building the
2098 // specified module.
2099 if (getLangOpts().CompilingPCH &&
2100 isForModuleBuilding(M, getLangOpts().CurrentModule,
2101 getLangOpts().ModuleName))
2104 assert(!CurLexerSubmodule && "should not have marked this as a module yet");
2105 CurLexerSubmodule = M;
2107 // Let the macro handling code know that any future macros are within
2108 // the new submodule.
2109 EnterSubmodule(M, HashLoc, /*ForPragma*/false);
2111 // Let the parser know that any future declarations are within the new
2113 // FIXME: There's no point doing this if we're handling a #__include_macros
2115 EnterAnnotationToken(SourceRange(HashLoc, End), tok::annot_module_begin, M);
2119 /// HandleIncludeNextDirective - Implements \#include_next.
2121 void Preprocessor::HandleIncludeNextDirective(SourceLocation HashLoc,
2122 Token &IncludeNextTok) {
2123 Diag(IncludeNextTok, diag::ext_pp_include_next_directive);
2125 // #include_next is like #include, except that we start searching after
2126 // the current found directory. If we can't do this, issue a
2128 const DirectoryLookup *Lookup = CurDirLookup;
2129 const FileEntry *LookupFromFile = nullptr;
2130 if (isInPrimaryFile() && LangOpts.IsHeaderFile) {
2131 // If the main file is a header, then it's either for PCH/AST generation,
2132 // or libclang opened it. Either way, handle it as a normal include below
2133 // and do not complain about include_next.
2134 } else if (isInPrimaryFile()) {
2136 Diag(IncludeNextTok, diag::pp_include_next_in_primary);
2137 } else if (CurLexerSubmodule) {
2138 // Start looking up in the directory *after* the one in which the current
2139 // file would be found, if any.
2140 assert(CurPPLexer && "#include_next directive in macro?");
2141 LookupFromFile = CurPPLexer->getFileEntry();
2143 } else if (!Lookup) {
2144 Diag(IncludeNextTok, diag::pp_include_next_absolute_path);
2146 // Start looking up in the next directory.
2150 return HandleIncludeDirective(HashLoc, IncludeNextTok, Lookup,
2154 /// HandleMicrosoftImportDirective - Implements \#import for Microsoft Mode
2155 void Preprocessor::HandleMicrosoftImportDirective(Token &Tok) {
2156 // The Microsoft #import directive takes a type library and generates header
2157 // files from it, and includes those. This is beyond the scope of what clang
2158 // does, so we ignore it and error out. However, #import can optionally have
2159 // trailing attributes that span multiple lines. We're going to eat those
2160 // so we can continue processing from there.
2161 Diag(Tok, diag::err_pp_import_directive_ms );
2163 // Read tokens until we get to the end of the directive. Note that the
2164 // directive can be split over multiple lines using the backslash character.
2165 DiscardUntilEndOfDirective();
2168 /// HandleImportDirective - Implements \#import.
2170 void Preprocessor::HandleImportDirective(SourceLocation HashLoc,
2172 if (!LangOpts.ObjC1) { // #import is standard for ObjC.
2173 if (LangOpts.MSVCCompat)
2174 return HandleMicrosoftImportDirective(ImportTok);
2175 Diag(ImportTok, diag::ext_pp_import_directive);
2177 return HandleIncludeDirective(HashLoc, ImportTok, nullptr, nullptr, true);
2180 /// HandleIncludeMacrosDirective - The -imacros command line option turns into a
2181 /// pseudo directive in the predefines buffer. This handles it by sucking all
2182 /// tokens through the preprocessor and discarding them (only keeping the side
2183 /// effects on the preprocessor).
2184 void Preprocessor::HandleIncludeMacrosDirective(SourceLocation HashLoc,
2185 Token &IncludeMacrosTok) {
2186 // This directive should only occur in the predefines buffer. If not, emit an
2187 // error and reject it.
2188 SourceLocation Loc = IncludeMacrosTok.getLocation();
2189 if (SourceMgr.getBufferName(Loc) != "<built-in>") {
2190 Diag(IncludeMacrosTok.getLocation(),
2191 diag::pp_include_macros_out_of_predefines);
2192 DiscardUntilEndOfDirective();
2196 // Treat this as a normal #include for checking purposes. If this is
2197 // successful, it will push a new lexer onto the include stack.
2198 HandleIncludeDirective(HashLoc, IncludeMacrosTok);
2203 assert(TmpTok.isNot(tok::eof) && "Didn't find end of -imacros!");
2204 } while (TmpTok.isNot(tok::hashhash));
2207 //===----------------------------------------------------------------------===//
2208 // Preprocessor Macro Directive Handling.
2209 //===----------------------------------------------------------------------===//
2211 /// ReadMacroParameterList - The ( starting a parameter list of a macro
2212 /// definition has just been read. Lex the rest of the parameters and the
2213 /// closing ), updating MI with what we learn. Return true if an error occurs
2214 /// parsing the param list.
2215 bool Preprocessor::ReadMacroParameterList(MacroInfo *MI, Token &Tok) {
2216 SmallVector<IdentifierInfo*, 32> Parameters;
2219 LexUnexpandedToken(Tok);
2220 switch (Tok.getKind()) {
2222 // Found the end of the parameter list.
2223 if (Parameters.empty()) // #define FOO()
2225 // Otherwise we have #define FOO(A,)
2226 Diag(Tok, diag::err_pp_expected_ident_in_arg_list);
2228 case tok::ellipsis: // #define X(... -> C99 varargs
2230 Diag(Tok, LangOpts.CPlusPlus11 ?
2231 diag::warn_cxx98_compat_variadic_macro :
2232 diag::ext_variadic_macro);
2234 // OpenCL v1.2 s6.9.e: variadic macros are not supported.
2235 if (LangOpts.OpenCL) {
2236 Diag(Tok, diag::err_pp_opencl_variadic_macros);
2240 // Lex the token after the identifier.
2241 LexUnexpandedToken(Tok);
2242 if (Tok.isNot(tok::r_paren)) {
2243 Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
2246 // Add the __VA_ARGS__ identifier as a parameter.
2247 Parameters.push_back(Ident__VA_ARGS__);
2248 MI->setIsC99Varargs();
2249 MI->setParameterList(Parameters, BP);
2251 case tok::eod: // #define X(
2252 Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
2255 // Handle keywords and identifiers here to accept things like
2256 // #define Foo(for) for.
2257 IdentifierInfo *II = Tok.getIdentifierInfo();
2260 Diag(Tok, diag::err_pp_invalid_tok_in_arg_list);
2264 // If this is already used as a parameter, it is used multiple times (e.g.
2266 if (std::find(Parameters.begin(), Parameters.end(), II) !=
2267 Parameters.end()) { // C99 6.10.3p6
2268 Diag(Tok, diag::err_pp_duplicate_name_in_arg_list) << II;
2272 // Add the parameter to the macro info.
2273 Parameters.push_back(II);
2275 // Lex the token after the identifier.
2276 LexUnexpandedToken(Tok);
2278 switch (Tok.getKind()) {
2279 default: // #define X(A B
2280 Diag(Tok, diag::err_pp_expected_comma_in_arg_list);
2282 case tok::r_paren: // #define X(A)
2283 MI->setParameterList(Parameters, BP);
2285 case tok::comma: // #define X(A,
2287 case tok::ellipsis: // #define X(A... -> GCC extension
2288 // Diagnose extension.
2289 Diag(Tok, diag::ext_named_variadic_macro);
2291 // Lex the token after the identifier.
2292 LexUnexpandedToken(Tok);
2293 if (Tok.isNot(tok::r_paren)) {
2294 Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
2298 MI->setIsGNUVarargs();
2299 MI->setParameterList(Parameters, BP);
2306 static bool isConfigurationPattern(Token &MacroName, MacroInfo *MI,
2307 const LangOptions &LOptions) {
2308 if (MI->getNumTokens() == 1) {
2309 const Token &Value = MI->getReplacementToken(0);
2311 // Macro that is identity, like '#define inline inline' is a valid pattern.
2312 if (MacroName.getKind() == Value.getKind())
2315 // Macro that maps a keyword to the same keyword decorated with leading/
2316 // trailing underscores is a valid pattern:
2317 // #define inline __inline
2318 // #define inline __inline__
2319 // #define inline _inline (in MS compatibility mode)
2320 StringRef MacroText = MacroName.getIdentifierInfo()->getName();
2321 if (IdentifierInfo *II = Value.getIdentifierInfo()) {
2322 if (!II->isKeyword(LOptions))
2324 StringRef ValueText = II->getName();
2325 StringRef TrimmedValue = ValueText;
2326 if (!ValueText.startswith("__")) {
2327 if (ValueText.startswith("_"))
2328 TrimmedValue = TrimmedValue.drop_front(1);
2332 TrimmedValue = TrimmedValue.drop_front(2);
2333 if (TrimmedValue.endswith("__"))
2334 TrimmedValue = TrimmedValue.drop_back(2);
2336 return TrimmedValue.equals(MacroText);
2343 return MacroName.isOneOf(tok::kw_extern, tok::kw_inline, tok::kw_static,
2345 MI->getNumTokens() == 0;
2348 // ReadOptionalMacroParameterListAndBody - This consumes all (i.e. the
2349 // entire line) of the macro's tokens and adds them to MacroInfo, and while
2350 // doing so performs certain validity checks including (but not limited to):
2351 // - # (stringization) is followed by a macro parameter
2353 // Returns a nullptr if an invalid sequence of tokens is encountered or returns
2354 // a pointer to a MacroInfo object.
2356 MacroInfo *Preprocessor::ReadOptionalMacroParameterListAndBody(
2357 const Token &MacroNameTok, const bool ImmediatelyAfterHeaderGuard) {
2359 Token LastTok = MacroNameTok;
2360 // Create the new macro.
2361 MacroInfo *const MI = AllocateMacroInfo(MacroNameTok.getLocation());
2364 LexUnexpandedToken(Tok);
2366 // Used to un-poison and then re-poison identifiers of the __VA_ARGS__ ilk
2367 // within their appropriate context.
2368 VariadicMacroScopeGuard VariadicMacroScopeGuard(*this);
2370 // If this is a function-like macro definition, parse the argument list,
2371 // marking each of the identifiers as being used as macro arguments. Also,
2372 // check other constraints on the first token of the macro body.
2373 if (Tok.is(tok::eod)) {
2374 if (ImmediatelyAfterHeaderGuard) {
2375 // Save this macro information since it may part of a header guard.
2376 CurPPLexer->MIOpt.SetDefinedMacro(MacroNameTok.getIdentifierInfo(),
2377 MacroNameTok.getLocation());
2379 // If there is no body to this macro, we have no special handling here.
2380 } else if (Tok.hasLeadingSpace()) {
2381 // This is a normal token with leading space. Clear the leading space
2382 // marker on the first token to get proper expansion.
2383 Tok.clearFlag(Token::LeadingSpace);
2384 } else if (Tok.is(tok::l_paren)) {
2385 // This is a function-like macro definition. Read the argument list.
2386 MI->setIsFunctionLike();
2387 if (ReadMacroParameterList(MI, LastTok)) {
2388 // Throw away the rest of the line.
2389 if (CurPPLexer->ParsingPreprocessorDirective)
2390 DiscardUntilEndOfDirective();
2394 // If this is a definition of an ISO C/C++ variadic function-like macro (not
2395 // using the GNU named varargs extension) inform our variadic scope guard
2396 // which un-poisons and re-poisons certain identifiers (e.g. __VA_ARGS__)
2397 // allowed only within the definition of a variadic macro.
2399 if (MI->isC99Varargs()) {
2400 VariadicMacroScopeGuard.enterScope();
2403 // Read the first token after the arg list for down below.
2404 LexUnexpandedToken(Tok);
2405 } else if (LangOpts.C99 || LangOpts.CPlusPlus11) {
2406 // C99 requires whitespace between the macro definition and the body. Emit
2407 // a diagnostic for something like "#define X+".
2408 Diag(Tok, diag::ext_c99_whitespace_required_after_macro_name);
2410 // C90 6.8 TC1 says: "In the definition of an object-like macro, if the
2411 // first character of a replacement list is not a character required by
2412 // subclause 5.2.1, then there shall be white-space separation between the
2413 // identifier and the replacement list.". 5.2.1 lists this set:
2414 // "A-Za-z0-9!"#%&'()*+,_./:;<=>?[\]^_{|}~" as well as whitespace, which
2415 // is irrelevant here.
2416 bool isInvalid = false;
2417 if (Tok.is(tok::at)) // @ is not in the list above.
2419 else if (Tok.is(tok::unknown)) {
2420 // If we have an unknown token, it is something strange like "`". Since
2421 // all of valid characters would have lexed into a single character
2422 // token of some sort, we know this is not a valid case.
2426 Diag(Tok, diag::ext_missing_whitespace_after_macro_name);
2428 Diag(Tok, diag::warn_missing_whitespace_after_macro_name);
2431 if (!Tok.is(tok::eod))
2434 // Read the rest of the macro body.
2435 if (MI->isObjectLike()) {
2436 // Object-like macros are very simple, just read their body.
2437 while (Tok.isNot(tok::eod)) {
2439 MI->AddTokenToBody(Tok);
2440 // Get the next token of the macro.
2441 LexUnexpandedToken(Tok);
2444 // Otherwise, read the body of a function-like macro. While we are at it,
2445 // check C99 6.10.3.2p1: ensure that # operators are followed by macro
2446 // parameters in function-like macro expansions.
2448 VAOptDefinitionContext VAOCtx(*this);
2450 while (Tok.isNot(tok::eod)) {
2453 if (!Tok.isOneOf(tok::hash, tok::hashat, tok::hashhash)) {
2454 MI->AddTokenToBody(Tok);
2456 if (VAOCtx.isVAOptToken(Tok)) {
2457 // If we're already within a VAOPT, emit an error.
2458 if (VAOCtx.isInVAOpt()) {
2459 Diag(Tok, diag::err_pp_vaopt_nested_use);
2462 // Ensure VAOPT is followed by a '(' .
2463 LexUnexpandedToken(Tok);
2464 if (Tok.isNot(tok::l_paren)) {
2465 Diag(Tok, diag::err_pp_missing_lparen_in_vaopt_use);
2468 MI->AddTokenToBody(Tok);
2469 VAOCtx.sawVAOptFollowedByOpeningParens(Tok.getLocation());
2470 LexUnexpandedToken(Tok);
2471 if (Tok.is(tok::hashhash)) {
2472 Diag(Tok, diag::err_vaopt_paste_at_start);
2476 } else if (VAOCtx.isInVAOpt()) {
2477 if (Tok.is(tok::r_paren)) {
2478 if (VAOCtx.sawClosingParen()) {
2479 const unsigned NumTokens = MI->getNumTokens();
2480 assert(NumTokens >= 3 && "Must have seen at least __VA_OPT__( "
2481 "and a subsequent tok::r_paren");
2482 if (MI->getReplacementToken(NumTokens - 2).is(tok::hashhash)) {
2483 Diag(Tok, diag::err_vaopt_paste_at_end);
2487 } else if (Tok.is(tok::l_paren)) {
2488 VAOCtx.sawOpeningParen(Tok.getLocation());
2491 // Get the next token of the macro.
2492 LexUnexpandedToken(Tok);
2496 // If we're in -traditional mode, then we should ignore stringification
2497 // and token pasting. Mark the tokens as unknown so as not to confuse
2499 if (getLangOpts().TraditionalCPP) {
2500 Tok.setKind(tok::unknown);
2501 MI->AddTokenToBody(Tok);
2503 // Get the next token of the macro.
2504 LexUnexpandedToken(Tok);
2508 if (Tok.is(tok::hashhash)) {
2509 // If we see token pasting, check if it looks like the gcc comma
2510 // pasting extension. We'll use this information to suppress
2511 // diagnostics later on.
2513 // Get the next token of the macro.
2514 LexUnexpandedToken(Tok);
2516 if (Tok.is(tok::eod)) {
2517 MI->AddTokenToBody(LastTok);
2521 unsigned NumTokens = MI->getNumTokens();
2522 if (NumTokens && Tok.getIdentifierInfo() == Ident__VA_ARGS__ &&
2523 MI->getReplacementToken(NumTokens-1).is(tok::comma))
2524 MI->setHasCommaPasting();
2526 // Things look ok, add the '##' token to the macro.
2527 MI->AddTokenToBody(LastTok);
2531 // Our Token is a stringization operator.
2532 // Get the next token of the macro.
2533 LexUnexpandedToken(Tok);
2535 // Check for a valid macro arg identifier or __VA_OPT__.
2536 if (!VAOCtx.isVAOptToken(Tok) &&
2537 (Tok.getIdentifierInfo() == nullptr ||
2538 MI->getParameterNum(Tok.getIdentifierInfo()) == -1)) {
2540 // If this is assembler-with-cpp mode, we accept random gibberish after
2541 // the '#' because '#' is often a comment character. However, change
2542 // the kind of the token to tok::unknown so that the preprocessor isn't
2544 if (getLangOpts().AsmPreprocessor && Tok.isNot(tok::eod)) {
2545 LastTok.setKind(tok::unknown);
2546 MI->AddTokenToBody(LastTok);
2549 Diag(Tok, diag::err_pp_stringize_not_parameter)
2550 << LastTok.is(tok::hashat);
2555 // Things look ok, add the '#' and param name tokens to the macro.
2556 MI->AddTokenToBody(LastTok);
2558 // If the token following '#' is VAOPT, let the next iteration handle it
2559 // and check it for correctness, otherwise add the token and prime the
2560 // loop with the next one.
2561 if (!VAOCtx.isVAOptToken(Tok)) {
2562 MI->AddTokenToBody(Tok);
2565 // Get the next token of the macro.
2566 LexUnexpandedToken(Tok);
2569 if (VAOCtx.isInVAOpt()) {
2570 assert(Tok.is(tok::eod) && "Must be at End Of preprocessing Directive");
2571 Diag(Tok, diag::err_pp_expected_after)
2572 << LastTok.getKind() << tok::r_paren;
2573 Diag(VAOCtx.getUnmatchedOpeningParenLoc(), diag::note_matching) << tok::l_paren;
2577 MI->setDefinitionEndLoc(LastTok.getLocation());
2580 /// HandleDefineDirective - Implements \#define. This consumes the entire macro
2581 /// line then lets the caller lex the next real token.
2582 void Preprocessor::HandleDefineDirective(
2583 Token &DefineTok, const bool ImmediatelyAfterHeaderGuard) {
2587 bool MacroShadowsKeyword;
2588 ReadMacroName(MacroNameTok, MU_Define, &MacroShadowsKeyword);
2590 // Error reading macro name? If so, diagnostic already issued.
2591 if (MacroNameTok.is(tok::eod))
2594 // If we are supposed to keep comments in #defines, reenable comment saving
2596 if (CurLexer) CurLexer->SetCommentRetentionState(KeepMacroComments);
2598 MacroInfo *const MI = ReadOptionalMacroParameterListAndBody(
2599 MacroNameTok, ImmediatelyAfterHeaderGuard);
2603 if (MacroShadowsKeyword &&
2604 !isConfigurationPattern(MacroNameTok, MI, getLangOpts())) {
2605 Diag(MacroNameTok, diag::warn_pp_macro_hides_keyword);
2607 // Check that there is no paste (##) operator at the beginning or end of the
2608 // replacement list.
2609 unsigned NumTokens = MI->getNumTokens();
2610 if (NumTokens != 0) {
2611 if (MI->getReplacementToken(0).is(tok::hashhash)) {
2612 Diag(MI->getReplacementToken(0), diag::err_paste_at_start);
2615 if (MI->getReplacementToken(NumTokens-1).is(tok::hashhash)) {
2616 Diag(MI->getReplacementToken(NumTokens-1), diag::err_paste_at_end);
2621 // When skipping just warn about macros that do not match.
2622 if (SkippingUntilPCHThroughHeader) {
2623 const MacroInfo *OtherMI = getMacroInfo(MacroNameTok.getIdentifierInfo());
2624 if (!OtherMI || !MI->isIdenticalTo(*OtherMI, *this,
2625 /*Syntactic=*/LangOpts.MicrosoftExt))
2626 Diag(MI->getDefinitionLoc(), diag::warn_pp_macro_def_mismatch_with_pch)
2627 << MacroNameTok.getIdentifierInfo();
2631 // Finally, if this identifier already had a macro defined for it, verify that
2632 // the macro bodies are identical, and issue diagnostics if they are not.
2633 if (const MacroInfo *OtherMI=getMacroInfo(MacroNameTok.getIdentifierInfo())) {
2634 // In Objective-C, ignore attempts to directly redefine the builtin
2635 // definitions of the ownership qualifiers. It's still possible to
2637 auto isObjCProtectedMacro = [](const IdentifierInfo *II) -> bool {
2638 return II->isStr("__strong") ||
2639 II->isStr("__weak") ||
2640 II->isStr("__unsafe_unretained") ||
2641 II->isStr("__autoreleasing");
2643 if (getLangOpts().ObjC1 &&
2644 SourceMgr.getFileID(OtherMI->getDefinitionLoc())
2645 == getPredefinesFileID() &&
2646 isObjCProtectedMacro(MacroNameTok.getIdentifierInfo())) {
2647 // Warn if it changes the tokens.
2648 if ((!getDiagnostics().getSuppressSystemWarnings() ||
2649 !SourceMgr.isInSystemHeader(DefineTok.getLocation())) &&
2650 !MI->isIdenticalTo(*OtherMI, *this,
2651 /*Syntactic=*/LangOpts.MicrosoftExt)) {
2652 Diag(MI->getDefinitionLoc(), diag::warn_pp_objc_macro_redef_ignored);
2654 assert(!OtherMI->isWarnIfUnused());
2658 // It is very common for system headers to have tons of macro redefinitions
2659 // and for warnings to be disabled in system headers. If this is the case,
2660 // then don't bother calling MacroInfo::isIdenticalTo.
2661 if (!getDiagnostics().getSuppressSystemWarnings() ||
2662 !SourceMgr.isInSystemHeader(DefineTok.getLocation())) {
2663 if (!OtherMI->isUsed() && OtherMI->isWarnIfUnused())
2664 Diag(OtherMI->getDefinitionLoc(), diag::pp_macro_not_used);
2666 // Warn if defining "__LINE__" and other builtins, per C99 6.10.8/4 and
2667 // C++ [cpp.predefined]p4, but allow it as an extension.
2668 if (OtherMI->isBuiltinMacro())
2669 Diag(MacroNameTok, diag::ext_pp_redef_builtin_macro);
2670 // Macros must be identical. This means all tokens and whitespace
2671 // separation must be the same. C99 6.10.3p2.
2672 else if (!OtherMI->isAllowRedefinitionsWithoutWarning() &&
2673 !MI->isIdenticalTo(*OtherMI, *this, /*Syntactic=*/LangOpts.MicrosoftExt)) {
2674 Diag(MI->getDefinitionLoc(), diag::ext_pp_macro_redef)
2675 << MacroNameTok.getIdentifierInfo();
2676 Diag(OtherMI->getDefinitionLoc(), diag::note_previous_definition);
2679 if (OtherMI->isWarnIfUnused())
2680 WarnUnusedMacroLocs.erase(OtherMI->getDefinitionLoc());
2683 DefMacroDirective *MD =
2684 appendDefMacroDirective(MacroNameTok.getIdentifierInfo(), MI);
2686 assert(!MI->isUsed());
2687 // If we need warning for not using the macro, add its location in the
2688 // warn-because-unused-macro set. If it gets used it will be removed from set.
2689 if (getSourceManager().isInMainFile(MI->getDefinitionLoc()) &&
2690 !Diags->isIgnored(diag::pp_macro_not_used, MI->getDefinitionLoc())) {
2691 MI->setIsWarnIfUnused(true);
2692 WarnUnusedMacroLocs.insert(MI->getDefinitionLoc());
2695 // If the callbacks want to know, tell them about the macro definition.
2697 Callbacks->MacroDefined(MacroNameTok, MD);
2700 /// HandleUndefDirective - Implements \#undef.
2702 void Preprocessor::HandleUndefDirective() {
2706 ReadMacroName(MacroNameTok, MU_Undef);
2708 // Error reading macro name? If so, diagnostic already issued.
2709 if (MacroNameTok.is(tok::eod))
2712 // Check to see if this is the last token on the #undef line.
2713 CheckEndOfDirective("undef");
2715 // Okay, we have a valid identifier to undef.
2716 auto *II = MacroNameTok.getIdentifierInfo();
2717 auto MD = getMacroDefinition(II);
2718 UndefMacroDirective *Undef = nullptr;
2720 // If the macro is not defined, this is a noop undef.
2721 if (const MacroInfo *MI = MD.getMacroInfo()) {
2722 if (!MI->isUsed() && MI->isWarnIfUnused())
2723 Diag(MI->getDefinitionLoc(), diag::pp_macro_not_used);
2725 if (MI->isWarnIfUnused())
2726 WarnUnusedMacroLocs.erase(MI->getDefinitionLoc());
2728 Undef = AllocateUndefMacroDirective(MacroNameTok.getLocation());
2731 // If the callbacks want to know, tell them about the macro #undef.
2732 // Note: no matter if the macro was defined or not.
2734 Callbacks->MacroUndefined(MacroNameTok, MD, Undef);
2737 appendMacroDirective(II, Undef);
2740 //===----------------------------------------------------------------------===//
2741 // Preprocessor Conditional Directive Handling.
2742 //===----------------------------------------------------------------------===//
2744 /// HandleIfdefDirective - Implements the \#ifdef/\#ifndef directive. isIfndef
2745 /// is true when this is a \#ifndef directive. ReadAnyTokensBeforeDirective is
2746 /// true if any tokens have been returned or pp-directives activated before this
2747 /// \#ifndef has been lexed.
2749 void Preprocessor::HandleIfdefDirective(Token &Result,
2750 const Token &HashToken,
2752 bool ReadAnyTokensBeforeDirective) {
2754 Token DirectiveTok = Result;
2757 ReadMacroName(MacroNameTok);
2759 // Error reading macro name? If so, diagnostic already issued.
2760 if (MacroNameTok.is(tok::eod)) {
2761 // Skip code until we get to #endif. This helps with recovery by not
2762 // emitting an error when the #endif is reached.
2763 SkipExcludedConditionalBlock(HashToken.getLocation(),
2764 DirectiveTok.getLocation(),
2765 /*Foundnonskip*/ false, /*FoundElse*/ false);
2769 // Check to see if this is the last token on the #if[n]def line.
2770 CheckEndOfDirective(isIfndef ? "ifndef" : "ifdef");
2772 IdentifierInfo *MII = MacroNameTok.getIdentifierInfo();
2773 auto MD = getMacroDefinition(MII);
2774 MacroInfo *MI = MD.getMacroInfo();
2776 if (CurPPLexer->getConditionalStackDepth() == 0) {
2777 // If the start of a top-level #ifdef and if the macro is not defined,
2778 // inform MIOpt that this might be the start of a proper include guard.
2779 // Otherwise it is some other form of unknown conditional which we can't
2781 if (!ReadAnyTokensBeforeDirective && !MI) {
2782 assert(isIfndef && "#ifdef shouldn't reach here");
2783 CurPPLexer->MIOpt.EnterTopLevelIfndef(MII, MacroNameTok.getLocation());
2785 CurPPLexer->MIOpt.EnterTopLevelConditional();
2788 // If there is a macro, process it.
2789 if (MI) // Mark it used.
2790 markMacroAsUsed(MI);
2794 Callbacks->Ifndef(DirectiveTok.getLocation(), MacroNameTok, MD);
2796 Callbacks->Ifdef(DirectiveTok.getLocation(), MacroNameTok, MD);
2799 // Should we include the stuff contained by this directive?
2800 if (PPOpts->SingleFileParseMode && !MI) {
2801 // In 'single-file-parse mode' undefined identifiers trigger parsing of all
2802 // the directive blocks.
2803 CurPPLexer->pushConditionalLevel(DirectiveTok.getLocation(),
2804 /*wasskip*/false, /*foundnonskip*/false,
2805 /*foundelse*/false);
2806 } else if (!MI == isIfndef) {
2807 // Yes, remember that we are inside a conditional, then lex the next token.
2808 CurPPLexer->pushConditionalLevel(DirectiveTok.getLocation(),
2809 /*wasskip*/false, /*foundnonskip*/true,
2810 /*foundelse*/false);
2812 // No, skip the contents of this block.
2813 SkipExcludedConditionalBlock(HashToken.getLocation(),
2814 DirectiveTok.getLocation(),
2815 /*Foundnonskip*/ false,
2816 /*FoundElse*/ false);
2820 /// HandleIfDirective - Implements the \#if directive.
2822 void Preprocessor::HandleIfDirective(Token &IfToken,
2823 const Token &HashToken,
2824 bool ReadAnyTokensBeforeDirective) {
2827 // Parse and evaluate the conditional expression.
2828 IdentifierInfo *IfNDefMacro = nullptr;
2829 const SourceLocation ConditionalBegin = CurPPLexer->getSourceLocation();
2830 const DirectiveEvalResult DER = EvaluateDirectiveExpression(IfNDefMacro);
2831 const bool ConditionalTrue = DER.Conditional;
2832 const SourceLocation ConditionalEnd = CurPPLexer->getSourceLocation();
2834 // If this condition is equivalent to #ifndef X, and if this is the first
2835 // directive seen, handle it for the multiple-include optimization.
2836 if (CurPPLexer->getConditionalStackDepth() == 0) {
2837 if (!ReadAnyTokensBeforeDirective && IfNDefMacro && ConditionalTrue)
2838 // FIXME: Pass in the location of the macro name, not the 'if' token.
2839 CurPPLexer->MIOpt.EnterTopLevelIfndef(IfNDefMacro, IfToken.getLocation());
2841 CurPPLexer->MIOpt.EnterTopLevelConditional();
2845 Callbacks->If(IfToken.getLocation(),
2846 SourceRange(ConditionalBegin, ConditionalEnd),
2847 (ConditionalTrue ? PPCallbacks::CVK_True : PPCallbacks::CVK_False));
2849 // Should we include the stuff contained by this directive?
2850 if (PPOpts->SingleFileParseMode && DER.IncludedUndefinedIds) {
2851 // In 'single-file-parse mode' undefined identifiers trigger parsing of all
2852 // the directive blocks.
2853 CurPPLexer->pushConditionalLevel(IfToken.getLocation(), /*wasskip*/false,
2854 /*foundnonskip*/false, /*foundelse*/false);
2855 } else if (ConditionalTrue) {
2856 // Yes, remember that we are inside a conditional, then lex the next token.
2857 CurPPLexer->pushConditionalLevel(IfToken.getLocation(), /*wasskip*/false,
2858 /*foundnonskip*/true, /*foundelse*/false);
2860 // No, skip the contents of this block.
2861 SkipExcludedConditionalBlock(HashToken.getLocation(), IfToken.getLocation(),
2862 /*Foundnonskip*/ false,
2863 /*FoundElse*/ false);
2867 /// HandleEndifDirective - Implements the \#endif directive.
2869 void Preprocessor::HandleEndifDirective(Token &EndifToken) {
2872 // Check that this is the whole directive.
2873 CheckEndOfDirective("endif");
2875 PPConditionalInfo CondInfo;
2876 if (CurPPLexer->popConditionalLevel(CondInfo)) {
2877 // No conditionals on the stack: this is an #endif without an #if.
2878 Diag(EndifToken, diag::err_pp_endif_without_if);
2882 // If this the end of a top-level #endif, inform MIOpt.
2883 if (CurPPLexer->getConditionalStackDepth() == 0)
2884 CurPPLexer->MIOpt.ExitTopLevelConditional();
2886 assert(!CondInfo.WasSkipping && !CurPPLexer->LexingRawMode &&
2887 "This code should only be reachable in the non-skipping case!");
2890 Callbacks->Endif(EndifToken.getLocation(), CondInfo.IfLoc);
2893 /// HandleElseDirective - Implements the \#else directive.
2895 void Preprocessor::HandleElseDirective(Token &Result, const Token &HashToken) {
2898 // #else directive in a non-skipping conditional... start skipping.
2899 CheckEndOfDirective("else");
2901 PPConditionalInfo CI;
2902 if (CurPPLexer->popConditionalLevel(CI)) {
2903 Diag(Result, diag::pp_err_else_without_if);
2907 // If this is a top-level #else, inform the MIOpt.
2908 if (CurPPLexer->getConditionalStackDepth() == 0)
2909 CurPPLexer->MIOpt.EnterTopLevelConditional();
2911 // If this is a #else with a #else before it, report the error.
2912 if (CI.FoundElse) Diag(Result, diag::pp_err_else_after_else);
2915 Callbacks->Else(Result.getLocation(), CI.IfLoc);
2917 if (PPOpts->SingleFileParseMode && !CI.FoundNonSkip) {
2918 // In 'single-file-parse mode' undefined identifiers trigger parsing of all
2919 // the directive blocks.
2920 CurPPLexer->pushConditionalLevel(CI.IfLoc, /*wasskip*/false,
2921 /*foundnonskip*/false, /*foundelse*/true);
2925 // Finally, skip the rest of the contents of this block.
2926 SkipExcludedConditionalBlock(HashToken.getLocation(), CI.IfLoc,
2927 /*Foundnonskip*/ true,
2928 /*FoundElse*/ true, Result.getLocation());
2931 /// HandleElifDirective - Implements the \#elif directive.
2933 void Preprocessor::HandleElifDirective(Token &ElifToken,
2934 const Token &HashToken) {
2937 // #elif directive in a non-skipping conditional... start skipping.
2938 // We don't care what the condition is, because we will always skip it (since
2939 // the block immediately before it was included).
2940 const SourceLocation ConditionalBegin = CurPPLexer->getSourceLocation();
2941 DiscardUntilEndOfDirective();
2942 const SourceLocation ConditionalEnd = CurPPLexer->getSourceLocation();
2944 PPConditionalInfo CI;
2945 if (CurPPLexer->popConditionalLevel(CI)) {
2946 Diag(ElifToken, diag::pp_err_elif_without_if);
2950 // If this is a top-level #elif, inform the MIOpt.
2951 if (CurPPLexer->getConditionalStackDepth() == 0)
2952 CurPPLexer->MIOpt.EnterTopLevelConditional();
2954 // If this is a #elif with a #else before it, report the error.
2955 if (CI.FoundElse) Diag(ElifToken, diag::pp_err_elif_after_else);
2958 Callbacks->Elif(ElifToken.getLocation(),
2959 SourceRange(ConditionalBegin, ConditionalEnd),
2960 PPCallbacks::CVK_NotEvaluated, CI.IfLoc);
2962 if (PPOpts->SingleFileParseMode && !CI.FoundNonSkip) {
2963 // In 'single-file-parse mode' undefined identifiers trigger parsing of all
2964 // the directive blocks.
2965 CurPPLexer->pushConditionalLevel(ElifToken.getLocation(), /*wasskip*/false,
2966 /*foundnonskip*/false, /*foundelse*/false);
2970 // Finally, skip the rest of the contents of this block.
2971 SkipExcludedConditionalBlock(
2972 HashToken.getLocation(), CI.IfLoc, /*Foundnonskip*/ true,
2973 /*FoundElse*/ CI.FoundElse, ElifToken.getLocation());