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