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 expasion for the
     11 // preprocessor.
     12 //
     13 //===----------------------------------------------------------------------===//
     14 
     15 #include "clang/Lex/Preprocessor.h"
     16 #include "MacroArgs.h"
     17 #include "clang/Lex/MacroInfo.h"
     18 #include "clang/Basic/SourceManager.h"
     19 #include "clang/Basic/FileManager.h"
     20 #include "clang/Basic/TargetInfo.h"
     21 #include "clang/Lex/LexDiagnostic.h"
     22 #include "clang/Lex/CodeCompletionHandler.h"
     23 #include "clang/Lex/ExternalPreprocessorSource.h"
     24 #include "clang/Lex/LiteralSupport.h"
     25 #include "llvm/ADT/StringSwitch.h"
     26 #include "llvm/ADT/STLExtras.h"
     27 #include "llvm/Config/config.h"
     28 #include "llvm/Support/raw_ostream.h"
     29 #include "llvm/Support/ErrorHandling.h"
     30 #include <cstdio>
     31 #include <ctime>
     32 using namespace clang;
     33 
     34 MacroInfo *Preprocessor::getInfoForMacro(IdentifierInfo *II) const {
     35   assert(II->hasMacroDefinition() && "Identifier is not a macro!");
     36 
     37   llvm::DenseMap<IdentifierInfo*, MacroInfo*>::const_iterator Pos
     38     = Macros.find(II);
     39   if (Pos == Macros.end()) {
     40     // Load this macro from the external source.
     41     getExternalSource()->LoadMacroDefinition(II);
     42     Pos = Macros.find(II);
     43   }
     44   assert(Pos != Macros.end() && "Identifier macro info is missing!");
     45   return Pos->second;
     46 }
     47 
     48 /// setMacroInfo - Specify a macro for this identifier.
     49 ///
     50 void Preprocessor::setMacroInfo(IdentifierInfo *II, MacroInfo *MI) {
     51   if (MI) {
     52     Macros[II] = MI;
     53     II->setHasMacroDefinition(true);
     54   } else if (II->hasMacroDefinition()) {
     55     Macros.erase(II);
     56     II->setHasMacroDefinition(false);
     57   }
     58 }
     59 
     60 /// RegisterBuiltinMacro - Register the specified identifier in the identifier
     61 /// table and mark it as a builtin macro to be expanded.
     62 static IdentifierInfo *RegisterBuiltinMacro(Preprocessor &PP, const char *Name){
     63   // Get the identifier.
     64   IdentifierInfo *Id = PP.getIdentifierInfo(Name);
     65 
     66   // Mark it as being a macro that is builtin.
     67   MacroInfo *MI = PP.AllocateMacroInfo(SourceLocation());
     68   MI->setIsBuiltinMacro();
     69   PP.setMacroInfo(Id, MI);
     70   return Id;
     71 }
     72 
     73 
     74 /// RegisterBuiltinMacros - Register builtin macros, such as __LINE__ with the
     75 /// identifier table.
     76 void Preprocessor::RegisterBuiltinMacros() {
     77   Ident__LINE__ = RegisterBuiltinMacro(*this, "__LINE__");
     78   Ident__FILE__ = RegisterBuiltinMacro(*this, "__FILE__");
     79   Ident__DATE__ = RegisterBuiltinMacro(*this, "__DATE__");
     80   Ident__TIME__ = RegisterBuiltinMacro(*this, "__TIME__");
     81   Ident__COUNTER__ = RegisterBuiltinMacro(*this, "__COUNTER__");
     82   Ident_Pragma  = RegisterBuiltinMacro(*this, "_Pragma");
     83 
     84   // GCC Extensions.
     85   Ident__BASE_FILE__     = RegisterBuiltinMacro(*this, "__BASE_FILE__");
     86   Ident__INCLUDE_LEVEL__ = RegisterBuiltinMacro(*this, "__INCLUDE_LEVEL__");
     87   Ident__TIMESTAMP__     = RegisterBuiltinMacro(*this, "__TIMESTAMP__");
     88 
     89   // Clang Extensions.
     90   Ident__has_feature      = RegisterBuiltinMacro(*this, "__has_feature");
     91   Ident__has_extension    = RegisterBuiltinMacro(*this, "__has_extension");
     92   Ident__has_builtin      = RegisterBuiltinMacro(*this, "__has_builtin");
     93   Ident__has_attribute    = RegisterBuiltinMacro(*this, "__has_attribute");
     94   Ident__has_include      = RegisterBuiltinMacro(*this, "__has_include");
     95   Ident__has_include_next = RegisterBuiltinMacro(*this, "__has_include_next");
     96   Ident__has_warning      = RegisterBuiltinMacro(*this, "__has_warning");
     97 
     98   // Microsoft Extensions.
     99   if (Features.MicrosoftExt)
    100     Ident__pragma = RegisterBuiltinMacro(*this, "__pragma");
    101   else
    102     Ident__pragma = 0;
    103 }
    104 
    105 /// isTrivialSingleTokenExpansion - Return true if MI, which has a single token
    106 /// in its expansion, currently expands to that token literally.
    107 static bool isTrivialSingleTokenExpansion(const MacroInfo *MI,
    108                                           const IdentifierInfo *MacroIdent,
    109                                           Preprocessor &PP) {
    110   IdentifierInfo *II = MI->getReplacementToken(0).getIdentifierInfo();
    111 
    112   // If the token isn't an identifier, it's always literally expanded.
    113   if (II == 0) return true;
    114 
    115   // If the identifier is a macro, and if that macro is enabled, it may be
    116   // expanded so it's not a trivial expansion.
    117   if (II->hasMacroDefinition() && PP.getMacroInfo(II)->isEnabled() &&
    118       // Fast expanding "#define X X" is ok, because X would be disabled.
    119       II != MacroIdent)
    120     return false;
    121 
    122   // If this is an object-like macro invocation, it is safe to trivially expand
    123   // it.
    124   if (MI->isObjectLike()) return true;
    125 
    126   // If this is a function-like macro invocation, it's safe to trivially expand
    127   // as long as the identifier is not a macro argument.
    128   for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
    129        I != E; ++I)
    130     if (*I == II)
    131       return false;   // Identifier is a macro argument.
    132 
    133   return true;
    134 }
    135 
    136 
    137 /// isNextPPTokenLParen - Determine whether the next preprocessor token to be
    138 /// lexed is a '('.  If so, consume the token and return true, if not, this
    139 /// method should have no observable side-effect on the lexed tokens.
    140 bool Preprocessor::isNextPPTokenLParen() {
    141   // Do some quick tests for rejection cases.
    142   unsigned Val;
    143   if (CurLexer)
    144     Val = CurLexer->isNextPPTokenLParen();
    145   else if (CurPTHLexer)
    146     Val = CurPTHLexer->isNextPPTokenLParen();
    147   else
    148     Val = CurTokenLexer->isNextTokenLParen();
    149 
    150   if (Val == 2) {
    151     // We have run off the end.  If it's a source file we don't
    152     // examine enclosing ones (C99 5.1.1.2p4).  Otherwise walk up the
    153     // macro stack.
    154     if (CurPPLexer)
    155       return false;
    156     for (unsigned i = IncludeMacroStack.size(); i != 0; --i) {
    157       IncludeStackInfo &Entry = IncludeMacroStack[i-1];
    158       if (Entry.TheLexer)
    159         Val = Entry.TheLexer->isNextPPTokenLParen();
    160       else if (Entry.ThePTHLexer)
    161         Val = Entry.ThePTHLexer->isNextPPTokenLParen();
    162       else
    163         Val = Entry.TheTokenLexer->isNextTokenLParen();
    164 
    165       if (Val != 2)
    166         break;
    167 
    168       // Ran off the end of a source file?
    169       if (Entry.ThePPLexer)
    170         return false;
    171     }
    172   }
    173 
    174   // Okay, if we know that the token is a '(', lex it and return.  Otherwise we
    175   // have found something that isn't a '(' or we found the end of the
    176   // translation unit.  In either case, return false.
    177   return Val == 1;
    178 }
    179 
    180 /// HandleMacroExpandedIdentifier - If an identifier token is read that is to be
    181 /// expanded as a macro, handle it and return the next token as 'Identifier'.
    182 bool Preprocessor::HandleMacroExpandedIdentifier(Token &Identifier,
    183                                                  MacroInfo *MI) {
    184   // If this is a macro expansion in the "#if !defined(x)" line for the file,
    185   // then the macro could expand to different things in other contexts, we need
    186   // to disable the optimization in this case.
    187   if (CurPPLexer) CurPPLexer->MIOpt.ExpandedMacro();
    188 
    189   // If this is a builtin macro, like __LINE__ or _Pragma, handle it specially.
    190   if (MI->isBuiltinMacro()) {
    191     if (Callbacks) Callbacks->MacroExpands(Identifier, MI,
    192                                            Identifier.getLocation());
    193     ExpandBuiltinMacro(Identifier);
    194     return false;
    195   }
    196 
    197   /// Args - If this is a function-like macro expansion, this contains,
    198   /// for each macro argument, the list of tokens that were provided to the
    199   /// invocation.
    200   MacroArgs *Args = 0;
    201 
    202   // Remember where the end of the expansion occurred.  For an object-like
    203   // macro, this is the identifier.  For a function-like macro, this is the ')'.
    204   SourceLocation ExpansionEnd = Identifier.getLocation();
    205 
    206   // If this is a function-like macro, read the arguments.
    207   if (MI->isFunctionLike()) {
    208     // C99 6.10.3p10: If the preprocessing token immediately after the the macro
    209     // name isn't a '(', this macro should not be expanded.
    210     if (!isNextPPTokenLParen())
    211       return true;
    212 
    213     // Remember that we are now parsing the arguments to a macro invocation.
    214     // Preprocessor directives used inside macro arguments are not portable, and
    215     // this enables the warning.
    216     InMacroArgs = true;
    217     Args = ReadFunctionLikeMacroArgs(Identifier, MI, ExpansionEnd);
    218 
    219     // Finished parsing args.
    220     InMacroArgs = false;
    221 
    222     // If there was an error parsing the arguments, bail out.
    223     if (Args == 0) return false;
    224 
    225     ++NumFnMacroExpanded;
    226   } else {
    227     ++NumMacroExpanded;
    228   }
    229 
    230   // Notice that this macro has been used.
    231   markMacroAsUsed(MI);
    232 
    233   // Remember where the token is expanded.
    234   SourceLocation ExpandLoc = Identifier.getLocation();
    235 
    236   if (Callbacks) Callbacks->MacroExpands(Identifier, MI,
    237                                          SourceRange(ExpandLoc, ExpansionEnd));
    238 
    239   // If we started lexing a macro, enter the macro expansion body.
    240 
    241   // If this macro expands to no tokens, don't bother to push it onto the
    242   // expansion stack, only to take it right back off.
    243   if (MI->getNumTokens() == 0) {
    244     // No need for arg info.
    245     if (Args) Args->destroy(*this);
    246 
    247     // Ignore this macro use, just return the next token in the current
    248     // buffer.
    249     bool HadLeadingSpace = Identifier.hasLeadingSpace();
    250     bool IsAtStartOfLine = Identifier.isAtStartOfLine();
    251 
    252     Lex(Identifier);
    253 
    254     // If the identifier isn't on some OTHER line, inherit the leading
    255     // whitespace/first-on-a-line property of this token.  This handles
    256     // stuff like "! XX," -> "! ," and "   XX," -> "    ,", when XX is
    257     // empty.
    258     if (!Identifier.isAtStartOfLine()) {
    259       if (IsAtStartOfLine) Identifier.setFlag(Token::StartOfLine);
    260       if (HadLeadingSpace) Identifier.setFlag(Token::LeadingSpace);
    261     }
    262     Identifier.setFlag(Token::LeadingEmptyMacro);
    263     ++NumFastMacroExpanded;
    264     return false;
    265 
    266   } else if (MI->getNumTokens() == 1 &&
    267              isTrivialSingleTokenExpansion(MI, Identifier.getIdentifierInfo(),
    268                                            *this)) {
    269     // Otherwise, if this macro expands into a single trivially-expanded
    270     // token: expand it now.  This handles common cases like
    271     // "#define VAL 42".
    272 
    273     // No need for arg info.
    274     if (Args) Args->destroy(*this);
    275 
    276     // Propagate the isAtStartOfLine/hasLeadingSpace markers of the macro
    277     // identifier to the expanded token.
    278     bool isAtStartOfLine = Identifier.isAtStartOfLine();
    279     bool hasLeadingSpace = Identifier.hasLeadingSpace();
    280 
    281     // Replace the result token.
    282     Identifier = MI->getReplacementToken(0);
    283 
    284     // Restore the StartOfLine/LeadingSpace markers.
    285     Identifier.setFlagValue(Token::StartOfLine , isAtStartOfLine);
    286     Identifier.setFlagValue(Token::LeadingSpace, hasLeadingSpace);
    287 
    288     // Update the tokens location to include both its expansion and physical
    289     // locations.
    290     SourceLocation Loc =
    291       SourceMgr.createExpansionLoc(Identifier.getLocation(), ExpandLoc,
    292                                    ExpansionEnd,Identifier.getLength());
    293     Identifier.setLocation(Loc);
    294 
    295     // If this is a disabled macro or #define X X, we must mark the result as
    296     // unexpandable.
    297     if (IdentifierInfo *NewII = Identifier.getIdentifierInfo()) {
    298       if (MacroInfo *NewMI = getMacroInfo(NewII))
    299         if (!NewMI->isEnabled() || NewMI == MI)
    300           Identifier.setFlag(Token::DisableExpand);
    301     }
    302 
    303     // Since this is not an identifier token, it can't be macro expanded, so
    304     // we're done.
    305     ++NumFastMacroExpanded;
    306     return false;
    307   }
    308 
    309   // Start expanding the macro.
    310   EnterMacro(Identifier, ExpansionEnd, Args);
    311 
    312   // Now that the macro is at the top of the include stack, ask the
    313   // preprocessor to read the next token from it.
    314   Lex(Identifier);
    315   return false;
    316 }
    317 
    318 /// ReadFunctionLikeMacroArgs - After reading "MACRO" and knowing that the next
    319 /// token is the '(' of the macro, this method is invoked to read all of the
    320 /// actual arguments specified for the macro invocation.  This returns null on
    321 /// error.
    322 MacroArgs *Preprocessor::ReadFunctionLikeMacroArgs(Token &MacroName,
    323                                                    MacroInfo *MI,
    324                                                    SourceLocation &MacroEnd) {
    325   // The number of fixed arguments to parse.
    326   unsigned NumFixedArgsLeft = MI->getNumArgs();
    327   bool isVariadic = MI->isVariadic();
    328 
    329   // Outer loop, while there are more arguments, keep reading them.
    330   Token Tok;
    331 
    332   // Read arguments as unexpanded tokens.  This avoids issues, e.g., where
    333   // an argument value in a macro could expand to ',' or '(' or ')'.
    334   LexUnexpandedToken(Tok);
    335   assert(Tok.is(tok::l_paren) && "Error computing l-paren-ness?");
    336 
    337   // ArgTokens - Build up a list of tokens that make up each argument.  Each
    338   // argument is separated by an EOF token.  Use a SmallVector so we can avoid
    339   // heap allocations in the common case.
    340   SmallVector<Token, 64> ArgTokens;
    341 
    342   unsigned NumActuals = 0;
    343   while (Tok.isNot(tok::r_paren)) {
    344     assert((Tok.is(tok::l_paren) || Tok.is(tok::comma)) &&
    345            "only expect argument separators here");
    346 
    347     unsigned ArgTokenStart = ArgTokens.size();
    348     SourceLocation ArgStartLoc = Tok.getLocation();
    349 
    350     // C99 6.10.3p11: Keep track of the number of l_parens we have seen.  Note
    351     // that we already consumed the first one.
    352     unsigned NumParens = 0;
    353 
    354     while (1) {
    355       // Read arguments as unexpanded tokens.  This avoids issues, e.g., where
    356       // an argument value in a macro could expand to ',' or '(' or ')'.
    357       LexUnexpandedToken(Tok);
    358 
    359       if (Tok.is(tok::eof) || Tok.is(tok::eod)) { // "#if f(<eof>" & "#if f(\n"
    360         Diag(MacroName, diag::err_unterm_macro_invoc);
    361         // Do not lose the EOF/EOD.  Return it to the client.
    362         MacroName = Tok;
    363         return 0;
    364       } else if (Tok.is(tok::r_paren)) {
    365         // If we found the ) token, the macro arg list is done.
    366         if (NumParens-- == 0) {
    367           MacroEnd = Tok.getLocation();
    368           break;
    369         }
    370       } else if (Tok.is(tok::l_paren)) {
    371         ++NumParens;
    372       } else if (Tok.is(tok::comma) && NumParens == 0) {
    373         // Comma ends this argument if there are more fixed arguments expected.
    374         // However, if this is a variadic macro, and this is part of the
    375         // variadic part, then the comma is just an argument token.
    376         if (!isVariadic) break;
    377         if (NumFixedArgsLeft > 1)
    378           break;
    379       } else if (Tok.is(tok::comment) && !KeepMacroComments) {
    380         // If this is a comment token in the argument list and we're just in
    381         // -C mode (not -CC mode), discard the comment.
    382         continue;
    383       } else if (Tok.getIdentifierInfo() != 0) {
    384         // Reading macro arguments can cause macros that we are currently
    385         // expanding from to be popped off the expansion stack.  Doing so causes
    386         // them to be reenabled for expansion.  Here we record whether any
    387         // identifiers we lex as macro arguments correspond to disabled macros.
    388         // If so, we mark the token as noexpand.  This is a subtle aspect of
    389         // C99 6.10.3.4p2.
    390         if (MacroInfo *MI = getMacroInfo(Tok.getIdentifierInfo()))
    391           if (!MI->isEnabled())
    392             Tok.setFlag(Token::DisableExpand);
    393       } else if (Tok.is(tok::code_completion)) {
    394         if (CodeComplete)
    395           CodeComplete->CodeCompleteMacroArgument(MacroName.getIdentifierInfo(),
    396                                                   MI, NumActuals);
    397         // Don't mark that we reached the code-completion point because the
    398         // parser is going to handle the token and there will be another
    399         // code-completion callback.
    400       }
    401 
    402       ArgTokens.push_back(Tok);
    403     }
    404 
    405     // If this was an empty argument list foo(), don't add this as an empty
    406     // argument.
    407     if (ArgTokens.empty() && Tok.getKind() == tok::r_paren)
    408       break;
    409 
    410     // If this is not a variadic macro, and too many args were specified, emit
    411     // an error.
    412     if (!isVariadic && NumFixedArgsLeft == 0) {
    413       if (ArgTokens.size() != ArgTokenStart)
    414         ArgStartLoc = ArgTokens[ArgTokenStart].getLocation();
    415 
    416       // Emit the diagnostic at the macro name in case there is a missing ).
    417       // Emitting it at the , could be far away from the macro name.
    418       Diag(ArgStartLoc, diag::err_too_many_args_in_macro_invoc);
    419       return 0;
    420     }
    421 
    422     // Empty arguments are standard in C99 and C++0x, and are supported as an extension in
    423     // other modes.
    424     if (ArgTokens.size() == ArgTokenStart && !Features.C99)
    425       Diag(Tok, Features.CPlusPlus0x ?
    426            diag::warn_cxx98_compat_empty_fnmacro_arg :
    427            diag::ext_empty_fnmacro_arg);
    428 
    429     // Add a marker EOF token to the end of the token list for this argument.
    430     Token EOFTok;
    431     EOFTok.startToken();
    432     EOFTok.setKind(tok::eof);
    433     EOFTok.setLocation(Tok.getLocation());
    434     EOFTok.setLength(0);
    435     ArgTokens.push_back(EOFTok);
    436     ++NumActuals;
    437     assert(NumFixedArgsLeft != 0 && "Too many arguments parsed");
    438     --NumFixedArgsLeft;
    439   }
    440 
    441   // Okay, we either found the r_paren.  Check to see if we parsed too few
    442   // arguments.
    443   unsigned MinArgsExpected = MI->getNumArgs();
    444 
    445   // See MacroArgs instance var for description of this.
    446   bool isVarargsElided = false;
    447 
    448   if (NumActuals < MinArgsExpected) {
    449     // There are several cases where too few arguments is ok, handle them now.
    450     if (NumActuals == 0 && MinArgsExpected == 1) {
    451       // #define A(X)  or  #define A(...)   ---> A()
    452 
    453       // If there is exactly one argument, and that argument is missing,
    454       // then we have an empty "()" argument empty list.  This is fine, even if
    455       // the macro expects one argument (the argument is just empty).
    456       isVarargsElided = MI->isVariadic();
    457     } else if (MI->isVariadic() &&
    458                (NumActuals+1 == MinArgsExpected ||  // A(x, ...) -> A(X)
    459                 (NumActuals == 0 && MinArgsExpected == 2))) {// A(x,...) -> A()
    460       // Varargs where the named vararg parameter is missing: ok as extension.
    461       // #define A(x, ...)
    462       // A("blah")
    463       Diag(Tok, diag::ext_missing_varargs_arg);
    464 
    465       // Remember this occurred, allowing us to elide the comma when used for
    466       // cases like:
    467       //   #define A(x, foo...) blah(a, ## foo)
    468       //   #define B(x, ...) blah(a, ## __VA_ARGS__)
    469       //   #define C(...) blah(a, ## __VA_ARGS__)
    470       //  A(x) B(x) C()
    471       isVarargsElided = true;
    472     } else {
    473       // Otherwise, emit the error.
    474       Diag(Tok, diag::err_too_few_args_in_macro_invoc);
    475       return 0;
    476     }
    477 
    478     // Add a marker EOF token to the end of the token list for this argument.
    479     SourceLocation EndLoc = Tok.getLocation();
    480     Tok.startToken();
    481     Tok.setKind(tok::eof);
    482     Tok.setLocation(EndLoc);
    483     Tok.setLength(0);
    484     ArgTokens.push_back(Tok);
    485 
    486     // If we expect two arguments, add both as empty.
    487     if (NumActuals == 0 && MinArgsExpected == 2)
    488       ArgTokens.push_back(Tok);
    489 
    490   } else if (NumActuals > MinArgsExpected && !MI->isVariadic()) {
    491     // Emit the diagnostic at the macro name in case there is a missing ).
    492     // Emitting it at the , could be far away from the macro name.
    493     Diag(MacroName, diag::err_too_many_args_in_macro_invoc);
    494     return 0;
    495   }
    496 
    497   return MacroArgs::create(MI, ArgTokens, isVarargsElided, *this);
    498 }
    499 
    500 /// \brief Keeps macro expanded tokens for TokenLexers.
    501 //
    502 /// Works like a stack; a TokenLexer adds the macro expanded tokens that is
    503 /// going to lex in the cache and when it finishes the tokens are removed
    504 /// from the end of the cache.
    505 Token *Preprocessor::cacheMacroExpandedTokens(TokenLexer *tokLexer,
    506                                               ArrayRef<Token> tokens) {
    507   assert(tokLexer);
    508   if (tokens.empty())
    509     return 0;
    510 
    511   size_t newIndex = MacroExpandedTokens.size();
    512   bool cacheNeedsToGrow = tokens.size() >
    513                       MacroExpandedTokens.capacity()-MacroExpandedTokens.size();
    514   MacroExpandedTokens.append(tokens.begin(), tokens.end());
    515 
    516   if (cacheNeedsToGrow) {
    517     // Go through all the TokenLexers whose 'Tokens' pointer points in the
    518     // buffer and update the pointers to the (potential) new buffer array.
    519     for (unsigned i = 0, e = MacroExpandingLexersStack.size(); i != e; ++i) {
    520       TokenLexer *prevLexer;
    521       size_t tokIndex;
    522       llvm::tie(prevLexer, tokIndex) = MacroExpandingLexersStack[i];
    523       prevLexer->Tokens = MacroExpandedTokens.data() + tokIndex;
    524     }
    525   }
    526 
    527   MacroExpandingLexersStack.push_back(std::make_pair(tokLexer, newIndex));
    528   return MacroExpandedTokens.data() + newIndex;
    529 }
    530 
    531 void Preprocessor::removeCachedMacroExpandedTokensOfLastLexer() {
    532   assert(!MacroExpandingLexersStack.empty());
    533   size_t tokIndex = MacroExpandingLexersStack.back().second;
    534   assert(tokIndex < MacroExpandedTokens.size());
    535   // Pop the cached macro expanded tokens from the end.
    536   MacroExpandedTokens.resize(tokIndex);
    537   MacroExpandingLexersStack.pop_back();
    538 }
    539 
    540 /// ComputeDATE_TIME - Compute the current time, enter it into the specified
    541 /// scratch buffer, then return DATELoc/TIMELoc locations with the position of
    542 /// the identifier tokens inserted.
    543 static void ComputeDATE_TIME(SourceLocation &DATELoc, SourceLocation &TIMELoc,
    544                              Preprocessor &PP) {
    545   time_t TT = time(0);
    546   struct tm *TM = localtime(&TT);
    547 
    548   static const char * const Months[] = {
    549     "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"
    550   };
    551 
    552   char TmpBuffer[32];
    553 #ifdef LLVM_ON_WIN32
    554   sprintf(TmpBuffer, "\"%s %2d %4d\"", Months[TM->tm_mon], TM->tm_mday,
    555           TM->tm_year+1900);
    556 #else
    557   snprintf(TmpBuffer, sizeof(TmpBuffer), "\"%s %2d %4d\"", Months[TM->tm_mon], TM->tm_mday,
    558           TM->tm_year+1900);
    559 #endif
    560 
    561   Token TmpTok;
    562   TmpTok.startToken();
    563   PP.CreateString(TmpBuffer, strlen(TmpBuffer), TmpTok);
    564   DATELoc = TmpTok.getLocation();
    565 
    566 #ifdef LLVM_ON_WIN32
    567   sprintf(TmpBuffer, "\"%02d:%02d:%02d\"", TM->tm_hour, TM->tm_min, TM->tm_sec);
    568 #else
    569   snprintf(TmpBuffer, sizeof(TmpBuffer), "\"%02d:%02d:%02d\"", TM->tm_hour, TM->tm_min, TM->tm_sec);
    570 #endif
    571   PP.CreateString(TmpBuffer, strlen(TmpBuffer), TmpTok);
    572   TIMELoc = TmpTok.getLocation();
    573 }
    574 
    575 
    576 /// HasFeature - Return true if we recognize and implement the feature
    577 /// specified by the identifier as a standard language feature.
    578 static bool HasFeature(const Preprocessor &PP, const IdentifierInfo *II) {
    579   const LangOptions &LangOpts = PP.getLangOptions();
    580 
    581   return llvm::StringSwitch<bool>(II->getName())
    582            .Case("attribute_analyzer_noreturn", true)
    583            .Case("attribute_availability", true)
    584            .Case("attribute_cf_returns_not_retained", true)
    585            .Case("attribute_cf_returns_retained", true)
    586            .Case("attribute_deprecated_with_message", true)
    587            .Case("attribute_ext_vector_type", true)
    588            .Case("attribute_ns_returns_not_retained", true)
    589            .Case("attribute_ns_returns_retained", true)
    590            .Case("attribute_ns_consumes_self", true)
    591            .Case("attribute_ns_consumed", true)
    592            .Case("attribute_cf_consumed", true)
    593            .Case("attribute_objc_ivar_unused", true)
    594            .Case("attribute_objc_method_family", true)
    595            .Case("attribute_overloadable", true)
    596            .Case("attribute_unavailable_with_message", true)
    597            .Case("blocks", LangOpts.Blocks)
    598            .Case("cxx_exceptions", LangOpts.Exceptions)
    599            .Case("cxx_rtti", LangOpts.RTTI)
    600            .Case("enumerator_attributes", true)
    601            // Objective-C features
    602            .Case("objc_arr", LangOpts.ObjCAutoRefCount) // FIXME: REMOVE?
    603            .Case("objc_arc", LangOpts.ObjCAutoRefCount)
    604            .Case("objc_arc_weak", LangOpts.ObjCAutoRefCount &&
    605                  LangOpts.ObjCRuntimeHasWeak)
    606            .Case("objc_fixed_enum", LangOpts.ObjC2)
    607            .Case("objc_instancetype", LangOpts.ObjC2)
    608            .Case("objc_nonfragile_abi", LangOpts.ObjCNonFragileABI)
    609            .Case("objc_weak_class", LangOpts.ObjCNonFragileABI)
    610            .Case("ownership_holds", true)
    611            .Case("ownership_returns", true)
    612            .Case("ownership_takes", true)
    613            .Case("arc_cf_code_audited", true)
    614            // C1X features
    615            .Case("c_alignas", LangOpts.C1X)
    616            .Case("c_generic_selections", LangOpts.C1X)
    617            .Case("c_static_assert", LangOpts.C1X)
    618            // C++0x features
    619            .Case("cxx_access_control_sfinae", LangOpts.CPlusPlus0x)
    620            .Case("cxx_alias_templates", LangOpts.CPlusPlus0x)
    621            .Case("cxx_alignas", LangOpts.CPlusPlus0x)
    622            .Case("cxx_attributes", LangOpts.CPlusPlus0x)
    623            .Case("cxx_auto_type", LangOpts.CPlusPlus0x)
    624          //.Case("cxx_constexpr", false);
    625            .Case("cxx_decltype", LangOpts.CPlusPlus0x)
    626            .Case("cxx_default_function_template_args", LangOpts.CPlusPlus0x)
    627            .Case("cxx_delegating_constructors", LangOpts.CPlusPlus0x)
    628            .Case("cxx_deleted_functions", LangOpts.CPlusPlus0x)
    629            .Case("cxx_explicit_conversions", LangOpts.CPlusPlus0x)
    630          //.Case("cxx_generalized_initializers", LangOpts.CPlusPlus0x)
    631            .Case("cxx_implicit_moves", LangOpts.CPlusPlus0x)
    632          //.Case("cxx_inheriting_constructors", false)
    633            .Case("cxx_inline_namespaces", LangOpts.CPlusPlus0x)
    634          //.Case("cxx_lambdas", false)
    635            .Case("cxx_nonstatic_member_init", LangOpts.CPlusPlus0x)
    636            .Case("cxx_noexcept", LangOpts.CPlusPlus0x)
    637            .Case("cxx_nullptr", LangOpts.CPlusPlus0x)
    638            .Case("cxx_override_control", LangOpts.CPlusPlus0x)
    639            .Case("cxx_range_for", LangOpts.CPlusPlus0x)
    640          //.Case("cxx_raw_string_literals", false)
    641            .Case("cxx_reference_qualified_functions", LangOpts.CPlusPlus0x)
    642            .Case("cxx_rvalue_references", LangOpts.CPlusPlus0x)
    643            .Case("cxx_strong_enums", LangOpts.CPlusPlus0x)
    644            .Case("cxx_static_assert", LangOpts.CPlusPlus0x)
    645            .Case("cxx_trailing_return", LangOpts.CPlusPlus0x)
    646          //.Case("cxx_unicode_literals", false)
    647          //.Case("cxx_unrestricted_unions", false)
    648          //.Case("cxx_user_literals", false)
    649            .Case("cxx_variadic_templates", LangOpts.CPlusPlus0x)
    650            // Type traits
    651            .Case("has_nothrow_assign", LangOpts.CPlusPlus)
    652            .Case("has_nothrow_copy", LangOpts.CPlusPlus)
    653            .Case("has_nothrow_constructor", LangOpts.CPlusPlus)
    654            .Case("has_trivial_assign", LangOpts.CPlusPlus)
    655            .Case("has_trivial_copy", LangOpts.CPlusPlus)
    656            .Case("has_trivial_constructor", LangOpts.CPlusPlus)
    657            .Case("has_trivial_destructor", LangOpts.CPlusPlus)
    658            .Case("has_virtual_destructor", LangOpts.CPlusPlus)
    659            .Case("is_abstract", LangOpts.CPlusPlus)
    660            .Case("is_base_of", LangOpts.CPlusPlus)
    661            .Case("is_class", LangOpts.CPlusPlus)
    662            .Case("is_convertible_to", LangOpts.CPlusPlus)
    663             // __is_empty is available only if the horrible
    664             // "struct __is_empty" parsing hack hasn't been needed in this
    665             // translation unit. If it has, __is_empty reverts to a normal
    666             // identifier and __has_feature(is_empty) evaluates false.
    667            .Case("is_empty",
    668                  LangOpts.CPlusPlus &&
    669                  PP.getIdentifierInfo("__is_empty")->getTokenID()
    670                                                             != tok::identifier)
    671            .Case("is_enum", LangOpts.CPlusPlus)
    672            .Case("is_literal", LangOpts.CPlusPlus)
    673            .Case("is_standard_layout", LangOpts.CPlusPlus)
    674            // __is_pod is available only if the horrible
    675            // "struct __is_pod" parsing hack hasn't been needed in this
    676            // translation unit. If it has, __is_pod reverts to a normal
    677            // identifier and __has_feature(is_pod) evaluates false.
    678            .Case("is_pod",
    679                  LangOpts.CPlusPlus &&
    680                  PP.getIdentifierInfo("__is_pod")->getTokenID()
    681                                                             != tok::identifier)
    682            .Case("is_polymorphic", LangOpts.CPlusPlus)
    683            .Case("is_trivial", LangOpts.CPlusPlus)
    684            .Case("is_trivially_copyable", LangOpts.CPlusPlus)
    685            .Case("is_union", LangOpts.CPlusPlus)
    686            .Case("tls", PP.getTargetInfo().isTLSSupported())
    687            .Case("underlying_type", LangOpts.CPlusPlus)
    688            .Default(false);
    689 }
    690 
    691 /// HasExtension - Return true if we recognize and implement the feature
    692 /// specified by the identifier, either as an extension or a standard language
    693 /// feature.
    694 static bool HasExtension(const Preprocessor &PP, const IdentifierInfo *II) {
    695   if (HasFeature(PP, II))
    696     return true;
    697 
    698   // If the use of an extension results in an error diagnostic, extensions are
    699   // effectively unavailable, so just return false here.
    700   if (PP.getDiagnostics().getExtensionHandlingBehavior() ==
    701       DiagnosticsEngine::Ext_Error)
    702     return false;
    703 
    704   const LangOptions &LangOpts = PP.getLangOptions();
    705 
    706   // Because we inherit the feature list from HasFeature, this string switch
    707   // must be less restrictive than HasFeature's.
    708   return llvm::StringSwitch<bool>(II->getName())
    709            // C1X features supported by other languages as extensions.
    710            .Case("c_alignas", true)
    711            .Case("c_generic_selections", true)
    712            .Case("c_static_assert", true)
    713            // C++0x features supported by other languages as extensions.
    714            .Case("cxx_deleted_functions", LangOpts.CPlusPlus)
    715            .Case("cxx_explicit_conversions", LangOpts.CPlusPlus)
    716            .Case("cxx_inline_namespaces", LangOpts.CPlusPlus)
    717            .Case("cxx_nonstatic_member_init", LangOpts.CPlusPlus)
    718            .Case("cxx_override_control", LangOpts.CPlusPlus)
    719            .Case("cxx_range_for", LangOpts.CPlusPlus)
    720            .Case("cxx_reference_qualified_functions", LangOpts.CPlusPlus)
    721            .Case("cxx_rvalue_references", LangOpts.CPlusPlus)
    722            .Default(false);
    723 }
    724 
    725 /// HasAttribute -  Return true if we recognize and implement the attribute
    726 /// specified by the given identifier.
    727 static bool HasAttribute(const IdentifierInfo *II) {
    728     return llvm::StringSwitch<bool>(II->getName())
    729 #include "clang/Lex/AttrSpellings.inc"
    730         .Default(false);
    731 }
    732 
    733 /// EvaluateHasIncludeCommon - Process a '__has_include("path")'
    734 /// or '__has_include_next("path")' expression.
    735 /// Returns true if successful.
    736 static bool EvaluateHasIncludeCommon(Token &Tok,
    737                                      IdentifierInfo *II, Preprocessor &PP,
    738                                      const DirectoryLookup *LookupFrom) {
    739   SourceLocation LParenLoc;
    740 
    741   // Get '('.
    742   PP.LexNonComment(Tok);
    743 
    744   // Ensure we have a '('.
    745   if (Tok.isNot(tok::l_paren)) {
    746     PP.Diag(Tok.getLocation(), diag::err_pp_missing_lparen) << II->getName();
    747     return false;
    748   }
    749 
    750   // Save '(' location for possible missing ')' message.
    751   LParenLoc = Tok.getLocation();
    752 
    753   // Get the file name.
    754   PP.getCurrentLexer()->LexIncludeFilename(Tok);
    755 
    756   // Reserve a buffer to get the spelling.
    757   llvm::SmallString<128> FilenameBuffer;
    758   StringRef Filename;
    759   SourceLocation EndLoc;
    760 
    761   switch (Tok.getKind()) {
    762   case tok::eod:
    763     // If the token kind is EOD, the error has already been diagnosed.
    764     return false;
    765 
    766   case tok::angle_string_literal:
    767   case tok::string_literal: {
    768     bool Invalid = false;
    769     Filename = PP.getSpelling(Tok, FilenameBuffer, &Invalid);
    770     if (Invalid)
    771       return false;
    772     break;
    773   }
    774 
    775   case tok::less:
    776     // This could be a <foo/bar.h> file coming from a macro expansion.  In this
    777     // case, glue the tokens together into FilenameBuffer and interpret those.
    778     FilenameBuffer.push_back('<');
    779     if (PP.ConcatenateIncludeName(FilenameBuffer, EndLoc))
    780       return false;   // Found <eod> but no ">"?  Diagnostic already emitted.
    781     Filename = FilenameBuffer.str();
    782     break;
    783   default:
    784     PP.Diag(Tok.getLocation(), diag::err_pp_expects_filename);
    785     return false;
    786   }
    787 
    788   bool isAngled = PP.GetIncludeFilenameSpelling(Tok.getLocation(), Filename);
    789   // If GetIncludeFilenameSpelling set the start ptr to null, there was an
    790   // error.
    791   if (Filename.empty())
    792     return false;
    793 
    794   // Search include directories.
    795   const DirectoryLookup *CurDir;
    796   const FileEntry *File =
    797       PP.LookupFile(Filename, isAngled, LookupFrom, CurDir, NULL, NULL, NULL);
    798 
    799   // Get the result value.  Result = true means the file exists.
    800   bool Result = File != 0;
    801 
    802   // Get ')'.
    803   PP.LexNonComment(Tok);
    804 
    805   // Ensure we have a trailing ).
    806   if (Tok.isNot(tok::r_paren)) {
    807     PP.Diag(Tok.getLocation(), diag::err_pp_missing_rparen) << II->getName();
    808     PP.Diag(LParenLoc, diag::note_matching) << "(";
    809     return false;
    810   }
    811 
    812   return Result;
    813 }
    814 
    815 /// EvaluateHasInclude - Process a '__has_include("path")' expression.
    816 /// Returns true if successful.
    817 static bool EvaluateHasInclude(Token &Tok, IdentifierInfo *II,
    818                                Preprocessor &PP) {
    819   return EvaluateHasIncludeCommon(Tok, II, PP, NULL);
    820 }
    821 
    822 /// EvaluateHasIncludeNext - Process '__has_include_next("path")' expression.
    823 /// Returns true if successful.
    824 static bool EvaluateHasIncludeNext(Token &Tok,
    825                                    IdentifierInfo *II, Preprocessor &PP) {
    826   // __has_include_next is like __has_include, except that we start
    827   // searching after the current found directory.  If we can't do this,
    828   // issue a diagnostic.
    829   const DirectoryLookup *Lookup = PP.GetCurDirLookup();
    830   if (PP.isInPrimaryFile()) {
    831     Lookup = 0;
    832     PP.Diag(Tok, diag::pp_include_next_in_primary);
    833   } else if (Lookup == 0) {
    834     PP.Diag(Tok, diag::pp_include_next_absolute_path);
    835   } else {
    836     // Start looking up in the next directory.
    837     ++Lookup;
    838   }
    839 
    840   return EvaluateHasIncludeCommon(Tok, II, PP, Lookup);
    841 }
    842 
    843 /// ExpandBuiltinMacro - If an identifier token is read that is to be expanded
    844 /// as a builtin macro, handle it and return the next token as 'Tok'.
    845 void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
    846   // Figure out which token this is.
    847   IdentifierInfo *II = Tok.getIdentifierInfo();
    848   assert(II && "Can't be a macro without id info!");
    849 
    850   // If this is an _Pragma or Microsoft __pragma directive, expand it,
    851   // invoke the pragma handler, then lex the token after it.
    852   if (II == Ident_Pragma)
    853     return Handle_Pragma(Tok);
    854   else if (II == Ident__pragma) // in non-MS mode this is null
    855     return HandleMicrosoft__pragma(Tok);
    856 
    857   ++NumBuiltinMacroExpanded;
    858 
    859   llvm::SmallString<128> TmpBuffer;
    860   llvm::raw_svector_ostream OS(TmpBuffer);
    861 
    862   // Set up the return result.
    863   Tok.setIdentifierInfo(0);
    864   Tok.clearFlag(Token::NeedsCleaning);
    865 
    866   if (II == Ident__LINE__) {
    867     // C99 6.10.8: "__LINE__: The presumed line number (within the current
    868     // source file) of the current source line (an integer constant)".  This can
    869     // be affected by #line.
    870     SourceLocation Loc = Tok.getLocation();
    871 
    872     // Advance to the location of the first _, this might not be the first byte
    873     // of the token if it starts with an escaped newline.
    874     Loc = AdvanceToTokenCharacter(Loc, 0);
    875 
    876     // One wrinkle here is that GCC expands __LINE__ to location of the *end* of
    877     // a macro expansion.  This doesn't matter for object-like macros, but
    878     // can matter for a function-like macro that expands to contain __LINE__.
    879     // Skip down through expansion points until we find a file loc for the
    880     // end of the expansion history.
    881     Loc = SourceMgr.getExpansionRange(Loc).second;
    882     PresumedLoc PLoc = SourceMgr.getPresumedLoc(Loc);
    883 
    884     // __LINE__ expands to a simple numeric value.
    885     OS << (PLoc.isValid()? PLoc.getLine() : 1);
    886     Tok.setKind(tok::numeric_constant);
    887   } else if (II == Ident__FILE__ || II == Ident__BASE_FILE__) {
    888     // C99 6.10.8: "__FILE__: The presumed name of the current source file (a
    889     // character string literal)". This can be affected by #line.
    890     PresumedLoc PLoc = SourceMgr.getPresumedLoc(Tok.getLocation());
    891 
    892     // __BASE_FILE__ is a GNU extension that returns the top of the presumed
    893     // #include stack instead of the current file.
    894     if (II == Ident__BASE_FILE__ && PLoc.isValid()) {
    895       SourceLocation NextLoc = PLoc.getIncludeLoc();
    896       while (NextLoc.isValid()) {
    897         PLoc = SourceMgr.getPresumedLoc(NextLoc);
    898         if (PLoc.isInvalid())
    899           break;
    900 
    901         NextLoc = PLoc.getIncludeLoc();
    902       }
    903     }
    904 
    905     // Escape this filename.  Turn '\' -> '\\' '"' -> '\"'
    906     llvm::SmallString<128> FN;
    907     if (PLoc.isValid()) {
    908       FN += PLoc.getFilename();
    909       Lexer::Stringify(FN);
    910       OS << '"' << FN.str() << '"';
    911     }
    912     Tok.setKind(tok::string_literal);
    913   } else if (II == Ident__DATE__) {
    914     if (!DATELoc.isValid())
    915       ComputeDATE_TIME(DATELoc, TIMELoc, *this);
    916     Tok.setKind(tok::string_literal);
    917     Tok.setLength(strlen("\"Mmm dd yyyy\""));
    918     Tok.setLocation(SourceMgr.createExpansionLoc(DATELoc, Tok.getLocation(),
    919                                                  Tok.getLocation(),
    920                                                  Tok.getLength()));
    921     return;
    922   } else if (II == Ident__TIME__) {
    923     if (!TIMELoc.isValid())
    924       ComputeDATE_TIME(DATELoc, TIMELoc, *this);
    925     Tok.setKind(tok::string_literal);
    926     Tok.setLength(strlen("\"hh:mm:ss\""));
    927     Tok.setLocation(SourceMgr.createExpansionLoc(TIMELoc, Tok.getLocation(),
    928                                                  Tok.getLocation(),
    929                                                  Tok.getLength()));
    930     return;
    931   } else if (II == Ident__INCLUDE_LEVEL__) {
    932     // Compute the presumed include depth of this token.  This can be affected
    933     // by GNU line markers.
    934     unsigned Depth = 0;
    935 
    936     PresumedLoc PLoc = SourceMgr.getPresumedLoc(Tok.getLocation());
    937     if (PLoc.isValid()) {
    938       PLoc = SourceMgr.getPresumedLoc(PLoc.getIncludeLoc());
    939       for (; PLoc.isValid(); ++Depth)
    940         PLoc = SourceMgr.getPresumedLoc(PLoc.getIncludeLoc());
    941     }
    942 
    943     // __INCLUDE_LEVEL__ expands to a simple numeric value.
    944     OS << Depth;
    945     Tok.setKind(tok::numeric_constant);
    946   } else if (II == Ident__TIMESTAMP__) {
    947     // MSVC, ICC, GCC, VisualAge C++ extension.  The generated string should be
    948     // of the form "Ddd Mmm dd hh::mm::ss yyyy", which is returned by asctime.
    949 
    950     // Get the file that we are lexing out of.  If we're currently lexing from
    951     // a macro, dig into the include stack.
    952     const FileEntry *CurFile = 0;
    953     PreprocessorLexer *TheLexer = getCurrentFileLexer();
    954 
    955     if (TheLexer)
    956       CurFile = SourceMgr.getFileEntryForID(TheLexer->getFileID());
    957 
    958     const char *Result;
    959     if (CurFile) {
    960       time_t TT = CurFile->getModificationTime();
    961       struct tm *TM = localtime(&TT);
    962       Result = asctime(TM);
    963     } else {
    964       Result = "??? ??? ?? ??:??:?? ????\n";
    965     }
    966     // Surround the string with " and strip the trailing newline.
    967     OS << '"' << StringRef(Result, strlen(Result)-1) << '"';
    968     Tok.setKind(tok::string_literal);
    969   } else if (II == Ident__COUNTER__) {
    970     // __COUNTER__ expands to a simple numeric value.
    971     OS << CounterValue++;
    972     Tok.setKind(tok::numeric_constant);
    973   } else if (II == Ident__has_feature   ||
    974              II == Ident__has_extension ||
    975              II == Ident__has_builtin   ||
    976              II == Ident__has_attribute) {
    977     // The argument to these builtins should be a parenthesized identifier.
    978     SourceLocation StartLoc = Tok.getLocation();
    979 
    980     bool IsValid = false;
    981     IdentifierInfo *FeatureII = 0;
    982 
    983     // Read the '('.
    984     Lex(Tok);
    985     if (Tok.is(tok::l_paren)) {
    986       // Read the identifier
    987       Lex(Tok);
    988       if (Tok.is(tok::identifier)) {
    989         FeatureII = Tok.getIdentifierInfo();
    990 
    991         // Read the ')'.
    992         Lex(Tok);
    993         if (Tok.is(tok::r_paren))
    994           IsValid = true;
    995       }
    996     }
    997 
    998     bool Value = false;
    999     if (!IsValid)
   1000       Diag(StartLoc, diag::err_feature_check_malformed);
   1001     else if (II == Ident__has_builtin) {
   1002       // Check for a builtin is trivial.
   1003       Value = FeatureII->getBuiltinID() != 0;
   1004     } else if (II == Ident__has_attribute)
   1005       Value = HasAttribute(FeatureII);
   1006     else if (II == Ident__has_extension)
   1007       Value = HasExtension(*this, FeatureII);
   1008     else {
   1009       assert(II == Ident__has_feature && "Must be feature check");
   1010       Value = HasFeature(*this, FeatureII);
   1011     }
   1012 
   1013     OS << (int)Value;
   1014     Tok.setKind(tok::numeric_constant);
   1015   } else if (II == Ident__has_include ||
   1016              II == Ident__has_include_next) {
   1017     // The argument to these two builtins should be a parenthesized
   1018     // file name string literal using angle brackets (<>) or
   1019     // double-quotes ("").
   1020     bool Value;
   1021     if (II == Ident__has_include)
   1022       Value = EvaluateHasInclude(Tok, II, *this);
   1023     else
   1024       Value = EvaluateHasIncludeNext(Tok, II, *this);
   1025     OS << (int)Value;
   1026     Tok.setKind(tok::numeric_constant);
   1027   } else if (II == Ident__has_warning) {
   1028     // The argument should be a parenthesized string literal.
   1029     // The argument to these builtins should be a parenthesized identifier.
   1030     SourceLocation StartLoc = Tok.getLocation();
   1031     bool IsValid = false;
   1032     bool Value = false;
   1033     // Read the '('.
   1034     Lex(Tok);
   1035     do {
   1036       if (Tok.is(tok::l_paren)) {
   1037         // Read the string.
   1038         Lex(Tok);
   1039 
   1040         // We need at least one string literal.
   1041         if (!Tok.is(tok::string_literal)) {
   1042           StartLoc = Tok.getLocation();
   1043           IsValid = false;
   1044           // Eat tokens until ')'.
   1045           do Lex(Tok); while (!(Tok.is(tok::r_paren) || Tok.is(tok::eod)));
   1046           break;
   1047         }
   1048 
   1049         // String concatenation allows multiple strings, which can even come
   1050         // from macro expansion.
   1051         SmallVector<Token, 4> StrToks;
   1052         while (Tok.is(tok::string_literal)) {
   1053           StrToks.push_back(Tok);
   1054           LexUnexpandedToken(Tok);
   1055         }
   1056 
   1057         // Is the end a ')'?
   1058         if (!(IsValid = Tok.is(tok::r_paren)))
   1059           break;
   1060 
   1061         // Concatenate and parse the strings.
   1062         StringLiteralParser Literal(&StrToks[0], StrToks.size(), *this);
   1063         assert(Literal.isAscii() && "Didn't allow wide strings in");
   1064         if (Literal.hadError)
   1065           break;
   1066         if (Literal.Pascal) {
   1067           Diag(Tok, diag::warn_pragma_diagnostic_invalid);
   1068           break;
   1069         }
   1070 
   1071         StringRef WarningName(Literal.GetString());
   1072 
   1073         if (WarningName.size() < 3 || WarningName[0] != '-' ||
   1074             WarningName[1] != 'W') {
   1075           Diag(StrToks[0].getLocation(), diag::warn_has_warning_invalid_option);
   1076           break;
   1077         }
   1078 
   1079         // Finally, check if the warning flags maps to a diagnostic group.
   1080         // We construct a SmallVector here to talk to getDiagnosticIDs().
   1081         // Although we don't use the result, this isn't a hot path, and not
   1082         // worth special casing.
   1083         llvm::SmallVector<diag::kind, 10> Diags;
   1084         Value = !getDiagnostics().getDiagnosticIDs()->
   1085           getDiagnosticsInGroup(WarningName.substr(2), Diags);
   1086       }
   1087     } while (false);
   1088 
   1089     if (!IsValid)
   1090       Diag(StartLoc, diag::err_warning_check_malformed);
   1091 
   1092     OS << (int)Value;
   1093     Tok.setKind(tok::numeric_constant);
   1094   } else {
   1095     llvm_unreachable("Unknown identifier!");
   1096   }
   1097   CreateString(OS.str().data(), OS.str().size(), Tok,
   1098                Tok.getLocation(), Tok.getLocation());
   1099 }
   1100 
   1101 void Preprocessor::markMacroAsUsed(MacroInfo *MI) {
   1102   // If the 'used' status changed, and the macro requires 'unused' warning,
   1103   // remove its SourceLocation from the warn-for-unused-macro locations.
   1104   if (MI->isWarnIfUnused() && !MI->isUsed())
   1105     WarnUnusedMacroLocs.erase(MI->getDefinitionLoc());
   1106   MI->setIsUsed(true);
   1107 }
   1108