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