Home | History | Annotate | Download | only in Lex
      1 //===--- MacroExpansion.cpp - Top level Macro Expansion -------------------===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     10 // This file implements the top level handling of macro expansion for the
     11 // preprocessor.
     12 //
     13 //===----------------------------------------------------------------------===//
     14 
     15 #include "clang/Lex/Preprocessor.h"
     16 #include "clang/Basic/Attributes.h"
     17 #include "clang/Basic/FileManager.h"
     18 #include "clang/Basic/SourceManager.h"
     19 #include "clang/Basic/TargetInfo.h"
     20 #include "clang/Lex/CodeCompletionHandler.h"
     21 #include "clang/Lex/ExternalPreprocessorSource.h"
     22 #include "clang/Lex/LexDiagnostic.h"
     23 #include "clang/Lex/MacroArgs.h"
     24 #include "clang/Lex/MacroInfo.h"
     25 #include "llvm/ADT/STLExtras.h"
     26 #include "llvm/ADT/SmallString.h"
     27 #include "llvm/ADT/StringSwitch.h"
     28 #include "llvm/Config/llvm-config.h"
     29 #include "llvm/Support/ErrorHandling.h"
     30 #include "llvm/Support/Format.h"
     31 #include "llvm/Support/raw_ostream.h"
     32 #include <cstdio>
     33 #include <ctime>
     34 using namespace clang;
     35 
     36 MacroDirective *
     37 Preprocessor::getMacroDirectiveHistory(const IdentifierInfo *II) const {
     38   assert(II->hadMacroDefinition() && "Identifier has not been not a macro!");
     39 
     40   macro_iterator Pos = Macros.find(II);
     41   assert(Pos != Macros.end() && "Identifier macro info is missing!");
     42   return Pos->second;
     43 }
     44 
     45 void Preprocessor::appendMacroDirective(IdentifierInfo *II, MacroDirective *MD){
     46   assert(MD && "MacroDirective should be non-zero!");
     47   assert(!MD->getPrevious() && "Already attached to a MacroDirective history.");
     48 
     49   MacroDirective *&StoredMD = Macros[II];
     50   MD->setPrevious(StoredMD);
     51   StoredMD = MD;
     52   // Setup the identifier as having associated macro history.
     53   II->setHasMacroDefinition(true);
     54   if (!MD->isDefined())
     55     II->setHasMacroDefinition(false);
     56   bool isImportedMacro = isa<DefMacroDirective>(MD) &&
     57                          cast<DefMacroDirective>(MD)->isImported();
     58   if (II->isFromAST() && !isImportedMacro)
     59     II->setChangedSinceDeserialization();
     60 }
     61 
     62 void Preprocessor::setLoadedMacroDirective(IdentifierInfo *II,
     63                                            MacroDirective *MD) {
     64   assert(II && MD);
     65   MacroDirective *&StoredMD = Macros[II];
     66   assert(!StoredMD &&
     67          "the macro history was modified before initializing it from a pch");
     68   StoredMD = MD;
     69   // Setup the identifier as having associated macro history.
     70   II->setHasMacroDefinition(true);
     71   if (!MD->isDefined())
     72     II->setHasMacroDefinition(false);
     73 }
     74 
     75 /// RegisterBuiltinMacro - Register the specified identifier in the identifier
     76 /// table and mark it as a builtin macro to be expanded.
     77 static IdentifierInfo *RegisterBuiltinMacro(Preprocessor &PP, const char *Name){
     78   // Get the identifier.
     79   IdentifierInfo *Id = PP.getIdentifierInfo(Name);
     80 
     81   // Mark it as being a macro that is builtin.
     82   MacroInfo *MI = PP.AllocateMacroInfo(SourceLocation());
     83   MI->setIsBuiltinMacro();
     84   PP.appendDefMacroDirective(Id, MI);
     85   return Id;
     86 }
     87 
     88 
     89 /// RegisterBuiltinMacros - Register builtin macros, such as __LINE__ with the
     90 /// identifier table.
     91 void Preprocessor::RegisterBuiltinMacros() {
     92   Ident__LINE__ = RegisterBuiltinMacro(*this, "__LINE__");
     93   Ident__FILE__ = RegisterBuiltinMacro(*this, "__FILE__");
     94   Ident__DATE__ = RegisterBuiltinMacro(*this, "__DATE__");
     95   Ident__TIME__ = RegisterBuiltinMacro(*this, "__TIME__");
     96   Ident__COUNTER__ = RegisterBuiltinMacro(*this, "__COUNTER__");
     97   Ident_Pragma  = RegisterBuiltinMacro(*this, "_Pragma");
     98 
     99   // C++ Standing Document Extensions.
    100   Ident__has_cpp_attribute = RegisterBuiltinMacro(*this, "__has_cpp_attribute");
    101 
    102   // GCC Extensions.
    103   Ident__BASE_FILE__     = RegisterBuiltinMacro(*this, "__BASE_FILE__");
    104   Ident__INCLUDE_LEVEL__ = RegisterBuiltinMacro(*this, "__INCLUDE_LEVEL__");
    105   Ident__TIMESTAMP__     = RegisterBuiltinMacro(*this, "__TIMESTAMP__");
    106 
    107   // Microsoft Extensions.
    108   if (LangOpts.MicrosoftExt) {
    109     Ident__identifier = RegisterBuiltinMacro(*this, "__identifier");
    110     Ident__pragma = RegisterBuiltinMacro(*this, "__pragma");
    111   } else {
    112     Ident__identifier = nullptr;
    113     Ident__pragma = nullptr;
    114   }
    115 
    116   // Clang Extensions.
    117   Ident__has_feature      = RegisterBuiltinMacro(*this, "__has_feature");
    118   Ident__has_extension    = RegisterBuiltinMacro(*this, "__has_extension");
    119   Ident__has_builtin      = RegisterBuiltinMacro(*this, "__has_builtin");
    120   Ident__has_attribute    = RegisterBuiltinMacro(*this, "__has_attribute");
    121   Ident__has_declspec = RegisterBuiltinMacro(*this, "__has_declspec_attribute");
    122   Ident__has_include      = RegisterBuiltinMacro(*this, "__has_include");
    123   Ident__has_include_next = RegisterBuiltinMacro(*this, "__has_include_next");
    124   Ident__has_warning      = RegisterBuiltinMacro(*this, "__has_warning");
    125   Ident__is_identifier    = RegisterBuiltinMacro(*this, "__is_identifier");
    126 
    127   // Modules.
    128   if (LangOpts.Modules) {
    129     Ident__building_module  = RegisterBuiltinMacro(*this, "__building_module");
    130 
    131     // __MODULE__
    132     if (!LangOpts.CurrentModule.empty())
    133       Ident__MODULE__ = RegisterBuiltinMacro(*this, "__MODULE__");
    134     else
    135       Ident__MODULE__ = nullptr;
    136   } else {
    137     Ident__building_module = nullptr;
    138     Ident__MODULE__ = nullptr;
    139   }
    140 }
    141 
    142 /// isTrivialSingleTokenExpansion - Return true if MI, which has a single token
    143 /// in its expansion, currently expands to that token literally.
    144 static bool isTrivialSingleTokenExpansion(const MacroInfo *MI,
    145                                           const IdentifierInfo *MacroIdent,
    146                                           Preprocessor &PP) {
    147   IdentifierInfo *II = MI->getReplacementToken(0).getIdentifierInfo();
    148 
    149   // If the token isn't an identifier, it's always literally expanded.
    150   if (!II) return true;
    151 
    152   // If the information about this identifier is out of date, update it from
    153   // the external source.
    154   if (II->isOutOfDate())
    155     PP.getExternalSource()->updateOutOfDateIdentifier(*II);
    156 
    157   // If the identifier is a macro, and if that macro is enabled, it may be
    158   // expanded so it's not a trivial expansion.
    159   if (II->hasMacroDefinition() && PP.getMacroInfo(II)->isEnabled() &&
    160       // Fast expanding "#define X X" is ok, because X would be disabled.
    161       II != MacroIdent)
    162     return false;
    163 
    164   // If this is an object-like macro invocation, it is safe to trivially expand
    165   // it.
    166   if (MI->isObjectLike()) return true;
    167 
    168   // If this is a function-like macro invocation, it's safe to trivially expand
    169   // as long as the identifier is not a macro argument.
    170   for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
    171        I != E; ++I)
    172     if (*I == II)
    173       return false;   // Identifier is a macro argument.
    174 
    175   return true;
    176 }
    177 
    178 
    179 /// isNextPPTokenLParen - Determine whether the next preprocessor token to be
    180 /// lexed is a '('.  If so, consume the token and return true, if not, this
    181 /// method should have no observable side-effect on the lexed tokens.
    182 bool Preprocessor::isNextPPTokenLParen() {
    183   // Do some quick tests for rejection cases.
    184   unsigned Val;
    185   if (CurLexer)
    186     Val = CurLexer->isNextPPTokenLParen();
    187   else if (CurPTHLexer)
    188     Val = CurPTHLexer->isNextPPTokenLParen();
    189   else
    190     Val = CurTokenLexer->isNextTokenLParen();
    191 
    192   if (Val == 2) {
    193     // We have run off the end.  If it's a source file we don't
    194     // examine enclosing ones (C99 5.1.1.2p4).  Otherwise walk up the
    195     // macro stack.
    196     if (CurPPLexer)
    197       return false;
    198     for (unsigned i = IncludeMacroStack.size(); i != 0; --i) {
    199       IncludeStackInfo &Entry = IncludeMacroStack[i-1];
    200       if (Entry.TheLexer)
    201         Val = Entry.TheLexer->isNextPPTokenLParen();
    202       else if (Entry.ThePTHLexer)
    203         Val = Entry.ThePTHLexer->isNextPPTokenLParen();
    204       else
    205         Val = Entry.TheTokenLexer->isNextTokenLParen();
    206 
    207       if (Val != 2)
    208         break;
    209 
    210       // Ran off the end of a source file?
    211       if (Entry.ThePPLexer)
    212         return false;
    213     }
    214   }
    215 
    216   // Okay, if we know that the token is a '(', lex it and return.  Otherwise we
    217   // have found something that isn't a '(' or we found the end of the
    218   // translation unit.  In either case, return false.
    219   return Val == 1;
    220 }
    221 
    222 /// HandleMacroExpandedIdentifier - If an identifier token is read that is to be
    223 /// expanded as a macro, handle it and return the next token as 'Identifier'.
    224 bool Preprocessor::HandleMacroExpandedIdentifier(Token &Identifier,
    225                                                  MacroDirective *MD) {
    226   MacroDirective::DefInfo Def = MD->getDefinition();
    227   assert(Def.isValid());
    228   MacroInfo *MI = Def.getMacroInfo();
    229 
    230   // If this is a macro expansion in the "#if !defined(x)" line for the file,
    231   // then the macro could expand to different things in other contexts, we need
    232   // to disable the optimization in this case.
    233   if (CurPPLexer) CurPPLexer->MIOpt.ExpandedMacro();
    234 
    235   // If this is a builtin macro, like __LINE__ or _Pragma, handle it specially.
    236   if (MI->isBuiltinMacro()) {
    237     if (Callbacks) Callbacks->MacroExpands(Identifier, MD,
    238                                            Identifier.getLocation(),
    239                                            /*Args=*/nullptr);
    240     ExpandBuiltinMacro(Identifier);
    241     return true;
    242   }
    243 
    244   /// Args - If this is a function-like macro expansion, this contains,
    245   /// for each macro argument, the list of tokens that were provided to the
    246   /// invocation.
    247   MacroArgs *Args = nullptr;
    248 
    249   // Remember where the end of the expansion occurred.  For an object-like
    250   // macro, this is the identifier.  For a function-like macro, this is the ')'.
    251   SourceLocation ExpansionEnd = Identifier.getLocation();
    252 
    253   // If this is a function-like macro, read the arguments.
    254   if (MI->isFunctionLike()) {
    255     // Remember that we are now parsing the arguments to a macro invocation.
    256     // Preprocessor directives used inside macro arguments are not portable, and
    257     // this enables the warning.
    258     InMacroArgs = true;
    259     Args = ReadFunctionLikeMacroArgs(Identifier, MI, ExpansionEnd);
    260 
    261     // Finished parsing args.
    262     InMacroArgs = false;
    263 
    264     // If there was an error parsing the arguments, bail out.
    265     if (!Args) return true;
    266 
    267     ++NumFnMacroExpanded;
    268   } else {
    269     ++NumMacroExpanded;
    270   }
    271 
    272   // Notice that this macro has been used.
    273   markMacroAsUsed(MI);
    274 
    275   // Remember where the token is expanded.
    276   SourceLocation ExpandLoc = Identifier.getLocation();
    277   SourceRange ExpansionRange(ExpandLoc, ExpansionEnd);
    278 
    279   if (Callbacks) {
    280     if (InMacroArgs) {
    281       // We can have macro expansion inside a conditional directive while
    282       // reading the function macro arguments. To ensure, in that case, that
    283       // MacroExpands callbacks still happen in source order, queue this
    284       // callback to have it happen after the function macro callback.
    285       DelayedMacroExpandsCallbacks.push_back(
    286                               MacroExpandsInfo(Identifier, MD, ExpansionRange));
    287     } else {
    288       Callbacks->MacroExpands(Identifier, MD, ExpansionRange, Args);
    289       if (!DelayedMacroExpandsCallbacks.empty()) {
    290         for (unsigned i=0, e = DelayedMacroExpandsCallbacks.size(); i!=e; ++i) {
    291           MacroExpandsInfo &Info = DelayedMacroExpandsCallbacks[i];
    292           // FIXME: We lose macro args info with delayed callback.
    293           Callbacks->MacroExpands(Info.Tok, Info.MD, Info.Range,
    294                                   /*Args=*/nullptr);
    295         }
    296         DelayedMacroExpandsCallbacks.clear();
    297       }
    298     }
    299   }
    300 
    301   // If the macro definition is ambiguous, complain.
    302   if (Def.getDirective()->isAmbiguous()) {
    303     Diag(Identifier, diag::warn_pp_ambiguous_macro)
    304       << Identifier.getIdentifierInfo();
    305     Diag(MI->getDefinitionLoc(), diag::note_pp_ambiguous_macro_chosen)
    306       << Identifier.getIdentifierInfo();
    307     for (MacroDirective::DefInfo PrevDef = Def.getPreviousDefinition();
    308          PrevDef && !PrevDef.isUndefined();
    309          PrevDef = PrevDef.getPreviousDefinition()) {
    310       Diag(PrevDef.getMacroInfo()->getDefinitionLoc(),
    311            diag::note_pp_ambiguous_macro_other)
    312         << Identifier.getIdentifierInfo();
    313       if (!PrevDef.getDirective()->isAmbiguous())
    314         break;
    315     }
    316   }
    317 
    318   // If we started lexing a macro, enter the macro expansion body.
    319 
    320   // If this macro expands to no tokens, don't bother to push it onto the
    321   // expansion stack, only to take it right back off.
    322   if (MI->getNumTokens() == 0) {
    323     // No need for arg info.
    324     if (Args) Args->destroy(*this);
    325 
    326     // Propagate whitespace info as if we had pushed, then popped,
    327     // a macro context.
    328     Identifier.setFlag(Token::LeadingEmptyMacro);
    329     PropagateLineStartLeadingSpaceInfo(Identifier);
    330     ++NumFastMacroExpanded;
    331     return false;
    332   } else if (MI->getNumTokens() == 1 &&
    333              isTrivialSingleTokenExpansion(MI, Identifier.getIdentifierInfo(),
    334                                            *this)) {
    335     // Otherwise, if this macro expands into a single trivially-expanded
    336     // token: expand it now.  This handles common cases like
    337     // "#define VAL 42".
    338 
    339     // No need for arg info.
    340     if (Args) Args->destroy(*this);
    341 
    342     // Propagate the isAtStartOfLine/hasLeadingSpace markers of the macro
    343     // identifier to the expanded token.
    344     bool isAtStartOfLine = Identifier.isAtStartOfLine();
    345     bool hasLeadingSpace = Identifier.hasLeadingSpace();
    346 
    347     // Replace the result token.
    348     Identifier = MI->getReplacementToken(0);
    349 
    350     // Restore the StartOfLine/LeadingSpace markers.
    351     Identifier.setFlagValue(Token::StartOfLine , isAtStartOfLine);
    352     Identifier.setFlagValue(Token::LeadingSpace, hasLeadingSpace);
    353 
    354     // Update the tokens location to include both its expansion and physical
    355     // locations.
    356     SourceLocation Loc =
    357       SourceMgr.createExpansionLoc(Identifier.getLocation(), ExpandLoc,
    358                                    ExpansionEnd,Identifier.getLength());
    359     Identifier.setLocation(Loc);
    360 
    361     // If this is a disabled macro or #define X X, we must mark the result as
    362     // unexpandable.
    363     if (IdentifierInfo *NewII = Identifier.getIdentifierInfo()) {
    364       if (MacroInfo *NewMI = getMacroInfo(NewII))
    365         if (!NewMI->isEnabled() || NewMI == MI) {
    366           Identifier.setFlag(Token::DisableExpand);
    367           // Don't warn for "#define X X" like "#define bool bool" from
    368           // stdbool.h.
    369           if (NewMI != MI || MI->isFunctionLike())
    370             Diag(Identifier, diag::pp_disabled_macro_expansion);
    371         }
    372     }
    373 
    374     // Since this is not an identifier token, it can't be macro expanded, so
    375     // we're done.
    376     ++NumFastMacroExpanded;
    377     return true;
    378   }
    379 
    380   // Start expanding the macro.
    381   EnterMacro(Identifier, ExpansionEnd, MI, Args);
    382   return false;
    383 }
    384 
    385 enum Bracket {
    386   Brace,
    387   Paren
    388 };
    389 
    390 /// CheckMatchedBrackets - Returns true if the braces and parentheses in the
    391 /// token vector are properly nested.
    392 static bool CheckMatchedBrackets(const SmallVectorImpl<Token> &Tokens) {
    393   SmallVector<Bracket, 8> Brackets;
    394   for (SmallVectorImpl<Token>::const_iterator I = Tokens.begin(),
    395                                               E = Tokens.end();
    396        I != E; ++I) {
    397     if (I->is(tok::l_paren)) {
    398       Brackets.push_back(Paren);
    399     } else if (I->is(tok::r_paren)) {
    400       if (Brackets.empty() || Brackets.back() == Brace)
    401         return false;
    402       Brackets.pop_back();
    403     } else if (I->is(tok::l_brace)) {
    404       Brackets.push_back(Brace);
    405     } else if (I->is(tok::r_brace)) {
    406       if (Brackets.empty() || Brackets.back() == Paren)
    407         return false;
    408       Brackets.pop_back();
    409     }
    410   }
    411   if (!Brackets.empty())
    412     return false;
    413   return true;
    414 }
    415 
    416 /// GenerateNewArgTokens - Returns true if OldTokens can be converted to a new
    417 /// vector of tokens in NewTokens.  The new number of arguments will be placed
    418 /// in NumArgs and the ranges which need to surrounded in parentheses will be
    419 /// in ParenHints.
    420 /// Returns false if the token stream cannot be changed.  If this is because
    421 /// of an initializer list starting a macro argument, the range of those
    422 /// initializer lists will be place in InitLists.
    423 static bool GenerateNewArgTokens(Preprocessor &PP,
    424                                  SmallVectorImpl<Token> &OldTokens,
    425                                  SmallVectorImpl<Token> &NewTokens,
    426                                  unsigned &NumArgs,
    427                                  SmallVectorImpl<SourceRange> &ParenHints,
    428                                  SmallVectorImpl<SourceRange> &InitLists) {
    429   if (!CheckMatchedBrackets(OldTokens))
    430     return false;
    431 
    432   // Once it is known that the brackets are matched, only a simple count of the
    433   // braces is needed.
    434   unsigned Braces = 0;
    435 
    436   // First token of a new macro argument.
    437   SmallVectorImpl<Token>::iterator ArgStartIterator = OldTokens.begin();
    438 
    439   // First closing brace in a new macro argument.  Used to generate
    440   // SourceRanges for InitLists.
    441   SmallVectorImpl<Token>::iterator ClosingBrace = OldTokens.end();
    442   NumArgs = 0;
    443   Token TempToken;
    444   // Set to true when a macro separator token is found inside a braced list.
    445   // If true, the fixed argument spans multiple old arguments and ParenHints
    446   // will be updated.
    447   bool FoundSeparatorToken = false;
    448   for (SmallVectorImpl<Token>::iterator I = OldTokens.begin(),
    449                                         E = OldTokens.end();
    450        I != E; ++I) {
    451     if (I->is(tok::l_brace)) {
    452       ++Braces;
    453     } else if (I->is(tok::r_brace)) {
    454       --Braces;
    455       if (Braces == 0 && ClosingBrace == E && FoundSeparatorToken)
    456         ClosingBrace = I;
    457     } else if (I->is(tok::eof)) {
    458       // EOF token is used to separate macro arguments
    459       if (Braces != 0) {
    460         // Assume comma separator is actually braced list separator and change
    461         // it back to a comma.
    462         FoundSeparatorToken = true;
    463         I->setKind(tok::comma);
    464         I->setLength(1);
    465       } else { // Braces == 0
    466         // Separator token still separates arguments.
    467         ++NumArgs;
    468 
    469         // If the argument starts with a brace, it can't be fixed with
    470         // parentheses.  A different diagnostic will be given.
    471         if (FoundSeparatorToken && ArgStartIterator->is(tok::l_brace)) {
    472           InitLists.push_back(
    473               SourceRange(ArgStartIterator->getLocation(),
    474                           PP.getLocForEndOfToken(ClosingBrace->getLocation())));
    475           ClosingBrace = E;
    476         }
    477 
    478         // Add left paren
    479         if (FoundSeparatorToken) {
    480           TempToken.startToken();
    481           TempToken.setKind(tok::l_paren);
    482           TempToken.setLocation(ArgStartIterator->getLocation());
    483           TempToken.setLength(0);
    484           NewTokens.push_back(TempToken);
    485         }
    486 
    487         // Copy over argument tokens
    488         NewTokens.insert(NewTokens.end(), ArgStartIterator, I);
    489 
    490         // Add right paren and store the paren locations in ParenHints
    491         if (FoundSeparatorToken) {
    492           SourceLocation Loc = PP.getLocForEndOfToken((I - 1)->getLocation());
    493           TempToken.startToken();
    494           TempToken.setKind(tok::r_paren);
    495           TempToken.setLocation(Loc);
    496           TempToken.setLength(0);
    497           NewTokens.push_back(TempToken);
    498           ParenHints.push_back(SourceRange(ArgStartIterator->getLocation(),
    499                                            Loc));
    500         }
    501 
    502         // Copy separator token
    503         NewTokens.push_back(*I);
    504 
    505         // Reset values
    506         ArgStartIterator = I + 1;
    507         FoundSeparatorToken = false;
    508       }
    509     }
    510   }
    511 
    512   return !ParenHints.empty() && InitLists.empty();
    513 }
    514 
    515 /// ReadFunctionLikeMacroArgs - After reading "MACRO" and knowing that the next
    516 /// token is the '(' of the macro, this method is invoked to read all of the
    517 /// actual arguments specified for the macro invocation.  This returns null on
    518 /// error.
    519 MacroArgs *Preprocessor::ReadFunctionLikeMacroArgs(Token &MacroName,
    520                                                    MacroInfo *MI,
    521                                                    SourceLocation &MacroEnd) {
    522   // The number of fixed arguments to parse.
    523   unsigned NumFixedArgsLeft = MI->getNumArgs();
    524   bool isVariadic = MI->isVariadic();
    525 
    526   // Outer loop, while there are more arguments, keep reading them.
    527   Token Tok;
    528 
    529   // Read arguments as unexpanded tokens.  This avoids issues, e.g., where
    530   // an argument value in a macro could expand to ',' or '(' or ')'.
    531   LexUnexpandedToken(Tok);
    532   assert(Tok.is(tok::l_paren) && "Error computing l-paren-ness?");
    533 
    534   // ArgTokens - Build up a list of tokens that make up each argument.  Each
    535   // argument is separated by an EOF token.  Use a SmallVector so we can avoid
    536   // heap allocations in the common case.
    537   SmallVector<Token, 64> ArgTokens;
    538   bool ContainsCodeCompletionTok = false;
    539 
    540   SourceLocation TooManyArgsLoc;
    541 
    542   unsigned NumActuals = 0;
    543   while (Tok.isNot(tok::r_paren)) {
    544     if (ContainsCodeCompletionTok && (Tok.is(tok::eof) || Tok.is(tok::eod)))
    545       break;
    546 
    547     assert((Tok.is(tok::l_paren) || Tok.is(tok::comma)) &&
    548            "only expect argument separators here");
    549 
    550     unsigned ArgTokenStart = ArgTokens.size();
    551     SourceLocation ArgStartLoc = Tok.getLocation();
    552 
    553     // C99 6.10.3p11: Keep track of the number of l_parens we have seen.  Note
    554     // that we already consumed the first one.
    555     unsigned NumParens = 0;
    556 
    557     while (1) {
    558       // Read arguments as unexpanded tokens.  This avoids issues, e.g., where
    559       // an argument value in a macro could expand to ',' or '(' or ')'.
    560       LexUnexpandedToken(Tok);
    561 
    562       if (Tok.is(tok::eof) || Tok.is(tok::eod)) { // "#if f(<eof>" & "#if f(\n"
    563         if (!ContainsCodeCompletionTok) {
    564           Diag(MacroName, diag::err_unterm_macro_invoc);
    565           Diag(MI->getDefinitionLoc(), diag::note_macro_here)
    566             << MacroName.getIdentifierInfo();
    567           // Do not lose the EOF/EOD.  Return it to the client.
    568           MacroName = Tok;
    569           return nullptr;
    570         } else {
    571           // Do not lose the EOF/EOD.
    572           Token *Toks = new Token[1];
    573           Toks[0] = Tok;
    574           EnterTokenStream(Toks, 1, true, true);
    575           break;
    576         }
    577       } else if (Tok.is(tok::r_paren)) {
    578         // If we found the ) token, the macro arg list is done.
    579         if (NumParens-- == 0) {
    580           MacroEnd = Tok.getLocation();
    581           break;
    582         }
    583       } else if (Tok.is(tok::l_paren)) {
    584         ++NumParens;
    585       } else if (Tok.is(tok::comma) && NumParens == 0 &&
    586                  !(Tok.getFlags() & Token::IgnoredComma)) {
    587         // In Microsoft-compatibility mode, single commas from nested macro
    588         // expansions should not be considered as argument separators. We test
    589         // for this with the IgnoredComma token flag above.
    590 
    591         // Comma ends this argument if there are more fixed arguments expected.
    592         // However, if this is a variadic macro, and this is part of the
    593         // variadic part, then the comma is just an argument token.
    594         if (!isVariadic) break;
    595         if (NumFixedArgsLeft > 1)
    596           break;
    597       } else if (Tok.is(tok::comment) && !KeepMacroComments) {
    598         // If this is a comment token in the argument list and we're just in
    599         // -C mode (not -CC mode), discard the comment.
    600         continue;
    601       } else if (!Tok.isAnnotation() && Tok.getIdentifierInfo() != nullptr) {
    602         // Reading macro arguments can cause macros that we are currently
    603         // expanding from to be popped off the expansion stack.  Doing so causes
    604         // them to be reenabled for expansion.  Here we record whether any
    605         // identifiers we lex as macro arguments correspond to disabled macros.
    606         // If so, we mark the token as noexpand.  This is a subtle aspect of
    607         // C99 6.10.3.4p2.
    608         if (MacroInfo *MI = getMacroInfo(Tok.getIdentifierInfo()))
    609           if (!MI->isEnabled())
    610             Tok.setFlag(Token::DisableExpand);
    611       } else if (Tok.is(tok::code_completion)) {
    612         ContainsCodeCompletionTok = true;
    613         if (CodeComplete)
    614           CodeComplete->CodeCompleteMacroArgument(MacroName.getIdentifierInfo(),
    615                                                   MI, NumActuals);
    616         // Don't mark that we reached the code-completion point because the
    617         // parser is going to handle the token and there will be another
    618         // code-completion callback.
    619       }
    620 
    621       ArgTokens.push_back(Tok);
    622     }
    623 
    624     // If this was an empty argument list foo(), don't add this as an empty
    625     // argument.
    626     if (ArgTokens.empty() && Tok.getKind() == tok::r_paren)
    627       break;
    628 
    629     // If this is not a variadic macro, and too many args were specified, emit
    630     // an error.
    631     if (!isVariadic && NumFixedArgsLeft == 0 && TooManyArgsLoc.isInvalid()) {
    632       if (ArgTokens.size() != ArgTokenStart)
    633         TooManyArgsLoc = ArgTokens[ArgTokenStart].getLocation();
    634       else
    635         TooManyArgsLoc = ArgStartLoc;
    636     }
    637 
    638     // Empty arguments are standard in C99 and C++0x, and are supported as an
    639     // extension in other modes.
    640     if (ArgTokens.size() == ArgTokenStart && !LangOpts.C99)
    641       Diag(Tok, LangOpts.CPlusPlus11 ?
    642            diag::warn_cxx98_compat_empty_fnmacro_arg :
    643            diag::ext_empty_fnmacro_arg);
    644 
    645     // Add a marker EOF token to the end of the token list for this argument.
    646     Token EOFTok;
    647     EOFTok.startToken();
    648     EOFTok.setKind(tok::eof);
    649     EOFTok.setLocation(Tok.getLocation());
    650     EOFTok.setLength(0);
    651     ArgTokens.push_back(EOFTok);
    652     ++NumActuals;
    653     if (!ContainsCodeCompletionTok && NumFixedArgsLeft != 0)
    654       --NumFixedArgsLeft;
    655   }
    656 
    657   // Okay, we either found the r_paren.  Check to see if we parsed too few
    658   // arguments.
    659   unsigned MinArgsExpected = MI->getNumArgs();
    660 
    661   // If this is not a variadic macro, and too many args were specified, emit
    662   // an error.
    663   if (!isVariadic && NumActuals > MinArgsExpected &&
    664       !ContainsCodeCompletionTok) {
    665     // Emit the diagnostic at the macro name in case there is a missing ).
    666     // Emitting it at the , could be far away from the macro name.
    667     Diag(TooManyArgsLoc, diag::err_too_many_args_in_macro_invoc);
    668     Diag(MI->getDefinitionLoc(), diag::note_macro_here)
    669       << MacroName.getIdentifierInfo();
    670 
    671     // Commas from braced initializer lists will be treated as argument
    672     // separators inside macros.  Attempt to correct for this with parentheses.
    673     // TODO: See if this can be generalized to angle brackets for templates
    674     // inside macro arguments.
    675 
    676     SmallVector<Token, 4> FixedArgTokens;
    677     unsigned FixedNumArgs = 0;
    678     SmallVector<SourceRange, 4> ParenHints, InitLists;
    679     if (!GenerateNewArgTokens(*this, ArgTokens, FixedArgTokens, FixedNumArgs,
    680                               ParenHints, InitLists)) {
    681       if (!InitLists.empty()) {
    682         DiagnosticBuilder DB =
    683             Diag(MacroName,
    684                  diag::note_init_list_at_beginning_of_macro_argument);
    685         for (const SourceRange &Range : InitLists)
    686           DB << Range;
    687       }
    688       return nullptr;
    689     }
    690     if (FixedNumArgs != MinArgsExpected)
    691       return nullptr;
    692 
    693     DiagnosticBuilder DB = Diag(MacroName, diag::note_suggest_parens_for_macro);
    694     for (const SourceRange &ParenLocation : ParenHints) {
    695       DB << FixItHint::CreateInsertion(ParenLocation.getBegin(), "(");
    696       DB << FixItHint::CreateInsertion(ParenLocation.getEnd(), ")");
    697     }
    698     ArgTokens.swap(FixedArgTokens);
    699     NumActuals = FixedNumArgs;
    700   }
    701 
    702   // See MacroArgs instance var for description of this.
    703   bool isVarargsElided = false;
    704 
    705   if (ContainsCodeCompletionTok) {
    706     // Recover from not-fully-formed macro invocation during code-completion.
    707     Token EOFTok;
    708     EOFTok.startToken();
    709     EOFTok.setKind(tok::eof);
    710     EOFTok.setLocation(Tok.getLocation());
    711     EOFTok.setLength(0);
    712     for (; NumActuals < MinArgsExpected; ++NumActuals)
    713       ArgTokens.push_back(EOFTok);
    714   }
    715 
    716   if (NumActuals < MinArgsExpected) {
    717     // There are several cases where too few arguments is ok, handle them now.
    718     if (NumActuals == 0 && MinArgsExpected == 1) {
    719       // #define A(X)  or  #define A(...)   ---> A()
    720 
    721       // If there is exactly one argument, and that argument is missing,
    722       // then we have an empty "()" argument empty list.  This is fine, even if
    723       // the macro expects one argument (the argument is just empty).
    724       isVarargsElided = MI->isVariadic();
    725     } else if (MI->isVariadic() &&
    726                (NumActuals+1 == MinArgsExpected ||  // A(x, ...) -> A(X)
    727                 (NumActuals == 0 && MinArgsExpected == 2))) {// A(x,...) -> A()
    728       // Varargs where the named vararg parameter is missing: OK as extension.
    729       //   #define A(x, ...)
    730       //   A("blah")
    731       //
    732       // If the macro contains the comma pasting extension, the diagnostic
    733       // is suppressed; we know we'll get another diagnostic later.
    734       if (!MI->hasCommaPasting()) {
    735         Diag(Tok, diag::ext_missing_varargs_arg);
    736         Diag(MI->getDefinitionLoc(), diag::note_macro_here)
    737           << MacroName.getIdentifierInfo();
    738       }
    739 
    740       // Remember this occurred, allowing us to elide the comma when used for
    741       // cases like:
    742       //   #define A(x, foo...) blah(a, ## foo)
    743       //   #define B(x, ...) blah(a, ## __VA_ARGS__)
    744       //   #define C(...) blah(a, ## __VA_ARGS__)
    745       //  A(x) B(x) C()
    746       isVarargsElided = true;
    747     } else if (!ContainsCodeCompletionTok) {
    748       // Otherwise, emit the error.
    749       Diag(Tok, diag::err_too_few_args_in_macro_invoc);
    750       Diag(MI->getDefinitionLoc(), diag::note_macro_here)
    751         << MacroName.getIdentifierInfo();
    752       return nullptr;
    753     }
    754 
    755     // Add a marker EOF token to the end of the token list for this argument.
    756     SourceLocation EndLoc = Tok.getLocation();
    757     Tok.startToken();
    758     Tok.setKind(tok::eof);
    759     Tok.setLocation(EndLoc);
    760     Tok.setLength(0);
    761     ArgTokens.push_back(Tok);
    762 
    763     // If we expect two arguments, add both as empty.
    764     if (NumActuals == 0 && MinArgsExpected == 2)
    765       ArgTokens.push_back(Tok);
    766 
    767   } else if (NumActuals > MinArgsExpected && !MI->isVariadic() &&
    768              !ContainsCodeCompletionTok) {
    769     // Emit the diagnostic at the macro name in case there is a missing ).
    770     // Emitting it at the , could be far away from the macro name.
    771     Diag(MacroName, diag::err_too_many_args_in_macro_invoc);
    772     Diag(MI->getDefinitionLoc(), diag::note_macro_here)
    773       << MacroName.getIdentifierInfo();
    774     return nullptr;
    775   }
    776 
    777   return MacroArgs::create(MI, ArgTokens, isVarargsElided, *this);
    778 }
    779 
    780 /// \brief Keeps macro expanded tokens for TokenLexers.
    781 //
    782 /// Works like a stack; a TokenLexer adds the macro expanded tokens that is
    783 /// going to lex in the cache and when it finishes the tokens are removed
    784 /// from the end of the cache.
    785 Token *Preprocessor::cacheMacroExpandedTokens(TokenLexer *tokLexer,
    786                                               ArrayRef<Token> tokens) {
    787   assert(tokLexer);
    788   if (tokens.empty())
    789     return nullptr;
    790 
    791   size_t newIndex = MacroExpandedTokens.size();
    792   bool cacheNeedsToGrow = tokens.size() >
    793                       MacroExpandedTokens.capacity()-MacroExpandedTokens.size();
    794   MacroExpandedTokens.append(tokens.begin(), tokens.end());
    795 
    796   if (cacheNeedsToGrow) {
    797     // Go through all the TokenLexers whose 'Tokens' pointer points in the
    798     // buffer and update the pointers to the (potential) new buffer array.
    799     for (unsigned i = 0, e = MacroExpandingLexersStack.size(); i != e; ++i) {
    800       TokenLexer *prevLexer;
    801       size_t tokIndex;
    802       std::tie(prevLexer, tokIndex) = MacroExpandingLexersStack[i];
    803       prevLexer->Tokens = MacroExpandedTokens.data() + tokIndex;
    804     }
    805   }
    806 
    807   MacroExpandingLexersStack.push_back(std::make_pair(tokLexer, newIndex));
    808   return MacroExpandedTokens.data() + newIndex;
    809 }
    810 
    811 void Preprocessor::removeCachedMacroExpandedTokensOfLastLexer() {
    812   assert(!MacroExpandingLexersStack.empty());
    813   size_t tokIndex = MacroExpandingLexersStack.back().second;
    814   assert(tokIndex < MacroExpandedTokens.size());
    815   // Pop the cached macro expanded tokens from the end.
    816   MacroExpandedTokens.resize(tokIndex);
    817   MacroExpandingLexersStack.pop_back();
    818 }
    819 
    820 /// ComputeDATE_TIME - Compute the current time, enter it into the specified
    821 /// scratch buffer, then return DATELoc/TIMELoc locations with the position of
    822 /// the identifier tokens inserted.
    823 static void ComputeDATE_TIME(SourceLocation &DATELoc, SourceLocation &TIMELoc,
    824                              Preprocessor &PP) {
    825   time_t TT = time(nullptr);
    826   struct tm *TM = localtime(&TT);
    827 
    828   static const char * const Months[] = {
    829     "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"
    830   };
    831 
    832   {
    833     SmallString<32> TmpBuffer;
    834     llvm::raw_svector_ostream TmpStream(TmpBuffer);
    835     TmpStream << llvm::format("\"%s %2d %4d\"", Months[TM->tm_mon],
    836                               TM->tm_mday, TM->tm_year + 1900);
    837     Token TmpTok;
    838     TmpTok.startToken();
    839     PP.CreateString(TmpStream.str(), TmpTok);
    840     DATELoc = TmpTok.getLocation();
    841   }
    842 
    843   {
    844     SmallString<32> TmpBuffer;
    845     llvm::raw_svector_ostream TmpStream(TmpBuffer);
    846     TmpStream << llvm::format("\"%02d:%02d:%02d\"",
    847                               TM->tm_hour, TM->tm_min, TM->tm_sec);
    848     Token TmpTok;
    849     TmpTok.startToken();
    850     PP.CreateString(TmpStream.str(), TmpTok);
    851     TIMELoc = TmpTok.getLocation();
    852   }
    853 }
    854 
    855 
    856 /// HasFeature - Return true if we recognize and implement the feature
    857 /// specified by the identifier as a standard language feature.
    858 static bool HasFeature(const Preprocessor &PP, const IdentifierInfo *II) {
    859   const LangOptions &LangOpts = PP.getLangOpts();
    860   StringRef Feature = II->getName();
    861 
    862   // Normalize the feature name, __foo__ becomes foo.
    863   if (Feature.startswith("__") && Feature.endswith("__") && Feature.size() >= 4)
    864     Feature = Feature.substr(2, Feature.size() - 4);
    865 
    866   return llvm::StringSwitch<bool>(Feature)
    867       .Case("address_sanitizer", LangOpts.Sanitize.has(SanitizerKind::Address))
    868       .Case("attribute_analyzer_noreturn", true)
    869       .Case("attribute_availability", true)
    870       .Case("attribute_availability_with_message", true)
    871       .Case("attribute_availability_app_extension", true)
    872       .Case("attribute_cf_returns_not_retained", true)
    873       .Case("attribute_cf_returns_retained", true)
    874       .Case("attribute_deprecated_with_message", true)
    875       .Case("attribute_ext_vector_type", true)
    876       .Case("attribute_ns_returns_not_retained", true)
    877       .Case("attribute_ns_returns_retained", true)
    878       .Case("attribute_ns_consumes_self", true)
    879       .Case("attribute_ns_consumed", true)
    880       .Case("attribute_cf_consumed", true)
    881       .Case("attribute_objc_ivar_unused", true)
    882       .Case("attribute_objc_method_family", true)
    883       .Case("attribute_overloadable", true)
    884       .Case("attribute_unavailable_with_message", true)
    885       .Case("attribute_unused_on_fields", true)
    886       .Case("blocks", LangOpts.Blocks)
    887       .Case("c_thread_safety_attributes", true)
    888       .Case("cxx_exceptions", LangOpts.CXXExceptions)
    889       .Case("cxx_rtti", LangOpts.RTTI)
    890       .Case("enumerator_attributes", true)
    891       .Case("memory_sanitizer", LangOpts.Sanitize.has(SanitizerKind::Memory))
    892       .Case("thread_sanitizer", LangOpts.Sanitize.has(SanitizerKind::Thread))
    893       .Case("dataflow_sanitizer", LangOpts.Sanitize.has(SanitizerKind::DataFlow))
    894       // Objective-C features
    895       .Case("objc_arr", LangOpts.ObjCAutoRefCount) // FIXME: REMOVE?
    896       .Case("objc_arc", LangOpts.ObjCAutoRefCount)
    897       .Case("objc_arc_weak", LangOpts.ObjCARCWeak)
    898       .Case("objc_default_synthesize_properties", LangOpts.ObjC2)
    899       .Case("objc_fixed_enum", LangOpts.ObjC2)
    900       .Case("objc_instancetype", LangOpts.ObjC2)
    901       .Case("objc_modules", LangOpts.ObjC2 && LangOpts.Modules)
    902       .Case("objc_nonfragile_abi", LangOpts.ObjCRuntime.isNonFragile())
    903       .Case("objc_property_explicit_atomic",
    904             true) // Does clang support explicit "atomic" keyword?
    905       .Case("objc_protocol_qualifier_mangling", true)
    906       .Case("objc_weak_class", LangOpts.ObjCRuntime.hasWeakClassImport())
    907       .Case("ownership_holds", true)
    908       .Case("ownership_returns", true)
    909       .Case("ownership_takes", true)
    910       .Case("objc_bool", true)
    911       .Case("objc_subscripting", LangOpts.ObjCRuntime.isNonFragile())
    912       .Case("objc_array_literals", LangOpts.ObjC2)
    913       .Case("objc_dictionary_literals", LangOpts.ObjC2)
    914       .Case("objc_boxed_expressions", LangOpts.ObjC2)
    915       .Case("arc_cf_code_audited", true)
    916       .Case("objc_bridge_id", true)
    917       .Case("objc_bridge_id_on_typedefs", true)
    918       // C11 features
    919       .Case("c_alignas", LangOpts.C11)
    920       .Case("c_alignof", LangOpts.C11)
    921       .Case("c_atomic", LangOpts.C11)
    922       .Case("c_generic_selections", LangOpts.C11)
    923       .Case("c_static_assert", LangOpts.C11)
    924       .Case("c_thread_local",
    925             LangOpts.C11 && PP.getTargetInfo().isTLSSupported())
    926       // C++11 features
    927       .Case("cxx_access_control_sfinae", LangOpts.CPlusPlus11)
    928       .Case("cxx_alias_templates", LangOpts.CPlusPlus11)
    929       .Case("cxx_alignas", LangOpts.CPlusPlus11)
    930       .Case("cxx_alignof", LangOpts.CPlusPlus11)
    931       .Case("cxx_atomic", LangOpts.CPlusPlus11)
    932       .Case("cxx_attributes", LangOpts.CPlusPlus11)
    933       .Case("cxx_auto_type", LangOpts.CPlusPlus11)
    934       .Case("cxx_constexpr", LangOpts.CPlusPlus11)
    935       .Case("cxx_decltype", LangOpts.CPlusPlus11)
    936       .Case("cxx_decltype_incomplete_return_types", LangOpts.CPlusPlus11)
    937       .Case("cxx_default_function_template_args", LangOpts.CPlusPlus11)
    938       .Case("cxx_defaulted_functions", LangOpts.CPlusPlus11)
    939       .Case("cxx_delegating_constructors", LangOpts.CPlusPlus11)
    940       .Case("cxx_deleted_functions", LangOpts.CPlusPlus11)
    941       .Case("cxx_explicit_conversions", LangOpts.CPlusPlus11)
    942       .Case("cxx_generalized_initializers", LangOpts.CPlusPlus11)
    943       .Case("cxx_implicit_moves", LangOpts.CPlusPlus11)
    944       .Case("cxx_inheriting_constructors", LangOpts.CPlusPlus11)
    945       .Case("cxx_inline_namespaces", LangOpts.CPlusPlus11)
    946       .Case("cxx_lambdas", LangOpts.CPlusPlus11)
    947       .Case("cxx_local_type_template_args", LangOpts.CPlusPlus11)
    948       .Case("cxx_nonstatic_member_init", LangOpts.CPlusPlus11)
    949       .Case("cxx_noexcept", LangOpts.CPlusPlus11)
    950       .Case("cxx_nullptr", LangOpts.CPlusPlus11)
    951       .Case("cxx_override_control", LangOpts.CPlusPlus11)
    952       .Case("cxx_range_for", LangOpts.CPlusPlus11)
    953       .Case("cxx_raw_string_literals", LangOpts.CPlusPlus11)
    954       .Case("cxx_reference_qualified_functions", LangOpts.CPlusPlus11)
    955       .Case("cxx_rvalue_references", LangOpts.CPlusPlus11)
    956       .Case("cxx_strong_enums", LangOpts.CPlusPlus11)
    957       .Case("cxx_static_assert", LangOpts.CPlusPlus11)
    958       .Case("cxx_thread_local",
    959             LangOpts.CPlusPlus11 && PP.getTargetInfo().isTLSSupported())
    960       .Case("cxx_trailing_return", LangOpts.CPlusPlus11)
    961       .Case("cxx_unicode_literals", LangOpts.CPlusPlus11)
    962       .Case("cxx_unrestricted_unions", LangOpts.CPlusPlus11)
    963       .Case("cxx_user_literals", LangOpts.CPlusPlus11)
    964       .Case("cxx_variadic_templates", LangOpts.CPlusPlus11)
    965       // C++1y features
    966       .Case("cxx_aggregate_nsdmi", LangOpts.CPlusPlus14)
    967       .Case("cxx_binary_literals", LangOpts.CPlusPlus14)
    968       .Case("cxx_contextual_conversions", LangOpts.CPlusPlus14)
    969       .Case("cxx_decltype_auto", LangOpts.CPlusPlus14)
    970       .Case("cxx_generic_lambdas", LangOpts.CPlusPlus14)
    971       .Case("cxx_init_captures", LangOpts.CPlusPlus14)
    972       .Case("cxx_relaxed_constexpr", LangOpts.CPlusPlus14)
    973       .Case("cxx_return_type_deduction", LangOpts.CPlusPlus14)
    974       .Case("cxx_variable_templates", LangOpts.CPlusPlus14)
    975       // C++ TSes
    976       //.Case("cxx_runtime_arrays", LangOpts.CPlusPlusTSArrays)
    977       //.Case("cxx_concepts", LangOpts.CPlusPlusTSConcepts)
    978       // FIXME: Should this be __has_feature or __has_extension?
    979       //.Case("raw_invocation_type", LangOpts.CPlusPlus)
    980       // Type traits
    981       .Case("has_nothrow_assign", LangOpts.CPlusPlus)
    982       .Case("has_nothrow_copy", LangOpts.CPlusPlus)
    983       .Case("has_nothrow_constructor", LangOpts.CPlusPlus)
    984       .Case("has_trivial_assign", LangOpts.CPlusPlus)
    985       .Case("has_trivial_copy", LangOpts.CPlusPlus)
    986       .Case("has_trivial_constructor", LangOpts.CPlusPlus)
    987       .Case("has_trivial_destructor", LangOpts.CPlusPlus)
    988       .Case("has_virtual_destructor", LangOpts.CPlusPlus)
    989       .Case("is_abstract", LangOpts.CPlusPlus)
    990       .Case("is_base_of", LangOpts.CPlusPlus)
    991       .Case("is_class", LangOpts.CPlusPlus)
    992       .Case("is_constructible", LangOpts.CPlusPlus)
    993       .Case("is_convertible_to", LangOpts.CPlusPlus)
    994       .Case("is_empty", LangOpts.CPlusPlus)
    995       .Case("is_enum", LangOpts.CPlusPlus)
    996       .Case("is_final", LangOpts.CPlusPlus)
    997       .Case("is_literal", LangOpts.CPlusPlus)
    998       .Case("is_standard_layout", LangOpts.CPlusPlus)
    999       .Case("is_pod", LangOpts.CPlusPlus)
   1000       .Case("is_polymorphic", LangOpts.CPlusPlus)
   1001       .Case("is_sealed", LangOpts.MicrosoftExt)
   1002       .Case("is_trivial", LangOpts.CPlusPlus)
   1003       .Case("is_trivially_assignable", LangOpts.CPlusPlus)
   1004       .Case("is_trivially_constructible", LangOpts.CPlusPlus)
   1005       .Case("is_trivially_copyable", LangOpts.CPlusPlus)
   1006       .Case("is_union", LangOpts.CPlusPlus)
   1007       .Case("modules", LangOpts.Modules)
   1008       .Case("tls", PP.getTargetInfo().isTLSSupported())
   1009       .Case("underlying_type", LangOpts.CPlusPlus)
   1010       .Default(false);
   1011 }
   1012 
   1013 /// HasExtension - Return true if we recognize and implement the feature
   1014 /// specified by the identifier, either as an extension or a standard language
   1015 /// feature.
   1016 static bool HasExtension(const Preprocessor &PP, const IdentifierInfo *II) {
   1017   if (HasFeature(PP, II))
   1018     return true;
   1019 
   1020   // If the use of an extension results in an error diagnostic, extensions are
   1021   // effectively unavailable, so just return false here.
   1022   if (PP.getDiagnostics().getExtensionHandlingBehavior() >=
   1023       diag::Severity::Error)
   1024     return false;
   1025 
   1026   const LangOptions &LangOpts = PP.getLangOpts();
   1027   StringRef Extension = II->getName();
   1028 
   1029   // Normalize the extension name, __foo__ becomes foo.
   1030   if (Extension.startswith("__") && Extension.endswith("__") &&
   1031       Extension.size() >= 4)
   1032     Extension = Extension.substr(2, Extension.size() - 4);
   1033 
   1034   // Because we inherit the feature list from HasFeature, this string switch
   1035   // must be less restrictive than HasFeature's.
   1036   return llvm::StringSwitch<bool>(Extension)
   1037            // C11 features supported by other languages as extensions.
   1038            .Case("c_alignas", true)
   1039            .Case("c_alignof", true)
   1040            .Case("c_atomic", true)
   1041            .Case("c_generic_selections", true)
   1042            .Case("c_static_assert", true)
   1043            .Case("c_thread_local", PP.getTargetInfo().isTLSSupported())
   1044            // C++11 features supported by other languages as extensions.
   1045            .Case("cxx_atomic", LangOpts.CPlusPlus)
   1046            .Case("cxx_deleted_functions", LangOpts.CPlusPlus)
   1047            .Case("cxx_explicit_conversions", LangOpts.CPlusPlus)
   1048            .Case("cxx_inline_namespaces", LangOpts.CPlusPlus)
   1049            .Case("cxx_local_type_template_args", LangOpts.CPlusPlus)
   1050            .Case("cxx_nonstatic_member_init", LangOpts.CPlusPlus)
   1051            .Case("cxx_override_control", LangOpts.CPlusPlus)
   1052            .Case("cxx_range_for", LangOpts.CPlusPlus)
   1053            .Case("cxx_reference_qualified_functions", LangOpts.CPlusPlus)
   1054            .Case("cxx_rvalue_references", LangOpts.CPlusPlus)
   1055            // C++1y features supported by other languages as extensions.
   1056            .Case("cxx_binary_literals", true)
   1057            .Case("cxx_init_captures", LangOpts.CPlusPlus11)
   1058            .Case("cxx_variable_templates", LangOpts.CPlusPlus)
   1059            .Default(false);
   1060 }
   1061 
   1062 /// EvaluateHasIncludeCommon - Process a '__has_include("path")'
   1063 /// or '__has_include_next("path")' expression.
   1064 /// Returns true if successful.
   1065 static bool EvaluateHasIncludeCommon(Token &Tok,
   1066                                      IdentifierInfo *II, Preprocessor &PP,
   1067                                      const DirectoryLookup *LookupFrom,
   1068                                      const FileEntry *LookupFromFile) {
   1069   // Save the location of the current token.  If a '(' is later found, use
   1070   // that location.  If not, use the end of this location instead.
   1071   SourceLocation LParenLoc = Tok.getLocation();
   1072 
   1073   // These expressions are only allowed within a preprocessor directive.
   1074   if (!PP.isParsingIfOrElifDirective()) {
   1075     PP.Diag(LParenLoc, diag::err_pp_directive_required) << II->getName();
   1076     // Return a valid identifier token.
   1077     assert(Tok.is(tok::identifier));
   1078     Tok.setIdentifierInfo(II);
   1079     return false;
   1080   }
   1081 
   1082   // Get '('.
   1083   PP.LexNonComment(Tok);
   1084 
   1085   // Ensure we have a '('.
   1086   if (Tok.isNot(tok::l_paren)) {
   1087     // No '(', use end of last token.
   1088     LParenLoc = PP.getLocForEndOfToken(LParenLoc);
   1089     PP.Diag(LParenLoc, diag::err_pp_expected_after) << II << tok::l_paren;
   1090     // If the next token looks like a filename or the start of one,
   1091     // assume it is and process it as such.
   1092     if (!Tok.is(tok::angle_string_literal) && !Tok.is(tok::string_literal) &&
   1093         !Tok.is(tok::less))
   1094       return false;
   1095   } else {
   1096     // Save '(' location for possible missing ')' message.
   1097     LParenLoc = Tok.getLocation();
   1098 
   1099     if (PP.getCurrentLexer()) {
   1100       // Get the file name.
   1101       PP.getCurrentLexer()->LexIncludeFilename(Tok);
   1102     } else {
   1103       // We're in a macro, so we can't use LexIncludeFilename; just
   1104       // grab the next token.
   1105       PP.Lex(Tok);
   1106     }
   1107   }
   1108 
   1109   // Reserve a buffer to get the spelling.
   1110   SmallString<128> FilenameBuffer;
   1111   StringRef Filename;
   1112   SourceLocation EndLoc;
   1113 
   1114   switch (Tok.getKind()) {
   1115   case tok::eod:
   1116     // If the token kind is EOD, the error has already been diagnosed.
   1117     return false;
   1118 
   1119   case tok::angle_string_literal:
   1120   case tok::string_literal: {
   1121     bool Invalid = false;
   1122     Filename = PP.getSpelling(Tok, FilenameBuffer, &Invalid);
   1123     if (Invalid)
   1124       return false;
   1125     break;
   1126   }
   1127 
   1128   case tok::less:
   1129     // This could be a <foo/bar.h> file coming from a macro expansion.  In this
   1130     // case, glue the tokens together into FilenameBuffer and interpret those.
   1131     FilenameBuffer.push_back('<');
   1132     if (PP.ConcatenateIncludeName(FilenameBuffer, EndLoc)) {
   1133       // Let the caller know a <eod> was found by changing the Token kind.
   1134       Tok.setKind(tok::eod);
   1135       return false;   // Found <eod> but no ">"?  Diagnostic already emitted.
   1136     }
   1137     Filename = FilenameBuffer;
   1138     break;
   1139   default:
   1140     PP.Diag(Tok.getLocation(), diag::err_pp_expects_filename);
   1141     return false;
   1142   }
   1143 
   1144   SourceLocation FilenameLoc = Tok.getLocation();
   1145 
   1146   // Get ')'.
   1147   PP.LexNonComment(Tok);
   1148 
   1149   // Ensure we have a trailing ).
   1150   if (Tok.isNot(tok::r_paren)) {
   1151     PP.Diag(PP.getLocForEndOfToken(FilenameLoc), diag::err_pp_expected_after)
   1152         << II << tok::r_paren;
   1153     PP.Diag(LParenLoc, diag::note_matching) << tok::l_paren;
   1154     return false;
   1155   }
   1156 
   1157   bool isAngled = PP.GetIncludeFilenameSpelling(Tok.getLocation(), Filename);
   1158   // If GetIncludeFilenameSpelling set the start ptr to null, there was an
   1159   // error.
   1160   if (Filename.empty())
   1161     return false;
   1162 
   1163   // Search include directories.
   1164   const DirectoryLookup *CurDir;
   1165   const FileEntry *File =
   1166       PP.LookupFile(FilenameLoc, Filename, isAngled, LookupFrom, LookupFromFile,
   1167                     CurDir, nullptr, nullptr, nullptr);
   1168 
   1169   // Get the result value.  A result of true means the file exists.
   1170   return File != nullptr;
   1171 }
   1172 
   1173 /// EvaluateHasInclude - Process a '__has_include("path")' expression.
   1174 /// Returns true if successful.
   1175 static bool EvaluateHasInclude(Token &Tok, IdentifierInfo *II,
   1176                                Preprocessor &PP) {
   1177   return EvaluateHasIncludeCommon(Tok, II, PP, nullptr, nullptr);
   1178 }
   1179 
   1180 /// EvaluateHasIncludeNext - Process '__has_include_next("path")' expression.
   1181 /// Returns true if successful.
   1182 static bool EvaluateHasIncludeNext(Token &Tok,
   1183                                    IdentifierInfo *II, Preprocessor &PP) {
   1184   // __has_include_next is like __has_include, except that we start
   1185   // searching after the current found directory.  If we can't do this,
   1186   // issue a diagnostic.
   1187   // FIXME: Factor out duplication with
   1188   // Preprocessor::HandleIncludeNextDirective.
   1189   const DirectoryLookup *Lookup = PP.GetCurDirLookup();
   1190   const FileEntry *LookupFromFile = nullptr;
   1191   if (PP.isInPrimaryFile()) {
   1192     Lookup = nullptr;
   1193     PP.Diag(Tok, diag::pp_include_next_in_primary);
   1194   } else if (PP.getCurrentSubmodule()) {
   1195     // Start looking up in the directory *after* the one in which the current
   1196     // file would be found, if any.
   1197     assert(PP.getCurrentLexer() && "#include_next directive in macro?");
   1198     LookupFromFile = PP.getCurrentLexer()->getFileEntry();
   1199     Lookup = nullptr;
   1200   } else if (!Lookup) {
   1201     PP.Diag(Tok, diag::pp_include_next_absolute_path);
   1202   } else {
   1203     // Start looking up in the next directory.
   1204     ++Lookup;
   1205   }
   1206 
   1207   return EvaluateHasIncludeCommon(Tok, II, PP, Lookup, LookupFromFile);
   1208 }
   1209 
   1210 /// \brief Process __building_module(identifier) expression.
   1211 /// \returns true if we are building the named module, false otherwise.
   1212 static bool EvaluateBuildingModule(Token &Tok,
   1213                                    IdentifierInfo *II, Preprocessor &PP) {
   1214   // Get '('.
   1215   PP.LexNonComment(Tok);
   1216 
   1217   // Ensure we have a '('.
   1218   if (Tok.isNot(tok::l_paren)) {
   1219     PP.Diag(Tok.getLocation(), diag::err_pp_expected_after) << II
   1220                                                             << tok::l_paren;
   1221     return false;
   1222   }
   1223 
   1224   // Save '(' location for possible missing ')' message.
   1225   SourceLocation LParenLoc = Tok.getLocation();
   1226 
   1227   // Get the module name.
   1228   PP.LexNonComment(Tok);
   1229 
   1230   // Ensure that we have an identifier.
   1231   if (Tok.isNot(tok::identifier)) {
   1232     PP.Diag(Tok.getLocation(), diag::err_expected_id_building_module);
   1233     return false;
   1234   }
   1235 
   1236   bool Result
   1237     = Tok.getIdentifierInfo()->getName() == PP.getLangOpts().CurrentModule;
   1238 
   1239   // Get ')'.
   1240   PP.LexNonComment(Tok);
   1241 
   1242   // Ensure we have a trailing ).
   1243   if (Tok.isNot(tok::r_paren)) {
   1244     PP.Diag(Tok.getLocation(), diag::err_pp_expected_after) << II
   1245                                                             << tok::r_paren;
   1246     PP.Diag(LParenLoc, diag::note_matching) << tok::l_paren;
   1247     return false;
   1248   }
   1249 
   1250   return Result;
   1251 }
   1252 
   1253 /// ExpandBuiltinMacro - If an identifier token is read that is to be expanded
   1254 /// as a builtin macro, handle it and return the next token as 'Tok'.
   1255 void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
   1256   // Figure out which token this is.
   1257   IdentifierInfo *II = Tok.getIdentifierInfo();
   1258   assert(II && "Can't be a macro without id info!");
   1259 
   1260   // If this is an _Pragma or Microsoft __pragma directive, expand it,
   1261   // invoke the pragma handler, then lex the token after it.
   1262   if (II == Ident_Pragma)
   1263     return Handle_Pragma(Tok);
   1264   else if (II == Ident__pragma) // in non-MS mode this is null
   1265     return HandleMicrosoft__pragma(Tok);
   1266 
   1267   ++NumBuiltinMacroExpanded;
   1268 
   1269   SmallString<128> TmpBuffer;
   1270   llvm::raw_svector_ostream OS(TmpBuffer);
   1271 
   1272   // Set up the return result.
   1273   Tok.setIdentifierInfo(nullptr);
   1274   Tok.clearFlag(Token::NeedsCleaning);
   1275 
   1276   if (II == Ident__LINE__) {
   1277     // C99 6.10.8: "__LINE__: The presumed line number (within the current
   1278     // source file) of the current source line (an integer constant)".  This can
   1279     // be affected by #line.
   1280     SourceLocation Loc = Tok.getLocation();
   1281 
   1282     // Advance to the location of the first _, this might not be the first byte
   1283     // of the token if it starts with an escaped newline.
   1284     Loc = AdvanceToTokenCharacter(Loc, 0);
   1285 
   1286     // One wrinkle here is that GCC expands __LINE__ to location of the *end* of
   1287     // a macro expansion.  This doesn't matter for object-like macros, but
   1288     // can matter for a function-like macro that expands to contain __LINE__.
   1289     // Skip down through expansion points until we find a file loc for the
   1290     // end of the expansion history.
   1291     Loc = SourceMgr.getExpansionRange(Loc).second;
   1292     PresumedLoc PLoc = SourceMgr.getPresumedLoc(Loc);
   1293 
   1294     // __LINE__ expands to a simple numeric value.
   1295     OS << (PLoc.isValid()? PLoc.getLine() : 1);
   1296     Tok.setKind(tok::numeric_constant);
   1297   } else if (II == Ident__FILE__ || II == Ident__BASE_FILE__) {
   1298     // C99 6.10.8: "__FILE__: The presumed name of the current source file (a
   1299     // character string literal)". This can be affected by #line.
   1300     PresumedLoc PLoc = SourceMgr.getPresumedLoc(Tok.getLocation());
   1301 
   1302     // __BASE_FILE__ is a GNU extension that returns the top of the presumed
   1303     // #include stack instead of the current file.
   1304     if (II == Ident__BASE_FILE__ && PLoc.isValid()) {
   1305       SourceLocation NextLoc = PLoc.getIncludeLoc();
   1306       while (NextLoc.isValid()) {
   1307         PLoc = SourceMgr.getPresumedLoc(NextLoc);
   1308         if (PLoc.isInvalid())
   1309           break;
   1310 
   1311         NextLoc = PLoc.getIncludeLoc();
   1312       }
   1313     }
   1314 
   1315     // Escape this filename.  Turn '\' -> '\\' '"' -> '\"'
   1316     SmallString<128> FN;
   1317     if (PLoc.isValid()) {
   1318       FN += PLoc.getFilename();
   1319       Lexer::Stringify(FN);
   1320       OS << '"' << FN << '"';
   1321     }
   1322     Tok.setKind(tok::string_literal);
   1323   } else if (II == Ident__DATE__) {
   1324     Diag(Tok.getLocation(), diag::warn_pp_date_time);
   1325     if (!DATELoc.isValid())
   1326       ComputeDATE_TIME(DATELoc, TIMELoc, *this);
   1327     Tok.setKind(tok::string_literal);
   1328     Tok.setLength(strlen("\"Mmm dd yyyy\""));
   1329     Tok.setLocation(SourceMgr.createExpansionLoc(DATELoc, Tok.getLocation(),
   1330                                                  Tok.getLocation(),
   1331                                                  Tok.getLength()));
   1332     return;
   1333   } else if (II == Ident__TIME__) {
   1334     Diag(Tok.getLocation(), diag::warn_pp_date_time);
   1335     if (!TIMELoc.isValid())
   1336       ComputeDATE_TIME(DATELoc, TIMELoc, *this);
   1337     Tok.setKind(tok::string_literal);
   1338     Tok.setLength(strlen("\"hh:mm:ss\""));
   1339     Tok.setLocation(SourceMgr.createExpansionLoc(TIMELoc, Tok.getLocation(),
   1340                                                  Tok.getLocation(),
   1341                                                  Tok.getLength()));
   1342     return;
   1343   } else if (II == Ident__INCLUDE_LEVEL__) {
   1344     // Compute the presumed include depth of this token.  This can be affected
   1345     // by GNU line markers.
   1346     unsigned Depth = 0;
   1347 
   1348     PresumedLoc PLoc = SourceMgr.getPresumedLoc(Tok.getLocation());
   1349     if (PLoc.isValid()) {
   1350       PLoc = SourceMgr.getPresumedLoc(PLoc.getIncludeLoc());
   1351       for (; PLoc.isValid(); ++Depth)
   1352         PLoc = SourceMgr.getPresumedLoc(PLoc.getIncludeLoc());
   1353     }
   1354 
   1355     // __INCLUDE_LEVEL__ expands to a simple numeric value.
   1356     OS << Depth;
   1357     Tok.setKind(tok::numeric_constant);
   1358   } else if (II == Ident__TIMESTAMP__) {
   1359     Diag(Tok.getLocation(), diag::warn_pp_date_time);
   1360     // MSVC, ICC, GCC, VisualAge C++ extension.  The generated string should be
   1361     // of the form "Ddd Mmm dd hh::mm::ss yyyy", which is returned by asctime.
   1362 
   1363     // Get the file that we are lexing out of.  If we're currently lexing from
   1364     // a macro, dig into the include stack.
   1365     const FileEntry *CurFile = nullptr;
   1366     PreprocessorLexer *TheLexer = getCurrentFileLexer();
   1367 
   1368     if (TheLexer)
   1369       CurFile = SourceMgr.getFileEntryForID(TheLexer->getFileID());
   1370 
   1371     const char *Result;
   1372     if (CurFile) {
   1373       time_t TT = CurFile->getModificationTime();
   1374       struct tm *TM = localtime(&TT);
   1375       Result = asctime(TM);
   1376     } else {
   1377       Result = "??? ??? ?? ??:??:?? ????\n";
   1378     }
   1379     // Surround the string with " and strip the trailing newline.
   1380     OS << '"' << StringRef(Result).drop_back() << '"';
   1381     Tok.setKind(tok::string_literal);
   1382   } else if (II == Ident__COUNTER__) {
   1383     // __COUNTER__ expands to a simple numeric value.
   1384     OS << CounterValue++;
   1385     Tok.setKind(tok::numeric_constant);
   1386   } else if (II == Ident__has_feature   ||
   1387              II == Ident__has_extension ||
   1388              II == Ident__has_builtin   ||
   1389              II == Ident__is_identifier ||
   1390              II == Ident__has_attribute ||
   1391              II == Ident__has_declspec  ||
   1392              II == Ident__has_cpp_attribute) {
   1393     // The argument to these builtins should be a parenthesized identifier.
   1394     SourceLocation StartLoc = Tok.getLocation();
   1395 
   1396     bool IsValid = false;
   1397     IdentifierInfo *FeatureII = nullptr;
   1398     IdentifierInfo *ScopeII = nullptr;
   1399 
   1400     // Read the '('.
   1401     LexUnexpandedToken(Tok);
   1402     if (Tok.is(tok::l_paren)) {
   1403       // Read the identifier
   1404       LexUnexpandedToken(Tok);
   1405       if ((FeatureII = Tok.getIdentifierInfo())) {
   1406         // If we're checking __has_cpp_attribute, it is possible to receive a
   1407         // scope token. Read the "::", if it's available.
   1408         LexUnexpandedToken(Tok);
   1409         bool IsScopeValid = true;
   1410         if (II == Ident__has_cpp_attribute && Tok.is(tok::coloncolon)) {
   1411           LexUnexpandedToken(Tok);
   1412           // The first thing we read was not the feature, it was the scope.
   1413           ScopeII = FeatureII;
   1414           if ((FeatureII = Tok.getIdentifierInfo()))
   1415             LexUnexpandedToken(Tok);
   1416           else
   1417             IsScopeValid = false;
   1418         }
   1419         // Read the closing paren.
   1420         if (IsScopeValid && Tok.is(tok::r_paren))
   1421           IsValid = true;
   1422       }
   1423       // Eat tokens until ')'.
   1424       while (Tok.isNot(tok::r_paren) && Tok.isNot(tok::eod) &&
   1425              Tok.isNot(tok::eof))
   1426         LexUnexpandedToken(Tok);
   1427     }
   1428 
   1429     int Value = 0;
   1430     if (!IsValid)
   1431       Diag(StartLoc, diag::err_feature_check_malformed);
   1432     else if (II == Ident__is_identifier)
   1433       Value = FeatureII->getTokenID() == tok::identifier;
   1434     else if (II == Ident__has_builtin) {
   1435       // Check for a builtin is trivial.
   1436       Value = FeatureII->getBuiltinID() != 0;
   1437     } else if (II == Ident__has_attribute)
   1438       Value = hasAttribute(AttrSyntax::GNU, nullptr, FeatureII,
   1439                            getTargetInfo().getTriple(), getLangOpts());
   1440     else if (II == Ident__has_cpp_attribute)
   1441       Value = hasAttribute(AttrSyntax::CXX, ScopeII, FeatureII,
   1442                            getTargetInfo().getTriple(), getLangOpts());
   1443     else if (II == Ident__has_declspec)
   1444       Value = hasAttribute(AttrSyntax::Declspec, nullptr, FeatureII,
   1445                            getTargetInfo().getTriple(), getLangOpts());
   1446     else if (II == Ident__has_extension)
   1447       Value = HasExtension(*this, FeatureII);
   1448     else {
   1449       assert(II == Ident__has_feature && "Must be feature check");
   1450       Value = HasFeature(*this, FeatureII);
   1451     }
   1452 
   1453     if (!IsValid)
   1454       return;
   1455     OS << Value;
   1456     Tok.setKind(tok::numeric_constant);
   1457   } else if (II == Ident__has_include ||
   1458              II == Ident__has_include_next) {
   1459     // The argument to these two builtins should be a parenthesized
   1460     // file name string literal using angle brackets (<>) or
   1461     // double-quotes ("").
   1462     bool Value;
   1463     if (II == Ident__has_include)
   1464       Value = EvaluateHasInclude(Tok, II, *this);
   1465     else
   1466       Value = EvaluateHasIncludeNext(Tok, II, *this);
   1467 
   1468     if (Tok.isNot(tok::r_paren))
   1469       return;
   1470     OS << (int)Value;
   1471     Tok.setKind(tok::numeric_constant);
   1472   } else if (II == Ident__has_warning) {
   1473     // The argument should be a parenthesized string literal.
   1474     // The argument to these builtins should be a parenthesized identifier.
   1475     SourceLocation StartLoc = Tok.getLocation();
   1476     bool IsValid = false;
   1477     bool Value = false;
   1478     // Read the '('.
   1479     LexUnexpandedToken(Tok);
   1480     do {
   1481       if (Tok.isNot(tok::l_paren)) {
   1482         Diag(StartLoc, diag::err_warning_check_malformed);
   1483         break;
   1484       }
   1485 
   1486       LexUnexpandedToken(Tok);
   1487       std::string WarningName;
   1488       SourceLocation StrStartLoc = Tok.getLocation();
   1489       if (!FinishLexStringLiteral(Tok, WarningName, "'__has_warning'",
   1490                                   /*MacroExpansion=*/false)) {
   1491         // Eat tokens until ')'.
   1492         while (Tok.isNot(tok::r_paren) && Tok.isNot(tok::eod) &&
   1493                Tok.isNot(tok::eof))
   1494           LexUnexpandedToken(Tok);
   1495         break;
   1496       }
   1497 
   1498       // Is the end a ')'?
   1499       if (!(IsValid = Tok.is(tok::r_paren))) {
   1500         Diag(StartLoc, diag::err_warning_check_malformed);
   1501         break;
   1502       }
   1503 
   1504       // FIXME: Should we accept "-R..." flags here, or should that be handled
   1505       // by a separate __has_remark?
   1506       if (WarningName.size() < 3 || WarningName[0] != '-' ||
   1507           WarningName[1] != 'W') {
   1508         Diag(StrStartLoc, diag::warn_has_warning_invalid_option);
   1509         break;
   1510       }
   1511 
   1512       // Finally, check if the warning flags maps to a diagnostic group.
   1513       // We construct a SmallVector here to talk to getDiagnosticIDs().
   1514       // Although we don't use the result, this isn't a hot path, and not
   1515       // worth special casing.
   1516       SmallVector<diag::kind, 10> Diags;
   1517       Value = !getDiagnostics().getDiagnosticIDs()->
   1518         getDiagnosticsInGroup(diag::Flavor::WarningOrError,
   1519                               WarningName.substr(2), Diags);
   1520     } while (false);
   1521 
   1522     if (!IsValid)
   1523       return;
   1524     OS << (int)Value;
   1525     Tok.setKind(tok::numeric_constant);
   1526   } else if (II == Ident__building_module) {
   1527     // The argument to this builtin should be an identifier. The
   1528     // builtin evaluates to 1 when that identifier names the module we are
   1529     // currently building.
   1530     OS << (int)EvaluateBuildingModule(Tok, II, *this);
   1531     Tok.setKind(tok::numeric_constant);
   1532   } else if (II == Ident__MODULE__) {
   1533     // The current module as an identifier.
   1534     OS << getLangOpts().CurrentModule;
   1535     IdentifierInfo *ModuleII = getIdentifierInfo(getLangOpts().CurrentModule);
   1536     Tok.setIdentifierInfo(ModuleII);
   1537     Tok.setKind(ModuleII->getTokenID());
   1538   } else if (II == Ident__identifier) {
   1539     SourceLocation Loc = Tok.getLocation();
   1540 
   1541     // We're expecting '__identifier' '(' identifier ')'. Try to recover
   1542     // if the parens are missing.
   1543     LexNonComment(Tok);
   1544     if (Tok.isNot(tok::l_paren)) {
   1545       // No '(', use end of last token.
   1546       Diag(getLocForEndOfToken(Loc), diag::err_pp_expected_after)
   1547         << II << tok::l_paren;
   1548       // If the next token isn't valid as our argument, we can't recover.
   1549       if (!Tok.isAnnotation() && Tok.getIdentifierInfo())
   1550         Tok.setKind(tok::identifier);
   1551       return;
   1552     }
   1553 
   1554     SourceLocation LParenLoc = Tok.getLocation();
   1555     LexNonComment(Tok);
   1556 
   1557     if (!Tok.isAnnotation() && Tok.getIdentifierInfo())
   1558       Tok.setKind(tok::identifier);
   1559     else {
   1560       Diag(Tok.getLocation(), diag::err_pp_identifier_arg_not_identifier)
   1561         << Tok.getKind();
   1562       // Don't walk past anything that's not a real token.
   1563       if (Tok.is(tok::eof) || Tok.is(tok::eod) || Tok.isAnnotation())
   1564         return;
   1565     }
   1566 
   1567     // Discard the ')', preserving 'Tok' as our result.
   1568     Token RParen;
   1569     LexNonComment(RParen);
   1570     if (RParen.isNot(tok::r_paren)) {
   1571       Diag(getLocForEndOfToken(Tok.getLocation()), diag::err_pp_expected_after)
   1572         << Tok.getKind() << tok::r_paren;
   1573       Diag(LParenLoc, diag::note_matching) << tok::l_paren;
   1574     }
   1575     return;
   1576   } else {
   1577     llvm_unreachable("Unknown identifier!");
   1578   }
   1579   CreateString(OS.str(), Tok, Tok.getLocation(), Tok.getLocation());
   1580 }
   1581 
   1582 void Preprocessor::markMacroAsUsed(MacroInfo *MI) {
   1583   // If the 'used' status changed, and the macro requires 'unused' warning,
   1584   // remove its SourceLocation from the warn-for-unused-macro locations.
   1585   if (MI->isWarnIfUnused() && !MI->isUsed())
   1586     WarnUnusedMacroLocs.erase(MI->getDefinitionLoc());
   1587   MI->setIsUsed(true);
   1588 }
   1589