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