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