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