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