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