-//===--- MacroExpansion.cpp - Top level Macro Expansion -------------------===//
-//
-// The LLVM Compiler Infrastructure
-//
-// This file is distributed under the University of Illinois Open Source
-// License. See LICENSE.TXT for details.
-//
-//===----------------------------------------------------------------------===//
-//
-// This file implements the top level handling of macro expansion for the
-// preprocessor.
-//
-//===----------------------------------------------------------------------===//
-
-#include "clang/Basic/Attributes.h"
-#include "clang/Basic/FileManager.h"
-#include "clang/Basic/IdentifierTable.h"
-#include "clang/Basic/LLVM.h"
-#include "clang/Basic/LangOptions.h"
-#include "clang/Basic/ObjCRuntime.h"
-#include "clang/Basic/SourceLocation.h"
-#include "clang/Basic/TargetInfo.h"
-#include "clang/Lex/CodeCompletionHandler.h"
-#include "clang/Lex/DirectoryLookup.h"
-#include "clang/Lex/ExternalPreprocessorSource.h"
-#include "clang/Lex/LexDiagnostic.h"
-#include "clang/Lex/MacroArgs.h"
-#include "clang/Lex/MacroInfo.h"
-#include "clang/Lex/Preprocessor.h"
-#include "clang/Lex/PreprocessorLexer.h"
-#include "clang/Lex/PTHLexer.h"
-#include "clang/Lex/Token.h"
-#include "llvm/ADT/ArrayRef.h"
-#include "llvm/ADT/DenseMap.h"
-#include "llvm/ADT/DenseSet.h"
-#include "llvm/ADT/FoldingSet.h"
-#include "llvm/ADT/None.h"
-#include "llvm/ADT/Optional.h"
-#include "llvm/ADT/SmallString.h"
-#include "llvm/ADT/SmallVector.h"
-#include "llvm/ADT/STLExtras.h"
-#include "llvm/ADT/StringRef.h"
-#include "llvm/ADT/StringSwitch.h"
-#include "llvm/Config/llvm-config.h"
-#include "llvm/Support/Casting.h"
-#include "llvm/Support/ErrorHandling.h"
-#include "llvm/Support/Format.h"
-#include "llvm/Support/raw_ostream.h"
-#include <algorithm>
-#include <cassert>
-#include <cstddef>
-#include <cstring>
-#include <ctime>
-#include <string>
-#include <tuple>
-#include <utility>
-
-using namespace clang;
-
-MacroDirective *
-Preprocessor::getLocalMacroDirectiveHistory(const IdentifierInfo *II) const {
- if (!II->hadMacroDefinition())
- return nullptr;
- auto Pos = CurSubmoduleState->Macros.find(II);
- return Pos == CurSubmoduleState->Macros.end() ? nullptr
- : Pos->second.getLatest();
-}
-
-void Preprocessor::appendMacroDirective(IdentifierInfo *II, MacroDirective *MD){
- assert(MD && "MacroDirective should be non-zero!");
- assert(!MD->getPrevious() && "Already attached to a MacroDirective history.");
-
- MacroState &StoredMD = CurSubmoduleState->Macros[II];
- auto *OldMD = StoredMD.getLatest();
- MD->setPrevious(OldMD);
- StoredMD.setLatest(MD);
- StoredMD.overrideActiveModuleMacros(*this, II);
-
- if (needModuleMacros()) {
- // Track that we created a new macro directive, so we know we should
- // consider building a ModuleMacro for it when we get to the end of
- // the module.
- PendingModuleMacroNames.push_back(II);
- }
-
- // Set up the identifier as having associated macro history.
- II->setHasMacroDefinition(true);
- if (!MD->isDefined() && LeafModuleMacros.find(II) == LeafModuleMacros.end())
- II->setHasMacroDefinition(false);
- if (II->isFromAST())
- II->setChangedSinceDeserialization();
-}
-
-void Preprocessor::setLoadedMacroDirective(IdentifierInfo *II,
- MacroDirective *ED,
- MacroDirective *MD) {
- // Normally, when a macro is defined, it goes through appendMacroDirective()
- // above, which chains a macro to previous defines, undefs, etc.
- // However, in a pch, the whole macro history up to the end of the pch is
- // stored, so ASTReader goes through this function instead.
- // However, built-in macros are already registered in the Preprocessor
- // ctor, and ASTWriter stops writing the macro chain at built-in macros,
- // so in that case the chain from the pch needs to be spliced to the existing
- // built-in.
-
- assert(II && MD);
- MacroState &StoredMD = CurSubmoduleState->Macros[II];
-
- if (auto *OldMD = StoredMD.getLatest()) {
- // shouldIgnoreMacro() in ASTWriter also stops at macros from the
- // predefines buffer in module builds. However, in module builds, modules
- // are loaded completely before predefines are processed, so StoredMD
- // will be nullptr for them when they're loaded. StoredMD should only be
- // non-nullptr for builtins read from a pch file.
- assert(OldMD->getMacroInfo()->isBuiltinMacro() &&
- "only built-ins should have an entry here");
- assert(!OldMD->getPrevious() && "builtin should only have a single entry");
- ED->setPrevious(OldMD);
- StoredMD.setLatest(MD);
- } else {
- StoredMD = MD;
- }
-
- // Setup the identifier as having associated macro history.
- II->setHasMacroDefinition(true);
- if (!MD->isDefined() && LeafModuleMacros.find(II) == LeafModuleMacros.end())
- II->setHasMacroDefinition(false);
-}
-
-ModuleMacro *Preprocessor::addModuleMacro(Module *Mod, IdentifierInfo *II,
- MacroInfo *Macro,
- ArrayRef<ModuleMacro *> Overrides,
- bool &New) {
- llvm::FoldingSetNodeID ID;
- ModuleMacro::Profile(ID, Mod, II);
-
- void *InsertPos;
- if (auto *MM = ModuleMacros.FindNodeOrInsertPos(ID, InsertPos)) {
- New = false;
- return MM;
- }
-
- auto *MM = ModuleMacro::create(*this, Mod, II, Macro, Overrides);
- ModuleMacros.InsertNode(MM, InsertPos);
-
- // Each overridden macro is now overridden by one more macro.
- bool HidAny = false;
- for (auto *O : Overrides) {
- HidAny |= (O->NumOverriddenBy == 0);
- ++O->NumOverriddenBy;
- }
-
- // If we were the first overrider for any macro, it's no longer a leaf.
- auto &LeafMacros = LeafModuleMacros[II];
- if (HidAny) {
- LeafMacros.erase(std::remove_if(LeafMacros.begin(), LeafMacros.end(),
- [](ModuleMacro *MM) {
- return MM->NumOverriddenBy != 0;
- }),
- LeafMacros.end());
- }
-
- // The new macro is always a leaf macro.
- LeafMacros.push_back(MM);
- // The identifier now has defined macros (that may or may not be visible).
- II->setHasMacroDefinition(true);
-
- New = true;
- return MM;
-}
-
-ModuleMacro *Preprocessor::getModuleMacro(Module *Mod, IdentifierInfo *II) {
- llvm::FoldingSetNodeID ID;
- ModuleMacro::Profile(ID, Mod, II);
-
- void *InsertPos;
- return ModuleMacros.FindNodeOrInsertPos(ID, InsertPos);
-}
-
-void Preprocessor::updateModuleMacroInfo(const IdentifierInfo *II,
- ModuleMacroInfo &Info) {
- assert(Info.ActiveModuleMacrosGeneration !=
- CurSubmoduleState->VisibleModules.getGeneration() &&
- "don't need to update this macro name info");
- Info.ActiveModuleMacrosGeneration =
- CurSubmoduleState->VisibleModules.getGeneration();
-
- auto Leaf = LeafModuleMacros.find(II);
- if (Leaf == LeafModuleMacros.end()) {
- // No imported macros at all: nothing to do.
- return;
- }
-
- Info.ActiveModuleMacros.clear();
-
- // Every macro that's locally overridden is overridden by a visible macro.
- llvm::DenseMap<ModuleMacro *, int> NumHiddenOverrides;
- for (auto *O : Info.OverriddenMacros)
- NumHiddenOverrides[O] = -1;
-
- // Collect all macros that are not overridden by a visible macro.
- llvm::SmallVector<ModuleMacro *, 16> Worklist;
- for (auto *LeafMM : Leaf->second) {
- assert(LeafMM->getNumOverridingMacros() == 0 && "leaf macro overridden");
- if (NumHiddenOverrides.lookup(LeafMM) == 0)
- Worklist.push_back(LeafMM);
- }
- while (!Worklist.empty()) {
- auto *MM = Worklist.pop_back_val();
- if (CurSubmoduleState->VisibleModules.isVisible(MM->getOwningModule())) {
- // We only care about collecting definitions; undefinitions only act
- // to override other definitions.
- if (MM->getMacroInfo())
- Info.ActiveModuleMacros.push_back(MM);
- } else {
- for (auto *O : MM->overrides())
- if ((unsigned)++NumHiddenOverrides[O] == O->getNumOverridingMacros())
- Worklist.push_back(O);
- }
- }
- // Our reverse postorder walk found the macros in reverse order.
- std::reverse(Info.ActiveModuleMacros.begin(), Info.ActiveModuleMacros.end());
-
- // Determine whether the macro name is ambiguous.
- MacroInfo *MI = nullptr;
- bool IsSystemMacro = true;
- bool IsAmbiguous = false;
- if (auto *MD = Info.MD) {
- while (MD && isa<VisibilityMacroDirective>(MD))
- MD = MD->getPrevious();
- if (auto *DMD = dyn_cast_or_null<DefMacroDirective>(MD)) {
- MI = DMD->getInfo();
- IsSystemMacro &= SourceMgr.isInSystemHeader(DMD->getLocation());
- }
- }
- for (auto *Active : Info.ActiveModuleMacros) {
- auto *NewMI = Active->getMacroInfo();
-
- // Before marking the macro as ambiguous, check if this is a case where
- // both macros are in system headers. If so, we trust that the system
- // did not get it wrong. This also handles cases where Clang's own
- // headers have a different spelling of certain system macros:
- // #define LONG_MAX __LONG_MAX__ (clang's limits.h)
- // #define LONG_MAX 0x7fffffffffffffffL (system's limits.h)
- //
- // FIXME: Remove the defined-in-system-headers check. clang's limits.h
- // overrides the system limits.h's macros, so there's no conflict here.
- if (MI && NewMI != MI &&
- !MI->isIdenticalTo(*NewMI, *this, /*Syntactically=*/true))
- IsAmbiguous = true;
- IsSystemMacro &= Active->getOwningModule()->IsSystem ||
- SourceMgr.isInSystemHeader(NewMI->getDefinitionLoc());
- MI = NewMI;
- }
- Info.IsAmbiguous = IsAmbiguous && !IsSystemMacro;
-}
-
-void Preprocessor::dumpMacroInfo(const IdentifierInfo *II) {
- ArrayRef<ModuleMacro*> Leaf;
- auto LeafIt = LeafModuleMacros.find(II);
- if (LeafIt != LeafModuleMacros.end())
- Leaf = LeafIt->second;
- const MacroState *State = nullptr;
- auto Pos = CurSubmoduleState->Macros.find(II);
- if (Pos != CurSubmoduleState->Macros.end())
- State = &Pos->second;
-
- llvm::errs() << "MacroState " << State << " " << II->getNameStart();
- if (State && State->isAmbiguous(*this, II))
- llvm::errs() << " ambiguous";
- if (State && !State->getOverriddenMacros().empty()) {
- llvm::errs() << " overrides";
- for (auto *O : State->getOverriddenMacros())
- llvm::errs() << " " << O->getOwningModule()->getFullModuleName();
- }
- llvm::errs() << "\n";
-
- // Dump local macro directives.
- for (auto *MD = State ? State->getLatest() : nullptr; MD;
- MD = MD->getPrevious()) {
- llvm::errs() << " ";
- MD->dump();
- }
-
- // Dump module macros.
- llvm::DenseSet<ModuleMacro*> Active;
- for (auto *MM : State ? State->getActiveModuleMacros(*this, II) : None)
- Active.insert(MM);
- llvm::DenseSet<ModuleMacro*> Visited;
- llvm::SmallVector<ModuleMacro *, 16> Worklist(Leaf.begin(), Leaf.end());
- while (!Worklist.empty()) {
- auto *MM = Worklist.pop_back_val();
- llvm::errs() << " ModuleMacro " << MM << " "
- << MM->getOwningModule()->getFullModuleName();
- if (!MM->getMacroInfo())
- llvm::errs() << " undef";
-
- if (Active.count(MM))
- llvm::errs() << " active";
- else if (!CurSubmoduleState->VisibleModules.isVisible(
- MM->getOwningModule()))
- llvm::errs() << " hidden";
- else if (MM->getMacroInfo())
- llvm::errs() << " overridden";
-
- if (!MM->overrides().empty()) {
- llvm::errs() << " overrides";
- for (auto *O : MM->overrides()) {
- llvm::errs() << " " << O->getOwningModule()->getFullModuleName();
- if (Visited.insert(O).second)
- Worklist.push_back(O);
- }
- }
- llvm::errs() << "\n";
- if (auto *MI = MM->getMacroInfo()) {
- llvm::errs() << " ";
- MI->dump();
- llvm::errs() << "\n";
- }
- }
-}
-
-/// RegisterBuiltinMacro - Register the specified identifier in the identifier
-/// table and mark it as a builtin macro to be expanded.
-static IdentifierInfo *RegisterBuiltinMacro(Preprocessor &PP, const char *Name){
- // Get the identifier.
- IdentifierInfo *Id = PP.getIdentifierInfo(Name);
-
- // Mark it as being a macro that is builtin.
- MacroInfo *MI = PP.AllocateMacroInfo(SourceLocation());
- MI->setIsBuiltinMacro();
- PP.appendDefMacroDirective(Id, MI);
- return Id;
-}
-
-/// RegisterBuiltinMacros - Register builtin macros, such as __LINE__ with the
-/// identifier table.
-void Preprocessor::RegisterBuiltinMacros() {
- Ident__LINE__ = RegisterBuiltinMacro(*this, "__LINE__");
- Ident__FILE__ = RegisterBuiltinMacro(*this, "__FILE__");
- Ident__DATE__ = RegisterBuiltinMacro(*this, "__DATE__");
- Ident__TIME__ = RegisterBuiltinMacro(*this, "__TIME__");
- Ident__COUNTER__ = RegisterBuiltinMacro(*this, "__COUNTER__");
- Ident_Pragma = RegisterBuiltinMacro(*this, "_Pragma");
-
- // C++ Standing Document Extensions.
- if (LangOpts.CPlusPlus)
- Ident__has_cpp_attribute =
- RegisterBuiltinMacro(*this, "__has_cpp_attribute");
- else
- Ident__has_cpp_attribute = nullptr;
-
- // GCC Extensions.
- Ident__BASE_FILE__ = RegisterBuiltinMacro(*this, "__BASE_FILE__");
- Ident__INCLUDE_LEVEL__ = RegisterBuiltinMacro(*this, "__INCLUDE_LEVEL__");
- Ident__TIMESTAMP__ = RegisterBuiltinMacro(*this, "__TIMESTAMP__");
-
- // Microsoft Extensions.
- if (LangOpts.MicrosoftExt) {
- Ident__identifier = RegisterBuiltinMacro(*this, "__identifier");
- Ident__pragma = RegisterBuiltinMacro(*this, "__pragma");
- } else {
- Ident__identifier = nullptr;
- Ident__pragma = nullptr;
- }
-
- // Clang Extensions.
- Ident__has_feature = RegisterBuiltinMacro(*this, "__has_feature");
- Ident__has_extension = RegisterBuiltinMacro(*this, "__has_extension");
- Ident__has_builtin = RegisterBuiltinMacro(*this, "__has_builtin");
- Ident__has_attribute = RegisterBuiltinMacro(*this, "__has_attribute");
- Ident__has_declspec = RegisterBuiltinMacro(*this, "__has_declspec_attribute");
- Ident__has_include = RegisterBuiltinMacro(*this, "__has_include");
- Ident__has_include_next = RegisterBuiltinMacro(*this, "__has_include_next");
- Ident__has_warning = RegisterBuiltinMacro(*this, "__has_warning");
- Ident__is_identifier = RegisterBuiltinMacro(*this, "__is_identifier");
-
- // Modules.
- Ident__building_module = RegisterBuiltinMacro(*this, "__building_module");
- if (!LangOpts.CurrentModule.empty())
- Ident__MODULE__ = RegisterBuiltinMacro(*this, "__MODULE__");
- else
- Ident__MODULE__ = nullptr;
-}
-
-/// isTrivialSingleTokenExpansion - Return true if MI, which has a single token
-/// in its expansion, currently expands to that token literally.
-static bool isTrivialSingleTokenExpansion(const MacroInfo *MI,
- const IdentifierInfo *MacroIdent,
- Preprocessor &PP) {
- IdentifierInfo *II = MI->getReplacementToken(0).getIdentifierInfo();
-
- // If the token isn't an identifier, it's always literally expanded.
- if (!II) return true;
-
- // If the information about this identifier is out of date, update it from
- // the external source.
- if (II->isOutOfDate())
- PP.getExternalSource()->updateOutOfDateIdentifier(*II);
-
- // If the identifier is a macro, and if that macro is enabled, it may be
- // expanded so it's not a trivial expansion.
- if (auto *ExpansionMI = PP.getMacroInfo(II))
- if (ExpansionMI->isEnabled() &&
- // Fast expanding "#define X X" is ok, because X would be disabled.
- II != MacroIdent)
- return false;
-
- // If this is an object-like macro invocation, it is safe to trivially expand
- // it.
- if (MI->isObjectLike()) return true;
-
- // If this is a function-like macro invocation, it's safe to trivially expand
- // as long as the identifier is not a macro argument.
- return std::find(MI->param_begin(), MI->param_end(), II) == MI->param_end();
-}
-
-/// isNextPPTokenLParen - Determine whether the next preprocessor token to be
-/// lexed is a '('. If so, consume the token and return true, if not, this
-/// method should have no observable side-effect on the lexed tokens.
-bool Preprocessor::isNextPPTokenLParen() {
- // Do some quick tests for rejection cases.
- unsigned Val;
- if (CurLexer)
- Val = CurLexer->isNextPPTokenLParen();
- else if (CurPTHLexer)
- Val = CurPTHLexer->isNextPPTokenLParen();
- else
- Val = CurTokenLexer->isNextTokenLParen();
-
- if (Val == 2) {
- // We have run off the end. If it's a source file we don't
- // examine enclosing ones (C99 5.1.1.2p4). Otherwise walk up the
- // macro stack.
- if (CurPPLexer)
- return false;
- for (const IncludeStackInfo &Entry : llvm::reverse(IncludeMacroStack)) {
- if (Entry.TheLexer)
- Val = Entry.TheLexer->isNextPPTokenLParen();
- else if (Entry.ThePTHLexer)
- Val = Entry.ThePTHLexer->isNextPPTokenLParen();
- else
- Val = Entry.TheTokenLexer->isNextTokenLParen();
-
- if (Val != 2)
- break;
-
- // Ran off the end of a source file?
- if (Entry.ThePPLexer)
- return false;
- }
- }
-
- // Okay, if we know that the token is a '(', lex it and return. Otherwise we
- // have found something that isn't a '(' or we found the end of the
- // translation unit. In either case, return false.
- return Val == 1;
-}
-
-/// HandleMacroExpandedIdentifier - If an identifier token is read that is to be
-/// expanded as a macro, handle it and return the next token as 'Identifier'.
-bool Preprocessor::HandleMacroExpandedIdentifier(Token &Identifier,
- const MacroDefinition &M) {
- MacroInfo *MI = M.getMacroInfo();
-
- // If this is a macro expansion in the "#if !defined(x)" line for the file,
- // then the macro could expand to different things in other contexts, we need
- // to disable the optimization in this case.
- if (CurPPLexer) CurPPLexer->MIOpt.ExpandedMacro();
-
- // If this is a builtin macro, like __LINE__ or _Pragma, handle it specially.
- if (MI->isBuiltinMacro()) {
- if (Callbacks)
- Callbacks->MacroExpands(Identifier, M, Identifier.getLocation(),
- /*Args=*/nullptr);
- ExpandBuiltinMacro(Identifier);
- return true;
- }
-
- /// Args - If this is a function-like macro expansion, this contains,
- /// for each macro argument, the list of tokens that were provided to the
- /// invocation.
- MacroArgs *Args = nullptr;
-
- // Remember where the end of the expansion occurred. For an object-like
- // macro, this is the identifier. For a function-like macro, this is the ')'.
- SourceLocation ExpansionEnd = Identifier.getLocation();
-
- // If this is a function-like macro, read the arguments.
- if (MI->isFunctionLike()) {
- // Remember that we are now parsing the arguments to a macro invocation.
- // Preprocessor directives used inside macro arguments are not portable, and
- // this enables the warning.
- InMacroArgs = true;
- Args = ReadMacroCallArgumentList(Identifier, MI, ExpansionEnd);
-
- // Finished parsing args.
- InMacroArgs = false;
-
- // If there was an error parsing the arguments, bail out.
- if (!Args) return true;
-
- ++NumFnMacroExpanded;
- } else {
- ++NumMacroExpanded;
- }
-
- // Notice that this macro has been used.
- markMacroAsUsed(MI);
-
- // Remember where the token is expanded.
- SourceLocation ExpandLoc = Identifier.getLocation();
- SourceRange ExpansionRange(ExpandLoc, ExpansionEnd);
-
- if (Callbacks) {
- if (InMacroArgs) {
- // We can have macro expansion inside a conditional directive while
- // reading the function macro arguments. To ensure, in that case, that
- // MacroExpands callbacks still happen in source order, queue this
- // callback to have it happen after the function macro callback.
- DelayedMacroExpandsCallbacks.push_back(
- MacroExpandsInfo(Identifier, M, ExpansionRange));
- } else {
- Callbacks->MacroExpands(Identifier, M, ExpansionRange, Args);
- if (!DelayedMacroExpandsCallbacks.empty()) {
- for (const MacroExpandsInfo &Info : DelayedMacroExpandsCallbacks) {
- // FIXME: We lose macro args info with delayed callback.
- Callbacks->MacroExpands(Info.Tok, Info.MD, Info.Range,
- /*Args=*/nullptr);
- }
- DelayedMacroExpandsCallbacks.clear();
- }
- }
- }
-
- // If the macro definition is ambiguous, complain.
- if (M.isAmbiguous()) {
- Diag(Identifier, diag::warn_pp_ambiguous_macro)
- << Identifier.getIdentifierInfo();
- Diag(MI->getDefinitionLoc(), diag::note_pp_ambiguous_macro_chosen)
- << Identifier.getIdentifierInfo();
- M.forAllDefinitions([&](const MacroInfo *OtherMI) {
- if (OtherMI != MI)
- Diag(OtherMI->getDefinitionLoc(), diag::note_pp_ambiguous_macro_other)
- << Identifier.getIdentifierInfo();
- });
- }
-
- // If we started lexing a macro, enter the macro expansion body.
-
- // If this macro expands to no tokens, don't bother to push it onto the
- // expansion stack, only to take it right back off.
- if (MI->getNumTokens() == 0) {
- // No need for arg info.
- if (Args) Args->destroy(*this);
-
- // Propagate whitespace info as if we had pushed, then popped,
- // a macro context.
- Identifier.setFlag(Token::LeadingEmptyMacro);
- PropagateLineStartLeadingSpaceInfo(Identifier);
- ++NumFastMacroExpanded;
- return false;
- } else if (MI->getNumTokens() == 1 &&
- isTrivialSingleTokenExpansion(MI, Identifier.getIdentifierInfo(),
- *this)) {
- // Otherwise, if this macro expands into a single trivially-expanded
- // token: expand it now. This handles common cases like
- // "#define VAL 42".
-
- // No need for arg info.
- if (Args) Args->destroy(*this);
-
- // Propagate the isAtStartOfLine/hasLeadingSpace markers of the macro
- // identifier to the expanded token.
- bool isAtStartOfLine = Identifier.isAtStartOfLine();
- bool hasLeadingSpace = Identifier.hasLeadingSpace();
-
- // Replace the result token.
- Identifier = MI->getReplacementToken(0);
-
- // Restore the StartOfLine/LeadingSpace markers.
- Identifier.setFlagValue(Token::StartOfLine , isAtStartOfLine);
- Identifier.setFlagValue(Token::LeadingSpace, hasLeadingSpace);
-
- // Update the tokens location to include both its expansion and physical
- // locations.
- SourceLocation Loc =
- SourceMgr.createExpansionLoc(Identifier.getLocation(), ExpandLoc,
- ExpansionEnd,Identifier.getLength());
- Identifier.setLocation(Loc);
-
- // If this is a disabled macro or #define X X, we must mark the result as
- // unexpandable.
- if (IdentifierInfo *NewII = Identifier.getIdentifierInfo()) {
- if (MacroInfo *NewMI = getMacroInfo(NewII))
- if (!NewMI->isEnabled() || NewMI == MI) {
- Identifier.setFlag(Token::DisableExpand);
- // Don't warn for "#define X X" like "#define bool bool" from
- // stdbool.h.
- if (NewMI != MI || MI->isFunctionLike())
- Diag(Identifier, diag::pp_disabled_macro_expansion);
- }
- }
-
- // Since this is not an identifier token, it can't be macro expanded, so
- // we're done.
- ++NumFastMacroExpanded;
- return true;
- }
-
- // Start expanding the macro.
- EnterMacro(Identifier, ExpansionEnd, MI, Args);
- return false;
-}
-
-enum Bracket {
- Brace,
- Paren
-};
-
-/// CheckMatchedBrackets - Returns true if the braces and parentheses in the
-/// token vector are properly nested.
-static bool CheckMatchedBrackets(const SmallVectorImpl<Token> &Tokens) {
- SmallVector<Bracket, 8> Brackets;
- for (SmallVectorImpl<Token>::const_iterator I = Tokens.begin(),
- E = Tokens.end();
- I != E; ++I) {
- if (I->is(tok::l_paren)) {
- Brackets.push_back(Paren);
- } else if (I->is(tok::r_paren)) {
- if (Brackets.empty() || Brackets.back() == Brace)
- return false;
- Brackets.pop_back();
- } else if (I->is(tok::l_brace)) {
- Brackets.push_back(Brace);
- } else if (I->is(tok::r_brace)) {
- if (Brackets.empty() || Brackets.back() == Paren)
- return false;
- Brackets.pop_back();
- }
- }
- return Brackets.empty();
-}
-
-/// GenerateNewArgTokens - Returns true if OldTokens can be converted to a new
-/// vector of tokens in NewTokens. The new number of arguments will be placed
-/// in NumArgs and the ranges which need to surrounded in parentheses will be
-/// in ParenHints.
-/// Returns false if the token stream cannot be changed. If this is because
-/// of an initializer list starting a macro argument, the range of those
-/// initializer lists will be place in InitLists.
-static bool GenerateNewArgTokens(Preprocessor &PP,
- SmallVectorImpl<Token> &OldTokens,
- SmallVectorImpl<Token> &NewTokens,
- unsigned &NumArgs,
- SmallVectorImpl<SourceRange> &ParenHints,
- SmallVectorImpl<SourceRange> &InitLists) {
- if (!CheckMatchedBrackets(OldTokens))
- return false;
-
- // Once it is known that the brackets are matched, only a simple count of the
- // braces is needed.
- unsigned Braces = 0;
-
- // First token of a new macro argument.
- SmallVectorImpl<Token>::iterator ArgStartIterator = OldTokens.begin();
-
- // First closing brace in a new macro argument. Used to generate
- // SourceRanges for InitLists.
- SmallVectorImpl<Token>::iterator ClosingBrace = OldTokens.end();
- NumArgs = 0;
- Token TempToken;
- // Set to true when a macro separator token is found inside a braced list.
- // If true, the fixed argument spans multiple old arguments and ParenHints
- // will be updated.
- bool FoundSeparatorToken = false;
- for (SmallVectorImpl<Token>::iterator I = OldTokens.begin(),
- E = OldTokens.end();
- I != E; ++I) {
- if (I->is(tok::l_brace)) {
- ++Braces;
- } else if (I->is(tok::r_brace)) {
- --Braces;
- if (Braces == 0 && ClosingBrace == E && FoundSeparatorToken)
- ClosingBrace = I;
- } else if (I->is(tok::eof)) {
- // EOF token is used to separate macro arguments
- if (Braces != 0) {
- // Assume comma separator is actually braced list separator and change
- // it back to a comma.
- FoundSeparatorToken = true;
- I->setKind(tok::comma);
- I->setLength(1);
- } else { // Braces == 0
- // Separator token still separates arguments.
- ++NumArgs;
-
- // If the argument starts with a brace, it can't be fixed with
- // parentheses. A different diagnostic will be given.
- if (FoundSeparatorToken && ArgStartIterator->is(tok::l_brace)) {
- InitLists.push_back(
- SourceRange(ArgStartIterator->getLocation(),
- PP.getLocForEndOfToken(ClosingBrace->getLocation())));
- ClosingBrace = E;
- }
-
- // Add left paren
- if (FoundSeparatorToken) {
- TempToken.startToken();
- TempToken.setKind(tok::l_paren);
- TempToken.setLocation(ArgStartIterator->getLocation());
- TempToken.setLength(0);
- NewTokens.push_back(TempToken);
- }
-
- // Copy over argument tokens
- NewTokens.insert(NewTokens.end(), ArgStartIterator, I);
-
- // Add right paren and store the paren locations in ParenHints
- if (FoundSeparatorToken) {
- SourceLocation Loc = PP.getLocForEndOfToken((I - 1)->getLocation());
- TempToken.startToken();
- TempToken.setKind(tok::r_paren);
- TempToken.setLocation(Loc);
- TempToken.setLength(0);
- NewTokens.push_back(TempToken);
- ParenHints.push_back(SourceRange(ArgStartIterator->getLocation(),
- Loc));
- }
-
- // Copy separator token
- NewTokens.push_back(*I);
-
- // Reset values
- ArgStartIterator = I + 1;
- FoundSeparatorToken = false;
- }
- }
- }
-
- return !ParenHints.empty() && InitLists.empty();
-}
-
-/// ReadFunctionLikeMacroArgs - After reading "MACRO" and knowing that the next
-/// token is the '(' of the macro, this method is invoked to read all of the
-/// actual arguments specified for the macro invocation. This returns null on
-/// error.
-MacroArgs *Preprocessor::ReadMacroCallArgumentList(Token &MacroName,
- MacroInfo *MI,
- SourceLocation &MacroEnd) {
- // The number of fixed arguments to parse.
- unsigned NumFixedArgsLeft = MI->getNumParams();
- bool isVariadic = MI->isVariadic();
-
- // Outer loop, while there are more arguments, keep reading them.
- Token Tok;
-
- // Read arguments as unexpanded tokens. This avoids issues, e.g., where
- // an argument value in a macro could expand to ',' or '(' or ')'.
- LexUnexpandedToken(Tok);
- assert(Tok.is(tok::l_paren) && "Error computing l-paren-ness?");
-
- // ArgTokens - Build up a list of tokens that make up each argument. Each
- // argument is separated by an EOF token. Use a SmallVector so we can avoid
- // heap allocations in the common case.
- SmallVector<Token, 64> ArgTokens;
- bool ContainsCodeCompletionTok = false;
- bool FoundElidedComma = false;
-
- SourceLocation TooManyArgsLoc;
-
- unsigned NumActuals = 0;
- while (Tok.isNot(tok::r_paren)) {
- if (ContainsCodeCompletionTok && Tok.isOneOf(tok::eof, tok::eod))
- break;
-
- assert(Tok.isOneOf(tok::l_paren, tok::comma) &&
- "only expect argument separators here");
-
- size_t ArgTokenStart = ArgTokens.size();
- SourceLocation ArgStartLoc = Tok.getLocation();
-
- // C99 6.10.3p11: Keep track of the number of l_parens we have seen. Note
- // that we already consumed the first one.
- unsigned NumParens = 0;
-
- while (true) {
- // Read arguments as unexpanded tokens. This avoids issues, e.g., where
- // an argument value in a macro could expand to ',' or '(' or ')'.
- LexUnexpandedToken(Tok);
-
- if (Tok.isOneOf(tok::eof, tok::eod)) { // "#if f(<eof>" & "#if f(\n"
- if (!ContainsCodeCompletionTok) {
- Diag(MacroName, diag::err_unterm_macro_invoc);
- Diag(MI->getDefinitionLoc(), diag::note_macro_here)
- << MacroName.getIdentifierInfo();
- // Do not lose the EOF/EOD. Return it to the client.
- MacroName = Tok;
- return nullptr;
- }
- // Do not lose the EOF/EOD.
- auto Toks = llvm::make_unique<Token[]>(1);
- Toks[0] = Tok;
- EnterTokenStream(std::move(Toks), 1, true);
- break;
- } else if (Tok.is(tok::r_paren)) {
- // If we found the ) token, the macro arg list is done.
- if (NumParens-- == 0) {
- MacroEnd = Tok.getLocation();
- if (!ArgTokens.empty() &&
- ArgTokens.back().commaAfterElided()) {
- FoundElidedComma = true;
- }
- break;
- }
- } else if (Tok.is(tok::l_paren)) {
- ++NumParens;
- } else if (Tok.is(tok::comma) && NumParens == 0 &&
- !(Tok.getFlags() & Token::IgnoredComma)) {
- // In Microsoft-compatibility mode, single commas from nested macro
- // expansions should not be considered as argument separators. We test
- // for this with the IgnoredComma token flag above.
-
- // Comma ends this argument if there are more fixed arguments expected.
- // However, if this is a variadic macro, and this is part of the
- // variadic part, then the comma is just an argument token.
- if (!isVariadic) break;
- if (NumFixedArgsLeft > 1)
- break;
- } else if (Tok.is(tok::comment) && !KeepMacroComments) {
- // If this is a comment token in the argument list and we're just in
- // -C mode (not -CC mode), discard the comment.
- continue;
- } else if (!Tok.isAnnotation() && Tok.getIdentifierInfo() != nullptr) {
- // Reading macro arguments can cause macros that we are currently
- // expanding from to be popped off the expansion stack. Doing so causes
- // them to be reenabled for expansion. Here we record whether any
- // identifiers we lex as macro arguments correspond to disabled macros.
- // If so, we mark the token as noexpand. This is a subtle aspect of
- // C99 6.10.3.4p2.
- if (MacroInfo *MI = getMacroInfo(Tok.getIdentifierInfo()))
- if (!MI->isEnabled())
- Tok.setFlag(Token::DisableExpand);
- } else if (Tok.is(tok::code_completion)) {
- ContainsCodeCompletionTok = true;
- if (CodeComplete)
- CodeComplete->CodeCompleteMacroArgument(MacroName.getIdentifierInfo(),
- MI, NumActuals);
- // Don't mark that we reached the code-completion point because the
- // parser is going to handle the token and there will be another
- // code-completion callback.
- }
-
- ArgTokens.push_back(Tok);
- }
-
- // If this was an empty argument list foo(), don't add this as an empty
- // argument.
- if (ArgTokens.empty() && Tok.getKind() == tok::r_paren)
- break;
-
- // If this is not a variadic macro, and too many args were specified, emit
- // an error.
- if (!isVariadic && NumFixedArgsLeft == 0 && TooManyArgsLoc.isInvalid()) {
- if (ArgTokens.size() != ArgTokenStart)
- TooManyArgsLoc = ArgTokens[ArgTokenStart].getLocation();
- else
- TooManyArgsLoc = ArgStartLoc;
- }
-
- // Empty arguments are standard in C99 and C++0x, and are supported as an
- // extension in other modes.
- if (ArgTokens.size() == ArgTokenStart && !LangOpts.C99)
- Diag(Tok, LangOpts.CPlusPlus11 ?
- diag::warn_cxx98_compat_empty_fnmacro_arg :
- diag::ext_empty_fnmacro_arg);
-
- // Add a marker EOF token to the end of the token list for this argument.
- Token EOFTok;
- EOFTok.startToken();
- EOFTok.setKind(tok::eof);
- EOFTok.setLocation(Tok.getLocation());
- EOFTok.setLength(0);
- ArgTokens.push_back(EOFTok);
- ++NumActuals;
- if (!ContainsCodeCompletionTok && NumFixedArgsLeft != 0)
- --NumFixedArgsLeft;
- }
-
- // Okay, we either found the r_paren. Check to see if we parsed too few
- // arguments.
- unsigned MinArgsExpected = MI->getNumParams();
-
- // If this is not a variadic macro, and too many args were specified, emit
- // an error.
- if (!isVariadic && NumActuals > MinArgsExpected &&
- !ContainsCodeCompletionTok) {
- // Emit the diagnostic at the macro name in case there is a missing ).
- // Emitting it at the , could be far away from the macro name.
- Diag(TooManyArgsLoc, diag::err_too_many_args_in_macro_invoc);
- Diag(MI->getDefinitionLoc(), diag::note_macro_here)
- << MacroName.getIdentifierInfo();
-
- // Commas from braced initializer lists will be treated as argument
- // separators inside macros. Attempt to correct for this with parentheses.
- // TODO: See if this can be generalized to angle brackets for templates
- // inside macro arguments.
-
- SmallVector<Token, 4> FixedArgTokens;
- unsigned FixedNumArgs = 0;
- SmallVector<SourceRange, 4> ParenHints, InitLists;
- if (!GenerateNewArgTokens(*this, ArgTokens, FixedArgTokens, FixedNumArgs,
- ParenHints, InitLists)) {
- if (!InitLists.empty()) {
- DiagnosticBuilder DB =
- Diag(MacroName,
- diag::note_init_list_at_beginning_of_macro_argument);
- for (SourceRange Range : InitLists)
- DB << Range;
- }
- return nullptr;
- }
- if (FixedNumArgs != MinArgsExpected)
- return nullptr;
-
- DiagnosticBuilder DB = Diag(MacroName, diag::note_suggest_parens_for_macro);
- for (SourceRange ParenLocation : ParenHints) {
- DB << FixItHint::CreateInsertion(ParenLocation.getBegin(), "(");
- DB << FixItHint::CreateInsertion(ParenLocation.getEnd(), ")");
- }
- ArgTokens.swap(FixedArgTokens);
- NumActuals = FixedNumArgs;
- }
-
- // See MacroArgs instance var for description of this.
- bool isVarargsElided = false;
-
- if (ContainsCodeCompletionTok) {
- // Recover from not-fully-formed macro invocation during code-completion.
- Token EOFTok;
- EOFTok.startToken();
- EOFTok.setKind(tok::eof);
- EOFTok.setLocation(Tok.getLocation());
- EOFTok.setLength(0);
- for (; NumActuals < MinArgsExpected; ++NumActuals)
- ArgTokens.push_back(EOFTok);
- }
-
- if (NumActuals < MinArgsExpected) {
- // There are several cases where too few arguments is ok, handle them now.
- if (NumActuals == 0 && MinArgsExpected == 1) {
- // #define A(X) or #define A(...) ---> A()
-
- // If there is exactly one argument, and that argument is missing,
- // then we have an empty "()" argument empty list. This is fine, even if
- // the macro expects one argument (the argument is just empty).
- isVarargsElided = MI->isVariadic();
- } else if ((FoundElidedComma || MI->isVariadic()) &&
- (NumActuals+1 == MinArgsExpected || // A(x, ...) -> A(X)
- (NumActuals == 0 && MinArgsExpected == 2))) {// A(x,...) -> A()
- // Varargs where the named vararg parameter is missing: OK as extension.
- // #define A(x, ...)
- // A("blah")
- //
- // If the macro contains the comma pasting extension, the diagnostic
- // is suppressed; we know we'll get another diagnostic later.
- if (!MI->hasCommaPasting()) {
- Diag(Tok, diag::ext_missing_varargs_arg);
- Diag(MI->getDefinitionLoc(), diag::note_macro_here)
- << MacroName.getIdentifierInfo();
- }
-
- // Remember this occurred, allowing us to elide the comma when used for
- // cases like:
- // #define A(x, foo...) blah(a, ## foo)
- // #define B(x, ...) blah(a, ## __VA_ARGS__)
- // #define C(...) blah(a, ## __VA_ARGS__)
- // A(x) B(x) C()
- isVarargsElided = true;
- } else if (!ContainsCodeCompletionTok) {
- // Otherwise, emit the error.
- Diag(Tok, diag::err_too_few_args_in_macro_invoc);
- Diag(MI->getDefinitionLoc(), diag::note_macro_here)
- << MacroName.getIdentifierInfo();
- return nullptr;
- }
-
- // Add a marker EOF token to the end of the token list for this argument.
- SourceLocation EndLoc = Tok.getLocation();
- Tok.startToken();
- Tok.setKind(tok::eof);
- Tok.setLocation(EndLoc);
- Tok.setLength(0);
- ArgTokens.push_back(Tok);
-
- // If we expect two arguments, add both as empty.
- if (NumActuals == 0 && MinArgsExpected == 2)
- ArgTokens.push_back(Tok);
-
- } else if (NumActuals > MinArgsExpected && !MI->isVariadic() &&
- !ContainsCodeCompletionTok) {
- // Emit the diagnostic at the macro name in case there is a missing ).
- // Emitting it at the , could be far away from the macro name.
- Diag(MacroName, diag::err_too_many_args_in_macro_invoc);
- Diag(MI->getDefinitionLoc(), diag::note_macro_here)
- << MacroName.getIdentifierInfo();
- return nullptr;
- }
-
- return MacroArgs::create(MI, ArgTokens, isVarargsElided, *this);
-}
-
-/// \brief Keeps macro expanded tokens for TokenLexers.
-//
-/// Works like a stack; a TokenLexer adds the macro expanded tokens that is
-/// going to lex in the cache and when it finishes the tokens are removed
-/// from the end of the cache.
-Token *Preprocessor::cacheMacroExpandedTokens(TokenLexer *tokLexer,
- ArrayRef<Token> tokens) {
- assert(tokLexer);
- if (tokens.empty())
- return nullptr;
-
- size_t newIndex = MacroExpandedTokens.size();
- bool cacheNeedsToGrow = tokens.size() >
- MacroExpandedTokens.capacity()-MacroExpandedTokens.size();
- MacroExpandedTokens.append(tokens.begin(), tokens.end());
-
- if (cacheNeedsToGrow) {
- // Go through all the TokenLexers whose 'Tokens' pointer points in the
- // buffer and update the pointers to the (potential) new buffer array.
- for (const auto &Lexer : MacroExpandingLexersStack) {
- TokenLexer *prevLexer;
- size_t tokIndex;
- std::tie(prevLexer, tokIndex) = Lexer;
- prevLexer->Tokens = MacroExpandedTokens.data() + tokIndex;
- }
- }
-
- MacroExpandingLexersStack.push_back(std::make_pair(tokLexer, newIndex));
- return MacroExpandedTokens.data() + newIndex;
-}
-
-void Preprocessor::removeCachedMacroExpandedTokensOfLastLexer() {
- assert(!MacroExpandingLexersStack.empty());
- size_t tokIndex = MacroExpandingLexersStack.back().second;
- assert(tokIndex < MacroExpandedTokens.size());
- // Pop the cached macro expanded tokens from the end.
- MacroExpandedTokens.resize(tokIndex);
- MacroExpandingLexersStack.pop_back();
-}
-
-/// ComputeDATE_TIME - Compute the current time, enter it into the specified
-/// scratch buffer, then return DATELoc/TIMELoc locations with the position of
-/// the identifier tokens inserted.
-static void ComputeDATE_TIME(SourceLocation &DATELoc, SourceLocation &TIMELoc,
- Preprocessor &PP) {
- time_t TT = time(nullptr);
- struct tm *TM = localtime(&TT);
-
- static const char * const Months[] = {
- "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"
- };
-
- {
- SmallString<32> TmpBuffer;
- llvm::raw_svector_ostream TmpStream(TmpBuffer);
- TmpStream << llvm::format("\"%s %2d %4d\"", Months[TM->tm_mon],
- TM->tm_mday, TM->tm_year + 1900);
- Token TmpTok;
- TmpTok.startToken();
- PP.CreateString(TmpStream.str(), TmpTok);
- DATELoc = TmpTok.getLocation();
- }
-
- {
- SmallString<32> TmpBuffer;
- llvm::raw_svector_ostream TmpStream(TmpBuffer);
- TmpStream << llvm::format("\"%02d:%02d:%02d\"",
- TM->tm_hour, TM->tm_min, TM->tm_sec);
- Token TmpTok;
- TmpTok.startToken();
- PP.CreateString(TmpStream.str(), TmpTok);
- TIMELoc = TmpTok.getLocation();
- }
-}
-
-/// HasFeature - Return true if we recognize and implement the feature
-/// specified by the identifier as a standard language feature.
-static bool HasFeature(const Preprocessor &PP, StringRef Feature) {
- const LangOptions &LangOpts = PP.getLangOpts();
-
- // Normalize the feature name, __foo__ becomes foo.
- if (Feature.startswith("__") && Feature.endswith("__") && Feature.size() >= 4)
- Feature = Feature.substr(2, Feature.size() - 4);
-
- return llvm::StringSwitch<bool>(Feature)
- .Case("address_sanitizer",
- LangOpts.Sanitize.hasOneOf(SanitizerKind::Address |
- SanitizerKind::KernelAddress))
- .Case("assume_nonnull", true)
- .Case("attribute_analyzer_noreturn", true)
- .Case("attribute_availability", true)
- .Case("attribute_availability_with_message", true)
- .Case("attribute_availability_app_extension", true)
- .Case("attribute_availability_with_version_underscores", true)
- .Case("attribute_availability_tvos", true)
- .Case("attribute_availability_watchos", true)
- .Case("attribute_availability_with_strict", true)
- .Case("attribute_availability_with_replacement", true)
- .Case("attribute_availability_in_templates", true)
- .Case("attribute_cf_returns_not_retained", true)
- .Case("attribute_cf_returns_retained", true)
- .Case("attribute_cf_returns_on_parameters", true)
- .Case("attribute_deprecated_with_message", true)
- .Case("attribute_deprecated_with_replacement", true)
- .Case("attribute_ext_vector_type", true)
- .Case("attribute_ns_returns_not_retained", true)
- .Case("attribute_ns_returns_retained", true)
- .Case("attribute_ns_consumes_self", true)
- .Case("attribute_ns_consumed", true)
- .Case("attribute_cf_consumed", true)
- .Case("attribute_objc_ivar_unused", true)
- .Case("attribute_objc_method_family", true)
- .Case("attribute_overloadable", true)
- .Case("attribute_unavailable_with_message", true)
- .Case("attribute_unused_on_fields", true)
- .Case("attribute_diagnose_if_objc", true)
- .Case("blocks", LangOpts.Blocks)
- .Case("c_thread_safety_attributes", true)
- .Case("cxx_exceptions", LangOpts.CXXExceptions)
- .Case("cxx_rtti", LangOpts.RTTI && LangOpts.RTTIData)
- .Case("enumerator_attributes", true)
- .Case("nullability", true)
- .Case("nullability_on_arrays", true)
- .Case("memory_sanitizer", LangOpts.Sanitize.has(SanitizerKind::Memory))
- .Case("thread_sanitizer", LangOpts.Sanitize.has(SanitizerKind::Thread))
- .Case("dataflow_sanitizer", LangOpts.Sanitize.has(SanitizerKind::DataFlow))
- .Case("efficiency_sanitizer",
- LangOpts.Sanitize.hasOneOf(SanitizerKind::Efficiency))
- .Case("scudo", LangOpts.Sanitize.hasOneOf(SanitizerKind::Scudo))
- // Objective-C features
- .Case("objc_arr", LangOpts.ObjCAutoRefCount) // FIXME: REMOVE?
- .Case("objc_arc", LangOpts.ObjCAutoRefCount)
- .Case("objc_arc_weak", LangOpts.ObjCWeak)
- .Case("objc_default_synthesize_properties", LangOpts.ObjC2)
- .Case("objc_fixed_enum", LangOpts.ObjC2)
- .Case("objc_instancetype", LangOpts.ObjC2)
- .Case("objc_kindof", LangOpts.ObjC2)
- .Case("objc_modules", LangOpts.ObjC2 && LangOpts.Modules)
- .Case("objc_nonfragile_abi", LangOpts.ObjCRuntime.isNonFragile())
- .Case("objc_property_explicit_atomic",
- true) // Does clang support explicit "atomic" keyword?
- .Case("objc_protocol_qualifier_mangling", true)
- .Case("objc_weak_class", LangOpts.ObjCRuntime.hasWeakClassImport())
- .Case("ownership_holds", true)
- .Case("ownership_returns", true)
- .Case("ownership_takes", true)
- .Case("objc_bool", true)
- .Case("objc_subscripting", LangOpts.ObjCRuntime.isNonFragile())
- .Case("objc_array_literals", LangOpts.ObjC2)
- .Case("objc_dictionary_literals", LangOpts.ObjC2)
- .Case("objc_boxed_expressions", LangOpts.ObjC2)
- .Case("objc_boxed_nsvalue_expressions", LangOpts.ObjC2)
- .Case("arc_cf_code_audited", true)
- .Case("objc_bridge_id", true)
- .Case("objc_bridge_id_on_typedefs", true)
- .Case("objc_generics", LangOpts.ObjC2)
- .Case("objc_generics_variance", LangOpts.ObjC2)
- .Case("objc_class_property", LangOpts.ObjC2)
- // C11 features
- .Case("c_alignas", LangOpts.C11)
- .Case("c_alignof", LangOpts.C11)
- .Case("c_atomic", LangOpts.C11)
- .Case("c_generic_selections", LangOpts.C11)
- .Case("c_static_assert", LangOpts.C11)
- .Case("c_thread_local",
- LangOpts.C11 && PP.getTargetInfo().isTLSSupported())
- // C++11 features
- .Case("cxx_access_control_sfinae", LangOpts.CPlusPlus11)
- .Case("cxx_alias_templates", LangOpts.CPlusPlus11)
- .Case("cxx_alignas", LangOpts.CPlusPlus11)
- .Case("cxx_alignof", LangOpts.CPlusPlus11)
- .Case("cxx_atomic", LangOpts.CPlusPlus11)
- .Case("cxx_attributes", LangOpts.CPlusPlus11)
- .Case("cxx_auto_type", LangOpts.CPlusPlus11)
- .Case("cxx_constexpr", LangOpts.CPlusPlus11)
- .Case("cxx_constexpr_string_builtins", LangOpts.CPlusPlus11)
- .Case("cxx_decltype", LangOpts.CPlusPlus11)
- .Case("cxx_decltype_incomplete_return_types", LangOpts.CPlusPlus11)
- .Case("cxx_default_function_template_args", LangOpts.CPlusPlus11)
- .Case("cxx_defaulted_functions", LangOpts.CPlusPlus11)
- .Case("cxx_delegating_constructors", LangOpts.CPlusPlus11)
- .Case("cxx_deleted_functions", LangOpts.CPlusPlus11)
- .Case("cxx_explicit_conversions", LangOpts.CPlusPlus11)
- .Case("cxx_generalized_initializers", LangOpts.CPlusPlus11)
- .Case("cxx_implicit_moves", LangOpts.CPlusPlus11)
- .Case("cxx_inheriting_constructors", LangOpts.CPlusPlus11)
- .Case("cxx_inline_namespaces", LangOpts.CPlusPlus11)
- .Case("cxx_lambdas", LangOpts.CPlusPlus11)
- .Case("cxx_local_type_template_args", LangOpts.CPlusPlus11)
- .Case("cxx_nonstatic_member_init", LangOpts.CPlusPlus11)
- .Case("cxx_noexcept", LangOpts.CPlusPlus11)
- .Case("cxx_nullptr", LangOpts.CPlusPlus11)
- .Case("cxx_override_control", LangOpts.CPlusPlus11)
- .Case("cxx_range_for", LangOpts.CPlusPlus11)
- .Case("cxx_raw_string_literals", LangOpts.CPlusPlus11)
- .Case("cxx_reference_qualified_functions", LangOpts.CPlusPlus11)
- .Case("cxx_rvalue_references", LangOpts.CPlusPlus11)
- .Case("cxx_strong_enums", LangOpts.CPlusPlus11)
- .Case("cxx_static_assert", LangOpts.CPlusPlus11)
- .Case("cxx_thread_local",
- LangOpts.CPlusPlus11 && PP.getTargetInfo().isTLSSupported())
- .Case("cxx_trailing_return", LangOpts.CPlusPlus11)
- .Case("cxx_unicode_literals", LangOpts.CPlusPlus11)
- .Case("cxx_unrestricted_unions", LangOpts.CPlusPlus11)
- .Case("cxx_user_literals", LangOpts.CPlusPlus11)
- .Case("cxx_variadic_templates", LangOpts.CPlusPlus11)
- // C++14 features
- .Case("cxx_aggregate_nsdmi", LangOpts.CPlusPlus14)
- .Case("cxx_binary_literals", LangOpts.CPlusPlus14)
- .Case("cxx_contextual_conversions", LangOpts.CPlusPlus14)
- .Case("cxx_decltype_auto", LangOpts.CPlusPlus14)
- .Case("cxx_generic_lambdas", LangOpts.CPlusPlus14)
- .Case("cxx_init_captures", LangOpts.CPlusPlus14)
- .Case("cxx_relaxed_constexpr", LangOpts.CPlusPlus14)
- .Case("cxx_return_type_deduction", LangOpts.CPlusPlus14)
- .Case("cxx_variable_templates", LangOpts.CPlusPlus14)
- // NOTE: For features covered by SD-6, it is preferable to provide *only*
- // the SD-6 macro and not a __has_feature check.
-
- // C++ TSes
- //.Case("cxx_runtime_arrays", LangOpts.CPlusPlusTSArrays)
- //.Case("cxx_concepts", LangOpts.CPlusPlusTSConcepts)
- // FIXME: Should this be __has_feature or __has_extension?
- //.Case("raw_invocation_type", LangOpts.CPlusPlus)
- // Type traits
- // N.B. Additional type traits should not be added to the following list.
- // Instead, they should be detected by has_extension.
- .Case("has_nothrow_assign", LangOpts.CPlusPlus)
- .Case("has_nothrow_copy", LangOpts.CPlusPlus)
- .Case("has_nothrow_constructor", LangOpts.CPlusPlus)
- .Case("has_trivial_assign", LangOpts.CPlusPlus)
- .Case("has_trivial_copy", LangOpts.CPlusPlus)
- .Case("has_trivial_constructor", LangOpts.CPlusPlus)
- .Case("has_trivial_destructor", LangOpts.CPlusPlus)
- .Case("has_virtual_destructor", LangOpts.CPlusPlus)
- .Case("is_abstract", LangOpts.CPlusPlus)
- .Case("is_base_of", LangOpts.CPlusPlus)
- .Case("is_class", LangOpts.CPlusPlus)
- .Case("is_constructible", LangOpts.CPlusPlus)
- .Case("is_convertible_to", LangOpts.CPlusPlus)
- .Case("is_empty", LangOpts.CPlusPlus)
- .Case("is_enum", LangOpts.CPlusPlus)
- .Case("is_final", LangOpts.CPlusPlus)
- .Case("is_literal", LangOpts.CPlusPlus)
- .Case("is_standard_layout", LangOpts.CPlusPlus)
- .Case("is_pod", LangOpts.CPlusPlus)
- .Case("is_polymorphic", LangOpts.CPlusPlus)
- .Case("is_sealed", LangOpts.CPlusPlus && LangOpts.MicrosoftExt)
- .Case("is_trivial", LangOpts.CPlusPlus)
- .Case("is_trivially_assignable", LangOpts.CPlusPlus)
- .Case("is_trivially_constructible", LangOpts.CPlusPlus)
- .Case("is_trivially_copyable", LangOpts.CPlusPlus)
- .Case("is_union", LangOpts.CPlusPlus)
- .Case("modules", LangOpts.Modules)
- .Case("safe_stack", LangOpts.Sanitize.has(SanitizerKind::SafeStack))
- .Case("tls", PP.getTargetInfo().isTLSSupported())
- .Case("underlying_type", LangOpts.CPlusPlus)
- .Default(false);
-}
-
-/// HasExtension - Return true if we recognize and implement the feature
-/// specified by the identifier, either as an extension or a standard language
-/// feature.
-static bool HasExtension(const Preprocessor &PP, StringRef Extension) {
- if (HasFeature(PP, Extension))
- return true;
-
- // If the use of an extension results in an error diagnostic, extensions are
- // effectively unavailable, so just return false here.
- if (PP.getDiagnostics().getExtensionHandlingBehavior() >=
- diag::Severity::Error)
- return false;
-
- const LangOptions &LangOpts = PP.getLangOpts();
-
- // Normalize the extension name, __foo__ becomes foo.
- if (Extension.startswith("__") && Extension.endswith("__") &&
- Extension.size() >= 4)
- Extension = Extension.substr(2, Extension.size() - 4);
-
- // Because we inherit the feature list from HasFeature, this string switch
- // must be less restrictive than HasFeature's.
- return llvm::StringSwitch<bool>(Extension)
- // C11 features supported by other languages as extensions.
- .Case("c_alignas", true)
- .Case("c_alignof", true)
- .Case("c_atomic", true)
- .Case("c_generic_selections", true)
- .Case("c_static_assert", true)
- .Case("c_thread_local", PP.getTargetInfo().isTLSSupported())
- // C++11 features supported by other languages as extensions.
- .Case("cxx_atomic", LangOpts.CPlusPlus)
- .Case("cxx_deleted_functions", LangOpts.CPlusPlus)
- .Case("cxx_explicit_conversions", LangOpts.CPlusPlus)
- .Case("cxx_inline_namespaces", LangOpts.CPlusPlus)
- .Case("cxx_local_type_template_args", LangOpts.CPlusPlus)
- .Case("cxx_nonstatic_member_init", LangOpts.CPlusPlus)
- .Case("cxx_override_control", LangOpts.CPlusPlus)
- .Case("cxx_range_for", LangOpts.CPlusPlus)
- .Case("cxx_reference_qualified_functions", LangOpts.CPlusPlus)
- .Case("cxx_rvalue_references", LangOpts.CPlusPlus)
- .Case("cxx_variadic_templates", LangOpts.CPlusPlus)
- // C++14 features supported by other languages as extensions.
- .Case("cxx_binary_literals", true)
- .Case("cxx_init_captures", LangOpts.CPlusPlus11)
- .Case("cxx_variable_templates", LangOpts.CPlusPlus)
- // Miscellaneous language extensions
- .Case("overloadable_unmarked", true)
- .Default(false);
-}
-
-/// EvaluateHasIncludeCommon - Process a '__has_include("path")'
-/// or '__has_include_next("path")' expression.
-/// Returns true if successful.
-static bool EvaluateHasIncludeCommon(Token &Tok,
- IdentifierInfo *II, Preprocessor &PP,
- const DirectoryLookup *LookupFrom,
- const FileEntry *LookupFromFile) {
- // Save the location of the current token. If a '(' is later found, use
- // that location. If not, use the end of this location instead.
- SourceLocation LParenLoc = Tok.getLocation();
-
- // These expressions are only allowed within a preprocessor directive.
- if (!PP.isParsingIfOrElifDirective()) {
- PP.Diag(LParenLoc, diag::err_pp_directive_required) << II->getName();
- // Return a valid identifier token.
- assert(Tok.is(tok::identifier));
- Tok.setIdentifierInfo(II);
- return false;
- }
-
- // Get '('.
- PP.LexNonComment(Tok);
-
- // Ensure we have a '('.
- if (Tok.isNot(tok::l_paren)) {
- // No '(', use end of last token.
- LParenLoc = PP.getLocForEndOfToken(LParenLoc);
- PP.Diag(LParenLoc, diag::err_pp_expected_after) << II << tok::l_paren;
- // If the next token looks like a filename or the start of one,
- // assume it is and process it as such.
- if (!Tok.is(tok::angle_string_literal) && !Tok.is(tok::string_literal) &&
- !Tok.is(tok::less))
- return false;
- } else {
- // Save '(' location for possible missing ')' message.
- LParenLoc = Tok.getLocation();
-
- if (PP.getCurrentLexer()) {
- // Get the file name.
- PP.getCurrentLexer()->LexIncludeFilename(Tok);
- } else {
- // We're in a macro, so we can't use LexIncludeFilename; just
- // grab the next token.
- PP.Lex(Tok);
- }
- }
-
- // Reserve a buffer to get the spelling.
- SmallString<128> FilenameBuffer;
- StringRef Filename;
- SourceLocation EndLoc;
-
- switch (Tok.getKind()) {
- case tok::eod:
- // If the token kind is EOD, the error has already been diagnosed.
- return false;
-
- case tok::angle_string_literal:
- case tok::string_literal: {
- bool Invalid = false;
- Filename = PP.getSpelling(Tok, FilenameBuffer, &Invalid);
- if (Invalid)
- return false;
- break;
- }
-
- case tok::less:
- // This could be a <foo/bar.h> file coming from a macro expansion. In this
- // case, glue the tokens together into FilenameBuffer and interpret those.
- FilenameBuffer.push_back('<');
- if (PP.ConcatenateIncludeName(FilenameBuffer, EndLoc)) {
- // Let the caller know a <eod> was found by changing the Token kind.
- Tok.setKind(tok::eod);
- return false; // Found <eod> but no ">"? Diagnostic already emitted.
- }
- Filename = FilenameBuffer;
- break;
- default:
- PP.Diag(Tok.getLocation(), diag::err_pp_expects_filename);
- return false;
- }
-
- SourceLocation FilenameLoc = Tok.getLocation();
-
- // Get ')'.
- PP.LexNonComment(Tok);
-
- // Ensure we have a trailing ).
- if (Tok.isNot(tok::r_paren)) {
- PP.Diag(PP.getLocForEndOfToken(FilenameLoc), diag::err_pp_expected_after)
- << II << tok::r_paren;
- PP.Diag(LParenLoc, diag::note_matching) << tok::l_paren;
- return false;
- }
-
- bool isAngled = PP.GetIncludeFilenameSpelling(Tok.getLocation(), Filename);
- // If GetIncludeFilenameSpelling set the start ptr to null, there was an
- // error.
- if (Filename.empty())
- return false;
-
- // Search include directories.
- const DirectoryLookup *CurDir;
- const FileEntry *File =
- PP.LookupFile(FilenameLoc, Filename, isAngled, LookupFrom, LookupFromFile,
- CurDir, nullptr, nullptr, nullptr, nullptr);
-
- // Get the result value. A result of true means the file exists.
- return File != nullptr;
-}
-
-/// EvaluateHasInclude - Process a '__has_include("path")' expression.
-/// Returns true if successful.
-static bool EvaluateHasInclude(Token &Tok, IdentifierInfo *II,
- Preprocessor &PP) {
- return EvaluateHasIncludeCommon(Tok, II, PP, nullptr, nullptr);
-}
-
-/// EvaluateHasIncludeNext - Process '__has_include_next("path")' expression.
-/// Returns true if successful.
-static bool EvaluateHasIncludeNext(Token &Tok,
- IdentifierInfo *II, Preprocessor &PP) {
- // __has_include_next is like __has_include, except that we start
- // searching after the current found directory. If we can't do this,
- // issue a diagnostic.
- // FIXME: Factor out duplication with
- // Preprocessor::HandleIncludeNextDirective.
- const DirectoryLookup *Lookup = PP.GetCurDirLookup();
- const FileEntry *LookupFromFile = nullptr;
- if (PP.isInPrimaryFile() && PP.getLangOpts().IsHeaderFile) {
- // If the main file is a header, then it's either for PCH/AST generation,
- // or libclang opened it. Either way, handle it as a normal include below
- // and do not complain about __has_include_next.
- } else if (PP.isInPrimaryFile()) {
- Lookup = nullptr;
- PP.Diag(Tok, diag::pp_include_next_in_primary);
- } else if (PP.getCurrentLexerSubmodule()) {
- // Start looking up in the directory *after* the one in which the current
- // file would be found, if any.
- assert(PP.getCurrentLexer() && "#include_next directive in macro?");
- LookupFromFile = PP.getCurrentLexer()->getFileEntry();
- Lookup = nullptr;
- } else if (!Lookup) {
- PP.Diag(Tok, diag::pp_include_next_absolute_path);
- } else {
- // Start looking up in the next directory.
- ++Lookup;
- }
-
- return EvaluateHasIncludeCommon(Tok, II, PP, Lookup, LookupFromFile);
-}
-
-/// \brief Process single-argument builtin feature-like macros that return
-/// integer values.
-static void EvaluateFeatureLikeBuiltinMacro(llvm::raw_svector_ostream& OS,
- Token &Tok, IdentifierInfo *II,
- Preprocessor &PP,
- llvm::function_ref<
- int(Token &Tok,
- bool &HasLexedNextTok)> Op) {
- // Parse the initial '('.
- PP.LexUnexpandedToken(Tok);
- if (Tok.isNot(tok::l_paren)) {
- PP.Diag(Tok.getLocation(), diag::err_pp_expected_after) << II
- << tok::l_paren;
-
- // Provide a dummy '0' value on output stream to elide further errors.
- if (!Tok.isOneOf(tok::eof, tok::eod)) {
- OS << 0;
- Tok.setKind(tok::numeric_constant);
- }
- return;
- }
-
- unsigned ParenDepth = 1;
- SourceLocation LParenLoc = Tok.getLocation();
- llvm::Optional<int> Result;
-
- Token ResultTok;
- bool SuppressDiagnostic = false;
- while (true) {
- // Parse next token.
- PP.LexUnexpandedToken(Tok);
-
-already_lexed:
- switch (Tok.getKind()) {
- case tok::eof:
- case tok::eod:
- // Don't provide even a dummy value if the eod or eof marker is
- // reached. Simply provide a diagnostic.
- PP.Diag(Tok.getLocation(), diag::err_unterm_macro_invoc);
- return;
-
- case tok::comma:
- if (!SuppressDiagnostic) {
- PP.Diag(Tok.getLocation(), diag::err_too_many_args_in_macro_invoc);
- SuppressDiagnostic = true;
- }
- continue;
-
- case tok::l_paren:
- ++ParenDepth;
- if (Result.hasValue())
- break;
- if (!SuppressDiagnostic) {
- PP.Diag(Tok.getLocation(), diag::err_pp_nested_paren) << II;
- SuppressDiagnostic = true;
- }
- continue;
-
- case tok::r_paren:
- if (--ParenDepth > 0)
- continue;
-
- // The last ')' has been reached; return the value if one found or
- // a diagnostic and a dummy value.
- if (Result.hasValue())
- OS << Result.getValue();
- else {
- OS << 0;
- if (!SuppressDiagnostic)
- PP.Diag(Tok.getLocation(), diag::err_too_few_args_in_macro_invoc);
- }
- Tok.setKind(tok::numeric_constant);
- return;
-
- default: {
- // Parse the macro argument, if one not found so far.
- if (Result.hasValue())
- break;
-
- bool HasLexedNextToken = false;
- Result = Op(Tok, HasLexedNextToken);
- ResultTok = Tok;
- if (HasLexedNextToken)
- goto already_lexed;
- continue;
- }
- }
-
- // Diagnose missing ')'.
- if (!SuppressDiagnostic) {
- if (auto Diag = PP.Diag(Tok.getLocation(), diag::err_pp_expected_after)) {
- if (IdentifierInfo *LastII = ResultTok.getIdentifierInfo())
- Diag << LastII;
- else
- Diag << ResultTok.getKind();
- Diag << tok::r_paren << ResultTok.getLocation();
- }
- PP.Diag(LParenLoc, diag::note_matching) << tok::l_paren;
- SuppressDiagnostic = true;
- }
- }
-}
-
-/// \brief Helper function to return the IdentifierInfo structure of a Token
-/// or generate a diagnostic if none available.
-static IdentifierInfo *ExpectFeatureIdentifierInfo(Token &Tok,
- Preprocessor &PP,
- signed DiagID) {
- IdentifierInfo *II;
- if (!Tok.isAnnotation() && (II = Tok.getIdentifierInfo()))
- return II;
-
- PP.Diag(Tok.getLocation(), DiagID);
- return nullptr;
-}
-
-/// ExpandBuiltinMacro - If an identifier token is read that is to be expanded
-/// as a builtin macro, handle it and return the next token as 'Tok'.
-void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
- // Figure out which token this is.
- IdentifierInfo *II = Tok.getIdentifierInfo();
- assert(II && "Can't be a macro without id info!");
-
- // If this is an _Pragma or Microsoft __pragma directive, expand it,
- // invoke the pragma handler, then lex the token after it.
- if (II == Ident_Pragma)
- return Handle_Pragma(Tok);
- else if (II == Ident__pragma) // in non-MS mode this is null
- return HandleMicrosoft__pragma(Tok);
-
- ++NumBuiltinMacroExpanded;
-
- SmallString<128> TmpBuffer;
- llvm::raw_svector_ostream OS(TmpBuffer);
-
- // Set up the return result.
- Tok.setIdentifierInfo(nullptr);
- Tok.clearFlag(Token::NeedsCleaning);
-
- if (II == Ident__LINE__) {
- // C99 6.10.8: "__LINE__: The presumed line number (within the current
- // source file) of the current source line (an integer constant)". This can
- // be affected by #line.
- SourceLocation Loc = Tok.getLocation();
-
- // Advance to the location of the first _, this might not be the first byte
- // of the token if it starts with an escaped newline.
- Loc = AdvanceToTokenCharacter(Loc, 0);
-
- // One wrinkle here is that GCC expands __LINE__ to location of the *end* of
- // a macro expansion. This doesn't matter for object-like macros, but
- // can matter for a function-like macro that expands to contain __LINE__.
- // Skip down through expansion points until we find a file loc for the
- // end of the expansion history.
- Loc = SourceMgr.getExpansionRange(Loc).second;
- PresumedLoc PLoc = SourceMgr.getPresumedLoc(Loc);
-
- // __LINE__ expands to a simple numeric value.
- OS << (PLoc.isValid()? PLoc.getLine() : 1);
- Tok.setKind(tok::numeric_constant);
- } else if (II == Ident__FILE__ || II == Ident__BASE_FILE__) {
- // C99 6.10.8: "__FILE__: The presumed name of the current source file (a
- // character string literal)". This can be affected by #line.
- PresumedLoc PLoc = SourceMgr.getPresumedLoc(Tok.getLocation());
-
- // __BASE_FILE__ is a GNU extension that returns the top of the presumed
- // #include stack instead of the current file.
- if (II == Ident__BASE_FILE__ && PLoc.isValid()) {
- SourceLocation NextLoc = PLoc.getIncludeLoc();
- while (NextLoc.isValid()) {
- PLoc = SourceMgr.getPresumedLoc(NextLoc);
- if (PLoc.isInvalid())
- break;
-
- NextLoc = PLoc.getIncludeLoc();
- }
- }
-
- // Escape this filename. Turn '\' -> '\\' '"' -> '\"'
- SmallString<128> FN;
- if (PLoc.isValid()) {
- FN += PLoc.getFilename();
- Lexer::Stringify(FN);
- OS << '"' << FN << '"';
- }
- Tok.setKind(tok::string_literal);
- } else if (II == Ident__DATE__) {
- Diag(Tok.getLocation(), diag::warn_pp_date_time);
- if (!DATELoc.isValid())
- ComputeDATE_TIME(DATELoc, TIMELoc, *this);
- Tok.setKind(tok::string_literal);
- Tok.setLength(strlen("\"Mmm dd yyyy\""));
- Tok.setLocation(SourceMgr.createExpansionLoc(DATELoc, Tok.getLocation(),
- Tok.getLocation(),
- Tok.getLength()));
- return;
- } else if (II == Ident__TIME__) {
- Diag(Tok.getLocation(), diag::warn_pp_date_time);
- if (!TIMELoc.isValid())
- ComputeDATE_TIME(DATELoc, TIMELoc, *this);
- Tok.setKind(tok::string_literal);
- Tok.setLength(strlen("\"hh:mm:ss\""));
- Tok.setLocation(SourceMgr.createExpansionLoc(TIMELoc, Tok.getLocation(),
- Tok.getLocation(),
- Tok.getLength()));
- return;
- } else if (II == Ident__INCLUDE_LEVEL__) {
- // Compute the presumed include depth of this token. This can be affected
- // by GNU line markers.
- unsigned Depth = 0;
-
- PresumedLoc PLoc = SourceMgr.getPresumedLoc(Tok.getLocation());
- if (PLoc.isValid()) {
- PLoc = SourceMgr.getPresumedLoc(PLoc.getIncludeLoc());
- for (; PLoc.isValid(); ++Depth)
- PLoc = SourceMgr.getPresumedLoc(PLoc.getIncludeLoc());
- }
-
- // __INCLUDE_LEVEL__ expands to a simple numeric value.
- OS << Depth;
- Tok.setKind(tok::numeric_constant);
- } else if (II == Ident__TIMESTAMP__) {
- Diag(Tok.getLocation(), diag::warn_pp_date_time);
- // MSVC, ICC, GCC, VisualAge C++ extension. The generated string should be
- // of the form "Ddd Mmm dd hh::mm::ss yyyy", which is returned by asctime.
-
- // Get the file that we are lexing out of. If we're currently lexing from
- // a macro, dig into the include stack.
- const FileEntry *CurFile = nullptr;
- PreprocessorLexer *TheLexer = getCurrentFileLexer();
-
- if (TheLexer)
- CurFile = SourceMgr.getFileEntryForID(TheLexer->getFileID());
-
- const char *Result;
- if (CurFile) {
- time_t TT = CurFile->getModificationTime();
- struct tm *TM = localtime(&TT);
- Result = asctime(TM);
- } else {
- Result = "??? ??? ?? ??:??:?? ????\n";
- }
- // Surround the string with " and strip the trailing newline.
- OS << '"' << StringRef(Result).drop_back() << '"';
- Tok.setKind(tok::string_literal);
- } else if (II == Ident__COUNTER__) {
- // __COUNTER__ expands to a simple numeric value.
- OS << CounterValue++;
- Tok.setKind(tok::numeric_constant);
- } else if (II == Ident__has_feature) {
- EvaluateFeatureLikeBuiltinMacro(OS, Tok, II, *this,
- [this](Token &Tok, bool &HasLexedNextToken) -> int {
- IdentifierInfo *II = ExpectFeatureIdentifierInfo(Tok, *this,
- diag::err_feature_check_malformed);
- return II && HasFeature(*this, II->getName());
- });
- } else if (II == Ident__has_extension) {
- EvaluateFeatureLikeBuiltinMacro(OS, Tok, II, *this,
- [this](Token &Tok, bool &HasLexedNextToken) -> int {
- IdentifierInfo *II = ExpectFeatureIdentifierInfo(Tok, *this,
- diag::err_feature_check_malformed);
- return II && HasExtension(*this, II->getName());
- });
- } else if (II == Ident__has_builtin) {
- EvaluateFeatureLikeBuiltinMacro(OS, Tok, II, *this,
- [this](Token &Tok, bool &HasLexedNextToken) -> int {
- IdentifierInfo *II = ExpectFeatureIdentifierInfo(Tok, *this,
- diag::err_feature_check_malformed);
- if (!II)
- return false;
- else if (II->getBuiltinID() != 0)
- return true;
- else {
- const LangOptions &LangOpts = getLangOpts();
- return llvm::StringSwitch<bool>(II->getName())
- .Case("__make_integer_seq", LangOpts.CPlusPlus)
- .Case("__type_pack_element", LangOpts.CPlusPlus)
- .Case("__builtin_available", true)
- .Default(false);
- }
- });
- } else if (II == Ident__is_identifier) {
- EvaluateFeatureLikeBuiltinMacro(OS, Tok, II, *this,
- [](Token &Tok, bool &HasLexedNextToken) -> int {
- return Tok.is(tok::identifier);
- });
- } else if (II == Ident__has_attribute) {
- EvaluateFeatureLikeBuiltinMacro(OS, Tok, II, *this,
- [this](Token &Tok, bool &HasLexedNextToken) -> int {
- IdentifierInfo *II = ExpectFeatureIdentifierInfo(Tok, *this,
- diag::err_feature_check_malformed);
- return II ? hasAttribute(AttrSyntax::GNU, nullptr, II,
- getTargetInfo(), getLangOpts()) : 0;
- });
- } else if (II == Ident__has_declspec) {
- EvaluateFeatureLikeBuiltinMacro(OS, Tok, II, *this,
- [this](Token &Tok, bool &HasLexedNextToken) -> int {
- IdentifierInfo *II = ExpectFeatureIdentifierInfo(Tok, *this,
- diag::err_feature_check_malformed);
- return II ? hasAttribute(AttrSyntax::Declspec, nullptr, II,
- getTargetInfo(), getLangOpts()) : 0;
- });
- } else if (II == Ident__has_cpp_attribute) {
- EvaluateFeatureLikeBuiltinMacro(OS, Tok, II, *this,
- [this](Token &Tok, bool &HasLexedNextToken) -> int {
- IdentifierInfo *ScopeII = nullptr;
- IdentifierInfo *II = ExpectFeatureIdentifierInfo(Tok, *this,
- diag::err_feature_check_malformed);
- if (!II)
- return false;
-
- // It is possible to receive a scope token. Read the "::", if it is
- // available, and the subsequent identifier.
- LexUnexpandedToken(Tok);
- if (Tok.isNot(tok::coloncolon))
- HasLexedNextToken = true;
- else {
- ScopeII = II;
- LexUnexpandedToken(Tok);
- II = ExpectFeatureIdentifierInfo(Tok, *this,
- diag::err_feature_check_malformed);
- }
-
- return II ? hasAttribute(AttrSyntax::CXX, ScopeII, II,
- getTargetInfo(), getLangOpts()) : 0;
- });
- } else if (II == Ident__has_include ||
- II == Ident__has_include_next) {
- // The argument to these two builtins should be a parenthesized
- // file name string literal using angle brackets (<>) or
- // double-quotes ("").
- bool Value;
- if (II == Ident__has_include)
- Value = EvaluateHasInclude(Tok, II, *this);
- else
- Value = EvaluateHasIncludeNext(Tok, II, *this);
-
- if (Tok.isNot(tok::r_paren))
- return;
- OS << (int)Value;
- Tok.setKind(tok::numeric_constant);
- } else if (II == Ident__has_warning) {
- // The argument should be a parenthesized string literal.
- EvaluateFeatureLikeBuiltinMacro(OS, Tok, II, *this,
- [this](Token &Tok, bool &HasLexedNextToken) -> int {
- std::string WarningName;
- SourceLocation StrStartLoc = Tok.getLocation();
-
- HasLexedNextToken = Tok.is(tok::string_literal);
- if (!FinishLexStringLiteral(Tok, WarningName, "'__has_warning'",
- /*MacroExpansion=*/false))
- return false;
-
- // FIXME: Should we accept "-R..." flags here, or should that be
- // handled by a separate __has_remark?
- if (WarningName.size() < 3 || WarningName[0] != '-' ||
- WarningName[1] != 'W') {
- Diag(StrStartLoc, diag::warn_has_warning_invalid_option);
- return false;
- }
-
- // Finally, check if the warning flags maps to a diagnostic group.
- // We construct a SmallVector here to talk to getDiagnosticIDs().
- // Although we don't use the result, this isn't a hot path, and not
- // worth special casing.
- SmallVector<diag::kind, 10> Diags;
- return !getDiagnostics().getDiagnosticIDs()->
- getDiagnosticsInGroup(diag::Flavor::WarningOrError,
- WarningName.substr(2), Diags);
- });
- } else if (II == Ident__building_module) {
- // The argument to this builtin should be an identifier. The
- // builtin evaluates to 1 when that identifier names the module we are
- // currently building.
- EvaluateFeatureLikeBuiltinMacro(OS, Tok, II, *this,
- [this](Token &Tok, bool &HasLexedNextToken) -> int {
- IdentifierInfo *II = ExpectFeatureIdentifierInfo(Tok, *this,
- diag::err_expected_id_building_module);
- return getLangOpts().isCompilingModule() && II &&
- (II->getName() == getLangOpts().CurrentModule);
- });
- } else if (II == Ident__MODULE__) {
- // The current module as an identifier.
- OS << getLangOpts().CurrentModule;
- IdentifierInfo *ModuleII = getIdentifierInfo(getLangOpts().CurrentModule);
- Tok.setIdentifierInfo(ModuleII);
- Tok.setKind(ModuleII->getTokenID());
- } else if (II == Ident__identifier) {
- SourceLocation Loc = Tok.getLocation();
-
- // We're expecting '__identifier' '(' identifier ')'. Try to recover
- // if the parens are missing.
- LexNonComment(Tok);
- if (Tok.isNot(tok::l_paren)) {
- // No '(', use end of last token.
- Diag(getLocForEndOfToken(Loc), diag::err_pp_expected_after)
- << II << tok::l_paren;
- // If the next token isn't valid as our argument, we can't recover.
- if (!Tok.isAnnotation() && Tok.getIdentifierInfo())
- Tok.setKind(tok::identifier);
- return;
- }
-
- SourceLocation LParenLoc = Tok.getLocation();
- LexNonComment(Tok);
-
- if (!Tok.isAnnotation() && Tok.getIdentifierInfo())
- Tok.setKind(tok::identifier);
- else {
- Diag(Tok.getLocation(), diag::err_pp_identifier_arg_not_identifier)
- << Tok.getKind();
- // Don't walk past anything that's not a real token.
- if (Tok.isOneOf(tok::eof, tok::eod) || Tok.isAnnotation())
- return;
- }
-
- // Discard the ')', preserving 'Tok' as our result.
- Token RParen;
- LexNonComment(RParen);
- if (RParen.isNot(tok::r_paren)) {
- Diag(getLocForEndOfToken(Tok.getLocation()), diag::err_pp_expected_after)
- << Tok.getKind() << tok::r_paren;
- Diag(LParenLoc, diag::note_matching) << tok::l_paren;
- }
- return;
- } else {
- llvm_unreachable("Unknown identifier!");
- }
- CreateString(OS.str(), Tok, Tok.getLocation(), Tok.getLocation());
-}
-
-void Preprocessor::markMacroAsUsed(MacroInfo *MI) {
- // If the 'used' status changed, and the macro requires 'unused' warning,
- // remove its SourceLocation from the warn-for-unused-macro locations.
- if (MI->isWarnIfUnused() && !MI->isUsed())
- WarnUnusedMacroLocs.erase(MI->getDefinitionLoc());
- MI->setIsUsed(true);
-}
+//===--- MacroExpansion.cpp - Top level Macro Expansion -------------------===//\r
+//\r
+// The LLVM Compiler Infrastructure\r
+//\r
+// This file is distributed under the University of Illinois Open Source\r
+// License. See LICENSE.TXT for details.\r
+//\r
+//===----------------------------------------------------------------------===//\r
+//\r
+// This file implements the top level handling of macro expansion for the\r
+// preprocessor.\r
+//\r
+//===----------------------------------------------------------------------===//\r
+\r
+#include "clang/Basic/Attributes.h"\r
+#include "clang/Basic/FileManager.h"\r
+#include "clang/Basic/IdentifierTable.h"\r
+#include "clang/Basic/LLVM.h"\r
+#include "clang/Basic/LangOptions.h"\r
+#include "clang/Basic/ObjCRuntime.h"\r
+#include "clang/Basic/SourceLocation.h"\r
+#include "clang/Basic/TargetInfo.h"\r
+#include "clang/Lex/CodeCompletionHandler.h"\r
+#include "clang/Lex/DirectoryLookup.h"\r
+#include "clang/Lex/ExternalPreprocessorSource.h"\r
+#include "clang/Lex/LexDiagnostic.h"\r
+#include "clang/Lex/MacroArgs.h"\r
+#include "clang/Lex/MacroInfo.h"\r
+#include "clang/Lex/Preprocessor.h"\r
+#include "clang/Lex/PreprocessorLexer.h"\r
+#include "clang/Lex/PTHLexer.h"\r
+#include "clang/Lex/Token.h"\r
+#include "llvm/ADT/ArrayRef.h"\r
+#include "llvm/ADT/DenseMap.h"\r
+#include "llvm/ADT/DenseSet.h"\r
+#include "llvm/ADT/FoldingSet.h"\r
+#include "llvm/ADT/None.h"\r
+#include "llvm/ADT/Optional.h"\r
+#include "llvm/ADT/SmallString.h"\r
+#include "llvm/ADT/SmallVector.h"\r
+#include "llvm/ADT/STLExtras.h"\r
+#include "llvm/ADT/StringRef.h"\r
+#include "llvm/ADT/StringSwitch.h"\r
+#include "llvm/Config/llvm-config.h"\r
+#include "llvm/Support/Casting.h"\r
+#include "llvm/Support/ErrorHandling.h"\r
+#include "llvm/Support/Format.h"\r
+#include "llvm/Support/raw_ostream.h"\r
+#include <algorithm>\r
+#include <cassert>\r
+#include <cstddef>\r
+#include <cstring>\r
+#include <ctime>\r
+#include <string>\r
+#include <tuple>\r
+#include <utility>\r
+\r
+using namespace clang;\r
+\r
+MacroDirective *\r
+Preprocessor::getLocalMacroDirectiveHistory(const IdentifierInfo *II) const {\r
+ if (!II->hadMacroDefinition())\r
+ return nullptr;\r
+ auto Pos = CurSubmoduleState->Macros.find(II);\r
+ return Pos == CurSubmoduleState->Macros.end() ? nullptr\r
+ : Pos->second.getLatest();\r
+}\r
+\r
+void Preprocessor::appendMacroDirective(IdentifierInfo *II, MacroDirective *MD){\r
+ assert(MD && "MacroDirective should be non-zero!");\r
+ assert(!MD->getPrevious() && "Already attached to a MacroDirective history.");\r
+\r
+ MacroState &StoredMD = CurSubmoduleState->Macros[II];\r
+ auto *OldMD = StoredMD.getLatest();\r
+ MD->setPrevious(OldMD);\r
+ StoredMD.setLatest(MD);\r
+ StoredMD.overrideActiveModuleMacros(*this, II);\r
+\r
+ if (needModuleMacros()) {\r
+ // Track that we created a new macro directive, so we know we should\r
+ // consider building a ModuleMacro for it when we get to the end of\r
+ // the module.\r
+ PendingModuleMacroNames.push_back(II);\r
+ }\r
+\r
+ // Set up the identifier as having associated macro history.\r
+ II->setHasMacroDefinition(true);\r
+ if (!MD->isDefined() && LeafModuleMacros.find(II) == LeafModuleMacros.end())\r
+ II->setHasMacroDefinition(false);\r
+ if (II->isFromAST())\r
+ II->setChangedSinceDeserialization();\r
+}\r
+\r
+void Preprocessor::setLoadedMacroDirective(IdentifierInfo *II,\r
+ MacroDirective *ED,\r
+ MacroDirective *MD) {\r
+ // Normally, when a macro is defined, it goes through appendMacroDirective()\r
+ // above, which chains a macro to previous defines, undefs, etc.\r
+ // However, in a pch, the whole macro history up to the end of the pch is\r
+ // stored, so ASTReader goes through this function instead.\r
+ // However, built-in macros are already registered in the Preprocessor\r
+ // ctor, and ASTWriter stops writing the macro chain at built-in macros,\r
+ // so in that case the chain from the pch needs to be spliced to the existing\r
+ // built-in.\r
+\r
+ assert(II && MD);\r
+ MacroState &StoredMD = CurSubmoduleState->Macros[II];\r
+\r
+ if (auto *OldMD = StoredMD.getLatest()) {\r
+ // shouldIgnoreMacro() in ASTWriter also stops at macros from the\r
+ // predefines buffer in module builds. However, in module builds, modules\r
+ // are loaded completely before predefines are processed, so StoredMD\r
+ // will be nullptr for them when they're loaded. StoredMD should only be\r
+ // non-nullptr for builtins read from a pch file.\r
+ assert(OldMD->getMacroInfo()->isBuiltinMacro() &&\r
+ "only built-ins should have an entry here");\r
+ assert(!OldMD->getPrevious() && "builtin should only have a single entry");\r
+ ED->setPrevious(OldMD);\r
+ StoredMD.setLatest(MD);\r
+ } else {\r
+ StoredMD = MD;\r
+ }\r
+\r
+ // Setup the identifier as having associated macro history.\r
+ II->setHasMacroDefinition(true);\r
+ if (!MD->isDefined() && LeafModuleMacros.find(II) == LeafModuleMacros.end())\r
+ II->setHasMacroDefinition(false);\r
+}\r
+\r
+ModuleMacro *Preprocessor::addModuleMacro(Module *Mod, IdentifierInfo *II,\r
+ MacroInfo *Macro,\r
+ ArrayRef<ModuleMacro *> Overrides,\r
+ bool &New) {\r
+ llvm::FoldingSetNodeID ID;\r
+ ModuleMacro::Profile(ID, Mod, II);\r
+\r
+ void *InsertPos;\r
+ if (auto *MM = ModuleMacros.FindNodeOrInsertPos(ID, InsertPos)) {\r
+ New = false;\r
+ return MM;\r
+ }\r
+\r
+ auto *MM = ModuleMacro::create(*this, Mod, II, Macro, Overrides);\r
+ ModuleMacros.InsertNode(MM, InsertPos);\r
+\r
+ // Each overridden macro is now overridden by one more macro.\r
+ bool HidAny = false;\r
+ for (auto *O : Overrides) {\r
+ HidAny |= (O->NumOverriddenBy == 0);\r
+ ++O->NumOverriddenBy;\r
+ }\r
+\r
+ // If we were the first overrider for any macro, it's no longer a leaf.\r
+ auto &LeafMacros = LeafModuleMacros[II];\r
+ if (HidAny) {\r
+ LeafMacros.erase(std::remove_if(LeafMacros.begin(), LeafMacros.end(),\r
+ [](ModuleMacro *MM) {\r
+ return MM->NumOverriddenBy != 0;\r
+ }),\r
+ LeafMacros.end());\r
+ }\r
+\r
+ // The new macro is always a leaf macro.\r
+ LeafMacros.push_back(MM);\r
+ // The identifier now has defined macros (that may or may not be visible).\r
+ II->setHasMacroDefinition(true);\r
+\r
+ New = true;\r
+ return MM;\r
+}\r
+\r
+ModuleMacro *Preprocessor::getModuleMacro(Module *Mod, IdentifierInfo *II) {\r
+ llvm::FoldingSetNodeID ID;\r
+ ModuleMacro::Profile(ID, Mod, II);\r
+\r
+ void *InsertPos;\r
+ return ModuleMacros.FindNodeOrInsertPos(ID, InsertPos);\r
+}\r
+\r
+void Preprocessor::updateModuleMacroInfo(const IdentifierInfo *II,\r
+ ModuleMacroInfo &Info) {\r
+ assert(Info.ActiveModuleMacrosGeneration !=\r
+ CurSubmoduleState->VisibleModules.getGeneration() &&\r
+ "don't need to update this macro name info");\r
+ Info.ActiveModuleMacrosGeneration =\r
+ CurSubmoduleState->VisibleModules.getGeneration();\r
+\r
+ auto Leaf = LeafModuleMacros.find(II);\r
+ if (Leaf == LeafModuleMacros.end()) {\r
+ // No imported macros at all: nothing to do.\r
+ return;\r
+ }\r
+\r
+ Info.ActiveModuleMacros.clear();\r
+\r
+ // Every macro that's locally overridden is overridden by a visible macro.\r
+ llvm::DenseMap<ModuleMacro *, int> NumHiddenOverrides;\r
+ for (auto *O : Info.OverriddenMacros)\r
+ NumHiddenOverrides[O] = -1;\r
+\r
+ // Collect all macros that are not overridden by a visible macro.\r
+ llvm::SmallVector<ModuleMacro *, 16> Worklist;\r
+ for (auto *LeafMM : Leaf->second) {\r
+ assert(LeafMM->getNumOverridingMacros() == 0 && "leaf macro overridden");\r
+ if (NumHiddenOverrides.lookup(LeafMM) == 0)\r
+ Worklist.push_back(LeafMM);\r
+ }\r
+ while (!Worklist.empty()) {\r
+ auto *MM = Worklist.pop_back_val();\r
+ if (CurSubmoduleState->VisibleModules.isVisible(MM->getOwningModule())) {\r
+ // We only care about collecting definitions; undefinitions only act\r
+ // to override other definitions.\r
+ if (MM->getMacroInfo())\r
+ Info.ActiveModuleMacros.push_back(MM);\r
+ } else {\r
+ for (auto *O : MM->overrides())\r
+ if ((unsigned)++NumHiddenOverrides[O] == O->getNumOverridingMacros())\r
+ Worklist.push_back(O);\r
+ }\r
+ }\r
+ // Our reverse postorder walk found the macros in reverse order.\r
+ std::reverse(Info.ActiveModuleMacros.begin(), Info.ActiveModuleMacros.end());\r
+\r
+ // Determine whether the macro name is ambiguous.\r
+ MacroInfo *MI = nullptr;\r
+ bool IsSystemMacro = true;\r
+ bool IsAmbiguous = false;\r
+ if (auto *MD = Info.MD) {\r
+ while (MD && isa<VisibilityMacroDirective>(MD))\r
+ MD = MD->getPrevious();\r
+ if (auto *DMD = dyn_cast_or_null<DefMacroDirective>(MD)) {\r
+ MI = DMD->getInfo();\r
+ IsSystemMacro &= SourceMgr.isInSystemHeader(DMD->getLocation());\r
+ }\r
+ }\r
+ for (auto *Active : Info.ActiveModuleMacros) {\r
+ auto *NewMI = Active->getMacroInfo();\r
+\r
+ // Before marking the macro as ambiguous, check if this is a case where\r
+ // both macros are in system headers. If so, we trust that the system\r
+ // did not get it wrong. This also handles cases where Clang's own\r
+ // headers have a different spelling of certain system macros:\r
+ // #define LONG_MAX __LONG_MAX__ (clang's limits.h)\r
+ // #define LONG_MAX 0x7fffffffffffffffL (system's limits.h)\r
+ //\r
+ // FIXME: Remove the defined-in-system-headers check. clang's limits.h\r
+ // overrides the system limits.h's macros, so there's no conflict here.\r
+ if (MI && NewMI != MI &&\r
+ !MI->isIdenticalTo(*NewMI, *this, /*Syntactically=*/true))\r
+ IsAmbiguous = true;\r
+ IsSystemMacro &= Active->getOwningModule()->IsSystem ||\r
+ SourceMgr.isInSystemHeader(NewMI->getDefinitionLoc());\r
+ MI = NewMI;\r
+ }\r
+ Info.IsAmbiguous = IsAmbiguous && !IsSystemMacro;\r
+}\r
+\r
+void Preprocessor::dumpMacroInfo(const IdentifierInfo *II) {\r
+ ArrayRef<ModuleMacro*> Leaf;\r
+ auto LeafIt = LeafModuleMacros.find(II);\r
+ if (LeafIt != LeafModuleMacros.end())\r
+ Leaf = LeafIt->second;\r
+ const MacroState *State = nullptr;\r
+ auto Pos = CurSubmoduleState->Macros.find(II);\r
+ if (Pos != CurSubmoduleState->Macros.end())\r
+ State = &Pos->second;\r
+\r
+ llvm::errs() << "MacroState " << State << " " << II->getNameStart();\r
+ if (State && State->isAmbiguous(*this, II))\r
+ llvm::errs() << " ambiguous";\r
+ if (State && !State->getOverriddenMacros().empty()) {\r
+ llvm::errs() << " overrides";\r
+ for (auto *O : State->getOverriddenMacros())\r
+ llvm::errs() << " " << O->getOwningModule()->getFullModuleName();\r
+ }\r
+ llvm::errs() << "\n";\r
+\r
+ // Dump local macro directives.\r
+ for (auto *MD = State ? State->getLatest() : nullptr; MD;\r
+ MD = MD->getPrevious()) {\r
+ llvm::errs() << " ";\r
+ MD->dump();\r
+ }\r
+\r
+ // Dump module macros.\r
+ llvm::DenseSet<ModuleMacro*> Active;\r
+ for (auto *MM : State ? State->getActiveModuleMacros(*this, II) : None)\r
+ Active.insert(MM);\r
+ llvm::DenseSet<ModuleMacro*> Visited;\r
+ llvm::SmallVector<ModuleMacro *, 16> Worklist(Leaf.begin(), Leaf.end());\r
+ while (!Worklist.empty()) {\r
+ auto *MM = Worklist.pop_back_val();\r
+ llvm::errs() << " ModuleMacro " << MM << " "\r
+ << MM->getOwningModule()->getFullModuleName();\r
+ if (!MM->getMacroInfo())\r
+ llvm::errs() << " undef";\r
+\r
+ if (Active.count(MM))\r
+ llvm::errs() << " active";\r
+ else if (!CurSubmoduleState->VisibleModules.isVisible(\r
+ MM->getOwningModule()))\r
+ llvm::errs() << " hidden";\r
+ else if (MM->getMacroInfo())\r
+ llvm::errs() << " overridden";\r
+\r
+ if (!MM->overrides().empty()) {\r
+ llvm::errs() << " overrides";\r
+ for (auto *O : MM->overrides()) {\r
+ llvm::errs() << " " << O->getOwningModule()->getFullModuleName();\r
+ if (Visited.insert(O).second)\r
+ Worklist.push_back(O);\r
+ }\r
+ }\r
+ llvm::errs() << "\n";\r
+ if (auto *MI = MM->getMacroInfo()) {\r
+ llvm::errs() << " ";\r
+ MI->dump();\r
+ llvm::errs() << "\n";\r
+ }\r
+ }\r
+}\r
+\r
+/// RegisterBuiltinMacro - Register the specified identifier in the identifier\r
+/// table and mark it as a builtin macro to be expanded.\r
+static IdentifierInfo *RegisterBuiltinMacro(Preprocessor &PP, const char *Name){\r
+ // Get the identifier.\r
+ IdentifierInfo *Id = PP.getIdentifierInfo(Name);\r
+\r
+ // Mark it as being a macro that is builtin.\r
+ MacroInfo *MI = PP.AllocateMacroInfo(SourceLocation());\r
+ MI->setIsBuiltinMacro();\r
+ PP.appendDefMacroDirective(Id, MI);\r
+ return Id;\r
+}\r
+\r
+/// RegisterBuiltinMacros - Register builtin macros, such as __LINE__ with the\r
+/// identifier table.\r
+void Preprocessor::RegisterBuiltinMacros() {\r
+ Ident__LINE__ = RegisterBuiltinMacro(*this, "__LINE__");\r
+ Ident__FILE__ = RegisterBuiltinMacro(*this, "__FILE__");\r
+ Ident__DATE__ = RegisterBuiltinMacro(*this, "__DATE__");\r
+ Ident__TIME__ = RegisterBuiltinMacro(*this, "__TIME__");\r
+ Ident__COUNTER__ = RegisterBuiltinMacro(*this, "__COUNTER__");\r
+ Ident_Pragma = RegisterBuiltinMacro(*this, "_Pragma");\r
+\r
+ // C++ Standing Document Extensions.\r
+ if (LangOpts.CPlusPlus)\r
+ Ident__has_cpp_attribute =\r
+ RegisterBuiltinMacro(*this, "__has_cpp_attribute");\r
+ else\r
+ Ident__has_cpp_attribute = nullptr;\r
+\r
+ // GCC Extensions.\r
+ Ident__BASE_FILE__ = RegisterBuiltinMacro(*this, "__BASE_FILE__");\r
+ Ident__INCLUDE_LEVEL__ = RegisterBuiltinMacro(*this, "__INCLUDE_LEVEL__");\r
+ Ident__TIMESTAMP__ = RegisterBuiltinMacro(*this, "__TIMESTAMP__");\r
+\r
+ // Microsoft Extensions.\r
+ if (LangOpts.MicrosoftExt) {\r
+ Ident__identifier = RegisterBuiltinMacro(*this, "__identifier");\r
+ Ident__pragma = RegisterBuiltinMacro(*this, "__pragma");\r
+ } else {\r
+ Ident__identifier = nullptr;\r
+ Ident__pragma = nullptr;\r
+ }\r
+\r
+ // Clang Extensions.\r
+ Ident__has_feature = RegisterBuiltinMacro(*this, "__has_feature");\r
+ Ident__has_extension = RegisterBuiltinMacro(*this, "__has_extension");\r
+ Ident__has_builtin = RegisterBuiltinMacro(*this, "__has_builtin");\r
+ Ident__has_attribute = RegisterBuiltinMacro(*this, "__has_attribute");\r
+ Ident__has_c_attribute = RegisterBuiltinMacro(*this, "__has_c_attribute");\r
+ Ident__has_declspec = RegisterBuiltinMacro(*this, "__has_declspec_attribute");\r
+ Ident__has_include = RegisterBuiltinMacro(*this, "__has_include");\r
+ Ident__has_include_next = RegisterBuiltinMacro(*this, "__has_include_next");\r
+ Ident__has_warning = RegisterBuiltinMacro(*this, "__has_warning");\r
+ Ident__is_identifier = RegisterBuiltinMacro(*this, "__is_identifier");\r
+\r
+ // Modules.\r
+ Ident__building_module = RegisterBuiltinMacro(*this, "__building_module");\r
+ if (!LangOpts.CurrentModule.empty())\r
+ Ident__MODULE__ = RegisterBuiltinMacro(*this, "__MODULE__");\r
+ else\r
+ Ident__MODULE__ = nullptr;\r
+}\r
+\r
+/// isTrivialSingleTokenExpansion - Return true if MI, which has a single token\r
+/// in its expansion, currently expands to that token literally.\r
+static bool isTrivialSingleTokenExpansion(const MacroInfo *MI,\r
+ const IdentifierInfo *MacroIdent,\r
+ Preprocessor &PP) {\r
+ IdentifierInfo *II = MI->getReplacementToken(0).getIdentifierInfo();\r
+\r
+ // If the token isn't an identifier, it's always literally expanded.\r
+ if (!II) return true;\r
+\r
+ // If the information about this identifier is out of date, update it from\r
+ // the external source.\r
+ if (II->isOutOfDate())\r
+ PP.getExternalSource()->updateOutOfDateIdentifier(*II);\r
+\r
+ // If the identifier is a macro, and if that macro is enabled, it may be\r
+ // expanded so it's not a trivial expansion.\r
+ if (auto *ExpansionMI = PP.getMacroInfo(II))\r
+ if (ExpansionMI->isEnabled() &&\r
+ // Fast expanding "#define X X" is ok, because X would be disabled.\r
+ II != MacroIdent)\r
+ return false;\r
+\r
+ // If this is an object-like macro invocation, it is safe to trivially expand\r
+ // it.\r
+ if (MI->isObjectLike()) return true;\r
+\r
+ // If this is a function-like macro invocation, it's safe to trivially expand\r
+ // as long as the identifier is not a macro argument.\r
+ return std::find(MI->param_begin(), MI->param_end(), II) == MI->param_end();\r
+}\r
+\r
+/// isNextPPTokenLParen - Determine whether the next preprocessor token to be\r
+/// lexed is a '('. If so, consume the token and return true, if not, this\r
+/// method should have no observable side-effect on the lexed tokens.\r
+bool Preprocessor::isNextPPTokenLParen() {\r
+ // Do some quick tests for rejection cases.\r
+ unsigned Val;\r
+ if (CurLexer)\r
+ Val = CurLexer->isNextPPTokenLParen();\r
+ else if (CurPTHLexer)\r
+ Val = CurPTHLexer->isNextPPTokenLParen();\r
+ else\r
+ Val = CurTokenLexer->isNextTokenLParen();\r
+\r
+ if (Val == 2) {\r
+ // We have run off the end. If it's a source file we don't\r
+ // examine enclosing ones (C99 5.1.1.2p4). Otherwise walk up the\r
+ // macro stack.\r
+ if (CurPPLexer)\r
+ return false;\r
+ for (const IncludeStackInfo &Entry : llvm::reverse(IncludeMacroStack)) {\r
+ if (Entry.TheLexer)\r
+ Val = Entry.TheLexer->isNextPPTokenLParen();\r
+ else if (Entry.ThePTHLexer)\r
+ Val = Entry.ThePTHLexer->isNextPPTokenLParen();\r
+ else\r
+ Val = Entry.TheTokenLexer->isNextTokenLParen();\r
+\r
+ if (Val != 2)\r
+ break;\r
+\r
+ // Ran off the end of a source file?\r
+ if (Entry.ThePPLexer)\r
+ return false;\r
+ }\r
+ }\r
+\r
+ // Okay, if we know that the token is a '(', lex it and return. Otherwise we\r
+ // have found something that isn't a '(' or we found the end of the\r
+ // translation unit. In either case, return false.\r
+ return Val == 1;\r
+}\r
+\r
+/// HandleMacroExpandedIdentifier - If an identifier token is read that is to be\r
+/// expanded as a macro, handle it and return the next token as 'Identifier'.\r
+bool Preprocessor::HandleMacroExpandedIdentifier(Token &Identifier,\r
+ const MacroDefinition &M) {\r
+ MacroInfo *MI = M.getMacroInfo();\r
+\r
+ // If this is a macro expansion in the "#if !defined(x)" line for the file,\r
+ // then the macro could expand to different things in other contexts, we need\r
+ // to disable the optimization in this case.\r
+ if (CurPPLexer) CurPPLexer->MIOpt.ExpandedMacro();\r
+\r
+ // If this is a builtin macro, like __LINE__ or _Pragma, handle it specially.\r
+ if (MI->isBuiltinMacro()) {\r
+ if (Callbacks)\r
+ Callbacks->MacroExpands(Identifier, M, Identifier.getLocation(),\r
+ /*Args=*/nullptr);\r
+ ExpandBuiltinMacro(Identifier);\r
+ return true;\r
+ }\r
+\r
+ /// Args - If this is a function-like macro expansion, this contains,\r
+ /// for each macro argument, the list of tokens that were provided to the\r
+ /// invocation.\r
+ MacroArgs *Args = nullptr;\r
+\r
+ // Remember where the end of the expansion occurred. For an object-like\r
+ // macro, this is the identifier. For a function-like macro, this is the ')'.\r
+ SourceLocation ExpansionEnd = Identifier.getLocation();\r
+\r
+ // If this is a function-like macro, read the arguments.\r
+ if (MI->isFunctionLike()) {\r
+ // Remember that we are now parsing the arguments to a macro invocation.\r
+ // Preprocessor directives used inside macro arguments are not portable, and\r
+ // this enables the warning.\r
+ InMacroArgs = true;\r
+ Args = ReadMacroCallArgumentList(Identifier, MI, ExpansionEnd);\r
+\r
+ // Finished parsing args.\r
+ InMacroArgs = false;\r
+\r
+ // If there was an error parsing the arguments, bail out.\r
+ if (!Args) return true;\r
+\r
+ ++NumFnMacroExpanded;\r
+ } else {\r
+ ++NumMacroExpanded;\r
+ }\r
+\r
+ // Notice that this macro has been used.\r
+ markMacroAsUsed(MI);\r
+\r
+ // Remember where the token is expanded.\r
+ SourceLocation ExpandLoc = Identifier.getLocation();\r
+ SourceRange ExpansionRange(ExpandLoc, ExpansionEnd);\r
+\r
+ if (Callbacks) {\r
+ if (InMacroArgs) {\r
+ // We can have macro expansion inside a conditional directive while\r
+ // reading the function macro arguments. To ensure, in that case, that\r
+ // MacroExpands callbacks still happen in source order, queue this\r
+ // callback to have it happen after the function macro callback.\r
+ DelayedMacroExpandsCallbacks.push_back(\r
+ MacroExpandsInfo(Identifier, M, ExpansionRange));\r
+ } else {\r
+ Callbacks->MacroExpands(Identifier, M, ExpansionRange, Args);\r
+ if (!DelayedMacroExpandsCallbacks.empty()) {\r
+ for (const MacroExpandsInfo &Info : DelayedMacroExpandsCallbacks) {\r
+ // FIXME: We lose macro args info with delayed callback.\r
+ Callbacks->MacroExpands(Info.Tok, Info.MD, Info.Range,\r
+ /*Args=*/nullptr);\r
+ }\r
+ DelayedMacroExpandsCallbacks.clear();\r
+ }\r
+ }\r
+ }\r
+\r
+ // If the macro definition is ambiguous, complain.\r
+ if (M.isAmbiguous()) {\r
+ Diag(Identifier, diag::warn_pp_ambiguous_macro)\r
+ << Identifier.getIdentifierInfo();\r
+ Diag(MI->getDefinitionLoc(), diag::note_pp_ambiguous_macro_chosen)\r
+ << Identifier.getIdentifierInfo();\r
+ M.forAllDefinitions([&](const MacroInfo *OtherMI) {\r
+ if (OtherMI != MI)\r
+ Diag(OtherMI->getDefinitionLoc(), diag::note_pp_ambiguous_macro_other)\r
+ << Identifier.getIdentifierInfo();\r
+ });\r
+ }\r
+\r
+ // If we started lexing a macro, enter the macro expansion body.\r
+\r
+ // If this macro expands to no tokens, don't bother to push it onto the\r
+ // expansion stack, only to take it right back off.\r
+ if (MI->getNumTokens() == 0) {\r
+ // No need for arg info.\r
+ if (Args) Args->destroy(*this);\r
+\r
+ // Propagate whitespace info as if we had pushed, then popped,\r
+ // a macro context.\r
+ Identifier.setFlag(Token::LeadingEmptyMacro);\r
+ PropagateLineStartLeadingSpaceInfo(Identifier);\r
+ ++NumFastMacroExpanded;\r
+ return false;\r
+ } else if (MI->getNumTokens() == 1 &&\r
+ isTrivialSingleTokenExpansion(MI, Identifier.getIdentifierInfo(),\r
+ *this)) {\r
+ // Otherwise, if this macro expands into a single trivially-expanded\r
+ // token: expand it now. This handles common cases like\r
+ // "#define VAL 42".\r
+\r
+ // No need for arg info.\r
+ if (Args) Args->destroy(*this);\r
+\r
+ // Propagate the isAtStartOfLine/hasLeadingSpace markers of the macro\r
+ // identifier to the expanded token.\r
+ bool isAtStartOfLine = Identifier.isAtStartOfLine();\r
+ bool hasLeadingSpace = Identifier.hasLeadingSpace();\r
+\r
+ // Replace the result token.\r
+ Identifier = MI->getReplacementToken(0);\r
+\r
+ // Restore the StartOfLine/LeadingSpace markers.\r
+ Identifier.setFlagValue(Token::StartOfLine , isAtStartOfLine);\r
+ Identifier.setFlagValue(Token::LeadingSpace, hasLeadingSpace);\r
+\r
+ // Update the tokens location to include both its expansion and physical\r
+ // locations.\r
+ SourceLocation Loc =\r
+ SourceMgr.createExpansionLoc(Identifier.getLocation(), ExpandLoc,\r
+ ExpansionEnd,Identifier.getLength());\r
+ Identifier.setLocation(Loc);\r
+\r
+ // If this is a disabled macro or #define X X, we must mark the result as\r
+ // unexpandable.\r
+ if (IdentifierInfo *NewII = Identifier.getIdentifierInfo()) {\r
+ if (MacroInfo *NewMI = getMacroInfo(NewII))\r
+ if (!NewMI->isEnabled() || NewMI == MI) {\r
+ Identifier.setFlag(Token::DisableExpand);\r
+ // Don't warn for "#define X X" like "#define bool bool" from\r
+ // stdbool.h.\r
+ if (NewMI != MI || MI->isFunctionLike())\r
+ Diag(Identifier, diag::pp_disabled_macro_expansion);\r
+ }\r
+ }\r
+\r
+ // Since this is not an identifier token, it can't be macro expanded, so\r
+ // we're done.\r
+ ++NumFastMacroExpanded;\r
+ return true;\r
+ }\r
+\r
+ // Start expanding the macro.\r
+ EnterMacro(Identifier, ExpansionEnd, MI, Args);\r
+ return false;\r
+}\r
+\r
+enum Bracket {\r
+ Brace,\r
+ Paren\r
+};\r
+\r
+/// CheckMatchedBrackets - Returns true if the braces and parentheses in the\r
+/// token vector are properly nested.\r
+static bool CheckMatchedBrackets(const SmallVectorImpl<Token> &Tokens) {\r
+ SmallVector<Bracket, 8> Brackets;\r
+ for (SmallVectorImpl<Token>::const_iterator I = Tokens.begin(),\r
+ E = Tokens.end();\r
+ I != E; ++I) {\r
+ if (I->is(tok::l_paren)) {\r
+ Brackets.push_back(Paren);\r
+ } else if (I->is(tok::r_paren)) {\r
+ if (Brackets.empty() || Brackets.back() == Brace)\r
+ return false;\r
+ Brackets.pop_back();\r
+ } else if (I->is(tok::l_brace)) {\r
+ Brackets.push_back(Brace);\r
+ } else if (I->is(tok::r_brace)) {\r
+ if (Brackets.empty() || Brackets.back() == Paren)\r
+ return false;\r
+ Brackets.pop_back();\r
+ }\r
+ }\r
+ return Brackets.empty();\r
+}\r
+\r
+/// GenerateNewArgTokens - Returns true if OldTokens can be converted to a new\r
+/// vector of tokens in NewTokens. The new number of arguments will be placed\r
+/// in NumArgs and the ranges which need to surrounded in parentheses will be\r
+/// in ParenHints.\r
+/// Returns false if the token stream cannot be changed. If this is because\r
+/// of an initializer list starting a macro argument, the range of those\r
+/// initializer lists will be place in InitLists.\r
+static bool GenerateNewArgTokens(Preprocessor &PP,\r
+ SmallVectorImpl<Token> &OldTokens,\r
+ SmallVectorImpl<Token> &NewTokens,\r
+ unsigned &NumArgs,\r
+ SmallVectorImpl<SourceRange> &ParenHints,\r
+ SmallVectorImpl<SourceRange> &InitLists) {\r
+ if (!CheckMatchedBrackets(OldTokens))\r
+ return false;\r
+\r
+ // Once it is known that the brackets are matched, only a simple count of the\r
+ // braces is needed.\r
+ unsigned Braces = 0;\r
+\r
+ // First token of a new macro argument.\r
+ SmallVectorImpl<Token>::iterator ArgStartIterator = OldTokens.begin();\r
+\r
+ // First closing brace in a new macro argument. Used to generate\r
+ // SourceRanges for InitLists.\r
+ SmallVectorImpl<Token>::iterator ClosingBrace = OldTokens.end();\r
+ NumArgs = 0;\r
+ Token TempToken;\r
+ // Set to true when a macro separator token is found inside a braced list.\r
+ // If true, the fixed argument spans multiple old arguments and ParenHints\r
+ // will be updated.\r
+ bool FoundSeparatorToken = false;\r
+ for (SmallVectorImpl<Token>::iterator I = OldTokens.begin(),\r
+ E = OldTokens.end();\r
+ I != E; ++I) {\r
+ if (I->is(tok::l_brace)) {\r
+ ++Braces;\r
+ } else if (I->is(tok::r_brace)) {\r
+ --Braces;\r
+ if (Braces == 0 && ClosingBrace == E && FoundSeparatorToken)\r
+ ClosingBrace = I;\r
+ } else if (I->is(tok::eof)) {\r
+ // EOF token is used to separate macro arguments\r
+ if (Braces != 0) {\r
+ // Assume comma separator is actually braced list separator and change\r
+ // it back to a comma.\r
+ FoundSeparatorToken = true;\r
+ I->setKind(tok::comma);\r
+ I->setLength(1);\r
+ } else { // Braces == 0\r
+ // Separator token still separates arguments.\r
+ ++NumArgs;\r
+\r
+ // If the argument starts with a brace, it can't be fixed with\r
+ // parentheses. A different diagnostic will be given.\r
+ if (FoundSeparatorToken && ArgStartIterator->is(tok::l_brace)) {\r
+ InitLists.push_back(\r
+ SourceRange(ArgStartIterator->getLocation(),\r
+ PP.getLocForEndOfToken(ClosingBrace->getLocation())));\r
+ ClosingBrace = E;\r
+ }\r
+\r
+ // Add left paren\r
+ if (FoundSeparatorToken) {\r
+ TempToken.startToken();\r
+ TempToken.setKind(tok::l_paren);\r
+ TempToken.setLocation(ArgStartIterator->getLocation());\r
+ TempToken.setLength(0);\r
+ NewTokens.push_back(TempToken);\r
+ }\r
+\r
+ // Copy over argument tokens\r
+ NewTokens.insert(NewTokens.end(), ArgStartIterator, I);\r
+\r
+ // Add right paren and store the paren locations in ParenHints\r
+ if (FoundSeparatorToken) {\r
+ SourceLocation Loc = PP.getLocForEndOfToken((I - 1)->getLocation());\r
+ TempToken.startToken();\r
+ TempToken.setKind(tok::r_paren);\r
+ TempToken.setLocation(Loc);\r
+ TempToken.setLength(0);\r
+ NewTokens.push_back(TempToken);\r
+ ParenHints.push_back(SourceRange(ArgStartIterator->getLocation(),\r
+ Loc));\r
+ }\r
+\r
+ // Copy separator token\r
+ NewTokens.push_back(*I);\r
+\r
+ // Reset values\r
+ ArgStartIterator = I + 1;\r
+ FoundSeparatorToken = false;\r
+ }\r
+ }\r
+ }\r
+\r
+ return !ParenHints.empty() && InitLists.empty();\r
+}\r
+\r
+/// ReadFunctionLikeMacroArgs - After reading "MACRO" and knowing that the next\r
+/// token is the '(' of the macro, this method is invoked to read all of the\r
+/// actual arguments specified for the macro invocation. This returns null on\r
+/// error.\r
+MacroArgs *Preprocessor::ReadMacroCallArgumentList(Token &MacroName,\r
+ MacroInfo *MI,\r
+ SourceLocation &MacroEnd) {\r
+ // The number of fixed arguments to parse.\r
+ unsigned NumFixedArgsLeft = MI->getNumParams();\r
+ bool isVariadic = MI->isVariadic();\r
+\r
+ // Outer loop, while there are more arguments, keep reading them.\r
+ Token Tok;\r
+\r
+ // Read arguments as unexpanded tokens. This avoids issues, e.g., where\r
+ // an argument value in a macro could expand to ',' or '(' or ')'.\r
+ LexUnexpandedToken(Tok);\r
+ assert(Tok.is(tok::l_paren) && "Error computing l-paren-ness?");\r
+\r
+ // ArgTokens - Build up a list of tokens that make up each argument. Each\r
+ // argument is separated by an EOF token. Use a SmallVector so we can avoid\r
+ // heap allocations in the common case.\r
+ SmallVector<Token, 64> ArgTokens;\r
+ bool ContainsCodeCompletionTok = false;\r
+ bool FoundElidedComma = false;\r
+\r
+ SourceLocation TooManyArgsLoc;\r
+\r
+ unsigned NumActuals = 0;\r
+ while (Tok.isNot(tok::r_paren)) {\r
+ if (ContainsCodeCompletionTok && Tok.isOneOf(tok::eof, tok::eod))\r
+ break;\r
+\r
+ assert(Tok.isOneOf(tok::l_paren, tok::comma) &&\r
+ "only expect argument separators here");\r
+\r
+ size_t ArgTokenStart = ArgTokens.size();\r
+ SourceLocation ArgStartLoc = Tok.getLocation();\r
+\r
+ // C99 6.10.3p11: Keep track of the number of l_parens we have seen. Note\r
+ // that we already consumed the first one.\r
+ unsigned NumParens = 0;\r
+\r
+ while (true) {\r
+ // Read arguments as unexpanded tokens. This avoids issues, e.g., where\r
+ // an argument value in a macro could expand to ',' or '(' or ')'.\r
+ LexUnexpandedToken(Tok);\r
+\r
+ if (Tok.isOneOf(tok::eof, tok::eod)) { // "#if f(<eof>" & "#if f(\n"\r
+ if (!ContainsCodeCompletionTok) {\r
+ Diag(MacroName, diag::err_unterm_macro_invoc);\r
+ Diag(MI->getDefinitionLoc(), diag::note_macro_here)\r
+ << MacroName.getIdentifierInfo();\r
+ // Do not lose the EOF/EOD. Return it to the client.\r
+ MacroName = Tok;\r
+ return nullptr;\r
+ }\r
+ // Do not lose the EOF/EOD.\r
+ auto Toks = llvm::make_unique<Token[]>(1);\r
+ Toks[0] = Tok;\r
+ EnterTokenStream(std::move(Toks), 1, true);\r
+ break;\r
+ } else if (Tok.is(tok::r_paren)) {\r
+ // If we found the ) token, the macro arg list is done.\r
+ if (NumParens-- == 0) {\r
+ MacroEnd = Tok.getLocation();\r
+ if (!ArgTokens.empty() &&\r
+ ArgTokens.back().commaAfterElided()) {\r
+ FoundElidedComma = true;\r
+ }\r
+ break;\r
+ }\r
+ } else if (Tok.is(tok::l_paren)) {\r
+ ++NumParens;\r
+ } else if (Tok.is(tok::comma) && NumParens == 0 &&\r
+ !(Tok.getFlags() & Token::IgnoredComma)) {\r
+ // In Microsoft-compatibility mode, single commas from nested macro\r
+ // expansions should not be considered as argument separators. We test\r
+ // for this with the IgnoredComma token flag above.\r
+\r
+ // Comma ends this argument if there are more fixed arguments expected.\r
+ // However, if this is a variadic macro, and this is part of the\r
+ // variadic part, then the comma is just an argument token.\r
+ if (!isVariadic) break;\r
+ if (NumFixedArgsLeft > 1)\r
+ break;\r
+ } else if (Tok.is(tok::comment) && !KeepMacroComments) {\r
+ // If this is a comment token in the argument list and we're just in\r
+ // -C mode (not -CC mode), discard the comment.\r
+ continue;\r
+ } else if (!Tok.isAnnotation() && Tok.getIdentifierInfo() != nullptr) {\r
+ // Reading macro arguments can cause macros that we are currently\r
+ // expanding from to be popped off the expansion stack. Doing so causes\r
+ // them to be reenabled for expansion. Here we record whether any\r
+ // identifiers we lex as macro arguments correspond to disabled macros.\r
+ // If so, we mark the token as noexpand. This is a subtle aspect of\r
+ // C99 6.10.3.4p2.\r
+ if (MacroInfo *MI = getMacroInfo(Tok.getIdentifierInfo()))\r
+ if (!MI->isEnabled())\r
+ Tok.setFlag(Token::DisableExpand);\r
+ } else if (Tok.is(tok::code_completion)) {\r
+ ContainsCodeCompletionTok = true;\r
+ if (CodeComplete)\r
+ CodeComplete->CodeCompleteMacroArgument(MacroName.getIdentifierInfo(),\r
+ MI, NumActuals);\r
+ // Don't mark that we reached the code-completion point because the\r
+ // parser is going to handle the token and there will be another\r
+ // code-completion callback.\r
+ }\r
+\r
+ ArgTokens.push_back(Tok);\r
+ }\r
+\r
+ // If this was an empty argument list foo(), don't add this as an empty\r
+ // argument.\r
+ if (ArgTokens.empty() && Tok.getKind() == tok::r_paren)\r
+ break;\r
+\r
+ // If this is not a variadic macro, and too many args were specified, emit\r
+ // an error.\r
+ if (!isVariadic && NumFixedArgsLeft == 0 && TooManyArgsLoc.isInvalid()) {\r
+ if (ArgTokens.size() != ArgTokenStart)\r
+ TooManyArgsLoc = ArgTokens[ArgTokenStart].getLocation();\r
+ else\r
+ TooManyArgsLoc = ArgStartLoc;\r
+ }\r
+\r
+ // Empty arguments are standard in C99 and C++0x, and are supported as an\r
+ // extension in other modes.\r
+ if (ArgTokens.size() == ArgTokenStart && !LangOpts.C99)\r
+ Diag(Tok, LangOpts.CPlusPlus11 ?\r
+ diag::warn_cxx98_compat_empty_fnmacro_arg :\r
+ diag::ext_empty_fnmacro_arg);\r
+\r
+ // Add a marker EOF token to the end of the token list for this argument.\r
+ Token EOFTok;\r
+ EOFTok.startToken();\r
+ EOFTok.setKind(tok::eof);\r
+ EOFTok.setLocation(Tok.getLocation());\r
+ EOFTok.setLength(0);\r
+ ArgTokens.push_back(EOFTok);\r
+ ++NumActuals;\r
+ if (!ContainsCodeCompletionTok && NumFixedArgsLeft != 0)\r
+ --NumFixedArgsLeft;\r
+ }\r
+\r
+ // Okay, we either found the r_paren. Check to see if we parsed too few\r
+ // arguments.\r
+ unsigned MinArgsExpected = MI->getNumParams();\r
+\r
+ // If this is not a variadic macro, and too many args were specified, emit\r
+ // an error.\r
+ if (!isVariadic && NumActuals > MinArgsExpected &&\r
+ !ContainsCodeCompletionTok) {\r
+ // Emit the diagnostic at the macro name in case there is a missing ).\r
+ // Emitting it at the , could be far away from the macro name.\r
+ Diag(TooManyArgsLoc, diag::err_too_many_args_in_macro_invoc);\r
+ Diag(MI->getDefinitionLoc(), diag::note_macro_here)\r
+ << MacroName.getIdentifierInfo();\r
+\r
+ // Commas from braced initializer lists will be treated as argument\r
+ // separators inside macros. Attempt to correct for this with parentheses.\r
+ // TODO: See if this can be generalized to angle brackets for templates\r
+ // inside macro arguments.\r
+\r
+ SmallVector<Token, 4> FixedArgTokens;\r
+ unsigned FixedNumArgs = 0;\r
+ SmallVector<SourceRange, 4> ParenHints, InitLists;\r
+ if (!GenerateNewArgTokens(*this, ArgTokens, FixedArgTokens, FixedNumArgs,\r
+ ParenHints, InitLists)) {\r
+ if (!InitLists.empty()) {\r
+ DiagnosticBuilder DB =\r
+ Diag(MacroName,\r
+ diag::note_init_list_at_beginning_of_macro_argument);\r
+ for (SourceRange Range : InitLists)\r
+ DB << Range;\r
+ }\r
+ return nullptr;\r
+ }\r
+ if (FixedNumArgs != MinArgsExpected)\r
+ return nullptr;\r
+\r
+ DiagnosticBuilder DB = Diag(MacroName, diag::note_suggest_parens_for_macro);\r
+ for (SourceRange ParenLocation : ParenHints) {\r
+ DB << FixItHint::CreateInsertion(ParenLocation.getBegin(), "(");\r
+ DB << FixItHint::CreateInsertion(ParenLocation.getEnd(), ")");\r
+ }\r
+ ArgTokens.swap(FixedArgTokens);\r
+ NumActuals = FixedNumArgs;\r
+ }\r
+\r
+ // See MacroArgs instance var for description of this.\r
+ bool isVarargsElided = false;\r
+\r
+ if (ContainsCodeCompletionTok) {\r
+ // Recover from not-fully-formed macro invocation during code-completion.\r
+ Token EOFTok;\r
+ EOFTok.startToken();\r
+ EOFTok.setKind(tok::eof);\r
+ EOFTok.setLocation(Tok.getLocation());\r
+ EOFTok.setLength(0);\r
+ for (; NumActuals < MinArgsExpected; ++NumActuals)\r
+ ArgTokens.push_back(EOFTok);\r
+ }\r
+\r
+ if (NumActuals < MinArgsExpected) {\r
+ // There are several cases where too few arguments is ok, handle them now.\r
+ if (NumActuals == 0 && MinArgsExpected == 1) {\r
+ // #define A(X) or #define A(...) ---> A()\r
+\r
+ // If there is exactly one argument, and that argument is missing,\r
+ // then we have an empty "()" argument empty list. This is fine, even if\r
+ // the macro expects one argument (the argument is just empty).\r
+ isVarargsElided = MI->isVariadic();\r
+ } else if ((FoundElidedComma || MI->isVariadic()) &&\r
+ (NumActuals+1 == MinArgsExpected || // A(x, ...) -> A(X)\r
+ (NumActuals == 0 && MinArgsExpected == 2))) {// A(x,...) -> A()\r
+ // Varargs where the named vararg parameter is missing: OK as extension.\r
+ // #define A(x, ...)\r
+ // A("blah")\r
+ //\r
+ // If the macro contains the comma pasting extension, the diagnostic\r
+ // is suppressed; we know we'll get another diagnostic later.\r
+ if (!MI->hasCommaPasting()) {\r
+ Diag(Tok, diag::ext_missing_varargs_arg);\r
+ Diag(MI->getDefinitionLoc(), diag::note_macro_here)\r
+ << MacroName.getIdentifierInfo();\r
+ }\r
+\r
+ // Remember this occurred, allowing us to elide the comma when used for\r
+ // cases like:\r
+ // #define A(x, foo...) blah(a, ## foo)\r
+ // #define B(x, ...) blah(a, ## __VA_ARGS__)\r
+ // #define C(...) blah(a, ## __VA_ARGS__)\r
+ // A(x) B(x) C()\r
+ isVarargsElided = true;\r
+ } else if (!ContainsCodeCompletionTok) {\r
+ // Otherwise, emit the error.\r
+ Diag(Tok, diag::err_too_few_args_in_macro_invoc);\r
+ Diag(MI->getDefinitionLoc(), diag::note_macro_here)\r
+ << MacroName.getIdentifierInfo();\r
+ return nullptr;\r
+ }\r
+\r
+ // Add a marker EOF token to the end of the token list for this argument.\r
+ SourceLocation EndLoc = Tok.getLocation();\r
+ Tok.startToken();\r
+ Tok.setKind(tok::eof);\r
+ Tok.setLocation(EndLoc);\r
+ Tok.setLength(0);\r
+ ArgTokens.push_back(Tok);\r
+\r
+ // If we expect two arguments, add both as empty.\r
+ if (NumActuals == 0 && MinArgsExpected == 2)\r
+ ArgTokens.push_back(Tok);\r
+\r
+ } else if (NumActuals > MinArgsExpected && !MI->isVariadic() &&\r
+ !ContainsCodeCompletionTok) {\r
+ // Emit the diagnostic at the macro name in case there is a missing ).\r
+ // Emitting it at the , could be far away from the macro name.\r
+ Diag(MacroName, diag::err_too_many_args_in_macro_invoc);\r
+ Diag(MI->getDefinitionLoc(), diag::note_macro_here)\r
+ << MacroName.getIdentifierInfo();\r
+ return nullptr;\r
+ }\r
+\r
+ return MacroArgs::create(MI, ArgTokens, isVarargsElided, *this);\r
+}\r
+\r
+/// \brief Keeps macro expanded tokens for TokenLexers.\r
+//\r
+/// Works like a stack; a TokenLexer adds the macro expanded tokens that is\r
+/// going to lex in the cache and when it finishes the tokens are removed\r
+/// from the end of the cache.\r
+Token *Preprocessor::cacheMacroExpandedTokens(TokenLexer *tokLexer,\r
+ ArrayRef<Token> tokens) {\r
+ assert(tokLexer);\r
+ if (tokens.empty())\r
+ return nullptr;\r
+\r
+ size_t newIndex = MacroExpandedTokens.size();\r
+ bool cacheNeedsToGrow = tokens.size() >\r
+ MacroExpandedTokens.capacity()-MacroExpandedTokens.size();\r
+ MacroExpandedTokens.append(tokens.begin(), tokens.end());\r
+\r
+ if (cacheNeedsToGrow) {\r
+ // Go through all the TokenLexers whose 'Tokens' pointer points in the\r
+ // buffer and update the pointers to the (potential) new buffer array.\r
+ for (const auto &Lexer : MacroExpandingLexersStack) {\r
+ TokenLexer *prevLexer;\r
+ size_t tokIndex;\r
+ std::tie(prevLexer, tokIndex) = Lexer;\r
+ prevLexer->Tokens = MacroExpandedTokens.data() + tokIndex;\r
+ }\r
+ }\r
+\r
+ MacroExpandingLexersStack.push_back(std::make_pair(tokLexer, newIndex));\r
+ return MacroExpandedTokens.data() + newIndex;\r
+}\r
+\r
+void Preprocessor::removeCachedMacroExpandedTokensOfLastLexer() {\r
+ assert(!MacroExpandingLexersStack.empty());\r
+ size_t tokIndex = MacroExpandingLexersStack.back().second;\r
+ assert(tokIndex < MacroExpandedTokens.size());\r
+ // Pop the cached macro expanded tokens from the end.\r
+ MacroExpandedTokens.resize(tokIndex);\r
+ MacroExpandingLexersStack.pop_back();\r
+}\r
+\r
+/// ComputeDATE_TIME - Compute the current time, enter it into the specified\r
+/// scratch buffer, then return DATELoc/TIMELoc locations with the position of\r
+/// the identifier tokens inserted.\r
+static void ComputeDATE_TIME(SourceLocation &DATELoc, SourceLocation &TIMELoc,\r
+ Preprocessor &PP) {\r
+ time_t TT = time(nullptr);\r
+ struct tm *TM = localtime(&TT);\r
+\r
+ static const char * const Months[] = {\r
+ "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"\r
+ };\r
+\r
+ {\r
+ SmallString<32> TmpBuffer;\r
+ llvm::raw_svector_ostream TmpStream(TmpBuffer);\r
+ TmpStream << llvm::format("\"%s %2d %4d\"", Months[TM->tm_mon],\r
+ TM->tm_mday, TM->tm_year + 1900);\r
+ Token TmpTok;\r
+ TmpTok.startToken();\r
+ PP.CreateString(TmpStream.str(), TmpTok);\r
+ DATELoc = TmpTok.getLocation();\r
+ }\r
+\r
+ {\r
+ SmallString<32> TmpBuffer;\r
+ llvm::raw_svector_ostream TmpStream(TmpBuffer);\r
+ TmpStream << llvm::format("\"%02d:%02d:%02d\"",\r
+ TM->tm_hour, TM->tm_min, TM->tm_sec);\r
+ Token TmpTok;\r
+ TmpTok.startToken();\r
+ PP.CreateString(TmpStream.str(), TmpTok);\r
+ TIMELoc = TmpTok.getLocation();\r
+ }\r
+}\r
+\r
+/// HasFeature - Return true if we recognize and implement the feature\r
+/// specified by the identifier as a standard language feature.\r
+static bool HasFeature(const Preprocessor &PP, StringRef Feature) {\r
+ const LangOptions &LangOpts = PP.getLangOpts();\r
+\r
+ // Normalize the feature name, __foo__ becomes foo.\r
+ if (Feature.startswith("__") && Feature.endswith("__") && Feature.size() >= 4)\r
+ Feature = Feature.substr(2, Feature.size() - 4);\r
+\r
+ return llvm::StringSwitch<bool>(Feature)\r
+ .Case("address_sanitizer",\r
+ LangOpts.Sanitize.hasOneOf(SanitizerKind::Address |\r
+ SanitizerKind::KernelAddress))\r
+ .Case("assume_nonnull", true)\r
+ .Case("attribute_analyzer_noreturn", true)\r
+ .Case("attribute_availability", true)\r
+ .Case("attribute_availability_with_message", true)\r
+ .Case("attribute_availability_app_extension", true)\r
+ .Case("attribute_availability_with_version_underscores", true)\r
+ .Case("attribute_availability_tvos", true)\r
+ .Case("attribute_availability_watchos", true)\r
+ .Case("attribute_availability_with_strict", true)\r
+ .Case("attribute_availability_with_replacement", true)\r
+ .Case("attribute_availability_in_templates", true)\r
+ .Case("attribute_cf_returns_not_retained", true)\r
+ .Case("attribute_cf_returns_retained", true)\r
+ .Case("attribute_cf_returns_on_parameters", true)\r
+ .Case("attribute_deprecated_with_message", true)\r
+ .Case("attribute_deprecated_with_replacement", true)\r
+ .Case("attribute_ext_vector_type", true)\r
+ .Case("attribute_ns_returns_not_retained", true)\r
+ .Case("attribute_ns_returns_retained", true)\r
+ .Case("attribute_ns_consumes_self", true)\r
+ .Case("attribute_ns_consumed", true)\r
+ .Case("attribute_cf_consumed", true)\r
+ .Case("attribute_objc_ivar_unused", true)\r
+ .Case("attribute_objc_method_family", true)\r
+ .Case("attribute_overloadable", true)\r
+ .Case("attribute_unavailable_with_message", true)\r
+ .Case("attribute_unused_on_fields", true)\r
+ .Case("attribute_diagnose_if_objc", true)\r
+ .Case("blocks", LangOpts.Blocks)\r
+ .Case("c_thread_safety_attributes", true)\r
+ .Case("cxx_exceptions", LangOpts.CXXExceptions)\r
+ .Case("cxx_rtti", LangOpts.RTTI && LangOpts.RTTIData)\r
+ .Case("enumerator_attributes", true)\r
+ .Case("nullability", true)\r
+ .Case("nullability_on_arrays", true)\r
+ .Case("memory_sanitizer", LangOpts.Sanitize.has(SanitizerKind::Memory))\r
+ .Case("thread_sanitizer", LangOpts.Sanitize.has(SanitizerKind::Thread))\r
+ .Case("dataflow_sanitizer", LangOpts.Sanitize.has(SanitizerKind::DataFlow))\r
+ .Case("efficiency_sanitizer",\r
+ LangOpts.Sanitize.hasOneOf(SanitizerKind::Efficiency))\r
+ .Case("scudo", LangOpts.Sanitize.hasOneOf(SanitizerKind::Scudo))\r
+ // Objective-C features\r
+ .Case("objc_arr", LangOpts.ObjCAutoRefCount) // FIXME: REMOVE?\r
+ .Case("objc_arc", LangOpts.ObjCAutoRefCount)\r
+ .Case("objc_arc_weak", LangOpts.ObjCWeak)\r
+ .Case("objc_default_synthesize_properties", LangOpts.ObjC2)\r
+ .Case("objc_fixed_enum", LangOpts.ObjC2)\r
+ .Case("objc_instancetype", LangOpts.ObjC2)\r
+ .Case("objc_kindof", LangOpts.ObjC2)\r
+ .Case("objc_modules", LangOpts.ObjC2 && LangOpts.Modules)\r
+ .Case("objc_nonfragile_abi", LangOpts.ObjCRuntime.isNonFragile())\r
+ .Case("objc_property_explicit_atomic",\r
+ true) // Does clang support explicit "atomic" keyword?\r
+ .Case("objc_protocol_qualifier_mangling", true)\r
+ .Case("objc_weak_class", LangOpts.ObjCRuntime.hasWeakClassImport())\r
+ .Case("ownership_holds", true)\r
+ .Case("ownership_returns", true)\r
+ .Case("ownership_takes", true)\r
+ .Case("objc_bool", true)\r
+ .Case("objc_subscripting", LangOpts.ObjCRuntime.isNonFragile())\r
+ .Case("objc_array_literals", LangOpts.ObjC2)\r
+ .Case("objc_dictionary_literals", LangOpts.ObjC2)\r
+ .Case("objc_boxed_expressions", LangOpts.ObjC2)\r
+ .Case("objc_boxed_nsvalue_expressions", LangOpts.ObjC2)\r
+ .Case("arc_cf_code_audited", true)\r
+ .Case("objc_bridge_id", true)\r
+ .Case("objc_bridge_id_on_typedefs", true)\r
+ .Case("objc_generics", LangOpts.ObjC2)\r
+ .Case("objc_generics_variance", LangOpts.ObjC2)\r
+ .Case("objc_class_property", LangOpts.ObjC2)\r
+ // C11 features\r
+ .Case("c_alignas", LangOpts.C11)\r
+ .Case("c_alignof", LangOpts.C11)\r
+ .Case("c_atomic", LangOpts.C11)\r
+ .Case("c_generic_selections", LangOpts.C11)\r
+ .Case("c_static_assert", LangOpts.C11)\r
+ .Case("c_thread_local",\r
+ LangOpts.C11 && PP.getTargetInfo().isTLSSupported())\r
+ // C++11 features\r
+ .Case("cxx_access_control_sfinae", LangOpts.CPlusPlus11)\r
+ .Case("cxx_alias_templates", LangOpts.CPlusPlus11)\r
+ .Case("cxx_alignas", LangOpts.CPlusPlus11)\r
+ .Case("cxx_alignof", LangOpts.CPlusPlus11)\r
+ .Case("cxx_atomic", LangOpts.CPlusPlus11)\r
+ .Case("cxx_attributes", LangOpts.CPlusPlus11)\r
+ .Case("cxx_auto_type", LangOpts.CPlusPlus11)\r
+ .Case("cxx_constexpr", LangOpts.CPlusPlus11)\r
+ .Case("cxx_constexpr_string_builtins", LangOpts.CPlusPlus11)\r
+ .Case("cxx_decltype", LangOpts.CPlusPlus11)\r
+ .Case("cxx_decltype_incomplete_return_types", LangOpts.CPlusPlus11)\r
+ .Case("cxx_default_function_template_args", LangOpts.CPlusPlus11)\r
+ .Case("cxx_defaulted_functions", LangOpts.CPlusPlus11)\r
+ .Case("cxx_delegating_constructors", LangOpts.CPlusPlus11)\r
+ .Case("cxx_deleted_functions", LangOpts.CPlusPlus11)\r
+ .Case("cxx_explicit_conversions", LangOpts.CPlusPlus11)\r
+ .Case("cxx_generalized_initializers", LangOpts.CPlusPlus11)\r
+ .Case("cxx_implicit_moves", LangOpts.CPlusPlus11)\r
+ .Case("cxx_inheriting_constructors", LangOpts.CPlusPlus11)\r
+ .Case("cxx_inline_namespaces", LangOpts.CPlusPlus11)\r
+ .Case("cxx_lambdas", LangOpts.CPlusPlus11)\r
+ .Case("cxx_local_type_template_args", LangOpts.CPlusPlus11)\r
+ .Case("cxx_nonstatic_member_init", LangOpts.CPlusPlus11)\r
+ .Case("cxx_noexcept", LangOpts.CPlusPlus11)\r
+ .Case("cxx_nullptr", LangOpts.CPlusPlus11)\r
+ .Case("cxx_override_control", LangOpts.CPlusPlus11)\r
+ .Case("cxx_range_for", LangOpts.CPlusPlus11)\r
+ .Case("cxx_raw_string_literals", LangOpts.CPlusPlus11)\r
+ .Case("cxx_reference_qualified_functions", LangOpts.CPlusPlus11)\r
+ .Case("cxx_rvalue_references", LangOpts.CPlusPlus11)\r
+ .Case("cxx_strong_enums", LangOpts.CPlusPlus11)\r
+ .Case("cxx_static_assert", LangOpts.CPlusPlus11)\r
+ .Case("cxx_thread_local",\r
+ LangOpts.CPlusPlus11 && PP.getTargetInfo().isTLSSupported())\r
+ .Case("cxx_trailing_return", LangOpts.CPlusPlus11)\r
+ .Case("cxx_unicode_literals", LangOpts.CPlusPlus11)\r
+ .Case("cxx_unrestricted_unions", LangOpts.CPlusPlus11)\r
+ .Case("cxx_user_literals", LangOpts.CPlusPlus11)\r
+ .Case("cxx_variadic_templates", LangOpts.CPlusPlus11)\r
+ // C++14 features\r
+ .Case("cxx_aggregate_nsdmi", LangOpts.CPlusPlus14)\r
+ .Case("cxx_binary_literals", LangOpts.CPlusPlus14)\r
+ .Case("cxx_contextual_conversions", LangOpts.CPlusPlus14)\r
+ .Case("cxx_decltype_auto", LangOpts.CPlusPlus14)\r
+ .Case("cxx_generic_lambdas", LangOpts.CPlusPlus14)\r
+ .Case("cxx_init_captures", LangOpts.CPlusPlus14)\r
+ .Case("cxx_relaxed_constexpr", LangOpts.CPlusPlus14)\r
+ .Case("cxx_return_type_deduction", LangOpts.CPlusPlus14)\r
+ .Case("cxx_variable_templates", LangOpts.CPlusPlus14)\r
+ // NOTE: For features covered by SD-6, it is preferable to provide *only*\r
+ // the SD-6 macro and not a __has_feature check.\r
+\r
+ // C++ TSes\r
+ //.Case("cxx_runtime_arrays", LangOpts.CPlusPlusTSArrays)\r
+ //.Case("cxx_concepts", LangOpts.CPlusPlusTSConcepts)\r
+ // FIXME: Should this be __has_feature or __has_extension?\r
+ //.Case("raw_invocation_type", LangOpts.CPlusPlus)\r
+ // Type traits\r
+ // N.B. Additional type traits should not be added to the following list.\r
+ // Instead, they should be detected by has_extension.\r
+ .Case("has_nothrow_assign", LangOpts.CPlusPlus)\r
+ .Case("has_nothrow_copy", LangOpts.CPlusPlus)\r
+ .Case("has_nothrow_constructor", LangOpts.CPlusPlus)\r
+ .Case("has_trivial_assign", LangOpts.CPlusPlus)\r
+ .Case("has_trivial_copy", LangOpts.CPlusPlus)\r
+ .Case("has_trivial_constructor", LangOpts.CPlusPlus)\r
+ .Case("has_trivial_destructor", LangOpts.CPlusPlus)\r
+ .Case("has_virtual_destructor", LangOpts.CPlusPlus)\r
+ .Case("is_abstract", LangOpts.CPlusPlus)\r
+ .Case("is_base_of", LangOpts.CPlusPlus)\r
+ .Case("is_class", LangOpts.CPlusPlus)\r
+ .Case("is_constructible", LangOpts.CPlusPlus)\r
+ .Case("is_convertible_to", LangOpts.CPlusPlus)\r
+ .Case("is_empty", LangOpts.CPlusPlus)\r
+ .Case("is_enum", LangOpts.CPlusPlus)\r
+ .Case("is_final", LangOpts.CPlusPlus)\r
+ .Case("is_literal", LangOpts.CPlusPlus)\r
+ .Case("is_standard_layout", LangOpts.CPlusPlus)\r
+ .Case("is_pod", LangOpts.CPlusPlus)\r
+ .Case("is_polymorphic", LangOpts.CPlusPlus)\r
+ .Case("is_sealed", LangOpts.CPlusPlus && LangOpts.MicrosoftExt)\r
+ .Case("is_trivial", LangOpts.CPlusPlus)\r
+ .Case("is_trivially_assignable", LangOpts.CPlusPlus)\r
+ .Case("is_trivially_constructible", LangOpts.CPlusPlus)\r
+ .Case("is_trivially_copyable", LangOpts.CPlusPlus)\r
+ .Case("is_union", LangOpts.CPlusPlus)\r
+ .Case("modules", LangOpts.Modules)\r
+ .Case("safe_stack", LangOpts.Sanitize.has(SanitizerKind::SafeStack))\r
+ .Case("tls", PP.getTargetInfo().isTLSSupported())\r
+ .Case("underlying_type", LangOpts.CPlusPlus)\r
+ .Default(false);\r
+}\r
+\r
+/// HasExtension - Return true if we recognize and implement the feature\r
+/// specified by the identifier, either as an extension or a standard language\r
+/// feature.\r
+static bool HasExtension(const Preprocessor &PP, StringRef Extension) {\r
+ if (HasFeature(PP, Extension))\r
+ return true;\r
+\r
+ // If the use of an extension results in an error diagnostic, extensions are\r
+ // effectively unavailable, so just return false here.\r
+ if (PP.getDiagnostics().getExtensionHandlingBehavior() >=\r
+ diag::Severity::Error)\r
+ return false;\r
+\r
+ const LangOptions &LangOpts = PP.getLangOpts();\r
+\r
+ // Normalize the extension name, __foo__ becomes foo.\r
+ if (Extension.startswith("__") && Extension.endswith("__") &&\r
+ Extension.size() >= 4)\r
+ Extension = Extension.substr(2, Extension.size() - 4);\r
+\r
+ // Because we inherit the feature list from HasFeature, this string switch\r
+ // must be less restrictive than HasFeature's.\r
+ return llvm::StringSwitch<bool>(Extension)\r
+ // C11 features supported by other languages as extensions.\r
+ .Case("c_alignas", true)\r
+ .Case("c_alignof", true)\r
+ .Case("c_atomic", true)\r
+ .Case("c_generic_selections", true)\r
+ .Case("c_static_assert", true)\r
+ .Case("c_thread_local", PP.getTargetInfo().isTLSSupported())\r
+ // C++11 features supported by other languages as extensions.\r
+ .Case("cxx_atomic", LangOpts.CPlusPlus)\r
+ .Case("cxx_deleted_functions", LangOpts.CPlusPlus)\r
+ .Case("cxx_explicit_conversions", LangOpts.CPlusPlus)\r
+ .Case("cxx_inline_namespaces", LangOpts.CPlusPlus)\r
+ .Case("cxx_local_type_template_args", LangOpts.CPlusPlus)\r
+ .Case("cxx_nonstatic_member_init", LangOpts.CPlusPlus)\r
+ .Case("cxx_override_control", LangOpts.CPlusPlus)\r
+ .Case("cxx_range_for", LangOpts.CPlusPlus)\r
+ .Case("cxx_reference_qualified_functions", LangOpts.CPlusPlus)\r
+ .Case("cxx_rvalue_references", LangOpts.CPlusPlus)\r
+ .Case("cxx_variadic_templates", LangOpts.CPlusPlus)\r
+ // C++14 features supported by other languages as extensions.\r
+ .Case("cxx_binary_literals", true)\r
+ .Case("cxx_init_captures", LangOpts.CPlusPlus11)\r
+ .Case("cxx_variable_templates", LangOpts.CPlusPlus)\r
+ // Miscellaneous language extensions\r
+ .Case("overloadable_unmarked", true)\r
+ .Default(false);\r
+}\r
+\r
+/// EvaluateHasIncludeCommon - Process a '__has_include("path")'\r
+/// or '__has_include_next("path")' expression.\r
+/// Returns true if successful.\r
+static bool EvaluateHasIncludeCommon(Token &Tok,\r
+ IdentifierInfo *II, Preprocessor &PP,\r
+ const DirectoryLookup *LookupFrom,\r
+ const FileEntry *LookupFromFile) {\r
+ // Save the location of the current token. If a '(' is later found, use\r
+ // that location. If not, use the end of this location instead.\r
+ SourceLocation LParenLoc = Tok.getLocation();\r
+\r
+ // These expressions are only allowed within a preprocessor directive.\r
+ if (!PP.isParsingIfOrElifDirective()) {\r
+ PP.Diag(LParenLoc, diag::err_pp_directive_required) << II->getName();\r
+ // Return a valid identifier token.\r
+ assert(Tok.is(tok::identifier));\r
+ Tok.setIdentifierInfo(II);\r
+ return false;\r
+ }\r
+\r
+ // Get '('.\r
+ PP.LexNonComment(Tok);\r
+\r
+ // Ensure we have a '('.\r
+ if (Tok.isNot(tok::l_paren)) {\r
+ // No '(', use end of last token.\r
+ LParenLoc = PP.getLocForEndOfToken(LParenLoc);\r
+ PP.Diag(LParenLoc, diag::err_pp_expected_after) << II << tok::l_paren;\r
+ // If the next token looks like a filename or the start of one,\r
+ // assume it is and process it as such.\r
+ if (!Tok.is(tok::angle_string_literal) && !Tok.is(tok::string_literal) &&\r
+ !Tok.is(tok::less))\r
+ return false;\r
+ } else {\r
+ // Save '(' location for possible missing ')' message.\r
+ LParenLoc = Tok.getLocation();\r
+\r
+ if (PP.getCurrentLexer()) {\r
+ // Get the file name.\r
+ PP.getCurrentLexer()->LexIncludeFilename(Tok);\r
+ } else {\r
+ // We're in a macro, so we can't use LexIncludeFilename; just\r
+ // grab the next token.\r
+ PP.Lex(Tok);\r
+ }\r
+ }\r
+\r
+ // Reserve a buffer to get the spelling.\r
+ SmallString<128> FilenameBuffer;\r
+ StringRef Filename;\r
+ SourceLocation EndLoc;\r
+ \r
+ switch (Tok.getKind()) {\r
+ case tok::eod:\r
+ // If the token kind is EOD, the error has already been diagnosed.\r
+ return false;\r
+\r
+ case tok::angle_string_literal:\r
+ case tok::string_literal: {\r
+ bool Invalid = false;\r
+ Filename = PP.getSpelling(Tok, FilenameBuffer, &Invalid);\r
+ if (Invalid)\r
+ return false;\r
+ break;\r
+ }\r
+\r
+ case tok::less:\r
+ // This could be a <foo/bar.h> file coming from a macro expansion. In this\r
+ // case, glue the tokens together into FilenameBuffer and interpret those.\r
+ FilenameBuffer.push_back('<');\r
+ if (PP.ConcatenateIncludeName(FilenameBuffer, EndLoc)) {\r
+ // Let the caller know a <eod> was found by changing the Token kind.\r
+ Tok.setKind(tok::eod);\r
+ return false; // Found <eod> but no ">"? Diagnostic already emitted.\r
+ }\r
+ Filename = FilenameBuffer;\r
+ break;\r
+ default:\r
+ PP.Diag(Tok.getLocation(), diag::err_pp_expects_filename);\r
+ return false;\r
+ }\r
+\r
+ SourceLocation FilenameLoc = Tok.getLocation();\r
+\r
+ // Get ')'.\r
+ PP.LexNonComment(Tok);\r
+\r
+ // Ensure we have a trailing ).\r
+ if (Tok.isNot(tok::r_paren)) {\r
+ PP.Diag(PP.getLocForEndOfToken(FilenameLoc), diag::err_pp_expected_after)\r
+ << II << tok::r_paren;\r
+ PP.Diag(LParenLoc, diag::note_matching) << tok::l_paren;\r
+ return false;\r
+ }\r
+\r
+ bool isAngled = PP.GetIncludeFilenameSpelling(Tok.getLocation(), Filename);\r
+ // If GetIncludeFilenameSpelling set the start ptr to null, there was an\r
+ // error.\r
+ if (Filename.empty())\r
+ return false;\r
+\r
+ // Search include directories.\r
+ const DirectoryLookup *CurDir;\r
+ const FileEntry *File =\r
+ PP.LookupFile(FilenameLoc, Filename, isAngled, LookupFrom, LookupFromFile,\r
+ CurDir, nullptr, nullptr, nullptr, nullptr);\r
+\r
+ // Get the result value. A result of true means the file exists.\r
+ return File != nullptr;\r
+}\r
+\r
+/// EvaluateHasInclude - Process a '__has_include("path")' expression.\r
+/// Returns true if successful.\r
+static bool EvaluateHasInclude(Token &Tok, IdentifierInfo *II,\r
+ Preprocessor &PP) {\r
+ return EvaluateHasIncludeCommon(Tok, II, PP, nullptr, nullptr);\r
+}\r
+\r
+/// EvaluateHasIncludeNext - Process '__has_include_next("path")' expression.\r
+/// Returns true if successful.\r
+static bool EvaluateHasIncludeNext(Token &Tok,\r
+ IdentifierInfo *II, Preprocessor &PP) {\r
+ // __has_include_next is like __has_include, except that we start\r
+ // searching after the current found directory. If we can't do this,\r
+ // issue a diagnostic.\r
+ // FIXME: Factor out duplication with \r
+ // Preprocessor::HandleIncludeNextDirective.\r
+ const DirectoryLookup *Lookup = PP.GetCurDirLookup();\r
+ const FileEntry *LookupFromFile = nullptr;\r
+ if (PP.isInPrimaryFile() && PP.getLangOpts().IsHeaderFile) {\r
+ // If the main file is a header, then it's either for PCH/AST generation,\r
+ // or libclang opened it. Either way, handle it as a normal include below\r
+ // and do not complain about __has_include_next.\r
+ } else if (PP.isInPrimaryFile()) {\r
+ Lookup = nullptr;\r
+ PP.Diag(Tok, diag::pp_include_next_in_primary);\r
+ } else if (PP.getCurrentLexerSubmodule()) {\r
+ // Start looking up in the directory *after* the one in which the current\r
+ // file would be found, if any.\r
+ assert(PP.getCurrentLexer() && "#include_next directive in macro?");\r
+ LookupFromFile = PP.getCurrentLexer()->getFileEntry();\r
+ Lookup = nullptr;\r
+ } else if (!Lookup) {\r
+ PP.Diag(Tok, diag::pp_include_next_absolute_path);\r
+ } else {\r
+ // Start looking up in the next directory.\r
+ ++Lookup;\r
+ }\r
+\r
+ return EvaluateHasIncludeCommon(Tok, II, PP, Lookup, LookupFromFile);\r
+}\r
+\r
+/// \brief Process single-argument builtin feature-like macros that return\r
+/// integer values.\r
+static void EvaluateFeatureLikeBuiltinMacro(llvm::raw_svector_ostream& OS,\r
+ Token &Tok, IdentifierInfo *II,\r
+ Preprocessor &PP,\r
+ llvm::function_ref<\r
+ int(Token &Tok,\r
+ bool &HasLexedNextTok)> Op) {\r
+ // Parse the initial '('.\r
+ PP.LexUnexpandedToken(Tok);\r
+ if (Tok.isNot(tok::l_paren)) {\r
+ PP.Diag(Tok.getLocation(), diag::err_pp_expected_after) << II\r
+ << tok::l_paren;\r
+\r
+ // Provide a dummy '0' value on output stream to elide further errors.\r
+ if (!Tok.isOneOf(tok::eof, tok::eod)) {\r
+ OS << 0;\r
+ Tok.setKind(tok::numeric_constant);\r
+ }\r
+ return;\r
+ }\r
+\r
+ unsigned ParenDepth = 1;\r
+ SourceLocation LParenLoc = Tok.getLocation();\r
+ llvm::Optional<int> Result;\r
+\r
+ Token ResultTok;\r
+ bool SuppressDiagnostic = false;\r
+ while (true) {\r
+ // Parse next token.\r
+ PP.LexUnexpandedToken(Tok);\r
+\r
+already_lexed:\r
+ switch (Tok.getKind()) {\r
+ case tok::eof:\r
+ case tok::eod:\r
+ // Don't provide even a dummy value if the eod or eof marker is\r
+ // reached. Simply provide a diagnostic.\r
+ PP.Diag(Tok.getLocation(), diag::err_unterm_macro_invoc);\r
+ return;\r
+\r
+ case tok::comma:\r
+ if (!SuppressDiagnostic) {\r
+ PP.Diag(Tok.getLocation(), diag::err_too_many_args_in_macro_invoc);\r
+ SuppressDiagnostic = true;\r
+ }\r
+ continue;\r
+\r
+ case tok::l_paren:\r
+ ++ParenDepth;\r
+ if (Result.hasValue())\r
+ break;\r
+ if (!SuppressDiagnostic) {\r
+ PP.Diag(Tok.getLocation(), diag::err_pp_nested_paren) << II;\r
+ SuppressDiagnostic = true;\r
+ }\r
+ continue;\r
+\r
+ case tok::r_paren:\r
+ if (--ParenDepth > 0)\r
+ continue;\r
+\r
+ // The last ')' has been reached; return the value if one found or\r
+ // a diagnostic and a dummy value.\r
+ if (Result.hasValue())\r
+ OS << Result.getValue();\r
+ else {\r
+ OS << 0;\r
+ if (!SuppressDiagnostic)\r
+ PP.Diag(Tok.getLocation(), diag::err_too_few_args_in_macro_invoc);\r
+ }\r
+ Tok.setKind(tok::numeric_constant);\r
+ return;\r
+\r
+ default: {\r
+ // Parse the macro argument, if one not found so far.\r
+ if (Result.hasValue())\r
+ break;\r
+\r
+ bool HasLexedNextToken = false;\r
+ Result = Op(Tok, HasLexedNextToken);\r
+ ResultTok = Tok;\r
+ if (HasLexedNextToken)\r
+ goto already_lexed;\r
+ continue;\r
+ }\r
+ }\r
+\r
+ // Diagnose missing ')'.\r
+ if (!SuppressDiagnostic) {\r
+ if (auto Diag = PP.Diag(Tok.getLocation(), diag::err_pp_expected_after)) {\r
+ if (IdentifierInfo *LastII = ResultTok.getIdentifierInfo())\r
+ Diag << LastII;\r
+ else\r
+ Diag << ResultTok.getKind();\r
+ Diag << tok::r_paren << ResultTok.getLocation();\r
+ }\r
+ PP.Diag(LParenLoc, diag::note_matching) << tok::l_paren;\r
+ SuppressDiagnostic = true;\r
+ }\r
+ }\r
+}\r
+\r
+/// \brief Helper function to return the IdentifierInfo structure of a Token\r
+/// or generate a diagnostic if none available.\r
+static IdentifierInfo *ExpectFeatureIdentifierInfo(Token &Tok,\r
+ Preprocessor &PP,\r
+ signed DiagID) {\r
+ IdentifierInfo *II;\r
+ if (!Tok.isAnnotation() && (II = Tok.getIdentifierInfo()))\r
+ return II;\r
+\r
+ PP.Diag(Tok.getLocation(), DiagID);\r
+ return nullptr;\r
+}\r
+\r
+/// ExpandBuiltinMacro - If an identifier token is read that is to be expanded\r
+/// as a builtin macro, handle it and return the next token as 'Tok'.\r
+void Preprocessor::ExpandBuiltinMacro(Token &Tok) {\r
+ // Figure out which token this is.\r
+ IdentifierInfo *II = Tok.getIdentifierInfo();\r
+ assert(II && "Can't be a macro without id info!");\r
+\r
+ // If this is an _Pragma or Microsoft __pragma directive, expand it,\r
+ // invoke the pragma handler, then lex the token after it.\r
+ if (II == Ident_Pragma)\r
+ return Handle_Pragma(Tok);\r
+ else if (II == Ident__pragma) // in non-MS mode this is null\r
+ return HandleMicrosoft__pragma(Tok);\r
+\r
+ ++NumBuiltinMacroExpanded;\r
+\r
+ SmallString<128> TmpBuffer;\r
+ llvm::raw_svector_ostream OS(TmpBuffer);\r
+\r
+ // Set up the return result.\r
+ Tok.setIdentifierInfo(nullptr);\r
+ Tok.clearFlag(Token::NeedsCleaning);\r
+\r
+ if (II == Ident__LINE__) {\r
+ // C99 6.10.8: "__LINE__: The presumed line number (within the current\r
+ // source file) of the current source line (an integer constant)". This can\r
+ // be affected by #line.\r
+ SourceLocation Loc = Tok.getLocation();\r
+\r
+ // Advance to the location of the first _, this might not be the first byte\r
+ // of the token if it starts with an escaped newline.\r
+ Loc = AdvanceToTokenCharacter(Loc, 0);\r
+\r
+ // One wrinkle here is that GCC expands __LINE__ to location of the *end* of\r
+ // a macro expansion. This doesn't matter for object-like macros, but\r
+ // can matter for a function-like macro that expands to contain __LINE__.\r
+ // Skip down through expansion points until we find a file loc for the\r
+ // end of the expansion history.\r
+ Loc = SourceMgr.getExpansionRange(Loc).second;\r
+ PresumedLoc PLoc = SourceMgr.getPresumedLoc(Loc);\r
+\r
+ // __LINE__ expands to a simple numeric value.\r
+ OS << (PLoc.isValid()? PLoc.getLine() : 1);\r
+ Tok.setKind(tok::numeric_constant);\r
+ } else if (II == Ident__FILE__ || II == Ident__BASE_FILE__) {\r
+ // C99 6.10.8: "__FILE__: The presumed name of the current source file (a\r
+ // character string literal)". This can be affected by #line.\r
+ PresumedLoc PLoc = SourceMgr.getPresumedLoc(Tok.getLocation());\r
+\r
+ // __BASE_FILE__ is a GNU extension that returns the top of the presumed\r
+ // #include stack instead of the current file.\r
+ if (II == Ident__BASE_FILE__ && PLoc.isValid()) {\r
+ SourceLocation NextLoc = PLoc.getIncludeLoc();\r
+ while (NextLoc.isValid()) {\r
+ PLoc = SourceMgr.getPresumedLoc(NextLoc);\r
+ if (PLoc.isInvalid())\r
+ break;\r
+ \r
+ NextLoc = PLoc.getIncludeLoc();\r
+ }\r
+ }\r
+\r
+ // Escape this filename. Turn '\' -> '\\' '"' -> '\"'\r
+ SmallString<128> FN;\r
+ if (PLoc.isValid()) {\r
+ FN += PLoc.getFilename();\r
+ Lexer::Stringify(FN);\r
+ OS << '"' << FN << '"';\r
+ }\r
+ Tok.setKind(tok::string_literal);\r
+ } else if (II == Ident__DATE__) {\r
+ Diag(Tok.getLocation(), diag::warn_pp_date_time);\r
+ if (!DATELoc.isValid())\r
+ ComputeDATE_TIME(DATELoc, TIMELoc, *this);\r
+ Tok.setKind(tok::string_literal);\r
+ Tok.setLength(strlen("\"Mmm dd yyyy\""));\r
+ Tok.setLocation(SourceMgr.createExpansionLoc(DATELoc, Tok.getLocation(),\r
+ Tok.getLocation(),\r
+ Tok.getLength()));\r
+ return;\r
+ } else if (II == Ident__TIME__) {\r
+ Diag(Tok.getLocation(), diag::warn_pp_date_time);\r
+ if (!TIMELoc.isValid())\r
+ ComputeDATE_TIME(DATELoc, TIMELoc, *this);\r
+ Tok.setKind(tok::string_literal);\r
+ Tok.setLength(strlen("\"hh:mm:ss\""));\r
+ Tok.setLocation(SourceMgr.createExpansionLoc(TIMELoc, Tok.getLocation(),\r
+ Tok.getLocation(),\r
+ Tok.getLength()));\r
+ return;\r
+ } else if (II == Ident__INCLUDE_LEVEL__) {\r
+ // Compute the presumed include depth of this token. This can be affected\r
+ // by GNU line markers.\r
+ unsigned Depth = 0;\r
+\r
+ PresumedLoc PLoc = SourceMgr.getPresumedLoc(Tok.getLocation());\r
+ if (PLoc.isValid()) {\r
+ PLoc = SourceMgr.getPresumedLoc(PLoc.getIncludeLoc());\r
+ for (; PLoc.isValid(); ++Depth)\r
+ PLoc = SourceMgr.getPresumedLoc(PLoc.getIncludeLoc());\r
+ }\r
+\r
+ // __INCLUDE_LEVEL__ expands to a simple numeric value.\r
+ OS << Depth;\r
+ Tok.setKind(tok::numeric_constant);\r
+ } else if (II == Ident__TIMESTAMP__) {\r
+ Diag(Tok.getLocation(), diag::warn_pp_date_time);\r
+ // MSVC, ICC, GCC, VisualAge C++ extension. The generated string should be\r
+ // of the form "Ddd Mmm dd hh::mm::ss yyyy", which is returned by asctime.\r
+\r
+ // Get the file that we are lexing out of. If we're currently lexing from\r
+ // a macro, dig into the include stack.\r
+ const FileEntry *CurFile = nullptr;\r
+ PreprocessorLexer *TheLexer = getCurrentFileLexer();\r
+\r
+ if (TheLexer)\r
+ CurFile = SourceMgr.getFileEntryForID(TheLexer->getFileID());\r
+\r
+ const char *Result;\r
+ if (CurFile) {\r
+ time_t TT = CurFile->getModificationTime();\r
+ struct tm *TM = localtime(&TT);\r
+ Result = asctime(TM);\r
+ } else {\r
+ Result = "??? ??? ?? ??:??:?? ????\n";\r
+ }\r
+ // Surround the string with " and strip the trailing newline.\r
+ OS << '"' << StringRef(Result).drop_back() << '"';\r
+ Tok.setKind(tok::string_literal);\r
+ } else if (II == Ident__COUNTER__) {\r
+ // __COUNTER__ expands to a simple numeric value.\r
+ OS << CounterValue++;\r
+ Tok.setKind(tok::numeric_constant);\r
+ } else if (II == Ident__has_feature) {\r
+ EvaluateFeatureLikeBuiltinMacro(OS, Tok, II, *this,\r
+ [this](Token &Tok, bool &HasLexedNextToken) -> int {\r
+ IdentifierInfo *II = ExpectFeatureIdentifierInfo(Tok, *this,\r
+ diag::err_feature_check_malformed);\r
+ return II && HasFeature(*this, II->getName());\r
+ });\r
+ } else if (II == Ident__has_extension) {\r
+ EvaluateFeatureLikeBuiltinMacro(OS, Tok, II, *this,\r
+ [this](Token &Tok, bool &HasLexedNextToken) -> int {\r
+ IdentifierInfo *II = ExpectFeatureIdentifierInfo(Tok, *this,\r
+ diag::err_feature_check_malformed);\r
+ return II && HasExtension(*this, II->getName());\r
+ });\r
+ } else if (II == Ident__has_builtin) {\r
+ EvaluateFeatureLikeBuiltinMacro(OS, Tok, II, *this,\r
+ [this](Token &Tok, bool &HasLexedNextToken) -> int {\r
+ IdentifierInfo *II = ExpectFeatureIdentifierInfo(Tok, *this,\r
+ diag::err_feature_check_malformed);\r
+ if (!II)\r
+ return false;\r
+ else if (II->getBuiltinID() != 0)\r
+ return true;\r
+ else {\r
+ const LangOptions &LangOpts = getLangOpts();\r
+ return llvm::StringSwitch<bool>(II->getName())\r
+ .Case("__make_integer_seq", LangOpts.CPlusPlus)\r
+ .Case("__type_pack_element", LangOpts.CPlusPlus)\r
+ .Case("__builtin_available", true)\r
+ .Default(false);\r
+ }\r
+ });\r
+ } else if (II == Ident__is_identifier) {\r
+ EvaluateFeatureLikeBuiltinMacro(OS, Tok, II, *this,\r
+ [](Token &Tok, bool &HasLexedNextToken) -> int {\r
+ return Tok.is(tok::identifier);\r
+ });\r
+ } else if (II == Ident__has_attribute) {\r
+ EvaluateFeatureLikeBuiltinMacro(OS, Tok, II, *this,\r
+ [this](Token &Tok, bool &HasLexedNextToken) -> int {\r
+ IdentifierInfo *II = ExpectFeatureIdentifierInfo(Tok, *this,\r
+ diag::err_feature_check_malformed);\r
+ return II ? hasAttribute(AttrSyntax::GNU, nullptr, II,\r
+ getTargetInfo(), getLangOpts()) : 0;\r
+ });\r
+ } else if (II == Ident__has_declspec) {\r
+ EvaluateFeatureLikeBuiltinMacro(OS, Tok, II, *this,\r
+ [this](Token &Tok, bool &HasLexedNextToken) -> int {\r
+ IdentifierInfo *II = ExpectFeatureIdentifierInfo(Tok, *this,\r
+ diag::err_feature_check_malformed);\r
+ return II ? hasAttribute(AttrSyntax::Declspec, nullptr, II,\r
+ getTargetInfo(), getLangOpts()) : 0;\r
+ });\r
+ } else if (II == Ident__has_cpp_attribute ||\r
+ II == Ident__has_c_attribute) {\r
+ bool IsCXX = II == Ident__has_cpp_attribute;\r
+ EvaluateFeatureLikeBuiltinMacro(\r
+ OS, Tok, II, *this, [&](Token &Tok, bool &HasLexedNextToken) -> int {\r
+ IdentifierInfo *ScopeII = nullptr;\r
+ IdentifierInfo *II = ExpectFeatureIdentifierInfo(\r
+ Tok, *this, diag::err_feature_check_malformed);\r
+ if (!II)\r
+ return false;\r
+\r
+ // It is possible to receive a scope token. Read the "::", if it is\r
+ // available, and the subsequent identifier.\r
+ LexUnexpandedToken(Tok);\r
+ if (Tok.isNot(tok::coloncolon))\r
+ HasLexedNextToken = true;\r
+ else {\r
+ ScopeII = II;\r
+ LexUnexpandedToken(Tok);\r
+ II = ExpectFeatureIdentifierInfo(Tok, *this,\r
+ diag::err_feature_check_malformed);\r
+ }\r
+\r
+ AttrSyntax Syntax = IsCXX ? AttrSyntax::CXX : AttrSyntax::C;\r
+ return II ? hasAttribute(Syntax, ScopeII, II, getTargetInfo(),\r
+ getLangOpts())\r
+ : 0;\r
+ });\r
+ } else if (II == Ident__has_include ||\r
+ II == Ident__has_include_next) {\r
+ // The argument to these two builtins should be a parenthesized\r
+ // file name string literal using angle brackets (<>) or\r
+ // double-quotes ("").\r
+ bool Value;\r
+ if (II == Ident__has_include)\r
+ Value = EvaluateHasInclude(Tok, II, *this);\r
+ else\r
+ Value = EvaluateHasIncludeNext(Tok, II, *this);\r
+\r
+ if (Tok.isNot(tok::r_paren))\r
+ return;\r
+ OS << (int)Value;\r
+ Tok.setKind(tok::numeric_constant);\r
+ } else if (II == Ident__has_warning) {\r
+ // The argument should be a parenthesized string literal.\r
+ EvaluateFeatureLikeBuiltinMacro(OS, Tok, II, *this,\r
+ [this](Token &Tok, bool &HasLexedNextToken) -> int {\r
+ std::string WarningName;\r
+ SourceLocation StrStartLoc = Tok.getLocation();\r
+\r
+ HasLexedNextToken = Tok.is(tok::string_literal);\r
+ if (!FinishLexStringLiteral(Tok, WarningName, "'__has_warning'",\r
+ /*MacroExpansion=*/false))\r
+ return false;\r
+\r
+ // FIXME: Should we accept "-R..." flags here, or should that be\r
+ // handled by a separate __has_remark?\r
+ if (WarningName.size() < 3 || WarningName[0] != '-' ||\r
+ WarningName[1] != 'W') {\r
+ Diag(StrStartLoc, diag::warn_has_warning_invalid_option);\r
+ return false;\r
+ }\r
+\r
+ // Finally, check if the warning flags maps to a diagnostic group.\r
+ // We construct a SmallVector here to talk to getDiagnosticIDs().\r
+ // Although we don't use the result, this isn't a hot path, and not\r
+ // worth special casing.\r
+ SmallVector<diag::kind, 10> Diags;\r
+ return !getDiagnostics().getDiagnosticIDs()->\r
+ getDiagnosticsInGroup(diag::Flavor::WarningOrError,\r
+ WarningName.substr(2), Diags);\r
+ });\r
+ } else if (II == Ident__building_module) {\r
+ // The argument to this builtin should be an identifier. The\r
+ // builtin evaluates to 1 when that identifier names the module we are\r
+ // currently building.\r
+ EvaluateFeatureLikeBuiltinMacro(OS, Tok, II, *this,\r
+ [this](Token &Tok, bool &HasLexedNextToken) -> int {\r
+ IdentifierInfo *II = ExpectFeatureIdentifierInfo(Tok, *this,\r
+ diag::err_expected_id_building_module);\r
+ return getLangOpts().isCompilingModule() && II &&\r
+ (II->getName() == getLangOpts().CurrentModule);\r
+ });\r
+ } else if (II == Ident__MODULE__) {\r
+ // The current module as an identifier.\r
+ OS << getLangOpts().CurrentModule;\r
+ IdentifierInfo *ModuleII = getIdentifierInfo(getLangOpts().CurrentModule);\r
+ Tok.setIdentifierInfo(ModuleII);\r
+ Tok.setKind(ModuleII->getTokenID());\r
+ } else if (II == Ident__identifier) {\r
+ SourceLocation Loc = Tok.getLocation();\r
+\r
+ // We're expecting '__identifier' '(' identifier ')'. Try to recover\r
+ // if the parens are missing.\r
+ LexNonComment(Tok);\r
+ if (Tok.isNot(tok::l_paren)) {\r
+ // No '(', use end of last token.\r
+ Diag(getLocForEndOfToken(Loc), diag::err_pp_expected_after)\r
+ << II << tok::l_paren;\r
+ // If the next token isn't valid as our argument, we can't recover.\r
+ if (!Tok.isAnnotation() && Tok.getIdentifierInfo())\r
+ Tok.setKind(tok::identifier);\r
+ return;\r
+ }\r
+\r
+ SourceLocation LParenLoc = Tok.getLocation();\r
+ LexNonComment(Tok);\r
+\r
+ if (!Tok.isAnnotation() && Tok.getIdentifierInfo())\r
+ Tok.setKind(tok::identifier);\r
+ else {\r
+ Diag(Tok.getLocation(), diag::err_pp_identifier_arg_not_identifier)\r
+ << Tok.getKind();\r
+ // Don't walk past anything that's not a real token.\r
+ if (Tok.isOneOf(tok::eof, tok::eod) || Tok.isAnnotation())\r
+ return;\r
+ }\r
+\r
+ // Discard the ')', preserving 'Tok' as our result.\r
+ Token RParen;\r
+ LexNonComment(RParen);\r
+ if (RParen.isNot(tok::r_paren)) {\r
+ Diag(getLocForEndOfToken(Tok.getLocation()), diag::err_pp_expected_after)\r
+ << Tok.getKind() << tok::r_paren;\r
+ Diag(LParenLoc, diag::note_matching) << tok::l_paren;\r
+ }\r
+ return;\r
+ } else {\r
+ llvm_unreachable("Unknown identifier!");\r
+ }\r
+ CreateString(OS.str(), Tok, Tok.getLocation(), Tok.getLocation());\r
+}\r
+\r
+void Preprocessor::markMacroAsUsed(MacroInfo *MI) {\r
+ // If the 'used' status changed, and the macro requires 'unused' warning,\r
+ // remove its SourceLocation from the warn-for-unused-macro locations.\r
+ if (MI->isWarnIfUnused() && !MI->isUsed())\r
+ WarnUnusedMacroLocs.erase(MI->getDefinitionLoc());\r
+ MI->setIsUsed(true);\r
+}\r