[docs] Add LICENSE.txt to the root of the mono-repo
[llvm-project.git] / clang / lib / Lex / PPMacroExpansion.cpp
blob78ab8bae2f61077c11665ec639f86719d814f504
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.
12 //===----------------------------------------------------------------------===//
14 #include "clang/Basic/AttributeCommonInfo.h"
15 #include "clang/Basic/Attributes.h"
16 #include "clang/Basic/Builtins.h"
17 #include "clang/Basic/FileManager.h"
18 #include "clang/Basic/IdentifierTable.h"
19 #include "clang/Basic/LLVM.h"
20 #include "clang/Basic/LangOptions.h"
21 #include "clang/Basic/ObjCRuntime.h"
22 #include "clang/Basic/SourceLocation.h"
23 #include "clang/Basic/TargetInfo.h"
24 #include "clang/Lex/CodeCompletionHandler.h"
25 #include "clang/Lex/DirectoryLookup.h"
26 #include "clang/Lex/ExternalPreprocessorSource.h"
27 #include "clang/Lex/HeaderSearch.h"
28 #include "clang/Lex/LexDiagnostic.h"
29 #include "clang/Lex/LiteralSupport.h"
30 #include "clang/Lex/MacroArgs.h"
31 #include "clang/Lex/MacroInfo.h"
32 #include "clang/Lex/Preprocessor.h"
33 #include "clang/Lex/PreprocessorLexer.h"
34 #include "clang/Lex/PreprocessorOptions.h"
35 #include "clang/Lex/Token.h"
36 #include "llvm/ADT/ArrayRef.h"
37 #include "llvm/ADT/DenseMap.h"
38 #include "llvm/ADT/DenseSet.h"
39 #include "llvm/ADT/FoldingSet.h"
40 #include "llvm/ADT/None.h"
41 #include "llvm/ADT/Optional.h"
42 #include "llvm/ADT/STLExtras.h"
43 #include "llvm/ADT/SmallString.h"
44 #include "llvm/ADT/SmallVector.h"
45 #include "llvm/ADT/StringRef.h"
46 #include "llvm/ADT/StringSwitch.h"
47 #include "llvm/Support/Casting.h"
48 #include "llvm/Support/ErrorHandling.h"
49 #include "llvm/Support/Format.h"
50 #include "llvm/Support/Path.h"
51 #include "llvm/Support/raw_ostream.h"
52 #include <algorithm>
53 #include <cassert>
54 #include <cstddef>
55 #include <cstring>
56 #include <ctime>
57 #include <string>
58 #include <tuple>
59 #include <utility>
61 using namespace clang;
63 MacroDirective *
64 Preprocessor::getLocalMacroDirectiveHistory(const IdentifierInfo *II) const {
65 if (!II->hadMacroDefinition())
66 return nullptr;
67 auto Pos = CurSubmoduleState->Macros.find(II);
68 return Pos == CurSubmoduleState->Macros.end() ? nullptr
69 : Pos->second.getLatest();
72 void Preprocessor::appendMacroDirective(IdentifierInfo *II, MacroDirective *MD){
73 assert(MD && "MacroDirective should be non-zero!");
74 assert(!MD->getPrevious() && "Already attached to a MacroDirective history.");
76 MacroState &StoredMD = CurSubmoduleState->Macros[II];
77 auto *OldMD = StoredMD.getLatest();
78 MD->setPrevious(OldMD);
79 StoredMD.setLatest(MD);
80 StoredMD.overrideActiveModuleMacros(*this, II);
82 if (needModuleMacros()) {
83 // Track that we created a new macro directive, so we know we should
84 // consider building a ModuleMacro for it when we get to the end of
85 // the module.
86 PendingModuleMacroNames.push_back(II);
89 // Set up the identifier as having associated macro history.
90 II->setHasMacroDefinition(true);
91 if (!MD->isDefined() && LeafModuleMacros.find(II) == LeafModuleMacros.end())
92 II->setHasMacroDefinition(false);
93 if (II->isFromAST())
94 II->setChangedSinceDeserialization();
97 void Preprocessor::setLoadedMacroDirective(IdentifierInfo *II,
98 MacroDirective *ED,
99 MacroDirective *MD) {
100 // Normally, when a macro is defined, it goes through appendMacroDirective()
101 // above, which chains a macro to previous defines, undefs, etc.
102 // However, in a pch, the whole macro history up to the end of the pch is
103 // stored, so ASTReader goes through this function instead.
104 // However, built-in macros are already registered in the Preprocessor
105 // ctor, and ASTWriter stops writing the macro chain at built-in macros,
106 // so in that case the chain from the pch needs to be spliced to the existing
107 // built-in.
109 assert(II && MD);
110 MacroState &StoredMD = CurSubmoduleState->Macros[II];
112 if (auto *OldMD = StoredMD.getLatest()) {
113 // shouldIgnoreMacro() in ASTWriter also stops at macros from the
114 // predefines buffer in module builds. However, in module builds, modules
115 // are loaded completely before predefines are processed, so StoredMD
116 // will be nullptr for them when they're loaded. StoredMD should only be
117 // non-nullptr for builtins read from a pch file.
118 assert(OldMD->getMacroInfo()->isBuiltinMacro() &&
119 "only built-ins should have an entry here");
120 assert(!OldMD->getPrevious() && "builtin should only have a single entry");
121 ED->setPrevious(OldMD);
122 StoredMD.setLatest(MD);
123 } else {
124 StoredMD = MD;
127 // Setup the identifier as having associated macro history.
128 II->setHasMacroDefinition(true);
129 if (!MD->isDefined() && LeafModuleMacros.find(II) == LeafModuleMacros.end())
130 II->setHasMacroDefinition(false);
133 ModuleMacro *Preprocessor::addModuleMacro(Module *Mod, IdentifierInfo *II,
134 MacroInfo *Macro,
135 ArrayRef<ModuleMacro *> Overrides,
136 bool &New) {
137 llvm::FoldingSetNodeID ID;
138 ModuleMacro::Profile(ID, Mod, II);
140 void *InsertPos;
141 if (auto *MM = ModuleMacros.FindNodeOrInsertPos(ID, InsertPos)) {
142 New = false;
143 return MM;
146 auto *MM = ModuleMacro::create(*this, Mod, II, Macro, Overrides);
147 ModuleMacros.InsertNode(MM, InsertPos);
149 // Each overridden macro is now overridden by one more macro.
150 bool HidAny = false;
151 for (auto *O : Overrides) {
152 HidAny |= (O->NumOverriddenBy == 0);
153 ++O->NumOverriddenBy;
156 // If we were the first overrider for any macro, it's no longer a leaf.
157 auto &LeafMacros = LeafModuleMacros[II];
158 if (HidAny) {
159 llvm::erase_if(LeafMacros,
160 [](ModuleMacro *MM) { return MM->NumOverriddenBy != 0; });
163 // The new macro is always a leaf macro.
164 LeafMacros.push_back(MM);
165 // The identifier now has defined macros (that may or may not be visible).
166 II->setHasMacroDefinition(true);
168 New = true;
169 return MM;
172 ModuleMacro *Preprocessor::getModuleMacro(Module *Mod,
173 const IdentifierInfo *II) {
174 llvm::FoldingSetNodeID ID;
175 ModuleMacro::Profile(ID, Mod, II);
177 void *InsertPos;
178 return ModuleMacros.FindNodeOrInsertPos(ID, InsertPos);
181 void Preprocessor::updateModuleMacroInfo(const IdentifierInfo *II,
182 ModuleMacroInfo &Info) {
183 assert(Info.ActiveModuleMacrosGeneration !=
184 CurSubmoduleState->VisibleModules.getGeneration() &&
185 "don't need to update this macro name info");
186 Info.ActiveModuleMacrosGeneration =
187 CurSubmoduleState->VisibleModules.getGeneration();
189 auto Leaf = LeafModuleMacros.find(II);
190 if (Leaf == LeafModuleMacros.end()) {
191 // No imported macros at all: nothing to do.
192 return;
195 Info.ActiveModuleMacros.clear();
197 // Every macro that's locally overridden is overridden by a visible macro.
198 llvm::DenseMap<ModuleMacro *, int> NumHiddenOverrides;
199 for (auto *O : Info.OverriddenMacros)
200 NumHiddenOverrides[O] = -1;
202 // Collect all macros that are not overridden by a visible macro.
203 llvm::SmallVector<ModuleMacro *, 16> Worklist;
204 for (auto *LeafMM : Leaf->second) {
205 assert(LeafMM->getNumOverridingMacros() == 0 && "leaf macro overridden");
206 if (NumHiddenOverrides.lookup(LeafMM) == 0)
207 Worklist.push_back(LeafMM);
209 while (!Worklist.empty()) {
210 auto *MM = Worklist.pop_back_val();
211 if (CurSubmoduleState->VisibleModules.isVisible(MM->getOwningModule())) {
212 // We only care about collecting definitions; undefinitions only act
213 // to override other definitions.
214 if (MM->getMacroInfo())
215 Info.ActiveModuleMacros.push_back(MM);
216 } else {
217 for (auto *O : MM->overrides())
218 if ((unsigned)++NumHiddenOverrides[O] == O->getNumOverridingMacros())
219 Worklist.push_back(O);
222 // Our reverse postorder walk found the macros in reverse order.
223 std::reverse(Info.ActiveModuleMacros.begin(), Info.ActiveModuleMacros.end());
225 // Determine whether the macro name is ambiguous.
226 MacroInfo *MI = nullptr;
227 bool IsSystemMacro = true;
228 bool IsAmbiguous = false;
229 if (auto *MD = Info.MD) {
230 while (MD && isa<VisibilityMacroDirective>(MD))
231 MD = MD->getPrevious();
232 if (auto *DMD = dyn_cast_or_null<DefMacroDirective>(MD)) {
233 MI = DMD->getInfo();
234 IsSystemMacro &= SourceMgr.isInSystemHeader(DMD->getLocation());
237 for (auto *Active : Info.ActiveModuleMacros) {
238 auto *NewMI = Active->getMacroInfo();
240 // Before marking the macro as ambiguous, check if this is a case where
241 // both macros are in system headers. If so, we trust that the system
242 // did not get it wrong. This also handles cases where Clang's own
243 // headers have a different spelling of certain system macros:
244 // #define LONG_MAX __LONG_MAX__ (clang's limits.h)
245 // #define LONG_MAX 0x7fffffffffffffffL (system's limits.h)
247 // FIXME: Remove the defined-in-system-headers check. clang's limits.h
248 // overrides the system limits.h's macros, so there's no conflict here.
249 if (MI && NewMI != MI &&
250 !MI->isIdenticalTo(*NewMI, *this, /*Syntactically=*/true))
251 IsAmbiguous = true;
252 IsSystemMacro &= Active->getOwningModule()->IsSystem ||
253 SourceMgr.isInSystemHeader(NewMI->getDefinitionLoc());
254 MI = NewMI;
256 Info.IsAmbiguous = IsAmbiguous && !IsSystemMacro;
259 void Preprocessor::dumpMacroInfo(const IdentifierInfo *II) {
260 ArrayRef<ModuleMacro*> Leaf;
261 auto LeafIt = LeafModuleMacros.find(II);
262 if (LeafIt != LeafModuleMacros.end())
263 Leaf = LeafIt->second;
264 const MacroState *State = nullptr;
265 auto Pos = CurSubmoduleState->Macros.find(II);
266 if (Pos != CurSubmoduleState->Macros.end())
267 State = &Pos->second;
269 llvm::errs() << "MacroState " << State << " " << II->getNameStart();
270 if (State && State->isAmbiguous(*this, II))
271 llvm::errs() << " ambiguous";
272 if (State && !State->getOverriddenMacros().empty()) {
273 llvm::errs() << " overrides";
274 for (auto *O : State->getOverriddenMacros())
275 llvm::errs() << " " << O->getOwningModule()->getFullModuleName();
277 llvm::errs() << "\n";
279 // Dump local macro directives.
280 for (auto *MD = State ? State->getLatest() : nullptr; MD;
281 MD = MD->getPrevious()) {
282 llvm::errs() << " ";
283 MD->dump();
286 // Dump module macros.
287 llvm::DenseSet<ModuleMacro*> Active;
288 for (auto *MM : State ? State->getActiveModuleMacros(*this, II) : None)
289 Active.insert(MM);
290 llvm::DenseSet<ModuleMacro*> Visited;
291 llvm::SmallVector<ModuleMacro *, 16> Worklist(Leaf.begin(), Leaf.end());
292 while (!Worklist.empty()) {
293 auto *MM = Worklist.pop_back_val();
294 llvm::errs() << " ModuleMacro " << MM << " "
295 << MM->getOwningModule()->getFullModuleName();
296 if (!MM->getMacroInfo())
297 llvm::errs() << " undef";
299 if (Active.count(MM))
300 llvm::errs() << " active";
301 else if (!CurSubmoduleState->VisibleModules.isVisible(
302 MM->getOwningModule()))
303 llvm::errs() << " hidden";
304 else if (MM->getMacroInfo())
305 llvm::errs() << " overridden";
307 if (!MM->overrides().empty()) {
308 llvm::errs() << " overrides";
309 for (auto *O : MM->overrides()) {
310 llvm::errs() << " " << O->getOwningModule()->getFullModuleName();
311 if (Visited.insert(O).second)
312 Worklist.push_back(O);
315 llvm::errs() << "\n";
316 if (auto *MI = MM->getMacroInfo()) {
317 llvm::errs() << " ";
318 MI->dump();
319 llvm::errs() << "\n";
324 /// RegisterBuiltinMacro - Register the specified identifier in the identifier
325 /// table and mark it as a builtin macro to be expanded.
326 static IdentifierInfo *RegisterBuiltinMacro(Preprocessor &PP, const char *Name){
327 // Get the identifier.
328 IdentifierInfo *Id = PP.getIdentifierInfo(Name);
330 // Mark it as being a macro that is builtin.
331 MacroInfo *MI = PP.AllocateMacroInfo(SourceLocation());
332 MI->setIsBuiltinMacro();
333 PP.appendDefMacroDirective(Id, MI);
334 return Id;
337 /// RegisterBuiltinMacros - Register builtin macros, such as __LINE__ with the
338 /// identifier table.
339 void Preprocessor::RegisterBuiltinMacros() {
340 Ident__LINE__ = RegisterBuiltinMacro(*this, "__LINE__");
341 Ident__FILE__ = RegisterBuiltinMacro(*this, "__FILE__");
342 Ident__DATE__ = RegisterBuiltinMacro(*this, "__DATE__");
343 Ident__TIME__ = RegisterBuiltinMacro(*this, "__TIME__");
344 Ident__COUNTER__ = RegisterBuiltinMacro(*this, "__COUNTER__");
345 Ident_Pragma = RegisterBuiltinMacro(*this, "_Pragma");
346 Ident__FLT_EVAL_METHOD__ = RegisterBuiltinMacro(*this, "__FLT_EVAL_METHOD__");
348 // C++ Standing Document Extensions.
349 if (getLangOpts().CPlusPlus)
350 Ident__has_cpp_attribute =
351 RegisterBuiltinMacro(*this, "__has_cpp_attribute");
352 else
353 Ident__has_cpp_attribute = nullptr;
355 // GCC Extensions.
356 Ident__BASE_FILE__ = RegisterBuiltinMacro(*this, "__BASE_FILE__");
357 Ident__INCLUDE_LEVEL__ = RegisterBuiltinMacro(*this, "__INCLUDE_LEVEL__");
358 Ident__TIMESTAMP__ = RegisterBuiltinMacro(*this, "__TIMESTAMP__");
360 // Microsoft Extensions.
361 if (getLangOpts().MicrosoftExt) {
362 Ident__identifier = RegisterBuiltinMacro(*this, "__identifier");
363 Ident__pragma = RegisterBuiltinMacro(*this, "__pragma");
364 } else {
365 Ident__identifier = nullptr;
366 Ident__pragma = nullptr;
369 // Clang Extensions.
370 Ident__FILE_NAME__ = RegisterBuiltinMacro(*this, "__FILE_NAME__");
371 Ident__has_feature = RegisterBuiltinMacro(*this, "__has_feature");
372 Ident__has_extension = RegisterBuiltinMacro(*this, "__has_extension");
373 Ident__has_builtin = RegisterBuiltinMacro(*this, "__has_builtin");
374 Ident__has_attribute = RegisterBuiltinMacro(*this, "__has_attribute");
375 if (!getLangOpts().CPlusPlus)
376 Ident__has_c_attribute = RegisterBuiltinMacro(*this, "__has_c_attribute");
377 else
378 Ident__has_c_attribute = nullptr;
380 Ident__has_declspec = RegisterBuiltinMacro(*this, "__has_declspec_attribute");
381 Ident__has_include = RegisterBuiltinMacro(*this, "__has_include");
382 Ident__has_include_next = RegisterBuiltinMacro(*this, "__has_include_next");
383 Ident__has_warning = RegisterBuiltinMacro(*this, "__has_warning");
384 Ident__is_identifier = RegisterBuiltinMacro(*this, "__is_identifier");
385 Ident__is_target_arch = RegisterBuiltinMacro(*this, "__is_target_arch");
386 Ident__is_target_vendor = RegisterBuiltinMacro(*this, "__is_target_vendor");
387 Ident__is_target_os = RegisterBuiltinMacro(*this, "__is_target_os");
388 Ident__is_target_environment =
389 RegisterBuiltinMacro(*this, "__is_target_environment");
391 // Modules.
392 Ident__building_module = RegisterBuiltinMacro(*this, "__building_module");
393 if (!getLangOpts().CurrentModule.empty())
394 Ident__MODULE__ = RegisterBuiltinMacro(*this, "__MODULE__");
395 else
396 Ident__MODULE__ = nullptr;
399 /// isTrivialSingleTokenExpansion - Return true if MI, which has a single token
400 /// in its expansion, currently expands to that token literally.
401 static bool isTrivialSingleTokenExpansion(const MacroInfo *MI,
402 const IdentifierInfo *MacroIdent,
403 Preprocessor &PP) {
404 IdentifierInfo *II = MI->getReplacementToken(0).getIdentifierInfo();
406 // If the token isn't an identifier, it's always literally expanded.
407 if (!II) return true;
409 // If the information about this identifier is out of date, update it from
410 // the external source.
411 if (II->isOutOfDate())
412 PP.getExternalSource()->updateOutOfDateIdentifier(*II);
414 // If the identifier is a macro, and if that macro is enabled, it may be
415 // expanded so it's not a trivial expansion.
416 if (auto *ExpansionMI = PP.getMacroInfo(II))
417 if (ExpansionMI->isEnabled() &&
418 // Fast expanding "#define X X" is ok, because X would be disabled.
419 II != MacroIdent)
420 return false;
422 // If this is an object-like macro invocation, it is safe to trivially expand
423 // it.
424 if (MI->isObjectLike()) return true;
426 // If this is a function-like macro invocation, it's safe to trivially expand
427 // as long as the identifier is not a macro argument.
428 return !llvm::is_contained(MI->params(), II);
431 /// isNextPPTokenLParen - Determine whether the next preprocessor token to be
432 /// lexed is a '('. If so, consume the token and return true, if not, this
433 /// method should have no observable side-effect on the lexed tokens.
434 bool Preprocessor::isNextPPTokenLParen() {
435 // Do some quick tests for rejection cases.
436 unsigned Val;
437 if (CurLexer)
438 Val = CurLexer->isNextPPTokenLParen();
439 else
440 Val = CurTokenLexer->isNextTokenLParen();
442 if (Val == 2) {
443 // We have run off the end. If it's a source file we don't
444 // examine enclosing ones (C99 5.1.1.2p4). Otherwise walk up the
445 // macro stack.
446 if (CurPPLexer)
447 return false;
448 for (const IncludeStackInfo &Entry : llvm::reverse(IncludeMacroStack)) {
449 if (Entry.TheLexer)
450 Val = Entry.TheLexer->isNextPPTokenLParen();
451 else
452 Val = Entry.TheTokenLexer->isNextTokenLParen();
454 if (Val != 2)
455 break;
457 // Ran off the end of a source file?
458 if (Entry.ThePPLexer)
459 return false;
463 // Okay, if we know that the token is a '(', lex it and return. Otherwise we
464 // have found something that isn't a '(' or we found the end of the
465 // translation unit. In either case, return false.
466 return Val == 1;
469 /// HandleMacroExpandedIdentifier - If an identifier token is read that is to be
470 /// expanded as a macro, handle it and return the next token as 'Identifier'.
471 bool Preprocessor::HandleMacroExpandedIdentifier(Token &Identifier,
472 const MacroDefinition &M) {
473 emitMacroExpansionWarnings(Identifier);
475 MacroInfo *MI = M.getMacroInfo();
477 // If this is a macro expansion in the "#if !defined(x)" line for the file,
478 // then the macro could expand to different things in other contexts, we need
479 // to disable the optimization in this case.
480 if (CurPPLexer) CurPPLexer->MIOpt.ExpandedMacro();
482 // If this is a builtin macro, like __LINE__ or _Pragma, handle it specially.
483 if (MI->isBuiltinMacro()) {
484 if (Callbacks)
485 Callbacks->MacroExpands(Identifier, M, Identifier.getLocation(),
486 /*Args=*/nullptr);
487 ExpandBuiltinMacro(Identifier);
488 return true;
491 /// Args - If this is a function-like macro expansion, this contains,
492 /// for each macro argument, the list of tokens that were provided to the
493 /// invocation.
494 MacroArgs *Args = nullptr;
496 // Remember where the end of the expansion occurred. For an object-like
497 // macro, this is the identifier. For a function-like macro, this is the ')'.
498 SourceLocation ExpansionEnd = Identifier.getLocation();
500 // If this is a function-like macro, read the arguments.
501 if (MI->isFunctionLike()) {
502 // Remember that we are now parsing the arguments to a macro invocation.
503 // Preprocessor directives used inside macro arguments are not portable, and
504 // this enables the warning.
505 InMacroArgs = true;
506 ArgMacro = &Identifier;
508 Args = ReadMacroCallArgumentList(Identifier, MI, ExpansionEnd);
510 // Finished parsing args.
511 InMacroArgs = false;
512 ArgMacro = nullptr;
514 // If there was an error parsing the arguments, bail out.
515 if (!Args) return true;
517 ++NumFnMacroExpanded;
518 } else {
519 ++NumMacroExpanded;
522 // Notice that this macro has been used.
523 markMacroAsUsed(MI);
525 // Remember where the token is expanded.
526 SourceLocation ExpandLoc = Identifier.getLocation();
527 SourceRange ExpansionRange(ExpandLoc, ExpansionEnd);
529 if (Callbacks) {
530 if (InMacroArgs) {
531 // We can have macro expansion inside a conditional directive while
532 // reading the function macro arguments. To ensure, in that case, that
533 // MacroExpands callbacks still happen in source order, queue this
534 // callback to have it happen after the function macro callback.
535 DelayedMacroExpandsCallbacks.push_back(
536 MacroExpandsInfo(Identifier, M, ExpansionRange));
537 } else {
538 Callbacks->MacroExpands(Identifier, M, ExpansionRange, Args);
539 if (!DelayedMacroExpandsCallbacks.empty()) {
540 for (const MacroExpandsInfo &Info : DelayedMacroExpandsCallbacks) {
541 // FIXME: We lose macro args info with delayed callback.
542 Callbacks->MacroExpands(Info.Tok, Info.MD, Info.Range,
543 /*Args=*/nullptr);
545 DelayedMacroExpandsCallbacks.clear();
550 // If the macro definition is ambiguous, complain.
551 if (M.isAmbiguous()) {
552 Diag(Identifier, diag::warn_pp_ambiguous_macro)
553 << Identifier.getIdentifierInfo();
554 Diag(MI->getDefinitionLoc(), diag::note_pp_ambiguous_macro_chosen)
555 << Identifier.getIdentifierInfo();
556 M.forAllDefinitions([&](const MacroInfo *OtherMI) {
557 if (OtherMI != MI)
558 Diag(OtherMI->getDefinitionLoc(), diag::note_pp_ambiguous_macro_other)
559 << Identifier.getIdentifierInfo();
563 // If we started lexing a macro, enter the macro expansion body.
565 // If this macro expands to no tokens, don't bother to push it onto the
566 // expansion stack, only to take it right back off.
567 if (MI->getNumTokens() == 0) {
568 // No need for arg info.
569 if (Args) Args->destroy(*this);
571 // Propagate whitespace info as if we had pushed, then popped,
572 // a macro context.
573 Identifier.setFlag(Token::LeadingEmptyMacro);
574 PropagateLineStartLeadingSpaceInfo(Identifier);
575 ++NumFastMacroExpanded;
576 return false;
577 } else if (MI->getNumTokens() == 1 &&
578 isTrivialSingleTokenExpansion(MI, Identifier.getIdentifierInfo(),
579 *this)) {
580 // Otherwise, if this macro expands into a single trivially-expanded
581 // token: expand it now. This handles common cases like
582 // "#define VAL 42".
584 // No need for arg info.
585 if (Args) Args->destroy(*this);
587 // Propagate the isAtStartOfLine/hasLeadingSpace markers of the macro
588 // identifier to the expanded token.
589 bool isAtStartOfLine = Identifier.isAtStartOfLine();
590 bool hasLeadingSpace = Identifier.hasLeadingSpace();
592 // Replace the result token.
593 Identifier = MI->getReplacementToken(0);
595 // Restore the StartOfLine/LeadingSpace markers.
596 Identifier.setFlagValue(Token::StartOfLine , isAtStartOfLine);
597 Identifier.setFlagValue(Token::LeadingSpace, hasLeadingSpace);
599 // Update the tokens location to include both its expansion and physical
600 // locations.
601 SourceLocation Loc =
602 SourceMgr.createExpansionLoc(Identifier.getLocation(), ExpandLoc,
603 ExpansionEnd,Identifier.getLength());
604 Identifier.setLocation(Loc);
606 // If this is a disabled macro or #define X X, we must mark the result as
607 // unexpandable.
608 if (IdentifierInfo *NewII = Identifier.getIdentifierInfo()) {
609 if (MacroInfo *NewMI = getMacroInfo(NewII))
610 if (!NewMI->isEnabled() || NewMI == MI) {
611 Identifier.setFlag(Token::DisableExpand);
612 // Don't warn for "#define X X" like "#define bool bool" from
613 // stdbool.h.
614 if (NewMI != MI || MI->isFunctionLike())
615 Diag(Identifier, diag::pp_disabled_macro_expansion);
619 // Since this is not an identifier token, it can't be macro expanded, so
620 // we're done.
621 ++NumFastMacroExpanded;
622 return true;
625 // Start expanding the macro.
626 EnterMacro(Identifier, ExpansionEnd, MI, Args);
627 return false;
630 enum Bracket {
631 Brace,
632 Paren
635 /// CheckMatchedBrackets - Returns true if the braces and parentheses in the
636 /// token vector are properly nested.
637 static bool CheckMatchedBrackets(const SmallVectorImpl<Token> &Tokens) {
638 SmallVector<Bracket, 8> Brackets;
639 for (SmallVectorImpl<Token>::const_iterator I = Tokens.begin(),
640 E = Tokens.end();
641 I != E; ++I) {
642 if (I->is(tok::l_paren)) {
643 Brackets.push_back(Paren);
644 } else if (I->is(tok::r_paren)) {
645 if (Brackets.empty() || Brackets.back() == Brace)
646 return false;
647 Brackets.pop_back();
648 } else if (I->is(tok::l_brace)) {
649 Brackets.push_back(Brace);
650 } else if (I->is(tok::r_brace)) {
651 if (Brackets.empty() || Brackets.back() == Paren)
652 return false;
653 Brackets.pop_back();
656 return Brackets.empty();
659 /// GenerateNewArgTokens - Returns true if OldTokens can be converted to a new
660 /// vector of tokens in NewTokens. The new number of arguments will be placed
661 /// in NumArgs and the ranges which need to surrounded in parentheses will be
662 /// in ParenHints.
663 /// Returns false if the token stream cannot be changed. If this is because
664 /// of an initializer list starting a macro argument, the range of those
665 /// initializer lists will be place in InitLists.
666 static bool GenerateNewArgTokens(Preprocessor &PP,
667 SmallVectorImpl<Token> &OldTokens,
668 SmallVectorImpl<Token> &NewTokens,
669 unsigned &NumArgs,
670 SmallVectorImpl<SourceRange> &ParenHints,
671 SmallVectorImpl<SourceRange> &InitLists) {
672 if (!CheckMatchedBrackets(OldTokens))
673 return false;
675 // Once it is known that the brackets are matched, only a simple count of the
676 // braces is needed.
677 unsigned Braces = 0;
679 // First token of a new macro argument.
680 SmallVectorImpl<Token>::iterator ArgStartIterator = OldTokens.begin();
682 // First closing brace in a new macro argument. Used to generate
683 // SourceRanges for InitLists.
684 SmallVectorImpl<Token>::iterator ClosingBrace = OldTokens.end();
685 NumArgs = 0;
686 Token TempToken;
687 // Set to true when a macro separator token is found inside a braced list.
688 // If true, the fixed argument spans multiple old arguments and ParenHints
689 // will be updated.
690 bool FoundSeparatorToken = false;
691 for (SmallVectorImpl<Token>::iterator I = OldTokens.begin(),
692 E = OldTokens.end();
693 I != E; ++I) {
694 if (I->is(tok::l_brace)) {
695 ++Braces;
696 } else if (I->is(tok::r_brace)) {
697 --Braces;
698 if (Braces == 0 && ClosingBrace == E && FoundSeparatorToken)
699 ClosingBrace = I;
700 } else if (I->is(tok::eof)) {
701 // EOF token is used to separate macro arguments
702 if (Braces != 0) {
703 // Assume comma separator is actually braced list separator and change
704 // it back to a comma.
705 FoundSeparatorToken = true;
706 I->setKind(tok::comma);
707 I->setLength(1);
708 } else { // Braces == 0
709 // Separator token still separates arguments.
710 ++NumArgs;
712 // If the argument starts with a brace, it can't be fixed with
713 // parentheses. A different diagnostic will be given.
714 if (FoundSeparatorToken && ArgStartIterator->is(tok::l_brace)) {
715 InitLists.push_back(
716 SourceRange(ArgStartIterator->getLocation(),
717 PP.getLocForEndOfToken(ClosingBrace->getLocation())));
718 ClosingBrace = E;
721 // Add left paren
722 if (FoundSeparatorToken) {
723 TempToken.startToken();
724 TempToken.setKind(tok::l_paren);
725 TempToken.setLocation(ArgStartIterator->getLocation());
726 TempToken.setLength(0);
727 NewTokens.push_back(TempToken);
730 // Copy over argument tokens
731 NewTokens.insert(NewTokens.end(), ArgStartIterator, I);
733 // Add right paren and store the paren locations in ParenHints
734 if (FoundSeparatorToken) {
735 SourceLocation Loc = PP.getLocForEndOfToken((I - 1)->getLocation());
736 TempToken.startToken();
737 TempToken.setKind(tok::r_paren);
738 TempToken.setLocation(Loc);
739 TempToken.setLength(0);
740 NewTokens.push_back(TempToken);
741 ParenHints.push_back(SourceRange(ArgStartIterator->getLocation(),
742 Loc));
745 // Copy separator token
746 NewTokens.push_back(*I);
748 // Reset values
749 ArgStartIterator = I + 1;
750 FoundSeparatorToken = false;
755 return !ParenHints.empty() && InitLists.empty();
758 /// ReadFunctionLikeMacroArgs - After reading "MACRO" and knowing that the next
759 /// token is the '(' of the macro, this method is invoked to read all of the
760 /// actual arguments specified for the macro invocation. This returns null on
761 /// error.
762 MacroArgs *Preprocessor::ReadMacroCallArgumentList(Token &MacroName,
763 MacroInfo *MI,
764 SourceLocation &MacroEnd) {
765 // The number of fixed arguments to parse.
766 unsigned NumFixedArgsLeft = MI->getNumParams();
767 bool isVariadic = MI->isVariadic();
769 // Outer loop, while there are more arguments, keep reading them.
770 Token Tok;
772 // Read arguments as unexpanded tokens. This avoids issues, e.g., where
773 // an argument value in a macro could expand to ',' or '(' or ')'.
774 LexUnexpandedToken(Tok);
775 assert(Tok.is(tok::l_paren) && "Error computing l-paren-ness?");
777 // ArgTokens - Build up a list of tokens that make up each argument. Each
778 // argument is separated by an EOF token. Use a SmallVector so we can avoid
779 // heap allocations in the common case.
780 SmallVector<Token, 64> ArgTokens;
781 bool ContainsCodeCompletionTok = false;
782 bool FoundElidedComma = false;
784 SourceLocation TooManyArgsLoc;
786 unsigned NumActuals = 0;
787 while (Tok.isNot(tok::r_paren)) {
788 if (ContainsCodeCompletionTok && Tok.isOneOf(tok::eof, tok::eod))
789 break;
791 assert(Tok.isOneOf(tok::l_paren, tok::comma) &&
792 "only expect argument separators here");
794 size_t ArgTokenStart = ArgTokens.size();
795 SourceLocation ArgStartLoc = Tok.getLocation();
797 // C99 6.10.3p11: Keep track of the number of l_parens we have seen. Note
798 // that we already consumed the first one.
799 unsigned NumParens = 0;
801 while (true) {
802 // Read arguments as unexpanded tokens. This avoids issues, e.g., where
803 // an argument value in a macro could expand to ',' or '(' or ')'.
804 LexUnexpandedToken(Tok);
806 if (Tok.isOneOf(tok::eof, tok::eod)) { // "#if f(<eof>" & "#if f(\n"
807 if (!ContainsCodeCompletionTok) {
808 Diag(MacroName, diag::err_unterm_macro_invoc);
809 Diag(MI->getDefinitionLoc(), diag::note_macro_here)
810 << MacroName.getIdentifierInfo();
811 // Do not lose the EOF/EOD. Return it to the client.
812 MacroName = Tok;
813 return nullptr;
815 // Do not lose the EOF/EOD.
816 auto Toks = std::make_unique<Token[]>(1);
817 Toks[0] = Tok;
818 EnterTokenStream(std::move(Toks), 1, true, /*IsReinject*/ false);
819 break;
820 } else if (Tok.is(tok::r_paren)) {
821 // If we found the ) token, the macro arg list is done.
822 if (NumParens-- == 0) {
823 MacroEnd = Tok.getLocation();
824 if (!ArgTokens.empty() &&
825 ArgTokens.back().commaAfterElided()) {
826 FoundElidedComma = true;
828 break;
830 } else if (Tok.is(tok::l_paren)) {
831 ++NumParens;
832 } else if (Tok.is(tok::comma)) {
833 // In Microsoft-compatibility mode, single commas from nested macro
834 // expansions should not be considered as argument separators. We test
835 // for this with the IgnoredComma token flag.
836 if (Tok.getFlags() & Token::IgnoredComma) {
837 // However, in MSVC's preprocessor, subsequent expansions do treat
838 // these commas as argument separators. This leads to a common
839 // workaround used in macros that need to work in both MSVC and
840 // compliant preprocessors. Therefore, the IgnoredComma flag can only
841 // apply once to any given token.
842 Tok.clearFlag(Token::IgnoredComma);
843 } else if (NumParens == 0) {
844 // Comma ends this argument if there are more fixed arguments
845 // expected. However, if this is a variadic macro, and this is part of
846 // the variadic part, then the comma is just an argument token.
847 if (!isVariadic)
848 break;
849 if (NumFixedArgsLeft > 1)
850 break;
852 } else if (Tok.is(tok::comment) && !KeepMacroComments) {
853 // If this is a comment token in the argument list and we're just in
854 // -C mode (not -CC mode), discard the comment.
855 continue;
856 } else if (!Tok.isAnnotation() && Tok.getIdentifierInfo() != nullptr) {
857 // Reading macro arguments can cause macros that we are currently
858 // expanding from to be popped off the expansion stack. Doing so causes
859 // them to be reenabled for expansion. Here we record whether any
860 // identifiers we lex as macro arguments correspond to disabled macros.
861 // If so, we mark the token as noexpand. This is a subtle aspect of
862 // C99 6.10.3.4p2.
863 if (MacroInfo *MI = getMacroInfo(Tok.getIdentifierInfo()))
864 if (!MI->isEnabled())
865 Tok.setFlag(Token::DisableExpand);
866 } else if (Tok.is(tok::code_completion)) {
867 ContainsCodeCompletionTok = true;
868 if (CodeComplete)
869 CodeComplete->CodeCompleteMacroArgument(MacroName.getIdentifierInfo(),
870 MI, NumActuals);
871 // Don't mark that we reached the code-completion point because the
872 // parser is going to handle the token and there will be another
873 // code-completion callback.
876 ArgTokens.push_back(Tok);
879 // If this was an empty argument list foo(), don't add this as an empty
880 // argument.
881 if (ArgTokens.empty() && Tok.getKind() == tok::r_paren)
882 break;
884 // If this is not a variadic macro, and too many args were specified, emit
885 // an error.
886 if (!isVariadic && NumFixedArgsLeft == 0 && TooManyArgsLoc.isInvalid()) {
887 if (ArgTokens.size() != ArgTokenStart)
888 TooManyArgsLoc = ArgTokens[ArgTokenStart].getLocation();
889 else
890 TooManyArgsLoc = ArgStartLoc;
893 // Empty arguments are standard in C99 and C++0x, and are supported as an
894 // extension in other modes.
895 if (ArgTokens.size() == ArgTokenStart && !getLangOpts().C99)
896 Diag(Tok, getLangOpts().CPlusPlus11
897 ? diag::warn_cxx98_compat_empty_fnmacro_arg
898 : diag::ext_empty_fnmacro_arg);
900 // Add a marker EOF token to the end of the token list for this argument.
901 Token EOFTok;
902 EOFTok.startToken();
903 EOFTok.setKind(tok::eof);
904 EOFTok.setLocation(Tok.getLocation());
905 EOFTok.setLength(0);
906 ArgTokens.push_back(EOFTok);
907 ++NumActuals;
908 if (!ContainsCodeCompletionTok && NumFixedArgsLeft != 0)
909 --NumFixedArgsLeft;
912 // Okay, we either found the r_paren. Check to see if we parsed too few
913 // arguments.
914 unsigned MinArgsExpected = MI->getNumParams();
916 // If this is not a variadic macro, and too many args were specified, emit
917 // an error.
918 if (!isVariadic && NumActuals > MinArgsExpected &&
919 !ContainsCodeCompletionTok) {
920 // Emit the diagnostic at the macro name in case there is a missing ).
921 // Emitting it at the , could be far away from the macro name.
922 Diag(TooManyArgsLoc, diag::err_too_many_args_in_macro_invoc);
923 Diag(MI->getDefinitionLoc(), diag::note_macro_here)
924 << MacroName.getIdentifierInfo();
926 // Commas from braced initializer lists will be treated as argument
927 // separators inside macros. Attempt to correct for this with parentheses.
928 // TODO: See if this can be generalized to angle brackets for templates
929 // inside macro arguments.
931 SmallVector<Token, 4> FixedArgTokens;
932 unsigned FixedNumArgs = 0;
933 SmallVector<SourceRange, 4> ParenHints, InitLists;
934 if (!GenerateNewArgTokens(*this, ArgTokens, FixedArgTokens, FixedNumArgs,
935 ParenHints, InitLists)) {
936 if (!InitLists.empty()) {
937 DiagnosticBuilder DB =
938 Diag(MacroName,
939 diag::note_init_list_at_beginning_of_macro_argument);
940 for (SourceRange Range : InitLists)
941 DB << Range;
943 return nullptr;
945 if (FixedNumArgs != MinArgsExpected)
946 return nullptr;
948 DiagnosticBuilder DB = Diag(MacroName, diag::note_suggest_parens_for_macro);
949 for (SourceRange ParenLocation : ParenHints) {
950 DB << FixItHint::CreateInsertion(ParenLocation.getBegin(), "(");
951 DB << FixItHint::CreateInsertion(ParenLocation.getEnd(), ")");
953 ArgTokens.swap(FixedArgTokens);
954 NumActuals = FixedNumArgs;
957 // See MacroArgs instance var for description of this.
958 bool isVarargsElided = false;
960 if (ContainsCodeCompletionTok) {
961 // Recover from not-fully-formed macro invocation during code-completion.
962 Token EOFTok;
963 EOFTok.startToken();
964 EOFTok.setKind(tok::eof);
965 EOFTok.setLocation(Tok.getLocation());
966 EOFTok.setLength(0);
967 for (; NumActuals < MinArgsExpected; ++NumActuals)
968 ArgTokens.push_back(EOFTok);
971 if (NumActuals < MinArgsExpected) {
972 // There are several cases where too few arguments is ok, handle them now.
973 if (NumActuals == 0 && MinArgsExpected == 1) {
974 // #define A(X) or #define A(...) ---> A()
976 // If there is exactly one argument, and that argument is missing,
977 // then we have an empty "()" argument empty list. This is fine, even if
978 // the macro expects one argument (the argument is just empty).
979 isVarargsElided = MI->isVariadic();
980 } else if ((FoundElidedComma || MI->isVariadic()) &&
981 (NumActuals+1 == MinArgsExpected || // A(x, ...) -> A(X)
982 (NumActuals == 0 && MinArgsExpected == 2))) {// A(x,...) -> A()
983 // Varargs where the named vararg parameter is missing: OK as extension.
984 // #define A(x, ...)
985 // A("blah")
987 // If the macro contains the comma pasting extension, the diagnostic
988 // is suppressed; we know we'll get another diagnostic later.
989 if (!MI->hasCommaPasting()) {
990 // C++20 allows this construct, but standards before C++20 and all C
991 // standards do not allow the construct (we allow it as an extension).
992 Diag(Tok, getLangOpts().CPlusPlus20
993 ? diag::warn_cxx17_compat_missing_varargs_arg
994 : diag::ext_missing_varargs_arg);
995 Diag(MI->getDefinitionLoc(), diag::note_macro_here)
996 << MacroName.getIdentifierInfo();
999 // Remember this occurred, allowing us to elide the comma when used for
1000 // cases like:
1001 // #define A(x, foo...) blah(a, ## foo)
1002 // #define B(x, ...) blah(a, ## __VA_ARGS__)
1003 // #define C(...) blah(a, ## __VA_ARGS__)
1004 // A(x) B(x) C()
1005 isVarargsElided = true;
1006 } else if (!ContainsCodeCompletionTok) {
1007 // Otherwise, emit the error.
1008 Diag(Tok, diag::err_too_few_args_in_macro_invoc);
1009 Diag(MI->getDefinitionLoc(), diag::note_macro_here)
1010 << MacroName.getIdentifierInfo();
1011 return nullptr;
1014 // Add a marker EOF token to the end of the token list for this argument.
1015 SourceLocation EndLoc = Tok.getLocation();
1016 Tok.startToken();
1017 Tok.setKind(tok::eof);
1018 Tok.setLocation(EndLoc);
1019 Tok.setLength(0);
1020 ArgTokens.push_back(Tok);
1022 // If we expect two arguments, add both as empty.
1023 if (NumActuals == 0 && MinArgsExpected == 2)
1024 ArgTokens.push_back(Tok);
1026 } else if (NumActuals > MinArgsExpected && !MI->isVariadic() &&
1027 !ContainsCodeCompletionTok) {
1028 // Emit the diagnostic at the macro name in case there is a missing ).
1029 // Emitting it at the , could be far away from the macro name.
1030 Diag(MacroName, diag::err_too_many_args_in_macro_invoc);
1031 Diag(MI->getDefinitionLoc(), diag::note_macro_here)
1032 << MacroName.getIdentifierInfo();
1033 return nullptr;
1036 return MacroArgs::create(MI, ArgTokens, isVarargsElided, *this);
1039 /// Keeps macro expanded tokens for TokenLexers.
1041 /// Works like a stack; a TokenLexer adds the macro expanded tokens that is
1042 /// going to lex in the cache and when it finishes the tokens are removed
1043 /// from the end of the cache.
1044 Token *Preprocessor::cacheMacroExpandedTokens(TokenLexer *tokLexer,
1045 ArrayRef<Token> tokens) {
1046 assert(tokLexer);
1047 if (tokens.empty())
1048 return nullptr;
1050 size_t newIndex = MacroExpandedTokens.size();
1051 bool cacheNeedsToGrow = tokens.size() >
1052 MacroExpandedTokens.capacity()-MacroExpandedTokens.size();
1053 MacroExpandedTokens.append(tokens.begin(), tokens.end());
1055 if (cacheNeedsToGrow) {
1056 // Go through all the TokenLexers whose 'Tokens' pointer points in the
1057 // buffer and update the pointers to the (potential) new buffer array.
1058 for (const auto &Lexer : MacroExpandingLexersStack) {
1059 TokenLexer *prevLexer;
1060 size_t tokIndex;
1061 std::tie(prevLexer, tokIndex) = Lexer;
1062 prevLexer->Tokens = MacroExpandedTokens.data() + tokIndex;
1066 MacroExpandingLexersStack.push_back(std::make_pair(tokLexer, newIndex));
1067 return MacroExpandedTokens.data() + newIndex;
1070 void Preprocessor::removeCachedMacroExpandedTokensOfLastLexer() {
1071 assert(!MacroExpandingLexersStack.empty());
1072 size_t tokIndex = MacroExpandingLexersStack.back().second;
1073 assert(tokIndex < MacroExpandedTokens.size());
1074 // Pop the cached macro expanded tokens from the end.
1075 MacroExpandedTokens.resize(tokIndex);
1076 MacroExpandingLexersStack.pop_back();
1079 /// ComputeDATE_TIME - Compute the current time, enter it into the specified
1080 /// scratch buffer, then return DATELoc/TIMELoc locations with the position of
1081 /// the identifier tokens inserted.
1082 static void ComputeDATE_TIME(SourceLocation &DATELoc, SourceLocation &TIMELoc,
1083 Preprocessor &PP) {
1084 time_t TT = time(nullptr);
1085 struct tm *TM = localtime(&TT);
1087 static const char * const Months[] = {
1088 "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"
1092 SmallString<32> TmpBuffer;
1093 llvm::raw_svector_ostream TmpStream(TmpBuffer);
1094 TmpStream << llvm::format("\"%s %2d %4d\"", Months[TM->tm_mon],
1095 TM->tm_mday, TM->tm_year + 1900);
1096 Token TmpTok;
1097 TmpTok.startToken();
1098 PP.CreateString(TmpStream.str(), TmpTok);
1099 DATELoc = TmpTok.getLocation();
1103 SmallString<32> TmpBuffer;
1104 llvm::raw_svector_ostream TmpStream(TmpBuffer);
1105 TmpStream << llvm::format("\"%02d:%02d:%02d\"",
1106 TM->tm_hour, TM->tm_min, TM->tm_sec);
1107 Token TmpTok;
1108 TmpTok.startToken();
1109 PP.CreateString(TmpStream.str(), TmpTok);
1110 TIMELoc = TmpTok.getLocation();
1114 /// HasFeature - Return true if we recognize and implement the feature
1115 /// specified by the identifier as a standard language feature.
1116 static bool HasFeature(const Preprocessor &PP, StringRef Feature) {
1117 const LangOptions &LangOpts = PP.getLangOpts();
1119 // Normalize the feature name, __foo__ becomes foo.
1120 if (Feature.startswith("__") && Feature.endswith("__") && Feature.size() >= 4)
1121 Feature = Feature.substr(2, Feature.size() - 4);
1123 #define FEATURE(Name, Predicate) .Case(#Name, Predicate)
1124 return llvm::StringSwitch<bool>(Feature)
1125 #include "clang/Basic/Features.def"
1126 .Default(false);
1127 #undef FEATURE
1130 /// HasExtension - Return true if we recognize and implement the feature
1131 /// specified by the identifier, either as an extension or a standard language
1132 /// feature.
1133 static bool HasExtension(const Preprocessor &PP, StringRef Extension) {
1134 if (HasFeature(PP, Extension))
1135 return true;
1137 // If the use of an extension results in an error diagnostic, extensions are
1138 // effectively unavailable, so just return false here.
1139 if (PP.getDiagnostics().getExtensionHandlingBehavior() >=
1140 diag::Severity::Error)
1141 return false;
1143 const LangOptions &LangOpts = PP.getLangOpts();
1145 // Normalize the extension name, __foo__ becomes foo.
1146 if (Extension.startswith("__") && Extension.endswith("__") &&
1147 Extension.size() >= 4)
1148 Extension = Extension.substr(2, Extension.size() - 4);
1150 // Because we inherit the feature list from HasFeature, this string switch
1151 // must be less restrictive than HasFeature's.
1152 #define EXTENSION(Name, Predicate) .Case(#Name, Predicate)
1153 return llvm::StringSwitch<bool>(Extension)
1154 #include "clang/Basic/Features.def"
1155 .Default(false);
1156 #undef EXTENSION
1159 /// EvaluateHasIncludeCommon - Process a '__has_include("path")'
1160 /// or '__has_include_next("path")' expression.
1161 /// Returns true if successful.
1162 static bool EvaluateHasIncludeCommon(Token &Tok, IdentifierInfo *II,
1163 Preprocessor &PP,
1164 ConstSearchDirIterator LookupFrom,
1165 const FileEntry *LookupFromFile) {
1166 // Save the location of the current token. If a '(' is later found, use
1167 // that location. If not, use the end of this location instead.
1168 SourceLocation LParenLoc = Tok.getLocation();
1170 // These expressions are only allowed within a preprocessor directive.
1171 if (!PP.isParsingIfOrElifDirective()) {
1172 PP.Diag(LParenLoc, diag::err_pp_directive_required) << II;
1173 // Return a valid identifier token.
1174 assert(Tok.is(tok::identifier));
1175 Tok.setIdentifierInfo(II);
1176 return false;
1179 // Get '('. If we don't have a '(', try to form a header-name token.
1180 do {
1181 if (PP.LexHeaderName(Tok))
1182 return false;
1183 } while (Tok.getKind() == tok::comment);
1185 // Ensure we have a '('.
1186 if (Tok.isNot(tok::l_paren)) {
1187 // No '(', use end of last token.
1188 LParenLoc = PP.getLocForEndOfToken(LParenLoc);
1189 PP.Diag(LParenLoc, diag::err_pp_expected_after) << II << tok::l_paren;
1190 // If the next token looks like a filename or the start of one,
1191 // assume it is and process it as such.
1192 if (Tok.isNot(tok::header_name))
1193 return false;
1194 } else {
1195 // Save '(' location for possible missing ')' message.
1196 LParenLoc = Tok.getLocation();
1197 if (PP.LexHeaderName(Tok))
1198 return false;
1201 if (Tok.isNot(tok::header_name)) {
1202 PP.Diag(Tok.getLocation(), diag::err_pp_expects_filename);
1203 return false;
1206 // Reserve a buffer to get the spelling.
1207 SmallString<128> FilenameBuffer;
1208 bool Invalid = false;
1209 StringRef Filename = PP.getSpelling(Tok, FilenameBuffer, &Invalid);
1210 if (Invalid)
1211 return false;
1213 SourceLocation FilenameLoc = Tok.getLocation();
1215 // Get ')'.
1216 PP.LexNonComment(Tok);
1218 // Ensure we have a trailing ).
1219 if (Tok.isNot(tok::r_paren)) {
1220 PP.Diag(PP.getLocForEndOfToken(FilenameLoc), diag::err_pp_expected_after)
1221 << II << tok::r_paren;
1222 PP.Diag(LParenLoc, diag::note_matching) << tok::l_paren;
1223 return false;
1226 bool isAngled = PP.GetIncludeFilenameSpelling(Tok.getLocation(), Filename);
1227 // If GetIncludeFilenameSpelling set the start ptr to null, there was an
1228 // error.
1229 if (Filename.empty())
1230 return false;
1232 // Search include directories.
1233 Optional<FileEntryRef> File =
1234 PP.LookupFile(FilenameLoc, Filename, isAngled, LookupFrom, LookupFromFile,
1235 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr);
1237 if (PPCallbacks *Callbacks = PP.getPPCallbacks()) {
1238 SrcMgr::CharacteristicKind FileType = SrcMgr::C_User;
1239 if (File)
1240 FileType =
1241 PP.getHeaderSearchInfo().getFileDirFlavor(&File->getFileEntry());
1242 Callbacks->HasInclude(FilenameLoc, Filename, isAngled, File, FileType);
1245 // Get the result value. A result of true means the file exists.
1246 return File.has_value();
1249 bool Preprocessor::EvaluateHasInclude(Token &Tok, IdentifierInfo *II) {
1250 return EvaluateHasIncludeCommon(Tok, II, *this, nullptr, nullptr);
1253 bool Preprocessor::EvaluateHasIncludeNext(Token &Tok, IdentifierInfo *II) {
1254 ConstSearchDirIterator Lookup = nullptr;
1255 const FileEntry *LookupFromFile;
1256 std::tie(Lookup, LookupFromFile) = getIncludeNextStart(Tok);
1258 return EvaluateHasIncludeCommon(Tok, II, *this, Lookup, LookupFromFile);
1261 /// Process single-argument builtin feature-like macros that return
1262 /// integer values.
1263 static void EvaluateFeatureLikeBuiltinMacro(llvm::raw_svector_ostream& OS,
1264 Token &Tok, IdentifierInfo *II,
1265 Preprocessor &PP, bool ExpandArgs,
1266 llvm::function_ref<
1267 int(Token &Tok,
1268 bool &HasLexedNextTok)> Op) {
1269 // Parse the initial '('.
1270 PP.LexUnexpandedToken(Tok);
1271 if (Tok.isNot(tok::l_paren)) {
1272 PP.Diag(Tok.getLocation(), diag::err_pp_expected_after) << II
1273 << tok::l_paren;
1275 // Provide a dummy '0' value on output stream to elide further errors.
1276 if (!Tok.isOneOf(tok::eof, tok::eod)) {
1277 OS << 0;
1278 Tok.setKind(tok::numeric_constant);
1280 return;
1283 unsigned ParenDepth = 1;
1284 SourceLocation LParenLoc = Tok.getLocation();
1285 llvm::Optional<int> Result;
1287 Token ResultTok;
1288 bool SuppressDiagnostic = false;
1289 while (true) {
1290 // Parse next token.
1291 if (ExpandArgs)
1292 PP.Lex(Tok);
1293 else
1294 PP.LexUnexpandedToken(Tok);
1296 already_lexed:
1297 switch (Tok.getKind()) {
1298 case tok::eof:
1299 case tok::eod:
1300 // Don't provide even a dummy value if the eod or eof marker is
1301 // reached. Simply provide a diagnostic.
1302 PP.Diag(Tok.getLocation(), diag::err_unterm_macro_invoc);
1303 return;
1305 case tok::comma:
1306 if (!SuppressDiagnostic) {
1307 PP.Diag(Tok.getLocation(), diag::err_too_many_args_in_macro_invoc);
1308 SuppressDiagnostic = true;
1310 continue;
1312 case tok::l_paren:
1313 ++ParenDepth;
1314 if (Result)
1315 break;
1316 if (!SuppressDiagnostic) {
1317 PP.Diag(Tok.getLocation(), diag::err_pp_nested_paren) << II;
1318 SuppressDiagnostic = true;
1320 continue;
1322 case tok::r_paren:
1323 if (--ParenDepth > 0)
1324 continue;
1326 // The last ')' has been reached; return the value if one found or
1327 // a diagnostic and a dummy value.
1328 if (Result) {
1329 OS << Result.value();
1330 // For strict conformance to __has_cpp_attribute rules, use 'L'
1331 // suffix for dated literals.
1332 if (Result.value() > 1)
1333 OS << 'L';
1334 } else {
1335 OS << 0;
1336 if (!SuppressDiagnostic)
1337 PP.Diag(Tok.getLocation(), diag::err_too_few_args_in_macro_invoc);
1339 Tok.setKind(tok::numeric_constant);
1340 return;
1342 default: {
1343 // Parse the macro argument, if one not found so far.
1344 if (Result)
1345 break;
1347 bool HasLexedNextToken = false;
1348 Result = Op(Tok, HasLexedNextToken);
1349 ResultTok = Tok;
1350 if (HasLexedNextToken)
1351 goto already_lexed;
1352 continue;
1356 // Diagnose missing ')'.
1357 if (!SuppressDiagnostic) {
1358 if (auto Diag = PP.Diag(Tok.getLocation(), diag::err_pp_expected_after)) {
1359 if (IdentifierInfo *LastII = ResultTok.getIdentifierInfo())
1360 Diag << LastII;
1361 else
1362 Diag << ResultTok.getKind();
1363 Diag << tok::r_paren << ResultTok.getLocation();
1365 PP.Diag(LParenLoc, diag::note_matching) << tok::l_paren;
1366 SuppressDiagnostic = true;
1371 /// Helper function to return the IdentifierInfo structure of a Token
1372 /// or generate a diagnostic if none available.
1373 static IdentifierInfo *ExpectFeatureIdentifierInfo(Token &Tok,
1374 Preprocessor &PP,
1375 signed DiagID) {
1376 IdentifierInfo *II;
1377 if (!Tok.isAnnotation() && (II = Tok.getIdentifierInfo()))
1378 return II;
1380 PP.Diag(Tok.getLocation(), DiagID);
1381 return nullptr;
1384 /// Implements the __is_target_arch builtin macro.
1385 static bool isTargetArch(const TargetInfo &TI, const IdentifierInfo *II) {
1386 std::string ArchName = II->getName().lower() + "--";
1387 llvm::Triple Arch(ArchName);
1388 const llvm::Triple &TT = TI.getTriple();
1389 if (TT.isThumb()) {
1390 // arm matches thumb or thumbv7. armv7 matches thumbv7.
1391 if ((Arch.getSubArch() == llvm::Triple::NoSubArch ||
1392 Arch.getSubArch() == TT.getSubArch()) &&
1393 ((TT.getArch() == llvm::Triple::thumb &&
1394 Arch.getArch() == llvm::Triple::arm) ||
1395 (TT.getArch() == llvm::Triple::thumbeb &&
1396 Arch.getArch() == llvm::Triple::armeb)))
1397 return true;
1399 // Check the parsed arch when it has no sub arch to allow Clang to
1400 // match thumb to thumbv7 but to prohibit matching thumbv6 to thumbv7.
1401 return (Arch.getSubArch() == llvm::Triple::NoSubArch ||
1402 Arch.getSubArch() == TT.getSubArch()) &&
1403 Arch.getArch() == TT.getArch();
1406 /// Implements the __is_target_vendor builtin macro.
1407 static bool isTargetVendor(const TargetInfo &TI, const IdentifierInfo *II) {
1408 StringRef VendorName = TI.getTriple().getVendorName();
1409 if (VendorName.empty())
1410 VendorName = "unknown";
1411 return VendorName.equals_insensitive(II->getName());
1414 /// Implements the __is_target_os builtin macro.
1415 static bool isTargetOS(const TargetInfo &TI, const IdentifierInfo *II) {
1416 std::string OSName =
1417 (llvm::Twine("unknown-unknown-") + II->getName().lower()).str();
1418 llvm::Triple OS(OSName);
1419 if (OS.getOS() == llvm::Triple::Darwin) {
1420 // Darwin matches macos, ios, etc.
1421 return TI.getTriple().isOSDarwin();
1423 return TI.getTriple().getOS() == OS.getOS();
1426 /// Implements the __is_target_environment builtin macro.
1427 static bool isTargetEnvironment(const TargetInfo &TI,
1428 const IdentifierInfo *II) {
1429 std::string EnvName = (llvm::Twine("---") + II->getName().lower()).str();
1430 llvm::Triple Env(EnvName);
1431 return TI.getTriple().getEnvironment() == Env.getEnvironment();
1434 /// ExpandBuiltinMacro - If an identifier token is read that is to be expanded
1435 /// as a builtin macro, handle it and return the next token as 'Tok'.
1436 void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
1437 // Figure out which token this is.
1438 IdentifierInfo *II = Tok.getIdentifierInfo();
1439 assert(II && "Can't be a macro without id info!");
1441 // If this is an _Pragma or Microsoft __pragma directive, expand it,
1442 // invoke the pragma handler, then lex the token after it.
1443 if (II == Ident_Pragma)
1444 return Handle_Pragma(Tok);
1445 else if (II == Ident__pragma) // in non-MS mode this is null
1446 return HandleMicrosoft__pragma(Tok);
1448 ++NumBuiltinMacroExpanded;
1450 SmallString<128> TmpBuffer;
1451 llvm::raw_svector_ostream OS(TmpBuffer);
1453 // Set up the return result.
1454 Tok.setIdentifierInfo(nullptr);
1455 Tok.clearFlag(Token::NeedsCleaning);
1456 bool IsAtStartOfLine = Tok.isAtStartOfLine();
1457 bool HasLeadingSpace = Tok.hasLeadingSpace();
1459 if (II == Ident__LINE__) {
1460 // C99 6.10.8: "__LINE__: The presumed line number (within the current
1461 // source file) of the current source line (an integer constant)". This can
1462 // be affected by #line.
1463 SourceLocation Loc = Tok.getLocation();
1465 // Advance to the location of the first _, this might not be the first byte
1466 // of the token if it starts with an escaped newline.
1467 Loc = AdvanceToTokenCharacter(Loc, 0);
1469 // One wrinkle here is that GCC expands __LINE__ to location of the *end* of
1470 // a macro expansion. This doesn't matter for object-like macros, but
1471 // can matter for a function-like macro that expands to contain __LINE__.
1472 // Skip down through expansion points until we find a file loc for the
1473 // end of the expansion history.
1474 Loc = SourceMgr.getExpansionRange(Loc).getEnd();
1475 PresumedLoc PLoc = SourceMgr.getPresumedLoc(Loc);
1477 // __LINE__ expands to a simple numeric value.
1478 OS << (PLoc.isValid()? PLoc.getLine() : 1);
1479 Tok.setKind(tok::numeric_constant);
1480 } else if (II == Ident__FILE__ || II == Ident__BASE_FILE__ ||
1481 II == Ident__FILE_NAME__) {
1482 // C99 6.10.8: "__FILE__: The presumed name of the current source file (a
1483 // character string literal)". This can be affected by #line.
1484 PresumedLoc PLoc = SourceMgr.getPresumedLoc(Tok.getLocation());
1486 // __BASE_FILE__ is a GNU extension that returns the top of the presumed
1487 // #include stack instead of the current file.
1488 if (II == Ident__BASE_FILE__ && PLoc.isValid()) {
1489 SourceLocation NextLoc = PLoc.getIncludeLoc();
1490 while (NextLoc.isValid()) {
1491 PLoc = SourceMgr.getPresumedLoc(NextLoc);
1492 if (PLoc.isInvalid())
1493 break;
1495 NextLoc = PLoc.getIncludeLoc();
1499 // Escape this filename. Turn '\' -> '\\' '"' -> '\"'
1500 SmallString<256> FN;
1501 if (PLoc.isValid()) {
1502 // __FILE_NAME__ is a Clang-specific extension that expands to the
1503 // the last part of __FILE__.
1504 if (II == Ident__FILE_NAME__) {
1505 // Try to get the last path component, failing that return the original
1506 // presumed location.
1507 StringRef PLFileName = llvm::sys::path::filename(PLoc.getFilename());
1508 if (PLFileName != "")
1509 FN += PLFileName;
1510 else
1511 FN += PLoc.getFilename();
1512 } else {
1513 FN += PLoc.getFilename();
1515 processPathForFileMacro(FN, getLangOpts(), getTargetInfo());
1516 Lexer::Stringify(FN);
1517 OS << '"' << FN << '"';
1519 Tok.setKind(tok::string_literal);
1520 } else if (II == Ident__DATE__) {
1521 Diag(Tok.getLocation(), diag::warn_pp_date_time);
1522 if (!DATELoc.isValid())
1523 ComputeDATE_TIME(DATELoc, TIMELoc, *this);
1524 Tok.setKind(tok::string_literal);
1525 Tok.setLength(strlen("\"Mmm dd yyyy\""));
1526 Tok.setLocation(SourceMgr.createExpansionLoc(DATELoc, Tok.getLocation(),
1527 Tok.getLocation(),
1528 Tok.getLength()));
1529 return;
1530 } else if (II == Ident__TIME__) {
1531 Diag(Tok.getLocation(), diag::warn_pp_date_time);
1532 if (!TIMELoc.isValid())
1533 ComputeDATE_TIME(DATELoc, TIMELoc, *this);
1534 Tok.setKind(tok::string_literal);
1535 Tok.setLength(strlen("\"hh:mm:ss\""));
1536 Tok.setLocation(SourceMgr.createExpansionLoc(TIMELoc, Tok.getLocation(),
1537 Tok.getLocation(),
1538 Tok.getLength()));
1539 return;
1540 } else if (II == Ident__INCLUDE_LEVEL__) {
1541 // Compute the presumed include depth of this token. This can be affected
1542 // by GNU line markers.
1543 unsigned Depth = 0;
1545 PresumedLoc PLoc = SourceMgr.getPresumedLoc(Tok.getLocation());
1546 if (PLoc.isValid()) {
1547 PLoc = SourceMgr.getPresumedLoc(PLoc.getIncludeLoc());
1548 for (; PLoc.isValid(); ++Depth)
1549 PLoc = SourceMgr.getPresumedLoc(PLoc.getIncludeLoc());
1552 // __INCLUDE_LEVEL__ expands to a simple numeric value.
1553 OS << Depth;
1554 Tok.setKind(tok::numeric_constant);
1555 } else if (II == Ident__TIMESTAMP__) {
1556 Diag(Tok.getLocation(), diag::warn_pp_date_time);
1557 // MSVC, ICC, GCC, VisualAge C++ extension. The generated string should be
1558 // of the form "Ddd Mmm dd hh::mm::ss yyyy", which is returned by asctime.
1560 // Get the file that we are lexing out of. If we're currently lexing from
1561 // a macro, dig into the include stack.
1562 const FileEntry *CurFile = nullptr;
1563 PreprocessorLexer *TheLexer = getCurrentFileLexer();
1565 if (TheLexer)
1566 CurFile = SourceMgr.getFileEntryForID(TheLexer->getFileID());
1568 const char *Result;
1569 if (CurFile) {
1570 time_t TT = CurFile->getModificationTime();
1571 struct tm *TM = localtime(&TT);
1572 Result = asctime(TM);
1573 } else {
1574 Result = "??? ??? ?? ??:??:?? ????\n";
1576 // Surround the string with " and strip the trailing newline.
1577 OS << '"' << StringRef(Result).drop_back() << '"';
1578 Tok.setKind(tok::string_literal);
1579 } else if (II == Ident__FLT_EVAL_METHOD__) {
1580 // __FLT_EVAL_METHOD__ is set to the default value.
1581 if (getTUFPEvalMethod() ==
1582 LangOptions::FPEvalMethodKind::FEM_Indeterminable) {
1583 // This is possible if `AllowFPReassoc` or `AllowReciprocal` is enabled.
1584 // These modes can be triggered via the command line option `-ffast-math`
1585 // or via a `pragam float_control`.
1586 // __FLT_EVAL_METHOD__ expands to -1.
1587 // The `minus` operator is the next token we read from the stream.
1588 auto Toks = std::make_unique<Token[]>(1);
1589 OS << "-";
1590 Tok.setKind(tok::minus);
1591 // Push the token `1` to the stream.
1592 Token NumberToken;
1593 NumberToken.startToken();
1594 NumberToken.setKind(tok::numeric_constant);
1595 NumberToken.setLiteralData("1");
1596 NumberToken.setLength(1);
1597 Toks[0] = NumberToken;
1598 EnterTokenStream(std::move(Toks), 1, /*DisableMacroExpansion*/ false,
1599 /*IsReinject*/ false);
1600 } else {
1601 OS << getTUFPEvalMethod();
1602 // __FLT_EVAL_METHOD__ expands to a simple numeric value.
1603 Tok.setKind(tok::numeric_constant);
1604 if (getLastFPEvalPragmaLocation().isValid()) {
1605 // The program is ill-formed. The value of __FLT_EVAL_METHOD__ is
1606 // altered by the pragma.
1607 Diag(Tok, diag::err_illegal_use_of_flt_eval_macro);
1608 Diag(getLastFPEvalPragmaLocation(), diag::note_pragma_entered_here);
1611 } else if (II == Ident__COUNTER__) {
1612 // __COUNTER__ expands to a simple numeric value.
1613 OS << CounterValue++;
1614 Tok.setKind(tok::numeric_constant);
1615 } else if (II == Ident__has_feature) {
1616 EvaluateFeatureLikeBuiltinMacro(OS, Tok, II, *this, false,
1617 [this](Token &Tok, bool &HasLexedNextToken) -> int {
1618 IdentifierInfo *II = ExpectFeatureIdentifierInfo(Tok, *this,
1619 diag::err_feature_check_malformed);
1620 return II && HasFeature(*this, II->getName());
1622 } else if (II == Ident__has_extension) {
1623 EvaluateFeatureLikeBuiltinMacro(OS, Tok, II, *this, false,
1624 [this](Token &Tok, bool &HasLexedNextToken) -> int {
1625 IdentifierInfo *II = ExpectFeatureIdentifierInfo(Tok, *this,
1626 diag::err_feature_check_malformed);
1627 return II && HasExtension(*this, II->getName());
1629 } else if (II == Ident__has_builtin) {
1630 EvaluateFeatureLikeBuiltinMacro(OS, Tok, II, *this, false,
1631 [this](Token &Tok, bool &HasLexedNextToken) -> int {
1632 IdentifierInfo *II = ExpectFeatureIdentifierInfo(Tok, *this,
1633 diag::err_feature_check_malformed);
1634 if (!II)
1635 return false;
1636 else if (II->getBuiltinID() != 0) {
1637 switch (II->getBuiltinID()) {
1638 case Builtin::BI__builtin_operator_new:
1639 case Builtin::BI__builtin_operator_delete:
1640 // denotes date of behavior change to support calling arbitrary
1641 // usual allocation and deallocation functions. Required by libc++
1642 return 201802;
1643 default:
1644 return Builtin::evaluateRequiredTargetFeatures(
1645 getBuiltinInfo().getRequiredFeatures(II->getBuiltinID()),
1646 getTargetInfo().getTargetOpts().FeatureMap);
1648 return true;
1649 } else if (II->getTokenID() != tok::identifier ||
1650 II->hasRevertedTokenIDToIdentifier()) {
1651 // Treat all keywords that introduce a custom syntax of the form
1653 // '__some_keyword' '(' [...] ')'
1655 // as being "builtin functions", even if the syntax isn't a valid
1656 // function call (for example, because the builtin takes a type
1657 // argument).
1658 if (II->getName().startswith("__builtin_") ||
1659 II->getName().startswith("__is_") ||
1660 II->getName().startswith("__has_"))
1661 return true;
1662 return llvm::StringSwitch<bool>(II->getName())
1663 .Case("__array_rank", true)
1664 .Case("__array_extent", true)
1665 .Case("__reference_binds_to_temporary", true)
1666 #define TRANSFORM_TYPE_TRAIT_DEF(_, Trait) .Case("__" #Trait, true)
1667 #include "clang/Basic/TransformTypeTraits.def"
1668 .Default(false);
1669 } else {
1670 return llvm::StringSwitch<bool>(II->getName())
1671 // Report builtin templates as being builtins.
1672 .Case("__make_integer_seq", getLangOpts().CPlusPlus)
1673 .Case("__type_pack_element", getLangOpts().CPlusPlus)
1674 // Likewise for some builtin preprocessor macros.
1675 // FIXME: This is inconsistent; we usually suggest detecting
1676 // builtin macros via #ifdef. Don't add more cases here.
1677 .Case("__is_target_arch", true)
1678 .Case("__is_target_vendor", true)
1679 .Case("__is_target_os", true)
1680 .Case("__is_target_environment", true)
1681 .Default(false);
1684 } else if (II == Ident__is_identifier) {
1685 EvaluateFeatureLikeBuiltinMacro(OS, Tok, II, *this, false,
1686 [](Token &Tok, bool &HasLexedNextToken) -> int {
1687 return Tok.is(tok::identifier);
1689 } else if (II == Ident__has_attribute) {
1690 EvaluateFeatureLikeBuiltinMacro(OS, Tok, II, *this, true,
1691 [this](Token &Tok, bool &HasLexedNextToken) -> int {
1692 IdentifierInfo *II = ExpectFeatureIdentifierInfo(Tok, *this,
1693 diag::err_feature_check_malformed);
1694 return II ? hasAttribute(AttributeCommonInfo::Syntax::AS_GNU, nullptr,
1695 II, getTargetInfo(), getLangOpts())
1696 : 0;
1698 } else if (II == Ident__has_declspec) {
1699 EvaluateFeatureLikeBuiltinMacro(OS, Tok, II, *this, true,
1700 [this](Token &Tok, bool &HasLexedNextToken) -> int {
1701 IdentifierInfo *II = ExpectFeatureIdentifierInfo(Tok, *this,
1702 diag::err_feature_check_malformed);
1703 if (II) {
1704 const LangOptions &LangOpts = getLangOpts();
1705 return LangOpts.DeclSpecKeyword &&
1706 hasAttribute(AttributeCommonInfo::Syntax::AS_Declspec, nullptr,
1707 II, getTargetInfo(), LangOpts);
1710 return false;
1712 } else if (II == Ident__has_cpp_attribute ||
1713 II == Ident__has_c_attribute) {
1714 bool IsCXX = II == Ident__has_cpp_attribute;
1715 EvaluateFeatureLikeBuiltinMacro(OS, Tok, II, *this, true,
1716 [&](Token &Tok, bool &HasLexedNextToken) -> int {
1717 IdentifierInfo *ScopeII = nullptr;
1718 IdentifierInfo *II = ExpectFeatureIdentifierInfo(
1719 Tok, *this, diag::err_feature_check_malformed);
1720 if (!II)
1721 return false;
1723 // It is possible to receive a scope token. Read the "::", if it is
1724 // available, and the subsequent identifier.
1725 LexUnexpandedToken(Tok);
1726 if (Tok.isNot(tok::coloncolon))
1727 HasLexedNextToken = true;
1728 else {
1729 ScopeII = II;
1730 // Lex an expanded token for the attribute name.
1731 Lex(Tok);
1732 II = ExpectFeatureIdentifierInfo(Tok, *this,
1733 diag::err_feature_check_malformed);
1736 AttributeCommonInfo::Syntax Syntax =
1737 IsCXX ? AttributeCommonInfo::Syntax::AS_CXX11
1738 : AttributeCommonInfo::Syntax::AS_C2x;
1739 return II ? hasAttribute(Syntax, ScopeII, II, getTargetInfo(),
1740 getLangOpts())
1741 : 0;
1743 } else if (II == Ident__has_include ||
1744 II == Ident__has_include_next) {
1745 // The argument to these two builtins should be a parenthesized
1746 // file name string literal using angle brackets (<>) or
1747 // double-quotes ("").
1748 bool Value;
1749 if (II == Ident__has_include)
1750 Value = EvaluateHasInclude(Tok, II);
1751 else
1752 Value = EvaluateHasIncludeNext(Tok, II);
1754 if (Tok.isNot(tok::r_paren))
1755 return;
1756 OS << (int)Value;
1757 Tok.setKind(tok::numeric_constant);
1758 } else if (II == Ident__has_warning) {
1759 // The argument should be a parenthesized string literal.
1760 EvaluateFeatureLikeBuiltinMacro(OS, Tok, II, *this, false,
1761 [this](Token &Tok, bool &HasLexedNextToken) -> int {
1762 std::string WarningName;
1763 SourceLocation StrStartLoc = Tok.getLocation();
1765 HasLexedNextToken = Tok.is(tok::string_literal);
1766 if (!FinishLexStringLiteral(Tok, WarningName, "'__has_warning'",
1767 /*AllowMacroExpansion=*/false))
1768 return false;
1770 // FIXME: Should we accept "-R..." flags here, or should that be
1771 // handled by a separate __has_remark?
1772 if (WarningName.size() < 3 || WarningName[0] != '-' ||
1773 WarningName[1] != 'W') {
1774 Diag(StrStartLoc, diag::warn_has_warning_invalid_option);
1775 return false;
1778 // Finally, check if the warning flags maps to a diagnostic group.
1779 // We construct a SmallVector here to talk to getDiagnosticIDs().
1780 // Although we don't use the result, this isn't a hot path, and not
1781 // worth special casing.
1782 SmallVector<diag::kind, 10> Diags;
1783 return !getDiagnostics().getDiagnosticIDs()->
1784 getDiagnosticsInGroup(diag::Flavor::WarningOrError,
1785 WarningName.substr(2), Diags);
1787 } else if (II == Ident__building_module) {
1788 // The argument to this builtin should be an identifier. The
1789 // builtin evaluates to 1 when that identifier names the module we are
1790 // currently building.
1791 EvaluateFeatureLikeBuiltinMacro(OS, Tok, II, *this, false,
1792 [this](Token &Tok, bool &HasLexedNextToken) -> int {
1793 IdentifierInfo *II = ExpectFeatureIdentifierInfo(Tok, *this,
1794 diag::err_expected_id_building_module);
1795 return getLangOpts().isCompilingModule() && II &&
1796 (II->getName() == getLangOpts().CurrentModule);
1798 } else if (II == Ident__MODULE__) {
1799 // The current module as an identifier.
1800 OS << getLangOpts().CurrentModule;
1801 IdentifierInfo *ModuleII = getIdentifierInfo(getLangOpts().CurrentModule);
1802 Tok.setIdentifierInfo(ModuleII);
1803 Tok.setKind(ModuleII->getTokenID());
1804 } else if (II == Ident__identifier) {
1805 SourceLocation Loc = Tok.getLocation();
1807 // We're expecting '__identifier' '(' identifier ')'. Try to recover
1808 // if the parens are missing.
1809 LexNonComment(Tok);
1810 if (Tok.isNot(tok::l_paren)) {
1811 // No '(', use end of last token.
1812 Diag(getLocForEndOfToken(Loc), diag::err_pp_expected_after)
1813 << II << tok::l_paren;
1814 // If the next token isn't valid as our argument, we can't recover.
1815 if (!Tok.isAnnotation() && Tok.getIdentifierInfo())
1816 Tok.setKind(tok::identifier);
1817 return;
1820 SourceLocation LParenLoc = Tok.getLocation();
1821 LexNonComment(Tok);
1823 if (!Tok.isAnnotation() && Tok.getIdentifierInfo())
1824 Tok.setKind(tok::identifier);
1825 else if (Tok.is(tok::string_literal) && !Tok.hasUDSuffix()) {
1826 StringLiteralParser Literal(Tok, *this);
1827 if (Literal.hadError)
1828 return;
1830 Tok.setIdentifierInfo(getIdentifierInfo(Literal.GetString()));
1831 Tok.setKind(tok::identifier);
1832 } else {
1833 Diag(Tok.getLocation(), diag::err_pp_identifier_arg_not_identifier)
1834 << Tok.getKind();
1835 // Don't walk past anything that's not a real token.
1836 if (Tok.isOneOf(tok::eof, tok::eod) || Tok.isAnnotation())
1837 return;
1840 // Discard the ')', preserving 'Tok' as our result.
1841 Token RParen;
1842 LexNonComment(RParen);
1843 if (RParen.isNot(tok::r_paren)) {
1844 Diag(getLocForEndOfToken(Tok.getLocation()), diag::err_pp_expected_after)
1845 << Tok.getKind() << tok::r_paren;
1846 Diag(LParenLoc, diag::note_matching) << tok::l_paren;
1848 return;
1849 } else if (II == Ident__is_target_arch) {
1850 EvaluateFeatureLikeBuiltinMacro(
1851 OS, Tok, II, *this, false,
1852 [this](Token &Tok, bool &HasLexedNextToken) -> int {
1853 IdentifierInfo *II = ExpectFeatureIdentifierInfo(
1854 Tok, *this, diag::err_feature_check_malformed);
1855 return II && isTargetArch(getTargetInfo(), II);
1857 } else if (II == Ident__is_target_vendor) {
1858 EvaluateFeatureLikeBuiltinMacro(
1859 OS, Tok, II, *this, false,
1860 [this](Token &Tok, bool &HasLexedNextToken) -> int {
1861 IdentifierInfo *II = ExpectFeatureIdentifierInfo(
1862 Tok, *this, diag::err_feature_check_malformed);
1863 return II && isTargetVendor(getTargetInfo(), II);
1865 } else if (II == Ident__is_target_os) {
1866 EvaluateFeatureLikeBuiltinMacro(
1867 OS, Tok, II, *this, false,
1868 [this](Token &Tok, bool &HasLexedNextToken) -> int {
1869 IdentifierInfo *II = ExpectFeatureIdentifierInfo(
1870 Tok, *this, diag::err_feature_check_malformed);
1871 return II && isTargetOS(getTargetInfo(), II);
1873 } else if (II == Ident__is_target_environment) {
1874 EvaluateFeatureLikeBuiltinMacro(
1875 OS, Tok, II, *this, false,
1876 [this](Token &Tok, bool &HasLexedNextToken) -> int {
1877 IdentifierInfo *II = ExpectFeatureIdentifierInfo(
1878 Tok, *this, diag::err_feature_check_malformed);
1879 return II && isTargetEnvironment(getTargetInfo(), II);
1881 } else {
1882 llvm_unreachable("Unknown identifier!");
1884 CreateString(OS.str(), Tok, Tok.getLocation(), Tok.getLocation());
1885 Tok.setFlagValue(Token::StartOfLine, IsAtStartOfLine);
1886 Tok.setFlagValue(Token::LeadingSpace, HasLeadingSpace);
1889 void Preprocessor::markMacroAsUsed(MacroInfo *MI) {
1890 // If the 'used' status changed, and the macro requires 'unused' warning,
1891 // remove its SourceLocation from the warn-for-unused-macro locations.
1892 if (MI->isWarnIfUnused() && !MI->isUsed())
1893 WarnUnusedMacroLocs.erase(MI->getDefinitionLoc());
1894 MI->setIsUsed(true);
1897 void Preprocessor::processPathForFileMacro(SmallVectorImpl<char> &Path,
1898 const LangOptions &LangOpts,
1899 const TargetInfo &TI) {
1900 LangOpts.remapPathPrefix(Path);
1901 if (LangOpts.UseTargetPathSeparator) {
1902 if (TI.getTriple().isOSWindows())
1903 llvm::sys::path::remove_dots(Path, false,
1904 llvm::sys::path::Style::windows_backslash);
1905 else
1906 llvm::sys::path::remove_dots(Path, false, llvm::sys::path::Style::posix);