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 /// \brief Implements # directive processing for the Preprocessor.
13 //===----------------------------------------------------------------------===//
15 #include "clang/Lex/Preprocessor.h"
16 #include "clang/Basic/FileManager.h"
17 #include "clang/Basic/SourceManager.h"
18 #include "clang/Lex/CodeCompletionHandler.h"
19 #include "clang/Lex/HeaderSearch.h"
20 #include "clang/Lex/HeaderSearchOptions.h"
21 #include "clang/Lex/LexDiagnostic.h"
22 #include "clang/Lex/LiteralSupport.h"
23 #include "clang/Lex/MacroInfo.h"
24 #include "clang/Lex/ModuleLoader.h"
25 #include "clang/Lex/Pragma.h"
26 #include "llvm/ADT/APInt.h"
27 #include "llvm/Support/ErrorHandling.h"
28 #include "llvm/Support/Path.h"
29 #include "llvm/Support/SaveAndRestore.h"
30 using namespace clang;
32 //===----------------------------------------------------------------------===//
33 // Utility Methods for Preprocessor Directive Handling.
34 //===----------------------------------------------------------------------===//
36 MacroInfo *Preprocessor::AllocateMacroInfo() {
37 MacroInfoChain *MIChain = BP.Allocate<MacroInfoChain>();
38 MIChain->Next = MIChainHead;
39 MIChainHead = MIChain;
43 MacroInfo *Preprocessor::AllocateMacroInfo(SourceLocation L) {
44 MacroInfo *MI = AllocateMacroInfo();
45 new (MI) MacroInfo(L);
49 MacroInfo *Preprocessor::AllocateDeserializedMacroInfo(SourceLocation L,
50 unsigned SubModuleID) {
51 static_assert(llvm::AlignOf<MacroInfo>::Alignment >= sizeof(SubModuleID),
52 "alignment for MacroInfo is less than the ID");
53 DeserializedMacroInfoChain *MIChain =
54 BP.Allocate<DeserializedMacroInfoChain>();
55 MIChain->Next = DeserialMIChainHead;
56 DeserialMIChainHead = MIChain;
58 MacroInfo *MI = &MIChain->MI;
59 new (MI) MacroInfo(L);
60 MI->FromASTFile = true;
61 MI->setOwningModuleID(SubModuleID);
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 /// \brief 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 /// \brief 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 /// \brief 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 static MacroDiag shouldWarnOnMacroDef(Preprocessor &PP, IdentifierInfo *II) {
119 const LangOptions &Lang = PP.getLangOpts();
120 StringRef Text = II->getName();
121 if (isReservedId(Text, Lang))
122 return MD_ReservedMacro;
123 if (II->isKeyword(Lang))
124 return MD_KeywordDef;
125 if (Lang.CPlusPlus11 && (Text.equals("override") || Text.equals("final")))
126 return MD_KeywordDef;
130 static MacroDiag shouldWarnOnMacroUndef(Preprocessor &PP, IdentifierInfo *II) {
131 const LangOptions &Lang = PP.getLangOpts();
132 StringRef Text = II->getName();
133 // Do not warn on keyword undef. It is generally harmless and widely used.
134 if (isReservedId(Text, Lang))
135 return MD_ReservedMacro;
139 bool Preprocessor::CheckMacroName(Token &MacroNameTok, MacroUse isDefineUndef,
141 // Missing macro name?
142 if (MacroNameTok.is(tok::eod))
143 return Diag(MacroNameTok, diag::err_pp_missing_macro_name);
145 IdentifierInfo *II = MacroNameTok.getIdentifierInfo();
147 bool Invalid = false;
148 std::string Spelling = getSpelling(MacroNameTok, &Invalid);
150 return Diag(MacroNameTok, diag::err_pp_macro_not_identifier);
151 II = getIdentifierInfo(Spelling);
153 if (!II->isCPlusPlusOperatorKeyword())
154 return Diag(MacroNameTok, diag::err_pp_macro_not_identifier);
156 // C++ 2.5p2: Alternative tokens behave the same as its primary token
157 // except for their spellings.
158 Diag(MacroNameTok, getLangOpts().MicrosoftExt
159 ? diag::ext_pp_operator_used_as_macro_name
160 : diag::err_pp_operator_used_as_macro_name)
161 << II << MacroNameTok.getKind();
163 // Allow #defining |and| and friends for Microsoft compatibility or
164 // recovery when legacy C headers are included in C++.
165 MacroNameTok.setIdentifierInfo(II);
168 if ((isDefineUndef != MU_Other) && II->getPPKeywordID() == tok::pp_defined) {
169 // Error if defining "defined": C99 6.10.8/4, C++ [cpp.predefined]p4.
170 return Diag(MacroNameTok, diag::err_defined_macro_name);
173 if (isDefineUndef == MU_Undef) {
174 auto *MI = getMacroInfo(II);
175 if (MI && MI->isBuiltinMacro()) {
176 // Warn if undefining "__LINE__" and other builtins, per C99 6.10.8/4
177 // and C++ [cpp.predefined]p4], but allow it as an extension.
178 Diag(MacroNameTok, diag::ext_pp_undef_builtin_macro);
182 // If defining/undefining reserved identifier or a keyword, we need to issue
184 SourceLocation MacroNameLoc = MacroNameTok.getLocation();
187 if (!SourceMgr.isInSystemHeader(MacroNameLoc) &&
188 (strcmp(SourceMgr.getBufferName(MacroNameLoc), "<built-in>") != 0)) {
189 MacroDiag D = MD_NoWarn;
190 if (isDefineUndef == MU_Define) {
191 D = shouldWarnOnMacroDef(*this, II);
193 else if (isDefineUndef == MU_Undef)
194 D = shouldWarnOnMacroUndef(*this, II);
195 if (D == MD_KeywordDef) {
196 // We do not want to warn on some patterns widely used in configuration
197 // scripts. This requires analyzing next tokens, so do not issue warnings
198 // now, only inform caller.
202 if (D == MD_ReservedMacro)
203 Diag(MacroNameTok, diag::warn_pp_macro_is_reserved_id);
206 // Okay, we got a good identifier.
210 /// \brief Lex and validate a macro name, which occurs after a
211 /// \#define or \#undef.
213 /// This sets the token kind to eod and discards the rest of the macro line if
214 /// the macro name is invalid.
216 /// \param MacroNameTok Token that is expected to be a macro name.
217 /// \param isDefineUndef Context in which macro is used.
218 /// \param ShadowFlag Points to a flag that is set if macro shadows a keyword.
219 void Preprocessor::ReadMacroName(Token &MacroNameTok, MacroUse isDefineUndef,
221 // Read the token, don't allow macro expansion on it.
222 LexUnexpandedToken(MacroNameTok);
224 if (MacroNameTok.is(tok::code_completion)) {
226 CodeComplete->CodeCompleteMacroName(isDefineUndef == MU_Define);
227 setCodeCompletionReached();
228 LexUnexpandedToken(MacroNameTok);
231 if (!CheckMacroName(MacroNameTok, isDefineUndef, ShadowFlag))
234 // Invalid macro name, read and discard the rest of the line and set the
235 // token kind to tok::eod if necessary.
236 if (MacroNameTok.isNot(tok::eod)) {
237 MacroNameTok.setKind(tok::eod);
238 DiscardUntilEndOfDirective();
242 /// \brief Ensure that the next token is a tok::eod token.
244 /// If not, emit a diagnostic and consume up until the eod. If EnableMacros is
245 /// true, then we consider macros that expand to zero tokens as being ok.
246 void Preprocessor::CheckEndOfDirective(const char *DirType, bool EnableMacros) {
248 // Lex unexpanded tokens for most directives: macros might expand to zero
249 // tokens, causing us to miss diagnosing invalid lines. Some directives (like
250 // #line) allow empty macros.
254 LexUnexpandedToken(Tmp);
256 // There should be no tokens after the directive, but we allow them as an
258 while (Tmp.is(tok::comment)) // Skip comments in -C mode.
259 LexUnexpandedToken(Tmp);
261 if (Tmp.isNot(tok::eod)) {
262 // Add a fixit in GNU/C99/C++ mode. Don't offer a fixit for strict-C89,
263 // or if this is a macro-style preprocessing directive, because it is more
264 // trouble than it is worth to insert /**/ and check that there is no /**/
265 // in the range also.
267 if ((LangOpts.GNUMode || LangOpts.C99 || LangOpts.CPlusPlus) &&
269 Hint = FixItHint::CreateInsertion(Tmp.getLocation(),"//");
270 Diag(Tmp, diag::ext_pp_extra_tokens_at_eol) << DirType << Hint;
271 DiscardUntilEndOfDirective();
277 /// SkipExcludedConditionalBlock - We just read a \#if or related directive and
278 /// decided that the subsequent tokens are in the \#if'd out portion of the
279 /// file. Lex the rest of the file, until we see an \#endif. If
280 /// FoundNonSkipPortion is true, then we have already emitted code for part of
281 /// this \#if directive, so \#else/\#elif blocks should never be entered.
282 /// If ElseOk is true, then \#else directives are ok, if not, then we have
283 /// already seen one so a \#else directive is a duplicate. When this returns,
284 /// the caller can lex the first valid token.
285 void Preprocessor::SkipExcludedConditionalBlock(SourceLocation IfTokenLoc,
286 bool FoundNonSkipPortion,
288 SourceLocation ElseLoc) {
290 assert(!CurTokenLexer && CurPPLexer && "Lexing a macro, not a file?");
292 CurPPLexer->pushConditionalLevel(IfTokenLoc, /*isSkipping*/false,
293 FoundNonSkipPortion, FoundElse);
296 PTHSkipExcludedConditionalBlock();
300 // Enter raw mode to disable identifier lookup (and thus macro expansion),
301 // disabling warnings, etc.
302 CurPPLexer->LexingRawMode = true;
307 if (Tok.is(tok::code_completion)) {
309 CodeComplete->CodeCompleteInConditionalExclusion();
310 setCodeCompletionReached();
314 // If this is the end of the buffer, we have an error.
315 if (Tok.is(tok::eof)) {
316 // Emit errors for each unterminated conditional on the stack, including
318 while (!CurPPLexer->ConditionalStack.empty()) {
319 if (CurLexer->getFileLoc() != CodeCompletionFileLoc)
320 Diag(CurPPLexer->ConditionalStack.back().IfLoc,
321 diag::err_pp_unterminated_conditional);
322 CurPPLexer->ConditionalStack.pop_back();
325 // Just return and let the caller lex after this #include.
329 // If this token is not a preprocessor directive, just skip it.
330 if (Tok.isNot(tok::hash) || !Tok.isAtStartOfLine())
333 // We just parsed a # character at the start of a line, so we're in
334 // directive mode. Tell the lexer this so any newlines we see will be
335 // converted into an EOD token (this terminates the macro).
336 CurPPLexer->ParsingPreprocessorDirective = true;
337 if (CurLexer) CurLexer->SetKeepWhitespaceMode(false);
340 // Read the next token, the directive flavor.
341 LexUnexpandedToken(Tok);
343 // If this isn't an identifier directive (e.g. is "# 1\n" or "#\n", or
344 // something bogus), skip it.
345 if (Tok.isNot(tok::raw_identifier)) {
346 CurPPLexer->ParsingPreprocessorDirective = false;
347 // Restore comment saving mode.
348 if (CurLexer) CurLexer->resetExtendedTokenMode();
352 // If the first letter isn't i or e, it isn't intesting to us. We know that
353 // this is safe in the face of spelling differences, because there is no way
354 // to spell an i/e in a strange way that is another letter. Skipping this
355 // allows us to avoid looking up the identifier info for #define/#undef and
356 // other common directives.
357 StringRef RI = Tok.getRawIdentifier();
359 char FirstChar = RI[0];
360 if (FirstChar >= 'a' && FirstChar <= 'z' &&
361 FirstChar != 'i' && FirstChar != 'e') {
362 CurPPLexer->ParsingPreprocessorDirective = false;
363 // Restore comment saving mode.
364 if (CurLexer) CurLexer->resetExtendedTokenMode();
368 // Get the identifier name without trigraphs or embedded newlines. Note
369 // that we can't use Tok.getIdentifierInfo() because its lookup is disabled
371 char DirectiveBuf[20];
373 if (!Tok.needsCleaning() && RI.size() < 20) {
376 std::string DirectiveStr = getSpelling(Tok);
377 unsigned IdLen = DirectiveStr.size();
379 CurPPLexer->ParsingPreprocessorDirective = false;
380 // Restore comment saving mode.
381 if (CurLexer) CurLexer->resetExtendedTokenMode();
384 memcpy(DirectiveBuf, &DirectiveStr[0], IdLen);
385 Directive = StringRef(DirectiveBuf, IdLen);
388 if (Directive.startswith("if")) {
389 StringRef Sub = Directive.substr(2);
390 if (Sub.empty() || // "if"
391 Sub == "def" || // "ifdef"
392 Sub == "ndef") { // "ifndef"
393 // We know the entire #if/#ifdef/#ifndef block will be skipped, don't
394 // bother parsing the condition.
395 DiscardUntilEndOfDirective();
396 CurPPLexer->pushConditionalLevel(Tok.getLocation(), /*wasskipping*/true,
397 /*foundnonskip*/false,
400 } else if (Directive[0] == 'e') {
401 StringRef Sub = Directive.substr(1);
402 if (Sub == "ndif") { // "endif"
403 PPConditionalInfo CondInfo;
404 CondInfo.WasSkipping = true; // Silence bogus warning.
405 bool InCond = CurPPLexer->popConditionalLevel(CondInfo);
406 (void)InCond; // Silence warning in no-asserts mode.
407 assert(!InCond && "Can't be skipping if not in a conditional!");
409 // If we popped the outermost skipping block, we're done skipping!
410 if (!CondInfo.WasSkipping) {
411 // Restore the value of LexingRawMode so that trailing comments
412 // are handled correctly, if we've reached the outermost block.
413 CurPPLexer->LexingRawMode = false;
414 CheckEndOfDirective("endif");
415 CurPPLexer->LexingRawMode = true;
417 Callbacks->Endif(Tok.getLocation(), CondInfo.IfLoc);
420 DiscardUntilEndOfDirective();
422 } else if (Sub == "lse") { // "else".
423 // #else directive in a skipping conditional. If not in some other
424 // skipping conditional, and if #else hasn't already been seen, enter it
425 // as a non-skipping conditional.
426 PPConditionalInfo &CondInfo = CurPPLexer->peekConditionalLevel();
428 // If this is a #else with a #else before it, report the error.
429 if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_else_after_else);
431 // Note that we've seen a #else in this conditional.
432 CondInfo.FoundElse = true;
434 // If the conditional is at the top level, and the #if block wasn't
435 // entered, enter the #else block now.
436 if (!CondInfo.WasSkipping && !CondInfo.FoundNonSkip) {
437 CondInfo.FoundNonSkip = true;
438 // Restore the value of LexingRawMode so that trailing comments
439 // are handled correctly.
440 CurPPLexer->LexingRawMode = false;
441 CheckEndOfDirective("else");
442 CurPPLexer->LexingRawMode = true;
444 Callbacks->Else(Tok.getLocation(), CondInfo.IfLoc);
447 DiscardUntilEndOfDirective(); // C99 6.10p4.
449 } else if (Sub == "lif") { // "elif".
450 PPConditionalInfo &CondInfo = CurPPLexer->peekConditionalLevel();
452 // If this is a #elif with a #else before it, report the error.
453 if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_elif_after_else);
455 // If this is in a skipping block or if we're already handled this #if
456 // block, don't bother parsing the condition.
457 if (CondInfo.WasSkipping || CondInfo.FoundNonSkip) {
458 DiscardUntilEndOfDirective();
460 const SourceLocation CondBegin = CurPPLexer->getSourceLocation();
461 // Restore the value of LexingRawMode so that identifiers are
462 // looked up, etc, inside the #elif expression.
463 assert(CurPPLexer->LexingRawMode && "We have to be skipping here!");
464 CurPPLexer->LexingRawMode = false;
465 IdentifierInfo *IfNDefMacro = nullptr;
466 const bool CondValue = EvaluateDirectiveExpression(IfNDefMacro);
467 CurPPLexer->LexingRawMode = true;
469 const SourceLocation CondEnd = CurPPLexer->getSourceLocation();
470 Callbacks->Elif(Tok.getLocation(),
471 SourceRange(CondBegin, CondEnd),
472 (CondValue ? PPCallbacks::CVK_True : PPCallbacks::CVK_False), CondInfo.IfLoc);
474 // If this condition is true, enter it!
476 CondInfo.FoundNonSkip = true;
483 CurPPLexer->ParsingPreprocessorDirective = false;
484 // Restore comment saving mode.
485 if (CurLexer) CurLexer->resetExtendedTokenMode();
488 // Finally, if we are out of the conditional (saw an #endif or ran off the end
489 // of the file, just stop skipping and return to lexing whatever came after
491 CurPPLexer->LexingRawMode = false;
494 SourceLocation BeginLoc = ElseLoc.isValid() ? ElseLoc : IfTokenLoc;
495 Callbacks->SourceRangeSkipped(SourceRange(BeginLoc, Tok.getLocation()));
499 void Preprocessor::PTHSkipExcludedConditionalBlock() {
503 assert(CurPTHLexer->LexingRawMode == false);
505 // Skip to the next '#else', '#elif', or #endif.
506 if (CurPTHLexer->SkipBlock()) {
507 // We have reached an #endif. Both the '#' and 'endif' tokens
508 // have been consumed by the PTHLexer. Just pop off the condition level.
509 PPConditionalInfo CondInfo;
510 bool InCond = CurPTHLexer->popConditionalLevel(CondInfo);
511 (void)InCond; // Silence warning in no-asserts mode.
512 assert(!InCond && "Can't be skipping if not in a conditional!");
516 // We have reached a '#else' or '#elif'. Lex the next token to get
517 // the directive flavor.
519 LexUnexpandedToken(Tok);
521 // We can actually look up the IdentifierInfo here since we aren't in
523 tok::PPKeywordKind K = Tok.getIdentifierInfo()->getPPKeywordID();
525 if (K == tok::pp_else) {
526 // #else: Enter the else condition. We aren't in a nested condition
527 // since we skip those. We're always in the one matching the last
528 // blocked we skipped.
529 PPConditionalInfo &CondInfo = CurPTHLexer->peekConditionalLevel();
530 // Note that we've seen a #else in this conditional.
531 CondInfo.FoundElse = true;
533 // If the #if block wasn't entered then enter the #else block now.
534 if (!CondInfo.FoundNonSkip) {
535 CondInfo.FoundNonSkip = true;
537 // Scan until the eod token.
538 CurPTHLexer->ParsingPreprocessorDirective = true;
539 DiscardUntilEndOfDirective();
540 CurPTHLexer->ParsingPreprocessorDirective = false;
545 // Otherwise skip this block.
549 assert(K == tok::pp_elif);
550 PPConditionalInfo &CondInfo = CurPTHLexer->peekConditionalLevel();
552 // If this is a #elif with a #else before it, report the error.
553 if (CondInfo.FoundElse)
554 Diag(Tok, diag::pp_err_elif_after_else);
556 // If this is in a skipping block or if we're already handled this #if
557 // block, don't bother parsing the condition. We just skip this block.
558 if (CondInfo.FoundNonSkip)
561 // Evaluate the condition of the #elif.
562 IdentifierInfo *IfNDefMacro = nullptr;
563 CurPTHLexer->ParsingPreprocessorDirective = true;
564 bool ShouldEnter = EvaluateDirectiveExpression(IfNDefMacro);
565 CurPTHLexer->ParsingPreprocessorDirective = false;
567 // If this condition is true, enter it!
569 CondInfo.FoundNonSkip = true;
573 // Otherwise, skip this block and go to the next one.
578 Module *Preprocessor::getModuleForLocation(SourceLocation Loc) {
579 ModuleMap &ModMap = HeaderInfo.getModuleMap();
580 if (SourceMgr.isInMainFile(Loc)) {
581 if (Module *CurMod = getCurrentModule())
582 return CurMod; // Compiling a module.
583 return HeaderInfo.getModuleMap().SourceModule; // Compiling a source.
585 // Try to determine the module of the include directive.
586 // FIXME: Look into directly passing the FileEntry from LookupFile instead.
587 FileID IDOfIncl = SourceMgr.getFileID(SourceMgr.getExpansionLoc(Loc));
588 if (const FileEntry *EntryOfIncl = SourceMgr.getFileEntryForID(IDOfIncl)) {
589 // The include comes from a file.
590 return ModMap.findModuleForHeader(EntryOfIncl).getModule();
592 // The include does not come from a file,
593 // so it is probably a module compilation.
594 return getCurrentModule();
598 Module *Preprocessor::getModuleContainingLocation(SourceLocation Loc) {
599 return HeaderInfo.getModuleMap().inferModuleFromLocation(
600 FullSourceLoc(Loc, SourceMgr));
603 const FileEntry *Preprocessor::LookupFile(
604 SourceLocation FilenameLoc,
607 const DirectoryLookup *FromDir,
608 const FileEntry *FromFile,
609 const DirectoryLookup *&CurDir,
610 SmallVectorImpl<char> *SearchPath,
611 SmallVectorImpl<char> *RelativePath,
612 ModuleMap::KnownHeader *SuggestedModule,
614 Module *RequestingModule = getModuleForLocation(FilenameLoc);
616 // If the header lookup mechanism may be relative to the current inclusion
617 // stack, record the parent #includes.
618 SmallVector<std::pair<const FileEntry *, const DirectoryEntry *>, 16>
620 if (!FromDir && !FromFile) {
621 FileID FID = getCurrentFileLexer()->getFileID();
622 const FileEntry *FileEnt = SourceMgr.getFileEntryForID(FID);
624 // If there is no file entry associated with this file, it must be the
625 // predefines buffer or the module includes buffer. Any other file is not
626 // lexed with a normal lexer, so it won't be scanned for preprocessor
629 // If we have the predefines buffer, resolve #include references (which come
630 // from the -include command line argument) from the current working
631 // directory instead of relative to the main file.
633 // If we have the module includes buffer, resolve #include references (which
634 // come from header declarations in the module map) relative to the module
637 if (FID == SourceMgr.getMainFileID() && MainFileDir)
638 Includers.push_back(std::make_pair(nullptr, MainFileDir));
640 SourceMgr.getFileEntryForID(SourceMgr.getMainFileID())))
641 Includers.push_back(std::make_pair(FileEnt, FileMgr.getDirectory(".")));
643 Includers.push_back(std::make_pair(FileEnt, FileEnt->getDir()));
646 // MSVC searches the current include stack from top to bottom for
647 // headers included by quoted include directives.
648 // See: http://msdn.microsoft.com/en-us/library/36k2cdd4.aspx
649 if (LangOpts.MSVCCompat && !isAngled) {
650 for (unsigned i = 0, e = IncludeMacroStack.size(); i != e; ++i) {
651 IncludeStackInfo &ISEntry = IncludeMacroStack[e - i - 1];
652 if (IsFileLexer(ISEntry))
653 if ((FileEnt = ISEntry.ThePPLexer->getFileEntry()))
654 Includers.push_back(std::make_pair(FileEnt, FileEnt->getDir()));
659 CurDir = CurDirLookup;
662 // We're supposed to start looking from after a particular file. Search
663 // the include path until we find that file or run out of files.
664 const DirectoryLookup *TmpCurDir = CurDir;
665 const DirectoryLookup *TmpFromDir = nullptr;
666 while (const FileEntry *FE = HeaderInfo.LookupFile(
667 Filename, FilenameLoc, isAngled, TmpFromDir, TmpCurDir,
668 Includers, SearchPath, RelativePath, RequestingModule,
669 SuggestedModule, SkipCache)) {
670 // Keep looking as if this file did a #include_next.
671 TmpFromDir = TmpCurDir;
673 if (FE == FromFile) {
675 FromDir = TmpFromDir;
682 // Do a standard file entry lookup.
683 const FileEntry *FE = HeaderInfo.LookupFile(
684 Filename, FilenameLoc, isAngled, FromDir, CurDir, Includers, SearchPath,
685 RelativePath, RequestingModule, SuggestedModule, SkipCache);
687 if (SuggestedModule && !LangOpts.AsmPreprocessor)
688 HeaderInfo.getModuleMap().diagnoseHeaderInclusion(
689 RequestingModule, FilenameLoc, Filename, FE);
693 const FileEntry *CurFileEnt;
694 // Otherwise, see if this is a subframework header. If so, this is relative
695 // to one of the headers on the #include stack. Walk the list of the current
696 // headers on the #include stack and pass them to HeaderInfo.
698 if ((CurFileEnt = CurPPLexer->getFileEntry())) {
699 if ((FE = HeaderInfo.LookupSubframeworkHeader(Filename, CurFileEnt,
700 SearchPath, RelativePath,
703 if (SuggestedModule && !LangOpts.AsmPreprocessor)
704 HeaderInfo.getModuleMap().diagnoseHeaderInclusion(
705 RequestingModule, FilenameLoc, Filename, FE);
711 for (unsigned i = 0, e = IncludeMacroStack.size(); i != e; ++i) {
712 IncludeStackInfo &ISEntry = IncludeMacroStack[e-i-1];
713 if (IsFileLexer(ISEntry)) {
714 if ((CurFileEnt = ISEntry.ThePPLexer->getFileEntry())) {
715 if ((FE = HeaderInfo.LookupSubframeworkHeader(
716 Filename, CurFileEnt, SearchPath, RelativePath,
717 RequestingModule, SuggestedModule))) {
718 if (SuggestedModule && !LangOpts.AsmPreprocessor)
719 HeaderInfo.getModuleMap().diagnoseHeaderInclusion(
720 RequestingModule, FilenameLoc, Filename, FE);
727 // Otherwise, we really couldn't find the file.
732 //===----------------------------------------------------------------------===//
733 // Preprocessor Directive Handling.
734 //===----------------------------------------------------------------------===//
736 class Preprocessor::ResetMacroExpansionHelper {
738 ResetMacroExpansionHelper(Preprocessor *pp)
739 : PP(pp), save(pp->DisableMacroExpansion) {
740 if (pp->MacroExpansionInDirectivesOverride)
741 pp->DisableMacroExpansion = false;
743 ~ResetMacroExpansionHelper() {
744 PP->DisableMacroExpansion = save;
751 /// HandleDirective - This callback is invoked when the lexer sees a # token
752 /// at the start of a line. This consumes the directive, modifies the
753 /// lexer/preprocessor state, and advances the lexer(s) so that the next token
754 /// read is the correct one.
755 void Preprocessor::HandleDirective(Token &Result) {
756 // FIXME: Traditional: # with whitespace before it not recognized by K&R?
758 // We just parsed a # character at the start of a line, so we're in directive
759 // mode. Tell the lexer this so any newlines we see will be converted into an
760 // EOD token (which terminates the directive).
761 CurPPLexer->ParsingPreprocessorDirective = true;
762 if (CurLexer) CurLexer->SetKeepWhitespaceMode(false);
764 bool ImmediatelyAfterTopLevelIfndef =
765 CurPPLexer->MIOpt.getImmediatelyAfterTopLevelIfndef();
766 CurPPLexer->MIOpt.resetImmediatelyAfterTopLevelIfndef();
770 // We are about to read a token. For the multiple-include optimization FA to
771 // work, we have to remember if we had read any tokens *before* this
773 bool ReadAnyTokensBeforeDirective =CurPPLexer->MIOpt.getHasReadAnyTokensVal();
775 // Save the '#' token in case we need to return it later.
776 Token SavedHash = Result;
778 // Read the next token, the directive flavor. This isn't expanded due to
780 LexUnexpandedToken(Result);
782 // C99 6.10.3p11: Is this preprocessor directive in macro invocation? e.g.:
787 // If so, the user is relying on undefined behavior, emit a diagnostic. Do
788 // not support this for #include-like directives, since that can result in
789 // terrible diagnostics, and does not work in GCC.
791 if (IdentifierInfo *II = Result.getIdentifierInfo()) {
792 switch (II->getPPKeywordID()) {
793 case tok::pp_include:
795 case tok::pp_include_next:
796 case tok::pp___include_macros:
798 Diag(Result, diag::err_embedded_directive) << II->getName();
799 DiscardUntilEndOfDirective();
805 Diag(Result, diag::ext_embedded_directive);
808 // Temporarily enable macro expansion if set so
809 // and reset to previous state when returning from this function.
810 ResetMacroExpansionHelper helper(this);
812 switch (Result.getKind()) {
814 return; // null directive.
815 case tok::code_completion:
817 CodeComplete->CodeCompleteDirective(
818 CurPPLexer->getConditionalStackDepth() > 0);
819 setCodeCompletionReached();
821 case tok::numeric_constant: // # 7 GNU line marker directive.
822 if (getLangOpts().AsmPreprocessor)
823 break; // # 4 is not a preprocessor directive in .S files.
824 return HandleDigitDirective(Result);
826 IdentifierInfo *II = Result.getIdentifierInfo();
827 if (!II) break; // Not an identifier.
829 // Ask what the preprocessor keyword ID is.
830 switch (II->getPPKeywordID()) {
832 // C99 6.10.1 - Conditional Inclusion.
834 return HandleIfDirective(Result, ReadAnyTokensBeforeDirective);
836 return HandleIfdefDirective(Result, false, true/*not valid for miopt*/);
838 return HandleIfdefDirective(Result, true, ReadAnyTokensBeforeDirective);
840 return HandleElifDirective(Result);
842 return HandleElseDirective(Result);
844 return HandleEndifDirective(Result);
846 // C99 6.10.2 - Source File Inclusion.
847 case tok::pp_include:
849 return HandleIncludeDirective(SavedHash.getLocation(), Result);
850 case tok::pp___include_macros:
852 return HandleIncludeMacrosDirective(SavedHash.getLocation(), Result);
854 // C99 6.10.3 - Macro Replacement.
856 return HandleDefineDirective(Result, ImmediatelyAfterTopLevelIfndef);
858 return HandleUndefDirective(Result);
860 // C99 6.10.4 - Line Control.
862 return HandleLineDirective(Result);
864 // C99 6.10.5 - Error Directive.
866 return HandleUserDiagnosticDirective(Result, false);
868 // C99 6.10.6 - Pragma Directive.
870 return HandlePragmaDirective(SavedHash.getLocation(), PIK_HashPragma);
874 return HandleImportDirective(SavedHash.getLocation(), Result);
875 case tok::pp_include_next:
876 return HandleIncludeNextDirective(SavedHash.getLocation(), Result);
878 case tok::pp_warning:
879 Diag(Result, diag::ext_pp_warning_directive);
880 return HandleUserDiagnosticDirective(Result, true);
882 return HandleIdentSCCSDirective(Result);
884 return HandleIdentSCCSDirective(Result);
886 //isExtension = true; // FIXME: implement #assert
888 case tok::pp_unassert:
889 //isExtension = true; // FIXME: implement #unassert
892 case tok::pp___public_macro:
893 if (getLangOpts().Modules)
894 return HandleMacroPublicDirective(Result);
897 case tok::pp___private_macro:
898 if (getLangOpts().Modules)
899 return HandleMacroPrivateDirective(Result);
905 // If this is a .S file, treat unknown # directives as non-preprocessor
906 // directives. This is important because # may be a comment or introduce
907 // various pseudo-ops. Just return the # token and push back the following
908 // token to be lexed next time.
909 if (getLangOpts().AsmPreprocessor) {
910 Token *Toks = new Token[2];
911 // Return the # and the token after it.
915 // If the second token is a hashhash token, then we need to translate it to
916 // unknown so the token lexer doesn't try to perform token pasting.
917 if (Result.is(tok::hashhash))
918 Toks[1].setKind(tok::unknown);
920 // Enter this token stream so that we re-lex the tokens. Make sure to
921 // enable macro expansion, in case the token after the # is an identifier
923 EnterTokenStream(Toks, 2, false, true);
927 // If we reached here, the preprocessing token is not valid!
928 Diag(Result, diag::err_pp_invalid_directive);
930 // Read the rest of the PP line.
931 DiscardUntilEndOfDirective();
933 // Okay, we're done parsing the directive.
936 /// GetLineValue - Convert a numeric token into an unsigned value, emitting
937 /// Diagnostic DiagID if it is invalid, and returning the value in Val.
938 static bool GetLineValue(Token &DigitTok, unsigned &Val,
939 unsigned DiagID, Preprocessor &PP,
940 bool IsGNULineDirective=false) {
941 if (DigitTok.isNot(tok::numeric_constant)) {
942 PP.Diag(DigitTok, DiagID);
944 if (DigitTok.isNot(tok::eod))
945 PP.DiscardUntilEndOfDirective();
949 SmallString<64> IntegerBuffer;
950 IntegerBuffer.resize(DigitTok.getLength());
951 const char *DigitTokBegin = &IntegerBuffer[0];
952 bool Invalid = false;
953 unsigned ActualLength = PP.getSpelling(DigitTok, DigitTokBegin, &Invalid);
957 // Verify that we have a simple digit-sequence, and compute the value. This
958 // is always a simple digit string computed in decimal, so we do this manually
961 for (unsigned i = 0; i != ActualLength; ++i) {
962 // C++1y [lex.fcon]p1:
963 // Optional separating single quotes in a digit-sequence are ignored
964 if (DigitTokBegin[i] == '\'')
967 if (!isDigit(DigitTokBegin[i])) {
968 PP.Diag(PP.AdvanceToTokenCharacter(DigitTok.getLocation(), i),
969 diag::err_pp_line_digit_sequence) << IsGNULineDirective;
970 PP.DiscardUntilEndOfDirective();
974 unsigned NextVal = Val*10+(DigitTokBegin[i]-'0');
975 if (NextVal < Val) { // overflow.
976 PP.Diag(DigitTok, DiagID);
977 PP.DiscardUntilEndOfDirective();
983 if (DigitTokBegin[0] == '0' && Val)
984 PP.Diag(DigitTok.getLocation(), diag::warn_pp_line_decimal)
985 << IsGNULineDirective;
990 /// \brief Handle a \#line directive: C99 6.10.4.
992 /// The two acceptable forms are:
994 /// # line digit-sequence
995 /// # line digit-sequence "s-char-sequence"
997 void Preprocessor::HandleLineDirective(Token &Tok) {
998 // Read the line # and string argument. Per C99 6.10.4p5, these tokens are
1003 // Validate the number and convert it to an unsigned.
1005 if (GetLineValue(DigitTok, LineNo, diag::err_pp_line_requires_integer,*this))
1009 Diag(DigitTok, diag::ext_pp_line_zero);
1011 // Enforce C99 6.10.4p3: "The digit sequence shall not specify ... a
1012 // number greater than 2147483647". C90 requires that the line # be <= 32767.
1013 unsigned LineLimit = 32768U;
1014 if (LangOpts.C99 || LangOpts.CPlusPlus11)
1015 LineLimit = 2147483648U;
1016 if (LineNo >= LineLimit)
1017 Diag(DigitTok, diag::ext_pp_line_too_big) << LineLimit;
1018 else if (LangOpts.CPlusPlus11 && LineNo >= 32768U)
1019 Diag(DigitTok, diag::warn_cxx98_compat_pp_line_too_big);
1021 int FilenameID = -1;
1025 // If the StrTok is "eod", then it wasn't present. Otherwise, it must be a
1026 // string followed by eod.
1027 if (StrTok.is(tok::eod))
1029 else if (StrTok.isNot(tok::string_literal)) {
1030 Diag(StrTok, diag::err_pp_line_invalid_filename);
1031 return DiscardUntilEndOfDirective();
1032 } else if (StrTok.hasUDSuffix()) {
1033 Diag(StrTok, diag::err_invalid_string_udl);
1034 return DiscardUntilEndOfDirective();
1036 // Parse and validate the string, converting it into a unique ID.
1037 StringLiteralParser Literal(StrTok, *this);
1038 assert(Literal.isAscii() && "Didn't allow wide strings in");
1039 if (Literal.hadError)
1040 return DiscardUntilEndOfDirective();
1041 if (Literal.Pascal) {
1042 Diag(StrTok, diag::err_pp_linemarker_invalid_filename);
1043 return DiscardUntilEndOfDirective();
1045 FilenameID = SourceMgr.getLineTableFilenameID(Literal.GetString());
1047 // Verify that there is nothing after the string, other than EOD. Because
1048 // of C99 6.10.4p5, macros that expand to empty tokens are ok.
1049 CheckEndOfDirective("line", true);
1052 SourceMgr.AddLineNote(DigitTok.getLocation(), LineNo, FilenameID);
1055 Callbacks->FileChanged(CurPPLexer->getSourceLocation(),
1056 PPCallbacks::RenameFile,
1060 /// ReadLineMarkerFlags - Parse and validate any flags at the end of a GNU line
1061 /// marker directive.
1062 static bool ReadLineMarkerFlags(bool &IsFileEntry, bool &IsFileExit,
1063 bool &IsSystemHeader, bool &IsExternCHeader,
1068 if (FlagTok.is(tok::eod)) return false;
1069 if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag, PP))
1076 if (FlagTok.is(tok::eod)) return false;
1077 if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag,PP))
1079 } else if (FlagVal == 2) {
1082 SourceManager &SM = PP.getSourceManager();
1083 // If we are leaving the current presumed file, check to make sure the
1084 // presumed include stack isn't empty!
1086 SM.getDecomposedExpansionLoc(FlagTok.getLocation()).first;
1087 PresumedLoc PLoc = SM.getPresumedLoc(FlagTok.getLocation());
1088 if (PLoc.isInvalid())
1091 // If there is no include loc (main file) or if the include loc is in a
1092 // different physical file, then we aren't in a "1" line marker flag region.
1093 SourceLocation IncLoc = PLoc.getIncludeLoc();
1094 if (IncLoc.isInvalid() ||
1095 SM.getDecomposedExpansionLoc(IncLoc).first != CurFileID) {
1096 PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_pop);
1097 PP.DiscardUntilEndOfDirective();
1102 if (FlagTok.is(tok::eod)) return false;
1103 if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag,PP))
1107 // We must have 3 if there are still flags.
1109 PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag);
1110 PP.DiscardUntilEndOfDirective();
1114 IsSystemHeader = true;
1117 if (FlagTok.is(tok::eod)) return false;
1118 if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag, PP))
1121 // We must have 4 if there is yet another flag.
1123 PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag);
1124 PP.DiscardUntilEndOfDirective();
1128 IsExternCHeader = true;
1131 if (FlagTok.is(tok::eod)) return false;
1133 // There are no more valid flags here.
1134 PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag);
1135 PP.DiscardUntilEndOfDirective();
1139 /// HandleDigitDirective - Handle a GNU line marker directive, whose syntax is
1140 /// one of the following forms:
1143 /// # 42 "file" ('1' | '2')?
1144 /// # 42 "file" ('1' | '2')? '3' '4'?
1146 void Preprocessor::HandleDigitDirective(Token &DigitTok) {
1147 // Validate the number and convert it to an unsigned. GNU does not have a
1148 // line # limit other than it fit in 32-bits.
1150 if (GetLineValue(DigitTok, LineNo, diag::err_pp_linemarker_requires_integer,
1157 bool IsFileEntry = false, IsFileExit = false;
1158 bool IsSystemHeader = false, IsExternCHeader = false;
1159 int FilenameID = -1;
1161 // If the StrTok is "eod", then it wasn't present. Otherwise, it must be a
1162 // string followed by eod.
1163 if (StrTok.is(tok::eod))
1165 else if (StrTok.isNot(tok::string_literal)) {
1166 Diag(StrTok, diag::err_pp_linemarker_invalid_filename);
1167 return DiscardUntilEndOfDirective();
1168 } else if (StrTok.hasUDSuffix()) {
1169 Diag(StrTok, diag::err_invalid_string_udl);
1170 return DiscardUntilEndOfDirective();
1172 // Parse and validate the string, converting it into a unique ID.
1173 StringLiteralParser Literal(StrTok, *this);
1174 assert(Literal.isAscii() && "Didn't allow wide strings in");
1175 if (Literal.hadError)
1176 return DiscardUntilEndOfDirective();
1177 if (Literal.Pascal) {
1178 Diag(StrTok, diag::err_pp_linemarker_invalid_filename);
1179 return DiscardUntilEndOfDirective();
1181 FilenameID = SourceMgr.getLineTableFilenameID(Literal.GetString());
1183 // If a filename was present, read any flags that are present.
1184 if (ReadLineMarkerFlags(IsFileEntry, IsFileExit,
1185 IsSystemHeader, IsExternCHeader, *this))
1189 // Create a line note with this information.
1190 SourceMgr.AddLineNote(DigitTok.getLocation(), LineNo, FilenameID,
1191 IsFileEntry, IsFileExit,
1192 IsSystemHeader, IsExternCHeader);
1194 // If the preprocessor has callbacks installed, notify them of the #line
1195 // change. This is used so that the line marker comes out in -E mode for
1198 PPCallbacks::FileChangeReason Reason = PPCallbacks::RenameFile;
1200 Reason = PPCallbacks::EnterFile;
1201 else if (IsFileExit)
1202 Reason = PPCallbacks::ExitFile;
1203 SrcMgr::CharacteristicKind FileKind = SrcMgr::C_User;
1204 if (IsExternCHeader)
1205 FileKind = SrcMgr::C_ExternCSystem;
1206 else if (IsSystemHeader)
1207 FileKind = SrcMgr::C_System;
1209 Callbacks->FileChanged(CurPPLexer->getSourceLocation(), Reason, FileKind);
1214 /// HandleUserDiagnosticDirective - Handle a #warning or #error directive.
1216 void Preprocessor::HandleUserDiagnosticDirective(Token &Tok,
1218 // PTH doesn't emit #warning or #error directives.
1220 return CurPTHLexer->DiscardToEndOfLine();
1222 // Read the rest of the line raw. We do this because we don't want macros
1223 // to be expanded and we don't require that the tokens be valid preprocessing
1224 // tokens. For example, this is allowed: "#warning ` 'foo". GCC does
1225 // collapse multiple consequtive white space between tokens, but this isn't
1226 // specified by the standard.
1227 SmallString<128> Message;
1228 CurLexer->ReadToEndOfLine(&Message);
1230 // Find the first non-whitespace character, so that we can make the
1231 // diagnostic more succinct.
1232 StringRef Msg = StringRef(Message).ltrim(" ");
1235 Diag(Tok, diag::pp_hash_warning) << Msg;
1237 Diag(Tok, diag::err_pp_hash_error) << Msg;
1240 /// HandleIdentSCCSDirective - Handle a #ident/#sccs directive.
1242 void Preprocessor::HandleIdentSCCSDirective(Token &Tok) {
1243 // Yes, this directive is an extension.
1244 Diag(Tok, diag::ext_pp_ident_directive);
1246 // Read the string argument.
1250 // If the token kind isn't a string, it's a malformed directive.
1251 if (StrTok.isNot(tok::string_literal) &&
1252 StrTok.isNot(tok::wide_string_literal)) {
1253 Diag(StrTok, diag::err_pp_malformed_ident);
1254 if (StrTok.isNot(tok::eod))
1255 DiscardUntilEndOfDirective();
1259 if (StrTok.hasUDSuffix()) {
1260 Diag(StrTok, diag::err_invalid_string_udl);
1261 return DiscardUntilEndOfDirective();
1264 // Verify that there is nothing after the string, other than EOD.
1265 CheckEndOfDirective("ident");
1268 bool Invalid = false;
1269 std::string Str = getSpelling(StrTok, &Invalid);
1271 Callbacks->Ident(Tok.getLocation(), Str);
1275 /// \brief Handle a #public directive.
1276 void Preprocessor::HandleMacroPublicDirective(Token &Tok) {
1278 ReadMacroName(MacroNameTok, MU_Undef);
1280 // Error reading macro name? If so, diagnostic already issued.
1281 if (MacroNameTok.is(tok::eod))
1284 // Check to see if this is the last token on the #__public_macro line.
1285 CheckEndOfDirective("__public_macro");
1287 IdentifierInfo *II = MacroNameTok.getIdentifierInfo();
1288 // Okay, we finally have a valid identifier to undef.
1289 MacroDirective *MD = getLocalMacroDirective(II);
1291 // If the macro is not defined, this is an error.
1293 Diag(MacroNameTok, diag::err_pp_visibility_non_macro) << II;
1297 // Note that this macro has now been exported.
1298 appendMacroDirective(II, AllocateVisibilityMacroDirective(
1299 MacroNameTok.getLocation(), /*IsPublic=*/true));
1302 /// \brief Handle a #private directive.
1303 void Preprocessor::HandleMacroPrivateDirective(Token &Tok) {
1305 ReadMacroName(MacroNameTok, MU_Undef);
1307 // Error reading macro name? If so, diagnostic already issued.
1308 if (MacroNameTok.is(tok::eod))
1311 // Check to see if this is the last token on the #__private_macro line.
1312 CheckEndOfDirective("__private_macro");
1314 IdentifierInfo *II = MacroNameTok.getIdentifierInfo();
1315 // Okay, we finally have a valid identifier to undef.
1316 MacroDirective *MD = getLocalMacroDirective(II);
1318 // If the macro is not defined, this is an error.
1320 Diag(MacroNameTok, diag::err_pp_visibility_non_macro) << II;
1324 // Note that this macro has now been marked private.
1325 appendMacroDirective(II, AllocateVisibilityMacroDirective(
1326 MacroNameTok.getLocation(), /*IsPublic=*/false));
1329 //===----------------------------------------------------------------------===//
1330 // Preprocessor Include Directive Handling.
1331 //===----------------------------------------------------------------------===//
1333 /// GetIncludeFilenameSpelling - Turn the specified lexer token into a fully
1334 /// checked and spelled filename, e.g. as an operand of \#include. This returns
1335 /// true if the input filename was in <>'s or false if it were in ""'s. The
1336 /// caller is expected to provide a buffer that is large enough to hold the
1337 /// spelling of the filename, but is also expected to handle the case when
1338 /// this method decides to use a different buffer.
1339 bool Preprocessor::GetIncludeFilenameSpelling(SourceLocation Loc,
1340 StringRef &Buffer) {
1341 // Get the text form of the filename.
1342 assert(!Buffer.empty() && "Can't have tokens with empty spellings!");
1344 // Make sure the filename is <x> or "x".
1346 if (Buffer[0] == '<') {
1347 if (Buffer.back() != '>') {
1348 Diag(Loc, diag::err_pp_expects_filename);
1349 Buffer = StringRef();
1353 } else if (Buffer[0] == '"') {
1354 if (Buffer.back() != '"') {
1355 Diag(Loc, diag::err_pp_expects_filename);
1356 Buffer = StringRef();
1361 Diag(Loc, diag::err_pp_expects_filename);
1362 Buffer = StringRef();
1366 // Diagnose #include "" as invalid.
1367 if (Buffer.size() <= 2) {
1368 Diag(Loc, diag::err_pp_empty_filename);
1369 Buffer = StringRef();
1373 // Skip the brackets.
1374 Buffer = Buffer.substr(1, Buffer.size()-2);
1378 // \brief Handle cases where the \#include name is expanded from a macro
1379 // as multiple tokens, which need to be glued together.
1381 // This occurs for code like:
1383 // \#define FOO <a/b.h>
1386 // because in this case, "<a/b.h>" is returned as 7 tokens, not one.
1388 // This code concatenates and consumes tokens up to the '>' token. It returns
1389 // false if the > was found, otherwise it returns true if it finds and consumes
1391 bool Preprocessor::ConcatenateIncludeName(SmallString<128> &FilenameBuffer,
1392 SourceLocation &End) {
1396 while (CurTok.isNot(tok::eod)) {
1397 End = CurTok.getLocation();
1399 // FIXME: Provide code completion for #includes.
1400 if (CurTok.is(tok::code_completion)) {
1401 setCodeCompletionReached();
1406 // Append the spelling of this token to the buffer. If there was a space
1407 // before it, add it now.
1408 if (CurTok.hasLeadingSpace())
1409 FilenameBuffer.push_back(' ');
1411 // Get the spelling of the token, directly into FilenameBuffer if possible.
1412 unsigned PreAppendSize = FilenameBuffer.size();
1413 FilenameBuffer.resize(PreAppendSize+CurTok.getLength());
1415 const char *BufPtr = &FilenameBuffer[PreAppendSize];
1416 unsigned ActualLen = getSpelling(CurTok, BufPtr);
1418 // If the token was spelled somewhere else, copy it into FilenameBuffer.
1419 if (BufPtr != &FilenameBuffer[PreAppendSize])
1420 memcpy(&FilenameBuffer[PreAppendSize], BufPtr, ActualLen);
1422 // Resize FilenameBuffer to the correct size.
1423 if (CurTok.getLength() != ActualLen)
1424 FilenameBuffer.resize(PreAppendSize+ActualLen);
1426 // If we found the '>' marker, return success.
1427 if (CurTok.is(tok::greater))
1433 // If we hit the eod marker, emit an error and return true so that the caller
1434 // knows the EOD has been read.
1435 Diag(CurTok.getLocation(), diag::err_pp_expects_filename);
1439 /// \brief Push a token onto the token stream containing an annotation.
1440 static void EnterAnnotationToken(Preprocessor &PP,
1441 SourceLocation Begin, SourceLocation End,
1442 tok::TokenKind Kind, void *AnnotationVal) {
1443 // FIXME: Produce this as the current token directly, rather than
1444 // allocating a new token for it.
1445 Token *Tok = new Token[1];
1446 Tok[0].startToken();
1447 Tok[0].setKind(Kind);
1448 Tok[0].setLocation(Begin);
1449 Tok[0].setAnnotationEndLoc(End);
1450 Tok[0].setAnnotationValue(AnnotationVal);
1451 PP.EnterTokenStream(Tok, 1, true, true);
1454 /// \brief Produce a diagnostic informing the user that a #include or similar
1455 /// was implicitly treated as a module import.
1456 static void diagnoseAutoModuleImport(
1457 Preprocessor &PP, SourceLocation HashLoc, Token &IncludeTok,
1458 ArrayRef<std::pair<IdentifierInfo *, SourceLocation>> Path,
1459 SourceLocation PathEnd) {
1460 assert(PP.getLangOpts().ObjC2 && "no import syntax available");
1462 SmallString<128> PathString;
1463 for (unsigned I = 0, N = Path.size(); I != N; ++I) {
1466 PathString += Path[I].first->getName();
1468 int IncludeKind = 0;
1470 switch (IncludeTok.getIdentifierInfo()->getPPKeywordID()) {
1471 case tok::pp_include:
1475 case tok::pp_import:
1479 case tok::pp_include_next:
1483 case tok::pp___include_macros:
1488 llvm_unreachable("unknown include directive kind");
1491 CharSourceRange ReplaceRange(SourceRange(HashLoc, PathEnd),
1492 /*IsTokenRange=*/false);
1493 PP.Diag(HashLoc, diag::warn_auto_module_import)
1494 << IncludeKind << PathString
1495 << FixItHint::CreateReplacement(ReplaceRange,
1496 ("@import " + PathString + ";").str());
1499 /// HandleIncludeDirective - The "\#include" tokens have just been read, read
1500 /// the file to be included from the lexer, then include it! This is a common
1501 /// routine with functionality shared between \#include, \#include_next and
1502 /// \#import. LookupFrom is set when this is a \#include_next directive, it
1503 /// specifies the file to start searching from.
1504 void Preprocessor::HandleIncludeDirective(SourceLocation HashLoc,
1506 const DirectoryLookup *LookupFrom,
1507 const FileEntry *LookupFromFile,
1511 CurPPLexer->LexIncludeFilename(FilenameTok);
1513 // Reserve a buffer to get the spelling.
1514 SmallString<128> FilenameBuffer;
1517 SourceLocation CharEnd; // the end of this directive, in characters
1519 switch (FilenameTok.getKind()) {
1521 // If the token kind is EOD, the error has already been diagnosed.
1524 case tok::angle_string_literal:
1525 case tok::string_literal:
1526 Filename = getSpelling(FilenameTok, FilenameBuffer);
1527 End = FilenameTok.getLocation();
1528 CharEnd = End.getLocWithOffset(FilenameTok.getLength());
1532 // This could be a <foo/bar.h> file coming from a macro expansion. In this
1533 // case, glue the tokens together into FilenameBuffer and interpret those.
1534 FilenameBuffer.push_back('<');
1535 if (ConcatenateIncludeName(FilenameBuffer, End))
1536 return; // Found <eod> but no ">"? Diagnostic already emitted.
1537 Filename = FilenameBuffer;
1538 CharEnd = End.getLocWithOffset(1);
1541 Diag(FilenameTok.getLocation(), diag::err_pp_expects_filename);
1542 DiscardUntilEndOfDirective();
1546 CharSourceRange FilenameRange
1547 = CharSourceRange::getCharRange(FilenameTok.getLocation(), CharEnd);
1548 StringRef OriginalFilename = Filename;
1550 GetIncludeFilenameSpelling(FilenameTok.getLocation(), Filename);
1551 // If GetIncludeFilenameSpelling set the start ptr to null, there was an
1553 if (Filename.empty()) {
1554 DiscardUntilEndOfDirective();
1558 // Verify that there is nothing after the filename, other than EOD. Note that
1559 // we allow macros that expand to nothing after the filename, because this
1560 // falls into the category of "#include pp-tokens new-line" specified in
1562 CheckEndOfDirective(IncludeTok.getIdentifierInfo()->getNameStart(), true);
1564 // Check that we don't have infinite #include recursion.
1565 if (IncludeMacroStack.size() == MaxAllowedIncludeStackDepth-1) {
1566 Diag(FilenameTok, diag::err_pp_include_too_deep);
1570 // Complain about attempts to #include files in an audit pragma.
1571 if (PragmaARCCFCodeAuditedLoc.isValid()) {
1572 Diag(HashLoc, diag::err_pp_include_in_arc_cf_code_audited);
1573 Diag(PragmaARCCFCodeAuditedLoc, diag::note_pragma_entered_here);
1575 // Immediately leave the pragma.
1576 PragmaARCCFCodeAuditedLoc = SourceLocation();
1579 // Complain about attempts to #include files in an assume-nonnull pragma.
1580 if (PragmaAssumeNonNullLoc.isValid()) {
1581 Diag(HashLoc, diag::err_pp_include_in_assume_nonnull);
1582 Diag(PragmaAssumeNonNullLoc, diag::note_pragma_entered_here);
1584 // Immediately leave the pragma.
1585 PragmaAssumeNonNullLoc = SourceLocation();
1588 if (HeaderInfo.HasIncludeAliasMap()) {
1589 // Map the filename with the brackets still attached. If the name doesn't
1590 // map to anything, fall back on the filename we've already gotten the
1592 StringRef NewName = HeaderInfo.MapHeaderToIncludeAlias(OriginalFilename);
1593 if (!NewName.empty())
1597 // Search include directories.
1598 const DirectoryLookup *CurDir;
1599 SmallString<1024> SearchPath;
1600 SmallString<1024> RelativePath;
1601 // We get the raw path only if we have 'Callbacks' to which we later pass
1603 ModuleMap::KnownHeader SuggestedModule;
1604 SourceLocation FilenameLoc = FilenameTok.getLocation();
1605 SmallString<128> NormalizedPath;
1606 if (LangOpts.MSVCCompat) {
1607 NormalizedPath = Filename.str();
1608 #ifndef LLVM_ON_WIN32
1609 llvm::sys::path::native(NormalizedPath);
1612 const FileEntry *File = LookupFile(
1613 FilenameLoc, LangOpts.MSVCCompat ? NormalizedPath.c_str() : Filename,
1614 isAngled, LookupFrom, LookupFromFile, CurDir,
1615 Callbacks ? &SearchPath : nullptr, Callbacks ? &RelativePath : nullptr,
1620 // Give the clients a chance to recover.
1621 SmallString<128> RecoveryPath;
1622 if (Callbacks->FileNotFound(Filename, RecoveryPath)) {
1623 if (const DirectoryEntry *DE = FileMgr.getDirectory(RecoveryPath)) {
1624 // Add the recovery path to the list of search paths.
1625 DirectoryLookup DL(DE, SrcMgr::C_User, false);
1626 HeaderInfo.AddSearchPath(DL, isAngled);
1628 // Try the lookup again, skipping the cache.
1631 LangOpts.MSVCCompat ? NormalizedPath.c_str() : Filename, isAngled,
1632 LookupFrom, LookupFromFile, CurDir, nullptr, nullptr,
1633 &SuggestedModule, /*SkipCache*/ true);
1638 if (!SuppressIncludeNotFoundError) {
1639 // If the file could not be located and it was included via angle
1640 // brackets, we can attempt a lookup as though it were a quoted path to
1641 // provide the user with a possible fixit.
1645 LangOpts.MSVCCompat ? NormalizedPath.c_str() : Filename, false,
1646 LookupFrom, LookupFromFile, CurDir,
1647 Callbacks ? &SearchPath : nullptr,
1648 Callbacks ? &RelativePath : nullptr,
1651 SourceRange Range(FilenameTok.getLocation(), CharEnd);
1652 Diag(FilenameTok, diag::err_pp_file_not_found_not_fatal) <<
1654 FixItHint::CreateReplacement(Range, "\"" + Filename.str() + "\"");
1658 // If the file is still not found, just go with the vanilla diagnostic
1660 Diag(FilenameTok, diag::err_pp_file_not_found) << Filename;
1664 // Should we enter the source file? Set to false if either the source file is
1665 // known to have no effect beyond its effect on module visibility -- that is,
1666 // if it's got an include guard that is already defined or is a modular header
1667 // we've imported or already built.
1668 bool ShouldEnter = true;
1670 // Determine whether we should try to import the module for this #include, if
1671 // there is one. Don't do so if precompiled module support is disabled or we
1672 // are processing this module textually (because we're building the module).
1673 if (File && SuggestedModule && getLangOpts().Modules &&
1674 SuggestedModule.getModule()->getTopLevelModuleName() !=
1675 getLangOpts().CurrentModule &&
1676 SuggestedModule.getModule()->getTopLevelModuleName() !=
1677 getLangOpts().ImplementationOfModule) {
1679 // If this include corresponds to a module but that module is
1680 // unavailable, diagnose the situation and bail out.
1681 if (!SuggestedModule.getModule()->isAvailable()) {
1682 clang::Module::Requirement Requirement;
1683 clang::Module::UnresolvedHeaderDirective MissingHeader;
1684 Module *M = SuggestedModule.getModule();
1685 // Identify the cause.
1686 (void)M->isAvailable(getLangOpts(), getTargetInfo(), Requirement,
1688 if (MissingHeader.FileNameLoc.isValid()) {
1689 Diag(MissingHeader.FileNameLoc, diag::err_module_header_missing)
1690 << MissingHeader.IsUmbrella << MissingHeader.FileName;
1692 Diag(M->DefinitionLoc, diag::err_module_unavailable)
1693 << M->getFullModuleName() << Requirement.second << Requirement.first;
1695 Diag(FilenameTok.getLocation(),
1696 diag::note_implicit_top_level_module_import_here)
1697 << M->getTopLevelModuleName();
1701 // Compute the module access path corresponding to this module.
1702 // FIXME: Should we have a second loadModule() overload to avoid this
1703 // extra lookup step?
1704 SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 2> Path;
1705 for (Module *Mod = SuggestedModule.getModule(); Mod; Mod = Mod->Parent)
1706 Path.push_back(std::make_pair(getIdentifierInfo(Mod->Name),
1707 FilenameTok.getLocation()));
1708 std::reverse(Path.begin(), Path.end());
1710 // Warn that we're replacing the include/import with a module import.
1711 // We only do this in Objective-C, where we have a module-import syntax.
1712 if (getLangOpts().ObjC2)
1713 diagnoseAutoModuleImport(*this, HashLoc, IncludeTok, Path, CharEnd);
1715 // Load the module to import its macros. We'll make the declarations
1716 // visible when the parser gets here.
1717 // FIXME: Pass SuggestedModule in here rather than converting it to a path
1718 // and making the module loader convert it back again.
1719 ModuleLoadResult Imported = TheModuleLoader.loadModule(
1720 IncludeTok.getLocation(), Path, Module::Hidden,
1721 /*IsIncludeDirective=*/true);
1722 assert((Imported == nullptr || Imported == SuggestedModule.getModule()) &&
1723 "the imported module is different than the suggested one");
1726 ShouldEnter = false;
1727 else if (Imported.isMissingExpected()) {
1728 // We failed to find a submodule that we assumed would exist (because it
1729 // was in the directory of an umbrella header, for instance), but no
1730 // actual module exists for it (because the umbrella header is
1731 // incomplete). Treat this as a textual inclusion.
1732 SuggestedModule = ModuleMap::KnownHeader();
1734 // We hit an error processing the import. Bail out.
1735 if (hadModuleLoaderFatalFailure()) {
1736 // With a fatal failure in the module loader, we abort parsing.
1737 Token &Result = IncludeTok;
1739 Result.startToken();
1740 CurLexer->FormTokenWithChars(Result, CurLexer->BufferEnd, tok::eof);
1741 CurLexer->cutOffLexing();
1743 assert(CurPTHLexer && "#include but no current lexer set!");
1744 CurPTHLexer->getEOF(Result);
1752 // Notify the callback object that we've seen an inclusion directive.
1753 Callbacks->InclusionDirective(
1754 HashLoc, IncludeTok,
1755 LangOpts.MSVCCompat ? NormalizedPath.c_str() : Filename, isAngled,
1756 FilenameRange, File, SearchPath, RelativePath,
1757 ShouldEnter ? nullptr : SuggestedModule.getModule());
1763 // The #included file will be considered to be a system header if either it is
1764 // in a system include directory, or if the #includer is a system include
1766 SrcMgr::CharacteristicKind FileCharacter =
1767 std::max(HeaderInfo.getFileDirFlavor(File),
1768 SourceMgr.getFileCharacteristic(FilenameTok.getLocation()));
1770 // FIXME: If we have a suggested module, and we've already visited this file,
1771 // don't bother entering it again. We know it has no further effect.
1773 // Ask HeaderInfo if we should enter this #include file. If not, #including
1774 // this file will have no effect.
1776 !HeaderInfo.ShouldEnterIncludeFile(*this, File, isImport,
1777 SuggestedModule.getModule())) {
1778 ShouldEnter = false;
1780 Callbacks->FileSkipped(*File, FilenameTok, FileCharacter);
1783 // If we don't need to enter the file, stop now.
1785 // If this is a module import, make it visible if needed.
1786 if (auto *M = SuggestedModule.getModule()) {
1787 makeModuleVisible(M, HashLoc);
1789 if (IncludeTok.getIdentifierInfo()->getPPKeywordID() !=
1790 tok::pp___include_macros)
1791 EnterAnnotationToken(*this, HashLoc, End, tok::annot_module_include, M);
1796 // Look up the file, create a File ID for it.
1797 SourceLocation IncludePos = End;
1798 // If the filename string was the result of macro expansions, set the include
1799 // position on the file where it will be included and after the expansions.
1800 if (IncludePos.isMacroID())
1801 IncludePos = SourceMgr.getExpansionRange(IncludePos).second;
1802 FileID FID = SourceMgr.createFileID(File, IncludePos, FileCharacter);
1803 assert(FID.isValid() && "Expected valid file ID");
1805 // If all is good, enter the new file!
1806 if (EnterSourceFile(FID, CurDir, FilenameTok.getLocation()))
1809 // Determine if we're switching to building a new submodule, and which one.
1810 if (auto *M = SuggestedModule.getModule()) {
1811 assert(!CurSubmodule && "should not have marked this as a module yet");
1814 // Let the macro handling code know that any future macros are within
1815 // the new submodule.
1816 EnterSubmodule(M, HashLoc);
1818 // Let the parser know that any future declarations are within the new
1820 // FIXME: There's no point doing this if we're handling a #__include_macros
1822 EnterAnnotationToken(*this, HashLoc, End, tok::annot_module_begin, M);
1826 /// HandleIncludeNextDirective - Implements \#include_next.
1828 void Preprocessor::HandleIncludeNextDirective(SourceLocation HashLoc,
1829 Token &IncludeNextTok) {
1830 Diag(IncludeNextTok, diag::ext_pp_include_next_directive);
1832 // #include_next is like #include, except that we start searching after
1833 // the current found directory. If we can't do this, issue a
1835 const DirectoryLookup *Lookup = CurDirLookup;
1836 const FileEntry *LookupFromFile = nullptr;
1837 if (isInPrimaryFile()) {
1839 Diag(IncludeNextTok, diag::pp_include_next_in_primary);
1840 } else if (CurSubmodule) {
1841 // Start looking up in the directory *after* the one in which the current
1842 // file would be found, if any.
1843 assert(CurPPLexer && "#include_next directive in macro?");
1844 LookupFromFile = CurPPLexer->getFileEntry();
1846 } else if (!Lookup) {
1847 Diag(IncludeNextTok, diag::pp_include_next_absolute_path);
1849 // Start looking up in the next directory.
1853 return HandleIncludeDirective(HashLoc, IncludeNextTok, Lookup,
1857 /// HandleMicrosoftImportDirective - Implements \#import for Microsoft Mode
1858 void Preprocessor::HandleMicrosoftImportDirective(Token &Tok) {
1859 // The Microsoft #import directive takes a type library and generates header
1860 // files from it, and includes those. This is beyond the scope of what clang
1861 // does, so we ignore it and error out. However, #import can optionally have
1862 // trailing attributes that span multiple lines. We're going to eat those
1863 // so we can continue processing from there.
1864 Diag(Tok, diag::err_pp_import_directive_ms );
1866 // Read tokens until we get to the end of the directive. Note that the
1867 // directive can be split over multiple lines using the backslash character.
1868 DiscardUntilEndOfDirective();
1871 /// HandleImportDirective - Implements \#import.
1873 void Preprocessor::HandleImportDirective(SourceLocation HashLoc,
1875 if (!LangOpts.ObjC1) { // #import is standard for ObjC.
1876 if (LangOpts.MSVCCompat)
1877 return HandleMicrosoftImportDirective(ImportTok);
1878 Diag(ImportTok, diag::ext_pp_import_directive);
1880 return HandleIncludeDirective(HashLoc, ImportTok, nullptr, nullptr, true);
1883 /// HandleIncludeMacrosDirective - The -imacros command line option turns into a
1884 /// pseudo directive in the predefines buffer. This handles it by sucking all
1885 /// tokens through the preprocessor and discarding them (only keeping the side
1886 /// effects on the preprocessor).
1887 void Preprocessor::HandleIncludeMacrosDirective(SourceLocation HashLoc,
1888 Token &IncludeMacrosTok) {
1889 // This directive should only occur in the predefines buffer. If not, emit an
1890 // error and reject it.
1891 SourceLocation Loc = IncludeMacrosTok.getLocation();
1892 if (strcmp(SourceMgr.getBufferName(Loc), "<built-in>") != 0) {
1893 Diag(IncludeMacrosTok.getLocation(),
1894 diag::pp_include_macros_out_of_predefines);
1895 DiscardUntilEndOfDirective();
1899 // Treat this as a normal #include for checking purposes. If this is
1900 // successful, it will push a new lexer onto the include stack.
1901 HandleIncludeDirective(HashLoc, IncludeMacrosTok);
1906 assert(TmpTok.isNot(tok::eof) && "Didn't find end of -imacros!");
1907 } while (TmpTok.isNot(tok::hashhash));
1910 //===----------------------------------------------------------------------===//
1911 // Preprocessor Macro Directive Handling.
1912 //===----------------------------------------------------------------------===//
1914 /// ReadMacroDefinitionArgList - The ( starting an argument list of a macro
1915 /// definition has just been read. Lex the rest of the arguments and the
1916 /// closing ), updating MI with what we learn. Return true if an error occurs
1917 /// parsing the arg list.
1918 bool Preprocessor::ReadMacroDefinitionArgList(MacroInfo *MI, Token &Tok) {
1919 SmallVector<IdentifierInfo*, 32> Arguments;
1922 LexUnexpandedToken(Tok);
1923 switch (Tok.getKind()) {
1925 // Found the end of the argument list.
1926 if (Arguments.empty()) // #define FOO()
1928 // Otherwise we have #define FOO(A,)
1929 Diag(Tok, diag::err_pp_expected_ident_in_arg_list);
1931 case tok::ellipsis: // #define X(... -> C99 varargs
1933 Diag(Tok, LangOpts.CPlusPlus11 ?
1934 diag::warn_cxx98_compat_variadic_macro :
1935 diag::ext_variadic_macro);
1937 // OpenCL v1.2 s6.9.e: variadic macros are not supported.
1938 if (LangOpts.OpenCL) {
1939 Diag(Tok, diag::err_pp_opencl_variadic_macros);
1943 // Lex the token after the identifier.
1944 LexUnexpandedToken(Tok);
1945 if (Tok.isNot(tok::r_paren)) {
1946 Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
1949 // Add the __VA_ARGS__ identifier as an argument.
1950 Arguments.push_back(Ident__VA_ARGS__);
1951 MI->setIsC99Varargs();
1952 MI->setArgumentList(Arguments, BP);
1954 case tok::eod: // #define X(
1955 Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
1958 // Handle keywords and identifiers here to accept things like
1959 // #define Foo(for) for.
1960 IdentifierInfo *II = Tok.getIdentifierInfo();
1963 Diag(Tok, diag::err_pp_invalid_tok_in_arg_list);
1967 // If this is already used as an argument, it is used multiple times (e.g.
1969 if (std::find(Arguments.begin(), Arguments.end(), II) !=
1970 Arguments.end()) { // C99 6.10.3p6
1971 Diag(Tok, diag::err_pp_duplicate_name_in_arg_list) << II;
1975 // Add the argument to the macro info.
1976 Arguments.push_back(II);
1978 // Lex the token after the identifier.
1979 LexUnexpandedToken(Tok);
1981 switch (Tok.getKind()) {
1982 default: // #define X(A B
1983 Diag(Tok, diag::err_pp_expected_comma_in_arg_list);
1985 case tok::r_paren: // #define X(A)
1986 MI->setArgumentList(Arguments, BP);
1988 case tok::comma: // #define X(A,
1990 case tok::ellipsis: // #define X(A... -> GCC extension
1991 // Diagnose extension.
1992 Diag(Tok, diag::ext_named_variadic_macro);
1994 // Lex the token after the identifier.
1995 LexUnexpandedToken(Tok);
1996 if (Tok.isNot(tok::r_paren)) {
1997 Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
2001 MI->setIsGNUVarargs();
2002 MI->setArgumentList(Arguments, BP);
2009 static bool isConfigurationPattern(Token &MacroName, MacroInfo *MI,
2010 const LangOptions &LOptions) {
2011 if (MI->getNumTokens() == 1) {
2012 const Token &Value = MI->getReplacementToken(0);
2014 // Macro that is identity, like '#define inline inline' is a valid pattern.
2015 if (MacroName.getKind() == Value.getKind())
2018 // Macro that maps a keyword to the same keyword decorated with leading/
2019 // trailing underscores is a valid pattern:
2020 // #define inline __inline
2021 // #define inline __inline__
2022 // #define inline _inline (in MS compatibility mode)
2023 StringRef MacroText = MacroName.getIdentifierInfo()->getName();
2024 if (IdentifierInfo *II = Value.getIdentifierInfo()) {
2025 if (!II->isKeyword(LOptions))
2027 StringRef ValueText = II->getName();
2028 StringRef TrimmedValue = ValueText;
2029 if (!ValueText.startswith("__")) {
2030 if (ValueText.startswith("_"))
2031 TrimmedValue = TrimmedValue.drop_front(1);
2035 TrimmedValue = TrimmedValue.drop_front(2);
2036 if (TrimmedValue.endswith("__"))
2037 TrimmedValue = TrimmedValue.drop_back(2);
2039 return TrimmedValue.equals(MacroText);
2046 return MacroName.isOneOf(tok::kw_extern, tok::kw_inline, tok::kw_static,
2048 MI->getNumTokens() == 0;
2051 /// HandleDefineDirective - Implements \#define. This consumes the entire macro
2052 /// line then lets the caller lex the next real token.
2053 void Preprocessor::HandleDefineDirective(Token &DefineTok,
2054 bool ImmediatelyAfterHeaderGuard) {
2058 bool MacroShadowsKeyword;
2059 ReadMacroName(MacroNameTok, MU_Define, &MacroShadowsKeyword);
2061 // Error reading macro name? If so, diagnostic already issued.
2062 if (MacroNameTok.is(tok::eod))
2065 Token LastTok = MacroNameTok;
2067 // If we are supposed to keep comments in #defines, reenable comment saving
2069 if (CurLexer) CurLexer->SetCommentRetentionState(KeepMacroComments);
2071 // Create the new macro.
2072 MacroInfo *MI = AllocateMacroInfo(MacroNameTok.getLocation());
2075 LexUnexpandedToken(Tok);
2077 // If this is a function-like macro definition, parse the argument list,
2078 // marking each of the identifiers as being used as macro arguments. Also,
2079 // check other constraints on the first token of the macro body.
2080 if (Tok.is(tok::eod)) {
2081 if (ImmediatelyAfterHeaderGuard) {
2082 // Save this macro information since it may part of a header guard.
2083 CurPPLexer->MIOpt.SetDefinedMacro(MacroNameTok.getIdentifierInfo(),
2084 MacroNameTok.getLocation());
2086 // If there is no body to this macro, we have no special handling here.
2087 } else if (Tok.hasLeadingSpace()) {
2088 // This is a normal token with leading space. Clear the leading space
2089 // marker on the first token to get proper expansion.
2090 Tok.clearFlag(Token::LeadingSpace);
2091 } else if (Tok.is(tok::l_paren)) {
2092 // This is a function-like macro definition. Read the argument list.
2093 MI->setIsFunctionLike();
2094 if (ReadMacroDefinitionArgList(MI, LastTok)) {
2095 // Throw away the rest of the line.
2096 if (CurPPLexer->ParsingPreprocessorDirective)
2097 DiscardUntilEndOfDirective();
2101 // If this is a definition of a variadic C99 function-like macro, not using
2102 // the GNU named varargs extension, enabled __VA_ARGS__.
2104 // "Poison" __VA_ARGS__, which can only appear in the expansion of a macro.
2105 // This gets unpoisoned where it is allowed.
2106 assert(Ident__VA_ARGS__->isPoisoned() && "__VA_ARGS__ should be poisoned!");
2107 if (MI->isC99Varargs())
2108 Ident__VA_ARGS__->setIsPoisoned(false);
2110 // Read the first token after the arg list for down below.
2111 LexUnexpandedToken(Tok);
2112 } else if (LangOpts.C99 || LangOpts.CPlusPlus11) {
2113 // C99 requires whitespace between the macro definition and the body. Emit
2114 // a diagnostic for something like "#define X+".
2115 Diag(Tok, diag::ext_c99_whitespace_required_after_macro_name);
2117 // C90 6.8 TC1 says: "In the definition of an object-like macro, if the
2118 // first character of a replacement list is not a character required by
2119 // subclause 5.2.1, then there shall be white-space separation between the
2120 // identifier and the replacement list.". 5.2.1 lists this set:
2121 // "A-Za-z0-9!"#%&'()*+,_./:;<=>?[\]^_{|}~" as well as whitespace, which
2122 // is irrelevant here.
2123 bool isInvalid = false;
2124 if (Tok.is(tok::at)) // @ is not in the list above.
2126 else if (Tok.is(tok::unknown)) {
2127 // If we have an unknown token, it is something strange like "`". Since
2128 // all of valid characters would have lexed into a single character
2129 // token of some sort, we know this is not a valid case.
2133 Diag(Tok, diag::ext_missing_whitespace_after_macro_name);
2135 Diag(Tok, diag::warn_missing_whitespace_after_macro_name);
2138 if (!Tok.is(tok::eod))
2141 // Read the rest of the macro body.
2142 if (MI->isObjectLike()) {
2143 // Object-like macros are very simple, just read their body.
2144 while (Tok.isNot(tok::eod)) {
2146 MI->AddTokenToBody(Tok);
2147 // Get the next token of the macro.
2148 LexUnexpandedToken(Tok);
2152 // Otherwise, read the body of a function-like macro. While we are at it,
2153 // check C99 6.10.3.2p1: ensure that # operators are followed by macro
2154 // parameters in function-like macro expansions.
2155 while (Tok.isNot(tok::eod)) {
2158 if (Tok.isNot(tok::hash) && Tok.isNot(tok::hashhash)) {
2159 MI->AddTokenToBody(Tok);
2161 // Get the next token of the macro.
2162 LexUnexpandedToken(Tok);
2166 // If we're in -traditional mode, then we should ignore stringification
2167 // and token pasting. Mark the tokens as unknown so as not to confuse
2169 if (getLangOpts().TraditionalCPP) {
2170 Tok.setKind(tok::unknown);
2171 MI->AddTokenToBody(Tok);
2173 // Get the next token of the macro.
2174 LexUnexpandedToken(Tok);
2178 if (Tok.is(tok::hashhash)) {
2180 // If we see token pasting, check if it looks like the gcc comma
2181 // pasting extension. We'll use this information to suppress
2182 // diagnostics later on.
2184 // Get the next token of the macro.
2185 LexUnexpandedToken(Tok);
2187 if (Tok.is(tok::eod)) {
2188 MI->AddTokenToBody(LastTok);
2192 unsigned NumTokens = MI->getNumTokens();
2193 if (NumTokens && Tok.getIdentifierInfo() == Ident__VA_ARGS__ &&
2194 MI->getReplacementToken(NumTokens-1).is(tok::comma))
2195 MI->setHasCommaPasting();
2197 // Things look ok, add the '##' token to the macro.
2198 MI->AddTokenToBody(LastTok);
2202 // Get the next token of the macro.
2203 LexUnexpandedToken(Tok);
2205 // Check for a valid macro arg identifier.
2206 if (Tok.getIdentifierInfo() == nullptr ||
2207 MI->getArgumentNum(Tok.getIdentifierInfo()) == -1) {
2209 // If this is assembler-with-cpp mode, we accept random gibberish after
2210 // the '#' because '#' is often a comment character. However, change
2211 // the kind of the token to tok::unknown so that the preprocessor isn't
2213 if (getLangOpts().AsmPreprocessor && Tok.isNot(tok::eod)) {
2214 LastTok.setKind(tok::unknown);
2215 MI->AddTokenToBody(LastTok);
2218 Diag(Tok, diag::err_pp_stringize_not_parameter);
2220 // Disable __VA_ARGS__ again.
2221 Ident__VA_ARGS__->setIsPoisoned(true);
2226 // Things look ok, add the '#' and param name tokens to the macro.
2227 MI->AddTokenToBody(LastTok);
2228 MI->AddTokenToBody(Tok);
2231 // Get the next token of the macro.
2232 LexUnexpandedToken(Tok);
2236 if (MacroShadowsKeyword &&
2237 !isConfigurationPattern(MacroNameTok, MI, getLangOpts())) {
2238 Diag(MacroNameTok, diag::warn_pp_macro_hides_keyword);
2241 // Disable __VA_ARGS__ again.
2242 Ident__VA_ARGS__->setIsPoisoned(true);
2244 // Check that there is no paste (##) operator at the beginning or end of the
2245 // replacement list.
2246 unsigned NumTokens = MI->getNumTokens();
2247 if (NumTokens != 0) {
2248 if (MI->getReplacementToken(0).is(tok::hashhash)) {
2249 Diag(MI->getReplacementToken(0), diag::err_paste_at_start);
2252 if (MI->getReplacementToken(NumTokens-1).is(tok::hashhash)) {
2253 Diag(MI->getReplacementToken(NumTokens-1), diag::err_paste_at_end);
2258 MI->setDefinitionEndLoc(LastTok.getLocation());
2260 // Finally, if this identifier already had a macro defined for it, verify that
2261 // the macro bodies are identical, and issue diagnostics if they are not.
2262 if (const MacroInfo *OtherMI=getMacroInfo(MacroNameTok.getIdentifierInfo())) {
2263 // In Objective-C, ignore attempts to directly redefine the builtin
2264 // definitions of the ownership qualifiers. It's still possible to
2266 auto isObjCProtectedMacro = [](const IdentifierInfo *II) -> bool {
2267 return II->isStr("__strong") ||
2268 II->isStr("__weak") ||
2269 II->isStr("__unsafe_unretained") ||
2270 II->isStr("__autoreleasing");
2272 if (getLangOpts().ObjC1 &&
2273 SourceMgr.getFileID(OtherMI->getDefinitionLoc())
2274 == getPredefinesFileID() &&
2275 isObjCProtectedMacro(MacroNameTok.getIdentifierInfo())) {
2276 // Warn if it changes the tokens.
2277 if ((!getDiagnostics().getSuppressSystemWarnings() ||
2278 !SourceMgr.isInSystemHeader(DefineTok.getLocation())) &&
2279 !MI->isIdenticalTo(*OtherMI, *this,
2280 /*Syntactic=*/LangOpts.MicrosoftExt)) {
2281 Diag(MI->getDefinitionLoc(), diag::warn_pp_objc_macro_redef_ignored);
2283 assert(!OtherMI->isWarnIfUnused());
2287 // It is very common for system headers to have tons of macro redefinitions
2288 // and for warnings to be disabled in system headers. If this is the case,
2289 // then don't bother calling MacroInfo::isIdenticalTo.
2290 if (!getDiagnostics().getSuppressSystemWarnings() ||
2291 !SourceMgr.isInSystemHeader(DefineTok.getLocation())) {
2292 if (!OtherMI->isUsed() && OtherMI->isWarnIfUnused())
2293 Diag(OtherMI->getDefinitionLoc(), diag::pp_macro_not_used);
2295 // Warn if defining "__LINE__" and other builtins, per C99 6.10.8/4 and
2296 // C++ [cpp.predefined]p4, but allow it as an extension.
2297 if (OtherMI->isBuiltinMacro())
2298 Diag(MacroNameTok, diag::ext_pp_redef_builtin_macro);
2299 // Macros must be identical. This means all tokens and whitespace
2300 // separation must be the same. C99 6.10.3p2.
2301 else if (!OtherMI->isAllowRedefinitionsWithoutWarning() &&
2302 !MI->isIdenticalTo(*OtherMI, *this, /*Syntactic=*/LangOpts.MicrosoftExt)) {
2303 Diag(MI->getDefinitionLoc(), diag::ext_pp_macro_redef)
2304 << MacroNameTok.getIdentifierInfo();
2305 Diag(OtherMI->getDefinitionLoc(), diag::note_previous_definition);
2308 if (OtherMI->isWarnIfUnused())
2309 WarnUnusedMacroLocs.erase(OtherMI->getDefinitionLoc());
2312 DefMacroDirective *MD =
2313 appendDefMacroDirective(MacroNameTok.getIdentifierInfo(), MI);
2315 assert(!MI->isUsed());
2316 // If we need warning for not using the macro, add its location in the
2317 // warn-because-unused-macro set. If it gets used it will be removed from set.
2318 if (getSourceManager().isInMainFile(MI->getDefinitionLoc()) &&
2319 !Diags->isIgnored(diag::pp_macro_not_used, MI->getDefinitionLoc())) {
2320 MI->setIsWarnIfUnused(true);
2321 WarnUnusedMacroLocs.insert(MI->getDefinitionLoc());
2324 // If the callbacks want to know, tell them about the macro definition.
2326 Callbacks->MacroDefined(MacroNameTok, MD);
2329 /// HandleUndefDirective - Implements \#undef.
2331 void Preprocessor::HandleUndefDirective(Token &UndefTok) {
2335 ReadMacroName(MacroNameTok, MU_Undef);
2337 // Error reading macro name? If so, diagnostic already issued.
2338 if (MacroNameTok.is(tok::eod))
2341 // Check to see if this is the last token on the #undef line.
2342 CheckEndOfDirective("undef");
2344 // Okay, we have a valid identifier to undef.
2345 auto *II = MacroNameTok.getIdentifierInfo();
2346 auto MD = getMacroDefinition(II);
2348 // If the callbacks want to know, tell them about the macro #undef.
2349 // Note: no matter if the macro was defined or not.
2351 Callbacks->MacroUndefined(MacroNameTok, MD);
2353 // If the macro is not defined, this is a noop undef, just return.
2354 const MacroInfo *MI = MD.getMacroInfo();
2358 if (!MI->isUsed() && MI->isWarnIfUnused())
2359 Diag(MI->getDefinitionLoc(), diag::pp_macro_not_used);
2361 if (MI->isWarnIfUnused())
2362 WarnUnusedMacroLocs.erase(MI->getDefinitionLoc());
2364 appendMacroDirective(MacroNameTok.getIdentifierInfo(),
2365 AllocateUndefMacroDirective(MacroNameTok.getLocation()));
2369 //===----------------------------------------------------------------------===//
2370 // Preprocessor Conditional Directive Handling.
2371 //===----------------------------------------------------------------------===//
2373 /// HandleIfdefDirective - Implements the \#ifdef/\#ifndef directive. isIfndef
2374 /// is true when this is a \#ifndef directive. ReadAnyTokensBeforeDirective is
2375 /// true if any tokens have been returned or pp-directives activated before this
2376 /// \#ifndef has been lexed.
2378 void Preprocessor::HandleIfdefDirective(Token &Result, bool isIfndef,
2379 bool ReadAnyTokensBeforeDirective) {
2381 Token DirectiveTok = Result;
2384 ReadMacroName(MacroNameTok);
2386 // Error reading macro name? If so, diagnostic already issued.
2387 if (MacroNameTok.is(tok::eod)) {
2388 // Skip code until we get to #endif. This helps with recovery by not
2389 // emitting an error when the #endif is reached.
2390 SkipExcludedConditionalBlock(DirectiveTok.getLocation(),
2391 /*Foundnonskip*/false, /*FoundElse*/false);
2395 // Check to see if this is the last token on the #if[n]def line.
2396 CheckEndOfDirective(isIfndef ? "ifndef" : "ifdef");
2398 IdentifierInfo *MII = MacroNameTok.getIdentifierInfo();
2399 auto MD = getMacroDefinition(MII);
2400 MacroInfo *MI = MD.getMacroInfo();
2402 if (CurPPLexer->getConditionalStackDepth() == 0) {
2403 // If the start of a top-level #ifdef and if the macro is not defined,
2404 // inform MIOpt that this might be the start of a proper include guard.
2405 // Otherwise it is some other form of unknown conditional which we can't
2407 if (!ReadAnyTokensBeforeDirective && !MI) {
2408 assert(isIfndef && "#ifdef shouldn't reach here");
2409 CurPPLexer->MIOpt.EnterTopLevelIfndef(MII, MacroNameTok.getLocation());
2411 CurPPLexer->MIOpt.EnterTopLevelConditional();
2414 // If there is a macro, process it.
2415 if (MI) // Mark it used.
2416 markMacroAsUsed(MI);
2420 Callbacks->Ifndef(DirectiveTok.getLocation(), MacroNameTok, MD);
2422 Callbacks->Ifdef(DirectiveTok.getLocation(), MacroNameTok, MD);
2425 // Should we include the stuff contained by this directive?
2426 if (!MI == isIfndef) {
2427 // Yes, remember that we are inside a conditional, then lex the next token.
2428 CurPPLexer->pushConditionalLevel(DirectiveTok.getLocation(),
2429 /*wasskip*/false, /*foundnonskip*/true,
2430 /*foundelse*/false);
2432 // No, skip the contents of this block.
2433 SkipExcludedConditionalBlock(DirectiveTok.getLocation(),
2434 /*Foundnonskip*/false,
2435 /*FoundElse*/false);
2439 /// HandleIfDirective - Implements the \#if directive.
2441 void Preprocessor::HandleIfDirective(Token &IfToken,
2442 bool ReadAnyTokensBeforeDirective) {
2445 // Parse and evaluate the conditional expression.
2446 IdentifierInfo *IfNDefMacro = nullptr;
2447 const SourceLocation ConditionalBegin = CurPPLexer->getSourceLocation();
2448 const bool ConditionalTrue = EvaluateDirectiveExpression(IfNDefMacro);
2449 const SourceLocation ConditionalEnd = CurPPLexer->getSourceLocation();
2451 // If this condition is equivalent to #ifndef X, and if this is the first
2452 // directive seen, handle it for the multiple-include optimization.
2453 if (CurPPLexer->getConditionalStackDepth() == 0) {
2454 if (!ReadAnyTokensBeforeDirective && IfNDefMacro && ConditionalTrue)
2455 // FIXME: Pass in the location of the macro name, not the 'if' token.
2456 CurPPLexer->MIOpt.EnterTopLevelIfndef(IfNDefMacro, IfToken.getLocation());
2458 CurPPLexer->MIOpt.EnterTopLevelConditional();
2462 Callbacks->If(IfToken.getLocation(),
2463 SourceRange(ConditionalBegin, ConditionalEnd),
2464 (ConditionalTrue ? PPCallbacks::CVK_True : PPCallbacks::CVK_False));
2466 // Should we include the stuff contained by this directive?
2467 if (ConditionalTrue) {
2468 // Yes, remember that we are inside a conditional, then lex the next token.
2469 CurPPLexer->pushConditionalLevel(IfToken.getLocation(), /*wasskip*/false,
2470 /*foundnonskip*/true, /*foundelse*/false);
2472 // No, skip the contents of this block.
2473 SkipExcludedConditionalBlock(IfToken.getLocation(), /*Foundnonskip*/false,
2474 /*FoundElse*/false);
2478 /// HandleEndifDirective - Implements the \#endif directive.
2480 void Preprocessor::HandleEndifDirective(Token &EndifToken) {
2483 // Check that this is the whole directive.
2484 CheckEndOfDirective("endif");
2486 PPConditionalInfo CondInfo;
2487 if (CurPPLexer->popConditionalLevel(CondInfo)) {
2488 // No conditionals on the stack: this is an #endif without an #if.
2489 Diag(EndifToken, diag::err_pp_endif_without_if);
2493 // If this the end of a top-level #endif, inform MIOpt.
2494 if (CurPPLexer->getConditionalStackDepth() == 0)
2495 CurPPLexer->MIOpt.ExitTopLevelConditional();
2497 assert(!CondInfo.WasSkipping && !CurPPLexer->LexingRawMode &&
2498 "This code should only be reachable in the non-skipping case!");
2501 Callbacks->Endif(EndifToken.getLocation(), CondInfo.IfLoc);
2504 /// HandleElseDirective - Implements the \#else directive.
2506 void Preprocessor::HandleElseDirective(Token &Result) {
2509 // #else directive in a non-skipping conditional... start skipping.
2510 CheckEndOfDirective("else");
2512 PPConditionalInfo CI;
2513 if (CurPPLexer->popConditionalLevel(CI)) {
2514 Diag(Result, diag::pp_err_else_without_if);
2518 // If this is a top-level #else, inform the MIOpt.
2519 if (CurPPLexer->getConditionalStackDepth() == 0)
2520 CurPPLexer->MIOpt.EnterTopLevelConditional();
2522 // If this is a #else with a #else before it, report the error.
2523 if (CI.FoundElse) Diag(Result, diag::pp_err_else_after_else);
2526 Callbacks->Else(Result.getLocation(), CI.IfLoc);
2528 // Finally, skip the rest of the contents of this block.
2529 SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true,
2530 /*FoundElse*/true, Result.getLocation());
2533 /// HandleElifDirective - Implements the \#elif directive.
2535 void Preprocessor::HandleElifDirective(Token &ElifToken) {
2538 // #elif directive in a non-skipping conditional... start skipping.
2539 // We don't care what the condition is, because we will always skip it (since
2540 // the block immediately before it was included).
2541 const SourceLocation ConditionalBegin = CurPPLexer->getSourceLocation();
2542 DiscardUntilEndOfDirective();
2543 const SourceLocation ConditionalEnd = CurPPLexer->getSourceLocation();
2545 PPConditionalInfo CI;
2546 if (CurPPLexer->popConditionalLevel(CI)) {
2547 Diag(ElifToken, diag::pp_err_elif_without_if);
2551 // If this is a top-level #elif, inform the MIOpt.
2552 if (CurPPLexer->getConditionalStackDepth() == 0)
2553 CurPPLexer->MIOpt.EnterTopLevelConditional();
2555 // If this is a #elif with a #else before it, report the error.
2556 if (CI.FoundElse) Diag(ElifToken, diag::pp_err_elif_after_else);
2559 Callbacks->Elif(ElifToken.getLocation(),
2560 SourceRange(ConditionalBegin, ConditionalEnd),
2561 PPCallbacks::CVK_NotEvaluated, CI.IfLoc);
2563 // Finally, skip the rest of the contents of this block.
2564 SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true,
2565 /*FoundElse*/CI.FoundElse,
2566 ElifToken.getLocation());