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