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

Provided by KDAB

Privacy Policy
Learn to use CMake with our Intro Training
Find out more

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