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