1 //===--- PPMacroExpansion.cpp - Top level Macro Expansion -----------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file implements the top level handling of macro expansion for the
12 //===----------------------------------------------------------------------===//
14 #include "clang/Basic/Attributes.h"
15 #include "clang/Basic/Builtins.h"
16 #include "clang/Basic/FileManager.h"
17 #include "clang/Basic/IdentifierTable.h"
18 #include "clang/Basic/LLVM.h"
19 #include "clang/Basic/LangOptions.h"
20 #include "clang/Basic/ObjCRuntime.h"
21 #include "clang/Basic/SourceLocation.h"
22 #include "clang/Basic/TargetInfo.h"
23 #include "clang/Lex/CodeCompletionHandler.h"
24 #include "clang/Lex/DirectoryLookup.h"
25 #include "clang/Lex/ExternalPreprocessorSource.h"
26 #include "clang/Lex/HeaderSearch.h"
27 #include "clang/Lex/LexDiagnostic.h"
28 #include "clang/Lex/LiteralSupport.h"
29 #include "clang/Lex/MacroArgs.h"
30 #include "clang/Lex/MacroInfo.h"
31 #include "clang/Lex/Preprocessor.h"
32 #include "clang/Lex/PreprocessorLexer.h"
33 #include "clang/Lex/PreprocessorOptions.h"
34 #include "clang/Lex/Token.h"
35 #include "llvm/ADT/ArrayRef.h"
36 #include "llvm/ADT/DenseMap.h"
37 #include "llvm/ADT/DenseSet.h"
38 #include "llvm/ADT/FoldingSet.h"
39 #include "llvm/ADT/None.h"
40 #include "llvm/ADT/Optional.h"
41 #include "llvm/ADT/STLExtras.h"
42 #include "llvm/ADT/SmallString.h"
43 #include "llvm/ADT/SmallVector.h"
44 #include "llvm/ADT/StringRef.h"
45 #include "llvm/ADT/StringSwitch.h"
46 #include "llvm/Support/Casting.h"
47 #include "llvm/Support/ErrorHandling.h"
48 #include "llvm/Support/Format.h"
49 #include "llvm/Support/Path.h"
50 #include "llvm/Support/raw_ostream.h"
60 using namespace clang;
63 Preprocessor::getLocalMacroDirectiveHistory(const IdentifierInfo *II) const {
64 if (!II->hadMacroDefinition())
66 auto Pos = CurSubmoduleState->Macros.find(II);
67 return Pos == CurSubmoduleState->Macros.end() ? nullptr
68 : Pos->second.getLatest();
71 void Preprocessor::appendMacroDirective(IdentifierInfo *II, MacroDirective *MD){
72 assert(MD && "MacroDirective should be non-zero!");
73 assert(!MD->getPrevious() && "Already attached to a MacroDirective history.");
75 MacroState &StoredMD = CurSubmoduleState->Macros[II];
76 auto *OldMD = StoredMD.getLatest();
77 MD->setPrevious(OldMD);
78 StoredMD.setLatest(MD);
79 StoredMD.overrideActiveModuleMacros(*this, II);
81 if (needModuleMacros()) {
82 // Track that we created a new macro directive, so we know we should
83 // consider building a ModuleMacro for it when we get to the end of
85 PendingModuleMacroNames.push_back(II);
88 // Set up the identifier as having associated macro history.
89 II->setHasMacroDefinition(true);
90 if (!MD->isDefined() && LeafModuleMacros.find(II) == LeafModuleMacros.end())
91 II->setHasMacroDefinition(false);
93 II->setChangedSinceDeserialization();
96 void Preprocessor::setLoadedMacroDirective(IdentifierInfo *II,
99 // Normally, when a macro is defined, it goes through appendMacroDirective()
100 // above, which chains a macro to previous defines, undefs, etc.
101 // However, in a pch, the whole macro history up to the end of the pch is
102 // stored, so ASTReader goes through this function instead.
103 // However, built-in macros are already registered in the Preprocessor
104 // ctor, and ASTWriter stops writing the macro chain at built-in macros,
105 // so in that case the chain from the pch needs to be spliced to the existing
109 MacroState &StoredMD = CurSubmoduleState->Macros[II];
111 if (auto *OldMD = StoredMD.getLatest()) {
112 // shouldIgnoreMacro() in ASTWriter also stops at macros from the
113 // predefines buffer in module builds. However, in module builds, modules
114 // are loaded completely before predefines are processed, so StoredMD
115 // will be nullptr for them when they're loaded. StoredMD should only be
116 // non-nullptr for builtins read from a pch file.
117 assert(OldMD->getMacroInfo()->isBuiltinMacro() &&
118 "only built-ins should have an entry here");
119 assert(!OldMD->getPrevious() && "builtin should only have a single entry");
120 ED->setPrevious(OldMD);
121 StoredMD.setLatest(MD);
126 // Setup the identifier as having associated macro history.
127 II->setHasMacroDefinition(true);
128 if (!MD->isDefined() && LeafModuleMacros.find(II) == LeafModuleMacros.end())
129 II->setHasMacroDefinition(false);
132 ModuleMacro *Preprocessor::addModuleMacro(Module *Mod, IdentifierInfo *II,
134 ArrayRef<ModuleMacro *> Overrides,
136 llvm::FoldingSetNodeID ID;
137 ModuleMacro::Profile(ID, Mod, II);
140 if (auto *MM = ModuleMacros.FindNodeOrInsertPos(ID, InsertPos)) {
145 auto *MM = ModuleMacro::create(*this, Mod, II, Macro, Overrides);
146 ModuleMacros.InsertNode(MM, InsertPos);
148 // Each overridden macro is now overridden by one more macro.
150 for (auto *O : Overrides) {
151 HidAny |= (O->NumOverriddenBy == 0);
152 ++O->NumOverriddenBy;
155 // If we were the first overrider for any macro, it's no longer a leaf.
156 auto &LeafMacros = LeafModuleMacros[II];
158 llvm::erase_if(LeafMacros,
159 [](ModuleMacro *MM) { return MM->NumOverriddenBy != 0; });
162 // The new macro is always a leaf macro.
163 LeafMacros.push_back(MM);
164 // The identifier now has defined macros (that may or may not be visible).
165 II->setHasMacroDefinition(true);
171 ModuleMacro *Preprocessor::getModuleMacro(Module *Mod,
172 const IdentifierInfo *II) {
173 llvm::FoldingSetNodeID ID;
174 ModuleMacro::Profile(ID, Mod, II);
177 return ModuleMacros.FindNodeOrInsertPos(ID, InsertPos);
180 void Preprocessor::updateModuleMacroInfo(const IdentifierInfo *II,
181 ModuleMacroInfo &Info) {
182 assert(Info.ActiveModuleMacrosGeneration !=
183 CurSubmoduleState->VisibleModules.getGeneration() &&
184 "don't need to update this macro name info");
185 Info.ActiveModuleMacrosGeneration =
186 CurSubmoduleState->VisibleModules.getGeneration();
188 auto Leaf = LeafModuleMacros.find(II);
189 if (Leaf == LeafModuleMacros.end()) {
190 // No imported macros at all: nothing to do.
194 Info.ActiveModuleMacros.clear();
196 // Every macro that's locally overridden is overridden by a visible macro.
197 llvm::DenseMap<ModuleMacro *, int> NumHiddenOverrides;
198 for (auto *O : Info.OverriddenMacros)
199 NumHiddenOverrides[O] = -1;
201 // Collect all macros that are not overridden by a visible macro.
202 llvm::SmallVector<ModuleMacro *, 16> Worklist;
203 for (auto *LeafMM : Leaf->second) {
204 assert(LeafMM->getNumOverridingMacros() == 0 && "leaf macro overridden");
205 if (NumHiddenOverrides.lookup(LeafMM) == 0)
206 Worklist.push_back(LeafMM);
208 while (!Worklist.empty()) {
209 auto *MM = Worklist.pop_back_val();
210 if (CurSubmoduleState->VisibleModules.isVisible(MM->getOwningModule())) {
211 // We only care about collecting definitions; undefinitions only act
212 // to override other definitions.
213 if (MM->getMacroInfo())
214 Info.ActiveModuleMacros.push_back(MM);
216 for (auto *O : MM->overrides())
217 if ((unsigned)++NumHiddenOverrides[O] == O->getNumOverridingMacros())
218 Worklist.push_back(O);
221 // Our reverse postorder walk found the macros in reverse order.
222 std::reverse(Info.ActiveModuleMacros.begin(), Info.ActiveModuleMacros.end());
224 // Determine whether the macro name is ambiguous.
225 MacroInfo *MI = nullptr;
226 bool IsSystemMacro = true;
227 bool IsAmbiguous = false;
228 if (auto *MD = Info.MD) {
229 while (MD && isa<VisibilityMacroDirective>(MD))
230 MD = MD->getPrevious();
231 if (auto *DMD = dyn_cast_or_null<DefMacroDirective>(MD)) {
233 IsSystemMacro &= SourceMgr.isInSystemHeader(DMD->getLocation());
236 for (auto *Active : Info.ActiveModuleMacros) {
237 auto *NewMI = Active->getMacroInfo();
239 // Before marking the macro as ambiguous, check if this is a case where
240 // both macros are in system headers. If so, we trust that the system
241 // did not get it wrong. This also handles cases where Clang's own
242 // headers have a different spelling of certain system macros:
243 // #define LONG_MAX __LONG_MAX__ (clang's limits.h)
244 // #define LONG_MAX 0x7fffffffffffffffL (system's limits.h)
246 // FIXME: Remove the defined-in-system-headers check. clang's limits.h
247 // overrides the system limits.h's macros, so there's no conflict here.
248 if (MI && NewMI != MI &&
249 !MI->isIdenticalTo(*NewMI, *this, /*Syntactically=*/true))
251 IsSystemMacro &= Active->getOwningModule()->IsSystem ||
252 SourceMgr.isInSystemHeader(NewMI->getDefinitionLoc());
255 Info.IsAmbiguous = IsAmbiguous && !IsSystemMacro;
258 void Preprocessor::dumpMacroInfo(const IdentifierInfo *II) {
259 ArrayRef<ModuleMacro*> Leaf;
260 auto LeafIt = LeafModuleMacros.find(II);
261 if (LeafIt != LeafModuleMacros.end())
262 Leaf = LeafIt->second;
263 const MacroState *State = nullptr;
264 auto Pos = CurSubmoduleState->Macros.find(II);
265 if (Pos != CurSubmoduleState->Macros.end())
266 State = &Pos->second;
268 llvm::errs() << "MacroState " << State << " " << II->getNameStart();
269 if (State && State->isAmbiguous(*this, II))
270 llvm::errs() << " ambiguous";
271 if (State && !State->getOverriddenMacros().empty()) {
272 llvm::errs() << " overrides";
273 for (auto *O : State->getOverriddenMacros())
274 llvm::errs() << " " << O->getOwningModule()->getFullModuleName();
276 llvm::errs() << "\n";
278 // Dump local macro directives.
279 for (auto *MD = State ? State->getLatest() : nullptr; MD;
280 MD = MD->getPrevious()) {
285 // Dump module macros.
286 llvm::DenseSet<ModuleMacro*> Active;
287 for (auto *MM : State ? State->getActiveModuleMacros(*this, II) : None)
289 llvm::DenseSet<ModuleMacro*> Visited;
290 llvm::SmallVector<ModuleMacro *, 16> Worklist(Leaf.begin(), Leaf.end());
291 while (!Worklist.empty()) {
292 auto *MM = Worklist.pop_back_val();
293 llvm::errs() << " ModuleMacro " << MM << " "
294 << MM->getOwningModule()->getFullModuleName();
295 if (!MM->getMacroInfo())
296 llvm::errs() << " undef";
298 if (Active.count(MM))
299 llvm::errs() << " active";
300 else if (!CurSubmoduleState->VisibleModules.isVisible(
301 MM->getOwningModule()))
302 llvm::errs() << " hidden";
303 else if (MM->getMacroInfo())
304 llvm::errs() << " overridden";
306 if (!MM->overrides().empty()) {
307 llvm::errs() << " overrides";
308 for (auto *O : MM->overrides()) {
309 llvm::errs() << " " << O->getOwningModule()->getFullModuleName();
310 if (Visited.insert(O).second)
311 Worklist.push_back(O);
314 llvm::errs() << "\n";
315 if (auto *MI = MM->getMacroInfo()) {
318 llvm::errs() << "\n";
323 /// RegisterBuiltinMacro - Register the specified identifier in the identifier
324 /// table and mark it as a builtin macro to be expanded.
325 static IdentifierInfo *RegisterBuiltinMacro(Preprocessor &PP, const char *Name){
326 // Get the identifier.
327 IdentifierInfo *Id = PP.getIdentifierInfo(Name);
329 // Mark it as being a macro that is builtin.
330 MacroInfo *MI = PP.AllocateMacroInfo(SourceLocation());
331 MI->setIsBuiltinMacro();
332 PP.appendDefMacroDirective(Id, MI);
336 /// RegisterBuiltinMacros - Register builtin macros, such as __LINE__ with the
337 /// identifier table.
338 void Preprocessor::RegisterBuiltinMacros() {
339 Ident__LINE__ = RegisterBuiltinMacro(*this, "__LINE__");
340 Ident__FILE__ = RegisterBuiltinMacro(*this, "__FILE__");
341 Ident__DATE__ = RegisterBuiltinMacro(*this, "__DATE__");
342 Ident__TIME__ = RegisterBuiltinMacro(*this, "__TIME__");
343 Ident__COUNTER__ = RegisterBuiltinMacro(*this, "__COUNTER__");
344 Ident_Pragma = RegisterBuiltinMacro(*this, "_Pragma");
346 // C++ Standing Document Extensions.
347 if (getLangOpts().CPlusPlus)
348 Ident__has_cpp_attribute =
349 RegisterBuiltinMacro(*this, "__has_cpp_attribute");
351 Ident__has_cpp_attribute = nullptr;
354 Ident__BASE_FILE__ = RegisterBuiltinMacro(*this, "__BASE_FILE__");
355 Ident__INCLUDE_LEVEL__ = RegisterBuiltinMacro(*this, "__INCLUDE_LEVEL__");
356 Ident__TIMESTAMP__ = RegisterBuiltinMacro(*this, "__TIMESTAMP__");
358 // Microsoft Extensions.
359 if (getLangOpts().MicrosoftExt) {
360 Ident__identifier = RegisterBuiltinMacro(*this, "__identifier");
361 Ident__pragma = RegisterBuiltinMacro(*this, "__pragma");
363 Ident__identifier = nullptr;
364 Ident__pragma = nullptr;
368 Ident__FILE_NAME__ = RegisterBuiltinMacro(*this, "__FILE_NAME__");
369 Ident__has_feature = RegisterBuiltinMacro(*this, "__has_feature");
370 Ident__has_extension = RegisterBuiltinMacro(*this, "__has_extension");
371 Ident__has_builtin = RegisterBuiltinMacro(*this, "__has_builtin");
372 Ident__has_attribute = RegisterBuiltinMacro(*this, "__has_attribute");
373 if (!getLangOpts().CPlusPlus)
374 Ident__has_c_attribute = RegisterBuiltinMacro(*this, "__has_c_attribute");
376 Ident__has_c_attribute = nullptr;
378 Ident__has_declspec = RegisterBuiltinMacro(*this, "__has_declspec_attribute");
379 Ident__has_include = RegisterBuiltinMacro(*this, "__has_include");
380 Ident__has_include_next = RegisterBuiltinMacro(*this, "__has_include_next");
381 Ident__has_warning = RegisterBuiltinMacro(*this, "__has_warning");
382 Ident__is_identifier = RegisterBuiltinMacro(*this, "__is_identifier");
383 Ident__is_target_arch = RegisterBuiltinMacro(*this, "__is_target_arch");
384 Ident__is_target_vendor = RegisterBuiltinMacro(*this, "__is_target_vendor");
385 Ident__is_target_os = RegisterBuiltinMacro(*this, "__is_target_os");
386 Ident__is_target_environment =
387 RegisterBuiltinMacro(*this, "__is_target_environment");
390 Ident__building_module = RegisterBuiltinMacro(*this, "__building_module");
391 if (!getLangOpts().CurrentModule.empty())
392 Ident__MODULE__ = RegisterBuiltinMacro(*this, "__MODULE__");
394 Ident__MODULE__ = nullptr;
397 /// isTrivialSingleTokenExpansion - Return true if MI, which has a single token
398 /// in its expansion, currently expands to that token literally.
399 static bool isTrivialSingleTokenExpansion(const MacroInfo *MI,
400 const IdentifierInfo *MacroIdent,
402 IdentifierInfo *II = MI->getReplacementToken(0).getIdentifierInfo();
404 // If the token isn't an identifier, it's always literally expanded.
405 if (!II) return true;
407 // If the information about this identifier is out of date, update it from
408 // the external source.
409 if (II->isOutOfDate())
410 PP.getExternalSource()->updateOutOfDateIdentifier(*II);
412 // If the identifier is a macro, and if that macro is enabled, it may be
413 // expanded so it's not a trivial expansion.
414 if (auto *ExpansionMI = PP.getMacroInfo(II))
415 if (ExpansionMI->isEnabled() &&
416 // Fast expanding "#define X X" is ok, because X would be disabled.
420 // If this is an object-like macro invocation, it is safe to trivially expand
422 if (MI->isObjectLike()) return true;
424 // If this is a function-like macro invocation, it's safe to trivially expand
425 // as long as the identifier is not a macro argument.
426 return !llvm::is_contained(MI->params(), II);
429 /// isNextPPTokenLParen - Determine whether the next preprocessor token to be
430 /// lexed is a '('. If so, consume the token and return true, if not, this
431 /// method should have no observable side-effect on the lexed tokens.
432 bool Preprocessor::isNextPPTokenLParen() {
433 // Do some quick tests for rejection cases.
436 Val = CurLexer->isNextPPTokenLParen();
438 Val = CurTokenLexer->isNextTokenLParen();
441 // We have run off the end. If it's a source file we don't
442 // examine enclosing ones (C99 5.1.1.2p4). Otherwise walk up the
446 for (const IncludeStackInfo &Entry : llvm::reverse(IncludeMacroStack)) {
448 Val = Entry.TheLexer->isNextPPTokenLParen();
450 Val = Entry.TheTokenLexer->isNextTokenLParen();
455 // Ran off the end of a source file?
456 if (Entry.ThePPLexer)
461 // Okay, if we know that the token is a '(', lex it and return. Otherwise we
462 // have found something that isn't a '(' or we found the end of the
463 // translation unit. In either case, return false.
467 /// HandleMacroExpandedIdentifier - If an identifier token is read that is to be
468 /// expanded as a macro, handle it and return the next token as 'Identifier'.
469 bool Preprocessor::HandleMacroExpandedIdentifier(Token &Identifier,
470 const MacroDefinition &M) {
471 emitMacroExpansionWarnings(Identifier);
473 MacroInfo *MI = M.getMacroInfo();
475 // If this is a macro expansion in the "#if !defined(x)" line for the file,
476 // then the macro could expand to different things in other contexts, we need
477 // to disable the optimization in this case.
478 if (CurPPLexer) CurPPLexer->MIOpt.ExpandedMacro();
480 // If this is a builtin macro, like __LINE__ or _Pragma, handle it specially.
481 if (MI->isBuiltinMacro()) {
483 Callbacks->MacroExpands(Identifier, M, Identifier.getLocation(),
485 ExpandBuiltinMacro(Identifier);
489 /// Args - If this is a function-like macro expansion, this contains,
490 /// for each macro argument, the list of tokens that were provided to the
492 MacroArgs *Args = nullptr;
494 // Remember where the end of the expansion occurred. For an object-like
495 // macro, this is the identifier. For a function-like macro, this is the ')'.
496 SourceLocation ExpansionEnd = Identifier.getLocation();
498 // If this is a function-like macro, read the arguments.
499 if (MI->isFunctionLike()) {
500 // Remember that we are now parsing the arguments to a macro invocation.
501 // Preprocessor directives used inside macro arguments are not portable, and
502 // this enables the warning.
504 ArgMacro = &Identifier;
506 Args = ReadMacroCallArgumentList(Identifier, MI, ExpansionEnd);
508 // Finished parsing args.
512 // If there was an error parsing the arguments, bail out.
513 if (!Args) return true;
515 ++NumFnMacroExpanded;
520 // Notice that this macro has been used.
523 // Remember where the token is expanded.
524 SourceLocation ExpandLoc = Identifier.getLocation();
525 SourceRange ExpansionRange(ExpandLoc, ExpansionEnd);
529 // We can have macro expansion inside a conditional directive while
530 // reading the function macro arguments. To ensure, in that case, that
531 // MacroExpands callbacks still happen in source order, queue this
532 // callback to have it happen after the function macro callback.
533 DelayedMacroExpandsCallbacks.push_back(
534 MacroExpandsInfo(Identifier, M, ExpansionRange));
536 Callbacks->MacroExpands(Identifier, M, ExpansionRange, Args);
537 if (!DelayedMacroExpandsCallbacks.empty()) {
538 for (const MacroExpandsInfo &Info : DelayedMacroExpandsCallbacks) {
539 // FIXME: We lose macro args info with delayed callback.
540 Callbacks->MacroExpands(Info.Tok, Info.MD, Info.Range,
543 DelayedMacroExpandsCallbacks.clear();
548 // If the macro definition is ambiguous, complain.
549 if (M.isAmbiguous()) {
550 Diag(Identifier, diag::warn_pp_ambiguous_macro)
551 << Identifier.getIdentifierInfo();
552 Diag(MI->getDefinitionLoc(), diag::note_pp_ambiguous_macro_chosen)
553 << Identifier.getIdentifierInfo();
554 M.forAllDefinitions([&](const MacroInfo *OtherMI) {
556 Diag(OtherMI->getDefinitionLoc(), diag::note_pp_ambiguous_macro_other)
557 << Identifier.getIdentifierInfo();
561 // If we started lexing a macro, enter the macro expansion body.
563 // If this macro expands to no tokens, don't bother to push it onto the
564 // expansion stack, only to take it right back off.
565 if (MI->getNumTokens() == 0) {
566 // No need for arg info.
567 if (Args) Args->destroy(*this);
569 // Propagate whitespace info as if we had pushed, then popped,
571 Identifier.setFlag(Token::LeadingEmptyMacro);
572 PropagateLineStartLeadingSpaceInfo(Identifier);
573 ++NumFastMacroExpanded;
575 } else if (MI->getNumTokens() == 1 &&
576 isTrivialSingleTokenExpansion(MI, Identifier.getIdentifierInfo(),
578 // Otherwise, if this macro expands into a single trivially-expanded
579 // token: expand it now. This handles common cases like
582 // No need for arg info.
583 if (Args) Args->destroy(*this);
585 // Propagate the isAtStartOfLine/hasLeadingSpace markers of the macro
586 // identifier to the expanded token.
587 bool isAtStartOfLine = Identifier.isAtStartOfLine();
588 bool hasLeadingSpace = Identifier.hasLeadingSpace();
590 // Replace the result token.
591 Identifier = MI->getReplacementToken(0);
593 // Restore the StartOfLine/LeadingSpace markers.
594 Identifier.setFlagValue(Token::StartOfLine , isAtStartOfLine);
595 Identifier.setFlagValue(Token::LeadingSpace, hasLeadingSpace);
597 // Update the tokens location to include both its expansion and physical
600 SourceMgr.createExpansionLoc(Identifier.getLocation(), ExpandLoc,
601 ExpansionEnd,Identifier.getLength());
602 Identifier.setLocation(Loc);
604 // If this is a disabled macro or #define X X, we must mark the result as
606 if (IdentifierInfo *NewII = Identifier.getIdentifierInfo()) {
607 if (MacroInfo *NewMI = getMacroInfo(NewII))
608 if (!NewMI->isEnabled() || NewMI == MI) {
609 Identifier.setFlag(Token::DisableExpand);
610 // Don't warn for "#define X X" like "#define bool bool" from
612 if (NewMI != MI || MI->isFunctionLike())
613 Diag(Identifier, diag::pp_disabled_macro_expansion);
617 // Since this is not an identifier token, it can't be macro expanded, so
619 ++NumFastMacroExpanded;
623 // Start expanding the macro.
624 EnterMacro(Identifier, ExpansionEnd, MI, Args);
633 /// CheckMatchedBrackets - Returns true if the braces and parentheses in the
634 /// token vector are properly nested.
635 static bool CheckMatchedBrackets(const SmallVectorImpl<Token> &Tokens) {
636 SmallVector<Bracket, 8> Brackets;
637 for (SmallVectorImpl<Token>::const_iterator I = Tokens.begin(),
640 if (I->is(tok::l_paren)) {
641 Brackets.push_back(Paren);
642 } else if (I->is(tok::r_paren)) {
643 if (Brackets.empty() || Brackets.back() == Brace)
646 } else if (I->is(tok::l_brace)) {
647 Brackets.push_back(Brace);
648 } else if (I->is(tok::r_brace)) {
649 if (Brackets.empty() || Brackets.back() == Paren)
654 return Brackets.empty();
657 /// GenerateNewArgTokens - Returns true if OldTokens can be converted to a new
658 /// vector of tokens in NewTokens. The new number of arguments will be placed
659 /// in NumArgs and the ranges which need to surrounded in parentheses will be
661 /// Returns false if the token stream cannot be changed. If this is because
662 /// of an initializer list starting a macro argument, the range of those
663 /// initializer lists will be place in InitLists.
664 static bool GenerateNewArgTokens(Preprocessor &PP,
665 SmallVectorImpl<Token> &OldTokens,
666 SmallVectorImpl<Token> &NewTokens,
668 SmallVectorImpl<SourceRange> &ParenHints,
669 SmallVectorImpl<SourceRange> &InitLists) {
670 if (!CheckMatchedBrackets(OldTokens))
673 // Once it is known that the brackets are matched, only a simple count of the
677 // First token of a new macro argument.
678 SmallVectorImpl<Token>::iterator ArgStartIterator = OldTokens.begin();
680 // First closing brace in a new macro argument. Used to generate
681 // SourceRanges for InitLists.
682 SmallVectorImpl<Token>::iterator ClosingBrace = OldTokens.end();
685 // Set to true when a macro separator token is found inside a braced list.
686 // If true, the fixed argument spans multiple old arguments and ParenHints
688 bool FoundSeparatorToken = false;
689 for (SmallVectorImpl<Token>::iterator I = OldTokens.begin(),
692 if (I->is(tok::l_brace)) {
694 } else if (I->is(tok::r_brace)) {
696 if (Braces == 0 && ClosingBrace == E && FoundSeparatorToken)
698 } else if (I->is(tok::eof)) {
699 // EOF token is used to separate macro arguments
701 // Assume comma separator is actually braced list separator and change
702 // it back to a comma.
703 FoundSeparatorToken = true;
704 I->setKind(tok::comma);
706 } else { // Braces == 0
707 // Separator token still separates arguments.
710 // If the argument starts with a brace, it can't be fixed with
711 // parentheses. A different diagnostic will be given.
712 if (FoundSeparatorToken && ArgStartIterator->is(tok::l_brace)) {
714 SourceRange(ArgStartIterator->getLocation(),
715 PP.getLocForEndOfToken(ClosingBrace->getLocation())));
720 if (FoundSeparatorToken) {
721 TempToken.startToken();
722 TempToken.setKind(tok::l_paren);
723 TempToken.setLocation(ArgStartIterator->getLocation());
724 TempToken.setLength(0);
725 NewTokens.push_back(TempToken);
728 // Copy over argument tokens
729 NewTokens.insert(NewTokens.end(), ArgStartIterator, I);
731 // Add right paren and store the paren locations in ParenHints
732 if (FoundSeparatorToken) {
733 SourceLocation Loc = PP.getLocForEndOfToken((I - 1)->getLocation());
734 TempToken.startToken();
735 TempToken.setKind(tok::r_paren);
736 TempToken.setLocation(Loc);
737 TempToken.setLength(0);
738 NewTokens.push_back(TempToken);
739 ParenHints.push_back(SourceRange(ArgStartIterator->getLocation(),
743 // Copy separator token
744 NewTokens.push_back(*I);
747 ArgStartIterator = I + 1;
748 FoundSeparatorToken = false;
753 return !ParenHints.empty() && InitLists.empty();
756 /// ReadFunctionLikeMacroArgs - After reading "MACRO" and knowing that the next
757 /// token is the '(' of the macro, this method is invoked to read all of the
758 /// actual arguments specified for the macro invocation. This returns null on
760 MacroArgs *Preprocessor::ReadMacroCallArgumentList(Token &MacroName,
762 SourceLocation &MacroEnd) {
763 // The number of fixed arguments to parse.
764 unsigned NumFixedArgsLeft = MI->getNumParams();
765 bool isVariadic = MI->isVariadic();
767 // Outer loop, while there are more arguments, keep reading them.
770 // Read arguments as unexpanded tokens. This avoids issues, e.g., where
771 // an argument value in a macro could expand to ',' or '(' or ')'.
772 LexUnexpandedToken(Tok);
773 assert(Tok.is(tok::l_paren) && "Error computing l-paren-ness?");
775 // ArgTokens - Build up a list of tokens that make up each argument. Each
776 // argument is separated by an EOF token. Use a SmallVector so we can avoid
777 // heap allocations in the common case.
778 SmallVector<Token, 64> ArgTokens;
779 bool ContainsCodeCompletionTok = false;
780 bool FoundElidedComma = false;
782 SourceLocation TooManyArgsLoc;
784 unsigned NumActuals = 0;
785 while (Tok.isNot(tok::r_paren)) {
786 if (ContainsCodeCompletionTok && Tok.isOneOf(tok::eof, tok::eod))
789 assert(Tok.isOneOf(tok::l_paren, tok::comma) &&
790 "only expect argument separators here");
792 size_t ArgTokenStart = ArgTokens.size();
793 SourceLocation ArgStartLoc = Tok.getLocation();
795 // C99 6.10.3p11: Keep track of the number of l_parens we have seen. Note
796 // that we already consumed the first one.
797 unsigned NumParens = 0;
800 // Read arguments as unexpanded tokens. This avoids issues, e.g., where
801 // an argument value in a macro could expand to ',' or '(' or ')'.
802 LexUnexpandedToken(Tok);
804 if (Tok.isOneOf(tok::eof, tok::eod)) { // "#if f(<eof>" & "#if f(\n"
805 if (!ContainsCodeCompletionTok) {
806 Diag(MacroName, diag::err_unterm_macro_invoc);
807 Diag(MI->getDefinitionLoc(), diag::note_macro_here)
808 << MacroName.getIdentifierInfo();
809 // Do not lose the EOF/EOD. Return it to the client.
813 // Do not lose the EOF/EOD.
814 auto Toks = std::make_unique<Token[]>(1);
816 EnterTokenStream(std::move(Toks), 1, true, /*IsReinject*/ false);
818 } else if (Tok.is(tok::r_paren)) {
819 // If we found the ) token, the macro arg list is done.
820 if (NumParens-- == 0) {
821 MacroEnd = Tok.getLocation();
822 if (!ArgTokens.empty() &&
823 ArgTokens.back().commaAfterElided()) {
824 FoundElidedComma = true;
828 } else if (Tok.is(tok::l_paren)) {
830 } else if (Tok.is(tok::comma)) {
831 // In Microsoft-compatibility mode, single commas from nested macro
832 // expansions should not be considered as argument separators. We test
833 // for this with the IgnoredComma token flag.
834 if (Tok.getFlags() & Token::IgnoredComma) {
835 // However, in MSVC's preprocessor, subsequent expansions do treat
836 // these commas as argument separators. This leads to a common
837 // workaround used in macros that need to work in both MSVC and
838 // compliant preprocessors. Therefore, the IgnoredComma flag can only
839 // apply once to any given token.
840 Tok.clearFlag(Token::IgnoredComma);
841 } else if (NumParens == 0) {
842 // Comma ends this argument if there are more fixed arguments
843 // expected. However, if this is a variadic macro, and this is part of
844 // the variadic part, then the comma is just an argument token.
847 if (NumFixedArgsLeft > 1)
850 } else if (Tok.is(tok::comment) && !KeepMacroComments) {
851 // If this is a comment token in the argument list and we're just in
852 // -C mode (not -CC mode), discard the comment.
854 } else if (!Tok.isAnnotation() && Tok.getIdentifierInfo() != nullptr) {
855 // Reading macro arguments can cause macros that we are currently
856 // expanding from to be popped off the expansion stack. Doing so causes
857 // them to be reenabled for expansion. Here we record whether any
858 // identifiers we lex as macro arguments correspond to disabled macros.
859 // If so, we mark the token as noexpand. This is a subtle aspect of
861 if (MacroInfo *MI = getMacroInfo(Tok.getIdentifierInfo()))
862 if (!MI->isEnabled())
863 Tok.setFlag(Token::DisableExpand);
864 } else if (Tok.is(tok::code_completion)) {
865 ContainsCodeCompletionTok = true;
867 CodeComplete->CodeCompleteMacroArgument(MacroName.getIdentifierInfo(),
869 // Don't mark that we reached the code-completion point because the
870 // parser is going to handle the token and there will be another
871 // code-completion callback.
874 ArgTokens.push_back(Tok);
877 // If this was an empty argument list foo(), don't add this as an empty
879 if (ArgTokens.empty() && Tok.getKind() == tok::r_paren)
882 // If this is not a variadic macro, and too many args were specified, emit
884 if (!isVariadic && NumFixedArgsLeft == 0 && TooManyArgsLoc.isInvalid()) {
885 if (ArgTokens.size() != ArgTokenStart)
886 TooManyArgsLoc = ArgTokens[ArgTokenStart].getLocation();
888 TooManyArgsLoc = ArgStartLoc;
891 // Empty arguments are standard in C99 and C++0x, and are supported as an
892 // extension in other modes.
893 if (ArgTokens.size() == ArgTokenStart && !getLangOpts().C99)
894 Diag(Tok, getLangOpts().CPlusPlus11
895 ? diag::warn_cxx98_compat_empty_fnmacro_arg
896 : diag::ext_empty_fnmacro_arg);
898 // Add a marker EOF token to the end of the token list for this argument.
901 EOFTok.setKind(tok::eof);
902 EOFTok.setLocation(Tok.getLocation());
904 ArgTokens.push_back(EOFTok);
906 if (!ContainsCodeCompletionTok && NumFixedArgsLeft != 0)
910 // Okay, we either found the r_paren. Check to see if we parsed too few
912 unsigned MinArgsExpected = MI->getNumParams();
914 // If this is not a variadic macro, and too many args were specified, emit
916 if (!isVariadic && NumActuals > MinArgsExpected &&
917 !ContainsCodeCompletionTok) {
918 // Emit the diagnostic at the macro name in case there is a missing ).
919 // Emitting it at the , could be far away from the macro name.
920 Diag(TooManyArgsLoc, diag::err_too_many_args_in_macro_invoc);
921 Diag(MI->getDefinitionLoc(), diag::note_macro_here)
922 << MacroName.getIdentifierInfo();
924 // Commas from braced initializer lists will be treated as argument
925 // separators inside macros. Attempt to correct for this with parentheses.
926 // TODO: See if this can be generalized to angle brackets for templates
927 // inside macro arguments.
929 SmallVector<Token, 4> FixedArgTokens;
930 unsigned FixedNumArgs = 0;
931 SmallVector<SourceRange, 4> ParenHints, InitLists;
932 if (!GenerateNewArgTokens(*this, ArgTokens, FixedArgTokens, FixedNumArgs,
933 ParenHints, InitLists)) {
934 if (!InitLists.empty()) {
935 DiagnosticBuilder DB =
937 diag::note_init_list_at_beginning_of_macro_argument);
938 for (SourceRange Range : InitLists)
943 if (FixedNumArgs != MinArgsExpected)
946 DiagnosticBuilder DB = Diag(MacroName, diag::note_suggest_parens_for_macro);
947 for (SourceRange ParenLocation : ParenHints) {
948 DB << FixItHint::CreateInsertion(ParenLocation.getBegin(), "(");
949 DB << FixItHint::CreateInsertion(ParenLocation.getEnd(), ")");
951 ArgTokens.swap(FixedArgTokens);
952 NumActuals = FixedNumArgs;
955 // See MacroArgs instance var for description of this.
956 bool isVarargsElided = false;
958 if (ContainsCodeCompletionTok) {
959 // Recover from not-fully-formed macro invocation during code-completion.
962 EOFTok.setKind(tok::eof);
963 EOFTok.setLocation(Tok.getLocation());
965 for (; NumActuals < MinArgsExpected; ++NumActuals)
966 ArgTokens.push_back(EOFTok);
969 if (NumActuals < MinArgsExpected) {
970 // There are several cases where too few arguments is ok, handle them now.
971 if (NumActuals == 0 && MinArgsExpected == 1) {
972 // #define A(X) or #define A(...) ---> A()
974 // If there is exactly one argument, and that argument is missing,
975 // then we have an empty "()" argument empty list. This is fine, even if
976 // the macro expects one argument (the argument is just empty).
977 isVarargsElided = MI->isVariadic();
978 } else if ((FoundElidedComma || MI->isVariadic()) &&
979 (NumActuals+1 == MinArgsExpected || // A(x, ...) -> A(X)
980 (NumActuals == 0 && MinArgsExpected == 2))) {// A(x,...) -> A()
981 // Varargs where the named vararg parameter is missing: OK as extension.
985 // If the macro contains the comma pasting extension, the diagnostic
986 // is suppressed; we know we'll get another diagnostic later.
987 if (!MI->hasCommaPasting()) {
988 // C++20 allows this construct, but standards before C++20 and all C
989 // standards do not allow the construct (we allow it as an extension).
990 Diag(Tok, getLangOpts().CPlusPlus20
991 ? diag::warn_cxx17_compat_missing_varargs_arg
992 : diag::ext_missing_varargs_arg);
993 Diag(MI->getDefinitionLoc(), diag::note_macro_here)
994 << MacroName.getIdentifierInfo();
997 // Remember this occurred, allowing us to elide the comma when used for
999 // #define A(x, foo...) blah(a, ## foo)
1000 // #define B(x, ...) blah(a, ## __VA_ARGS__)
1001 // #define C(...) blah(a, ## __VA_ARGS__)
1003 isVarargsElided = true;
1004 } else if (!ContainsCodeCompletionTok) {
1005 // Otherwise, emit the error.
1006 Diag(Tok, diag::err_too_few_args_in_macro_invoc);
1007 Diag(MI->getDefinitionLoc(), diag::note_macro_here)
1008 << MacroName.getIdentifierInfo();
1012 // Add a marker EOF token to the end of the token list for this argument.
1013 SourceLocation EndLoc = Tok.getLocation();
1015 Tok.setKind(tok::eof);
1016 Tok.setLocation(EndLoc);
1018 ArgTokens.push_back(Tok);
1020 // If we expect two arguments, add both as empty.
1021 if (NumActuals == 0 && MinArgsExpected == 2)
1022 ArgTokens.push_back(Tok);
1024 } else if (NumActuals > MinArgsExpected && !MI->isVariadic() &&
1025 !ContainsCodeCompletionTok) {
1026 // Emit the diagnostic at the macro name in case there is a missing ).
1027 // Emitting it at the , could be far away from the macro name.
1028 Diag(MacroName, diag::err_too_many_args_in_macro_invoc);
1029 Diag(MI->getDefinitionLoc(), diag::note_macro_here)
1030 << MacroName.getIdentifierInfo();
1034 return MacroArgs::create(MI, ArgTokens, isVarargsElided, *this);
1037 /// Keeps macro expanded tokens for TokenLexers.
1039 /// Works like a stack; a TokenLexer adds the macro expanded tokens that is
1040 /// going to lex in the cache and when it finishes the tokens are removed
1041 /// from the end of the cache.
1042 Token *Preprocessor::cacheMacroExpandedTokens(TokenLexer *tokLexer,
1043 ArrayRef<Token> tokens) {
1048 size_t newIndex = MacroExpandedTokens.size();
1049 bool cacheNeedsToGrow = tokens.size() >
1050 MacroExpandedTokens.capacity()-MacroExpandedTokens.size();
1051 MacroExpandedTokens.append(tokens.begin(), tokens.end());
1053 if (cacheNeedsToGrow) {
1054 // Go through all the TokenLexers whose 'Tokens' pointer points in the
1055 // buffer and update the pointers to the (potential) new buffer array.
1056 for (const auto &Lexer : MacroExpandingLexersStack) {
1057 TokenLexer *prevLexer;
1059 std::tie(prevLexer, tokIndex) = Lexer;
1060 prevLexer->Tokens = MacroExpandedTokens.data() + tokIndex;
1064 MacroExpandingLexersStack.push_back(std::make_pair(tokLexer, newIndex));
1065 return MacroExpandedTokens.data() + newIndex;
1068 void Preprocessor::removeCachedMacroExpandedTokensOfLastLexer() {
1069 assert(!MacroExpandingLexersStack.empty());
1070 size_t tokIndex = MacroExpandingLexersStack.back().second;
1071 assert(tokIndex < MacroExpandedTokens.size());
1072 // Pop the cached macro expanded tokens from the end.
1073 MacroExpandedTokens.resize(tokIndex);
1074 MacroExpandingLexersStack.pop_back();
1077 /// ComputeDATE_TIME - Compute the current time, enter it into the specified
1078 /// scratch buffer, then return DATELoc/TIMELoc locations with the position of
1079 /// the identifier tokens inserted.
1080 static void ComputeDATE_TIME(SourceLocation &DATELoc, SourceLocation &TIMELoc,
1082 time_t TT = time(nullptr);
1083 struct tm *TM = localtime(&TT);
1085 static const char * const Months[] = {
1086 "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"
1090 SmallString<32> TmpBuffer;
1091 llvm::raw_svector_ostream TmpStream(TmpBuffer);
1092 TmpStream << llvm::format("\"%s %2d %4d\"", Months[TM->tm_mon],
1093 TM->tm_mday, TM->tm_year + 1900);
1095 TmpTok.startToken();
1096 PP.CreateString(TmpStream.str(), TmpTok);
1097 DATELoc = TmpTok.getLocation();
1101 SmallString<32> TmpBuffer;
1102 llvm::raw_svector_ostream TmpStream(TmpBuffer);
1103 TmpStream << llvm::format("\"%02d:%02d:%02d\"",
1104 TM->tm_hour, TM->tm_min, TM->tm_sec);
1106 TmpTok.startToken();
1107 PP.CreateString(TmpStream.str(), TmpTok);
1108 TIMELoc = TmpTok.getLocation();
1112 /// HasFeature - Return true if we recognize and implement the feature
1113 /// specified by the identifier as a standard language feature.
1114 static bool HasFeature(const Preprocessor &PP, StringRef Feature) {
1115 const LangOptions &LangOpts = PP.getLangOpts();
1117 // Normalize the feature name, __foo__ becomes foo.
1118 if (Feature.startswith("__") && Feature.endswith("__") && Feature.size() >= 4)
1119 Feature = Feature.substr(2, Feature.size() - 4);
1121 #define FEATURE(Name, Predicate) .Case(#Name, Predicate)
1122 return llvm::StringSwitch<bool>(Feature)
1123 #include "clang/Basic/Features.def"
1128 /// HasExtension - Return true if we recognize and implement the feature
1129 /// specified by the identifier, either as an extension or a standard language
1131 static bool HasExtension(const Preprocessor &PP, StringRef Extension) {
1132 if (HasFeature(PP, Extension))
1135 // If the use of an extension results in an error diagnostic, extensions are
1136 // effectively unavailable, so just return false here.
1137 if (PP.getDiagnostics().getExtensionHandlingBehavior() >=
1138 diag::Severity::Error)
1141 const LangOptions &LangOpts = PP.getLangOpts();
1143 // Normalize the extension name, __foo__ becomes foo.
1144 if (Extension.startswith("__") && Extension.endswith("__") &&
1145 Extension.size() >= 4)
1146 Extension = Extension.substr(2, Extension.size() - 4);
1148 // Because we inherit the feature list from HasFeature, this string switch
1149 // must be less restrictive than HasFeature's.
1150 #define EXTENSION(Name, Predicate) .Case(#Name, Predicate)
1151 return llvm::StringSwitch<bool>(Extension)
1152 #include "clang/Basic/Features.def"
1157 /// EvaluateHasIncludeCommon - Process a '__has_include("path")'
1158 /// or '__has_include_next("path")' expression.
1159 /// Returns true if successful.
1160 static bool EvaluateHasIncludeCommon(Token &Tok,
1161 IdentifierInfo *II, Preprocessor &PP,
1162 const DirectoryLookup *LookupFrom,
1163 const FileEntry *LookupFromFile) {
1164 // Save the location of the current token. If a '(' is later found, use
1165 // that location. If not, use the end of this location instead.
1166 SourceLocation LParenLoc = Tok.getLocation();
1168 // These expressions are only allowed within a preprocessor directive.
1169 if (!PP.isParsingIfOrElifDirective()) {
1170 PP.Diag(LParenLoc, diag::err_pp_directive_required) << II;
1171 // Return a valid identifier token.
1172 assert(Tok.is(tok::identifier));
1173 Tok.setIdentifierInfo(II);
1177 // Get '('. If we don't have a '(', try to form a header-name token.
1179 if (PP.LexHeaderName(Tok))
1181 } while (Tok.getKind() == tok::comment);
1183 // Ensure we have a '('.
1184 if (Tok.isNot(tok::l_paren)) {
1185 // No '(', use end of last token.
1186 LParenLoc = PP.getLocForEndOfToken(LParenLoc);
1187 PP.Diag(LParenLoc, diag::err_pp_expected_after) << II << tok::l_paren;
1188 // If the next token looks like a filename or the start of one,
1189 // assume it is and process it as such.
1190 if (Tok.isNot(tok::header_name))
1193 // Save '(' location for possible missing ')' message.
1194 LParenLoc = Tok.getLocation();
1195 if (PP.LexHeaderName(Tok))
1199 if (Tok.isNot(tok::header_name)) {
1200 PP.Diag(Tok.getLocation(), diag::err_pp_expects_filename);
1204 // Reserve a buffer to get the spelling.
1205 SmallString<128> FilenameBuffer;
1206 bool Invalid = false;
1207 StringRef Filename = PP.getSpelling(Tok, FilenameBuffer, &Invalid);
1211 SourceLocation FilenameLoc = Tok.getLocation();
1214 PP.LexNonComment(Tok);
1216 // Ensure we have a trailing ).
1217 if (Tok.isNot(tok::r_paren)) {
1218 PP.Diag(PP.getLocForEndOfToken(FilenameLoc), diag::err_pp_expected_after)
1219 << II << tok::r_paren;
1220 PP.Diag(LParenLoc, diag::note_matching) << tok::l_paren;
1224 bool isAngled = PP.GetIncludeFilenameSpelling(Tok.getLocation(), Filename);
1225 // If GetIncludeFilenameSpelling set the start ptr to null, there was an
1227 if (Filename.empty())
1230 // Search include directories.
1231 Optional<FileEntryRef> File =
1232 PP.LookupFile(FilenameLoc, Filename, isAngled, LookupFrom, LookupFromFile,
1233 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr);
1235 if (PPCallbacks *Callbacks = PP.getPPCallbacks()) {
1236 SrcMgr::CharacteristicKind FileType = SrcMgr::C_User;
1239 PP.getHeaderSearchInfo().getFileDirFlavor(&File->getFileEntry());
1240 Callbacks->HasInclude(FilenameLoc, Filename, isAngled, File, FileType);
1243 // Get the result value. A result of true means the file exists.
1244 return File.hasValue();
1247 /// EvaluateHasInclude - Process a '__has_include("path")' expression.
1248 /// Returns true if successful.
1249 static bool EvaluateHasInclude(Token &Tok, IdentifierInfo *II,
1251 return EvaluateHasIncludeCommon(Tok, II, PP, nullptr, nullptr);
1254 /// EvaluateHasIncludeNext - Process '__has_include_next("path")' expression.
1255 /// Returns true if successful.
1256 static bool EvaluateHasIncludeNext(Token &Tok,
1257 IdentifierInfo *II, Preprocessor &PP) {
1258 // __has_include_next is like __has_include, except that we start
1259 // searching after the current found directory. If we can't do this,
1260 // issue a diagnostic.
1261 // FIXME: Factor out duplication with
1262 // Preprocessor::HandleIncludeNextDirective.
1263 const DirectoryLookup *Lookup = PP.GetCurDirLookup();
1264 const FileEntry *LookupFromFile = nullptr;
1265 if (PP.isInPrimaryFile() && PP.getLangOpts().IsHeaderFile) {
1266 // If the main file is a header, then it's either for PCH/AST generation,
1267 // or libclang opened it. Either way, handle it as a normal include below
1268 // and do not complain about __has_include_next.
1269 } else if (PP.isInPrimaryFile()) {
1271 PP.Diag(Tok, diag::pp_include_next_in_primary);
1272 } else if (PP.getCurrentLexerSubmodule()) {
1273 // Start looking up in the directory *after* the one in which the current
1274 // file would be found, if any.
1275 assert(PP.getCurrentLexer() && "#include_next directive in macro?");
1276 LookupFromFile = PP.getCurrentLexer()->getFileEntry();
1278 } else if (!Lookup) {
1279 PP.Diag(Tok, diag::pp_include_next_absolute_path);
1281 // Start looking up in the next directory.
1285 return EvaluateHasIncludeCommon(Tok, II, PP, Lookup, LookupFromFile);
1288 /// Process single-argument builtin feature-like macros that return
1290 static void EvaluateFeatureLikeBuiltinMacro(llvm::raw_svector_ostream& OS,
1291 Token &Tok, IdentifierInfo *II,
1292 Preprocessor &PP, bool ExpandArgs,
1295 bool &HasLexedNextTok)> Op) {
1296 // Parse the initial '('.
1297 PP.LexUnexpandedToken(Tok);
1298 if (Tok.isNot(tok::l_paren)) {
1299 PP.Diag(Tok.getLocation(), diag::err_pp_expected_after) << II
1302 // Provide a dummy '0' value on output stream to elide further errors.
1303 if (!Tok.isOneOf(tok::eof, tok::eod)) {
1305 Tok.setKind(tok::numeric_constant);
1310 unsigned ParenDepth = 1;
1311 SourceLocation LParenLoc = Tok.getLocation();
1312 llvm::Optional<int> Result;
1315 bool SuppressDiagnostic = false;
1317 // Parse next token.
1321 PP.LexUnexpandedToken(Tok);
1324 switch (Tok.getKind()) {
1327 // Don't provide even a dummy value if the eod or eof marker is
1328 // reached. Simply provide a diagnostic.
1329 PP.Diag(Tok.getLocation(), diag::err_unterm_macro_invoc);
1333 if (!SuppressDiagnostic) {
1334 PP.Diag(Tok.getLocation(), diag::err_too_many_args_in_macro_invoc);
1335 SuppressDiagnostic = true;
1341 if (Result.hasValue())
1343 if (!SuppressDiagnostic) {
1344 PP.Diag(Tok.getLocation(), diag::err_pp_nested_paren) << II;
1345 SuppressDiagnostic = true;
1350 if (--ParenDepth > 0)
1353 // The last ')' has been reached; return the value if one found or
1354 // a diagnostic and a dummy value.
1355 if (Result.hasValue()) {
1356 OS << Result.getValue();
1357 // For strict conformance to __has_cpp_attribute rules, use 'L'
1358 // suffix for dated literals.
1359 if (Result.getValue() > 1)
1363 if (!SuppressDiagnostic)
1364 PP.Diag(Tok.getLocation(), diag::err_too_few_args_in_macro_invoc);
1366 Tok.setKind(tok::numeric_constant);
1370 // Parse the macro argument, if one not found so far.
1371 if (Result.hasValue())
1374 bool HasLexedNextToken = false;
1375 Result = Op(Tok, HasLexedNextToken);
1377 if (HasLexedNextToken)
1383 // Diagnose missing ')'.
1384 if (!SuppressDiagnostic) {
1385 if (auto Diag = PP.Diag(Tok.getLocation(), diag::err_pp_expected_after)) {
1386 if (IdentifierInfo *LastII = ResultTok.getIdentifierInfo())
1389 Diag << ResultTok.getKind();
1390 Diag << tok::r_paren << ResultTok.getLocation();
1392 PP.Diag(LParenLoc, diag::note_matching) << tok::l_paren;
1393 SuppressDiagnostic = true;
1398 /// Helper function to return the IdentifierInfo structure of a Token
1399 /// or generate a diagnostic if none available.
1400 static IdentifierInfo *ExpectFeatureIdentifierInfo(Token &Tok,
1404 if (!Tok.isAnnotation() && (II = Tok.getIdentifierInfo()))
1407 PP.Diag(Tok.getLocation(), DiagID);
1411 /// Implements the __is_target_arch builtin macro.
1412 static bool isTargetArch(const TargetInfo &TI, const IdentifierInfo *II) {
1413 std::string ArchName = II->getName().lower() + "--";
1414 llvm::Triple Arch(ArchName);
1415 const llvm::Triple &TT = TI.getTriple();
1417 // arm matches thumb or thumbv7. armv7 matches thumbv7.
1418 if ((Arch.getSubArch() == llvm::Triple::NoSubArch ||
1419 Arch.getSubArch() == TT.getSubArch()) &&
1420 ((TT.getArch() == llvm::Triple::thumb &&
1421 Arch.getArch() == llvm::Triple::arm) ||
1422 (TT.getArch() == llvm::Triple::thumbeb &&
1423 Arch.getArch() == llvm::Triple::armeb)))
1426 // Check the parsed arch when it has no sub arch to allow Clang to
1427 // match thumb to thumbv7 but to prohibit matching thumbv6 to thumbv7.
1428 return (Arch.getSubArch() == llvm::Triple::NoSubArch ||
1429 Arch.getSubArch() == TT.getSubArch()) &&
1430 Arch.getArch() == TT.getArch();
1433 /// Implements the __is_target_vendor builtin macro.
1434 static bool isTargetVendor(const TargetInfo &TI, const IdentifierInfo *II) {
1435 StringRef VendorName = TI.getTriple().getVendorName();
1436 if (VendorName.empty())
1437 VendorName = "unknown";
1438 return VendorName.equals_insensitive(II->getName());
1441 /// Implements the __is_target_os builtin macro.
1442 static bool isTargetOS(const TargetInfo &TI, const IdentifierInfo *II) {
1443 std::string OSName =
1444 (llvm::Twine("unknown-unknown-") + II->getName().lower()).str();
1445 llvm::Triple OS(OSName);
1446 if (OS.getOS() == llvm::Triple::Darwin) {
1447 // Darwin matches macos, ios, etc.
1448 return TI.getTriple().isOSDarwin();
1450 return TI.getTriple().getOS() == OS.getOS();
1453 /// Implements the __is_target_environment builtin macro.
1454 static bool isTargetEnvironment(const TargetInfo &TI,
1455 const IdentifierInfo *II) {
1456 std::string EnvName = (llvm::Twine("---") + II->getName().lower()).str();
1457 llvm::Triple Env(EnvName);
1458 return TI.getTriple().getEnvironment() == Env.getEnvironment();
1461 /// ExpandBuiltinMacro - If an identifier token is read that is to be expanded
1462 /// as a builtin macro, handle it and return the next token as 'Tok'.
1463 void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
1464 // Figure out which token this is.
1465 IdentifierInfo *II = Tok.getIdentifierInfo();
1466 assert(II && "Can't be a macro without id info!");
1468 // If this is an _Pragma or Microsoft __pragma directive, expand it,
1469 // invoke the pragma handler, then lex the token after it.
1470 if (II == Ident_Pragma)
1471 return Handle_Pragma(Tok);
1472 else if (II == Ident__pragma) // in non-MS mode this is null
1473 return HandleMicrosoft__pragma(Tok);
1475 ++NumBuiltinMacroExpanded;
1477 SmallString<128> TmpBuffer;
1478 llvm::raw_svector_ostream OS(TmpBuffer);
1480 // Set up the return result.
1481 Tok.setIdentifierInfo(nullptr);
1482 Tok.clearFlag(Token::NeedsCleaning);
1483 bool IsAtStartOfLine = Tok.isAtStartOfLine();
1484 bool HasLeadingSpace = Tok.hasLeadingSpace();
1486 if (II == Ident__LINE__) {
1487 // C99 6.10.8: "__LINE__: The presumed line number (within the current
1488 // source file) of the current source line (an integer constant)". This can
1489 // be affected by #line.
1490 SourceLocation Loc = Tok.getLocation();
1492 // Advance to the location of the first _, this might not be the first byte
1493 // of the token if it starts with an escaped newline.
1494 Loc = AdvanceToTokenCharacter(Loc, 0);
1496 // One wrinkle here is that GCC expands __LINE__ to location of the *end* of
1497 // a macro expansion. This doesn't matter for object-like macros, but
1498 // can matter for a function-like macro that expands to contain __LINE__.
1499 // Skip down through expansion points until we find a file loc for the
1500 // end of the expansion history.
1501 Loc = SourceMgr.getExpansionRange(Loc).getEnd();
1502 PresumedLoc PLoc = SourceMgr.getPresumedLoc(Loc);
1504 // __LINE__ expands to a simple numeric value.
1505 OS << (PLoc.isValid()? PLoc.getLine() : 1);
1506 Tok.setKind(tok::numeric_constant);
1507 } else if (II == Ident__FILE__ || II == Ident__BASE_FILE__ ||
1508 II == Ident__FILE_NAME__) {
1509 // C99 6.10.8: "__FILE__: The presumed name of the current source file (a
1510 // character string literal)". This can be affected by #line.
1511 PresumedLoc PLoc = SourceMgr.getPresumedLoc(Tok.getLocation());
1513 // __BASE_FILE__ is a GNU extension that returns the top of the presumed
1514 // #include stack instead of the current file.
1515 if (II == Ident__BASE_FILE__ && PLoc.isValid()) {
1516 SourceLocation NextLoc = PLoc.getIncludeLoc();
1517 while (NextLoc.isValid()) {
1518 PLoc = SourceMgr.getPresumedLoc(NextLoc);
1519 if (PLoc.isInvalid())
1522 NextLoc = PLoc.getIncludeLoc();
1526 // Escape this filename. Turn '\' -> '\\' '"' -> '\"'
1527 SmallString<256> FN;
1528 if (PLoc.isValid()) {
1529 // __FILE_NAME__ is a Clang-specific extension that expands to the
1530 // the last part of __FILE__.
1531 if (II == Ident__FILE_NAME__) {
1532 // Try to get the last path component, failing that return the original
1533 // presumed location.
1534 StringRef PLFileName = llvm::sys::path::filename(PLoc.getFilename());
1535 if (PLFileName != "")
1538 FN += PLoc.getFilename();
1540 FN += PLoc.getFilename();
1542 getLangOpts().remapPathPrefix(FN);
1543 Lexer::Stringify(FN);
1544 OS << '"' << FN << '"';
1546 Tok.setKind(tok::string_literal);
1547 } else if (II == Ident__DATE__) {
1548 Diag(Tok.getLocation(), diag::warn_pp_date_time);
1549 if (!DATELoc.isValid())
1550 ComputeDATE_TIME(DATELoc, TIMELoc, *this);
1551 Tok.setKind(tok::string_literal);
1552 Tok.setLength(strlen("\"Mmm dd yyyy\""));
1553 Tok.setLocation(SourceMgr.createExpansionLoc(DATELoc, Tok.getLocation(),
1557 } else if (II == Ident__TIME__) {
1558 Diag(Tok.getLocation(), diag::warn_pp_date_time);
1559 if (!TIMELoc.isValid())
1560 ComputeDATE_TIME(DATELoc, TIMELoc, *this);
1561 Tok.setKind(tok::string_literal);
1562 Tok.setLength(strlen("\"hh:mm:ss\""));
1563 Tok.setLocation(SourceMgr.createExpansionLoc(TIMELoc, Tok.getLocation(),
1567 } else if (II == Ident__INCLUDE_LEVEL__) {
1568 // Compute the presumed include depth of this token. This can be affected
1569 // by GNU line markers.
1572 PresumedLoc PLoc = SourceMgr.getPresumedLoc(Tok.getLocation());
1573 if (PLoc.isValid()) {
1574 PLoc = SourceMgr.getPresumedLoc(PLoc.getIncludeLoc());
1575 for (; PLoc.isValid(); ++Depth)
1576 PLoc = SourceMgr.getPresumedLoc(PLoc.getIncludeLoc());
1579 // __INCLUDE_LEVEL__ expands to a simple numeric value.
1581 Tok.setKind(tok::numeric_constant);
1582 } else if (II == Ident__TIMESTAMP__) {
1583 Diag(Tok.getLocation(), diag::warn_pp_date_time);
1584 // MSVC, ICC, GCC, VisualAge C++ extension. The generated string should be
1585 // of the form "Ddd Mmm dd hh::mm::ss yyyy", which is returned by asctime.
1587 // Get the file that we are lexing out of. If we're currently lexing from
1588 // a macro, dig into the include stack.
1589 const FileEntry *CurFile = nullptr;
1590 PreprocessorLexer *TheLexer = getCurrentFileLexer();
1593 CurFile = SourceMgr.getFileEntryForID(TheLexer->getFileID());
1597 time_t TT = CurFile->getModificationTime();
1598 struct tm *TM = localtime(&TT);
1599 Result = asctime(TM);
1601 Result = "??? ??? ?? ??:??:?? ????\n";
1603 // Surround the string with " and strip the trailing newline.
1604 OS << '"' << StringRef(Result).drop_back() << '"';
1605 Tok.setKind(tok::string_literal);
1606 } else if (II == Ident__COUNTER__) {
1607 // __COUNTER__ expands to a simple numeric value.
1608 OS << CounterValue++;
1609 Tok.setKind(tok::numeric_constant);
1610 } else if (II == Ident__has_feature) {
1611 EvaluateFeatureLikeBuiltinMacro(OS, Tok, II, *this, false,
1612 [this](Token &Tok, bool &HasLexedNextToken) -> int {
1613 IdentifierInfo *II = ExpectFeatureIdentifierInfo(Tok, *this,
1614 diag::err_feature_check_malformed);
1615 return II && HasFeature(*this, II->getName());
1617 } else if (II == Ident__has_extension) {
1618 EvaluateFeatureLikeBuiltinMacro(OS, Tok, II, *this, false,
1619 [this](Token &Tok, bool &HasLexedNextToken) -> int {
1620 IdentifierInfo *II = ExpectFeatureIdentifierInfo(Tok, *this,
1621 diag::err_feature_check_malformed);
1622 return II && HasExtension(*this, II->getName());
1624 } else if (II == Ident__has_builtin) {
1625 EvaluateFeatureLikeBuiltinMacro(OS, Tok, II, *this, false,
1626 [this](Token &Tok, bool &HasLexedNextToken) -> int {
1627 IdentifierInfo *II = ExpectFeatureIdentifierInfo(Tok, *this,
1628 diag::err_feature_check_malformed);
1631 else if (II->getBuiltinID() != 0) {
1632 switch (II->getBuiltinID()) {
1633 case Builtin::BI__builtin_operator_new:
1634 case Builtin::BI__builtin_operator_delete:
1635 // denotes date of behavior change to support calling arbitrary
1636 // usual allocation and deallocation functions. Required by libc++
1642 } else if (II->getTokenID() != tok::identifier ||
1643 II->hasRevertedTokenIDToIdentifier()) {
1644 // Treat all keywords that introduce a custom syntax of the form
1646 // '__some_keyword' '(' [...] ')'
1648 // as being "builtin functions", even if the syntax isn't a valid
1649 // function call (for example, because the builtin takes a type
1651 if (II->getName().startswith("__builtin_") ||
1652 II->getName().startswith("__is_") ||
1653 II->getName().startswith("__has_"))
1655 return llvm::StringSwitch<bool>(II->getName())
1656 .Case("__array_rank", true)
1657 .Case("__array_extent", true)
1658 .Case("__reference_binds_to_temporary", true)
1659 .Case("__underlying_type", true)
1662 return llvm::StringSwitch<bool>(II->getName())
1663 // Report builtin templates as being builtins.
1664 .Case("__make_integer_seq", getLangOpts().CPlusPlus)
1665 .Case("__type_pack_element", getLangOpts().CPlusPlus)
1666 // Likewise for some builtin preprocessor macros.
1667 // FIXME: This is inconsistent; we usually suggest detecting
1668 // builtin macros via #ifdef. Don't add more cases here.
1669 .Case("__is_target_arch", true)
1670 .Case("__is_target_vendor", true)
1671 .Case("__is_target_os", true)
1672 .Case("__is_target_environment", true)
1676 } else if (II == Ident__is_identifier) {
1677 EvaluateFeatureLikeBuiltinMacro(OS, Tok, II, *this, false,
1678 [](Token &Tok, bool &HasLexedNextToken) -> int {
1679 return Tok.is(tok::identifier);
1681 } else if (II == Ident__has_attribute) {
1682 EvaluateFeatureLikeBuiltinMacro(OS, Tok, II, *this, true,
1683 [this](Token &Tok, bool &HasLexedNextToken) -> int {
1684 IdentifierInfo *II = ExpectFeatureIdentifierInfo(Tok, *this,
1685 diag::err_feature_check_malformed);
1686 return II ? hasAttribute(AttrSyntax::GNU, nullptr, II,
1687 getTargetInfo(), getLangOpts()) : 0;
1689 } else if (II == Ident__has_declspec) {
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);
1695 const LangOptions &LangOpts = getLangOpts();
1696 return LangOpts.DeclSpecKeyword &&
1697 hasAttribute(AttrSyntax::Declspec, nullptr, II,
1698 getTargetInfo(), LangOpts);
1703 } else if (II == Ident__has_cpp_attribute ||
1704 II == Ident__has_c_attribute) {
1705 bool IsCXX = II == Ident__has_cpp_attribute;
1706 EvaluateFeatureLikeBuiltinMacro(OS, Tok, II, *this, true,
1707 [&](Token &Tok, bool &HasLexedNextToken) -> int {
1708 IdentifierInfo *ScopeII = nullptr;
1709 IdentifierInfo *II = ExpectFeatureIdentifierInfo(
1710 Tok, *this, diag::err_feature_check_malformed);
1714 // It is possible to receive a scope token. Read the "::", if it is
1715 // available, and the subsequent identifier.
1716 LexUnexpandedToken(Tok);
1717 if (Tok.isNot(tok::coloncolon))
1718 HasLexedNextToken = true;
1721 // Lex an expanded token for the attribute name.
1723 II = ExpectFeatureIdentifierInfo(Tok, *this,
1724 diag::err_feature_check_malformed);
1727 AttrSyntax Syntax = IsCXX ? AttrSyntax::CXX : AttrSyntax::C;
1728 return II ? hasAttribute(Syntax, ScopeII, II, getTargetInfo(),
1732 } else if (II == Ident__has_include ||
1733 II == Ident__has_include_next) {
1734 // The argument to these two builtins should be a parenthesized
1735 // file name string literal using angle brackets (<>) or
1736 // double-quotes ("").
1738 if (II == Ident__has_include)
1739 Value = EvaluateHasInclude(Tok, II, *this);
1741 Value = EvaluateHasIncludeNext(Tok, II, *this);
1743 if (Tok.isNot(tok::r_paren))
1746 Tok.setKind(tok::numeric_constant);
1747 } else if (II == Ident__has_warning) {
1748 // The argument should be a parenthesized string literal.
1749 EvaluateFeatureLikeBuiltinMacro(OS, Tok, II, *this, false,
1750 [this](Token &Tok, bool &HasLexedNextToken) -> int {
1751 std::string WarningName;
1752 SourceLocation StrStartLoc = Tok.getLocation();
1754 HasLexedNextToken = Tok.is(tok::string_literal);
1755 if (!FinishLexStringLiteral(Tok, WarningName, "'__has_warning'",
1756 /*AllowMacroExpansion=*/false))
1759 // FIXME: Should we accept "-R..." flags here, or should that be
1760 // handled by a separate __has_remark?
1761 if (WarningName.size() < 3 || WarningName[0] != '-' ||
1762 WarningName[1] != 'W') {
1763 Diag(StrStartLoc, diag::warn_has_warning_invalid_option);
1767 // Finally, check if the warning flags maps to a diagnostic group.
1768 // We construct a SmallVector here to talk to getDiagnosticIDs().
1769 // Although we don't use the result, this isn't a hot path, and not
1770 // worth special casing.
1771 SmallVector<diag::kind, 10> Diags;
1772 return !getDiagnostics().getDiagnosticIDs()->
1773 getDiagnosticsInGroup(diag::Flavor::WarningOrError,
1774 WarningName.substr(2), Diags);
1776 } else if (II == Ident__building_module) {
1777 // The argument to this builtin should be an identifier. The
1778 // builtin evaluates to 1 when that identifier names the module we are
1779 // currently building.
1780 EvaluateFeatureLikeBuiltinMacro(OS, Tok, II, *this, false,
1781 [this](Token &Tok, bool &HasLexedNextToken) -> int {
1782 IdentifierInfo *II = ExpectFeatureIdentifierInfo(Tok, *this,
1783 diag::err_expected_id_building_module);
1784 return getLangOpts().isCompilingModule() && II &&
1785 (II->getName() == getLangOpts().CurrentModule);
1787 } else if (II == Ident__MODULE__) {
1788 // The current module as an identifier.
1789 OS << getLangOpts().CurrentModule;
1790 IdentifierInfo *ModuleII = getIdentifierInfo(getLangOpts().CurrentModule);
1791 Tok.setIdentifierInfo(ModuleII);
1792 Tok.setKind(ModuleII->getTokenID());
1793 } else if (II == Ident__identifier) {
1794 SourceLocation Loc = Tok.getLocation();
1796 // We're expecting '__identifier' '(' identifier ')'. Try to recover
1797 // if the parens are missing.
1799 if (Tok.isNot(tok::l_paren)) {
1800 // No '(', use end of last token.
1801 Diag(getLocForEndOfToken(Loc), diag::err_pp_expected_after)
1802 << II << tok::l_paren;
1803 // If the next token isn't valid as our argument, we can't recover.
1804 if (!Tok.isAnnotation() && Tok.getIdentifierInfo())
1805 Tok.setKind(tok::identifier);
1809 SourceLocation LParenLoc = Tok.getLocation();
1812 if (!Tok.isAnnotation() && Tok.getIdentifierInfo())
1813 Tok.setKind(tok::identifier);
1814 else if (Tok.is(tok::string_literal) && !Tok.hasUDSuffix()) {
1815 StringLiteralParser Literal(Tok, *this);
1816 if (Literal.hadError)
1819 Tok.setIdentifierInfo(getIdentifierInfo(Literal.GetString()));
1820 Tok.setKind(tok::identifier);
1822 Diag(Tok.getLocation(), diag::err_pp_identifier_arg_not_identifier)
1824 // Don't walk past anything that's not a real token.
1825 if (Tok.isOneOf(tok::eof, tok::eod) || Tok.isAnnotation())
1829 // Discard the ')', preserving 'Tok' as our result.
1831 LexNonComment(RParen);
1832 if (RParen.isNot(tok::r_paren)) {
1833 Diag(getLocForEndOfToken(Tok.getLocation()), diag::err_pp_expected_after)
1834 << Tok.getKind() << tok::r_paren;
1835 Diag(LParenLoc, diag::note_matching) << tok::l_paren;
1838 } else if (II == Ident__is_target_arch) {
1839 EvaluateFeatureLikeBuiltinMacro(
1840 OS, Tok, II, *this, false,
1841 [this](Token &Tok, bool &HasLexedNextToken) -> int {
1842 IdentifierInfo *II = ExpectFeatureIdentifierInfo(
1843 Tok, *this, diag::err_feature_check_malformed);
1844 return II && isTargetArch(getTargetInfo(), II);
1846 } else if (II == Ident__is_target_vendor) {
1847 EvaluateFeatureLikeBuiltinMacro(
1848 OS, Tok, II, *this, false,
1849 [this](Token &Tok, bool &HasLexedNextToken) -> int {
1850 IdentifierInfo *II = ExpectFeatureIdentifierInfo(
1851 Tok, *this, diag::err_feature_check_malformed);
1852 return II && isTargetVendor(getTargetInfo(), II);
1854 } else if (II == Ident__is_target_os) {
1855 EvaluateFeatureLikeBuiltinMacro(
1856 OS, Tok, II, *this, false,
1857 [this](Token &Tok, bool &HasLexedNextToken) -> int {
1858 IdentifierInfo *II = ExpectFeatureIdentifierInfo(
1859 Tok, *this, diag::err_feature_check_malformed);
1860 return II && isTargetOS(getTargetInfo(), II);
1862 } else if (II == Ident__is_target_environment) {
1863 EvaluateFeatureLikeBuiltinMacro(
1864 OS, Tok, II, *this, false,
1865 [this](Token &Tok, bool &HasLexedNextToken) -> int {
1866 IdentifierInfo *II = ExpectFeatureIdentifierInfo(
1867 Tok, *this, diag::err_feature_check_malformed);
1868 return II && isTargetEnvironment(getTargetInfo(), II);
1871 llvm_unreachable("Unknown identifier!");
1873 CreateString(OS.str(), Tok, Tok.getLocation(), Tok.getLocation());
1874 Tok.setFlagValue(Token::StartOfLine, IsAtStartOfLine);
1875 Tok.setFlagValue(Token::LeadingSpace, HasLeadingSpace);
1878 void Preprocessor::markMacroAsUsed(MacroInfo *MI) {
1879 // If the 'used' status changed, and the macro requires 'unused' warning,
1880 // remove its SourceLocation from the warn-for-unused-macro locations.
1881 if (MI->isWarnIfUnused() && !MI->isUsed())
1882 WarnUnusedMacroLocs.erase(MI->getDefinitionLoc());
1883 MI->setIsUsed(true);