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

source code of clang/lib/Lex/PPMacroExpansion.cpp