#include "clang/Basic/AttributeCommonInfo.h"
#include "clang/Basic/Attributes.h"
#include "clang/Basic/Builtins.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/HeaderSearch.h"
#include "clang/Lex/LexDiagnostic.h"
#include "clang/Lex/LiteralSupport.h"
#include "clang/Lex/MacroArgs.h"
#include "clang/Lex/MacroInfo.h"
#include "clang/Lex/Preprocessor.h"
#include "clang/Lex/PreprocessorLexer.h"
#include "clang/Lex/PreprocessorOptions.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/STLExtras.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/StringSwitch.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/Format.h"
#include "llvm/Support/Path.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()) {
PendingModuleMacroNames.push_back(II);
}
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) {
assert(II && MD);
MacroState &StoredMD = CurSubmoduleState->Macros[II];
if (auto *OldMD = StoredMD.getLatest()) {
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;
}
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);
bool HidAny = false;
for (auto *O : Overrides) {
HidAny |= (O->NumOverriddenBy == 0);
++O->NumOverriddenBy;
}
auto &LeafMacros = LeafModuleMacros[II];
if (HidAny) {
llvm::erase_if(LeafMacros,
[](ModuleMacro *MM) { return MM->NumOverriddenBy != 0; });
}
LeafMacros.push_back(MM);
II->setHasMacroDefinition(true);
New = true;
return MM;
}
ModuleMacro *Preprocessor::getModuleMacro(Module *Mod,
const 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()) {
return;
}
Info.ActiveModuleMacros.clear();
llvm::DenseMap<ModuleMacro *, int> NumHiddenOverrides;
for (auto *O : Info.OverriddenMacros)
NumHiddenOverrides[O] = -1;
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())) {
if (MM->getMacroInfo())
Info.ActiveModuleMacros.push_back(MM);
} else {
for (auto *O : MM->overrides())
if ((unsigned)++NumHiddenOverrides[O] == O->getNumOverridingMacros())
Worklist.push_back(O);
}
}
std::reverse(Info.ActiveModuleMacros.begin(), Info.ActiveModuleMacros.end());
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();
if (MI && NewMI != MI &&
!MI->isIdenticalTo(*NewMI, *this, 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";
for (auto *MD = State ? State->getLatest() : nullptr; MD;
MD = MD->getPrevious()) {
llvm::errs() << " ";
MD->dump();
}
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";
}
}
}
static IdentifierInfo *RegisterBuiltinMacro(Preprocessor &PP, const char *Name){
IdentifierInfo *Id = PP.getIdentifierInfo(Name);
MacroInfo *MI = PP.AllocateMacroInfo(SourceLocation());
MI->setIsBuiltinMacro();
PP.appendDefMacroDirective(Id, MI);
return Id;
}
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");
Ident__FLT_EVAL_METHOD__ = RegisterBuiltinMacro(*this, "__FLT_EVAL_METHOD__");
if (getLangOpts().CPlusPlus)
Ident__has_cpp_attribute =
RegisterBuiltinMacro(*this, "__has_cpp_attribute");
else
Ident__has_cpp_attribute = nullptr;
Ident__BASE_FILE__ = RegisterBuiltinMacro(*this, "__BASE_FILE__");
Ident__INCLUDE_LEVEL__ = RegisterBuiltinMacro(*this, "__INCLUDE_LEVEL__");
Ident__TIMESTAMP__ = RegisterBuiltinMacro(*this, "__TIMESTAMP__");
if (getLangOpts().MicrosoftExt) {
Ident__identifier = RegisterBuiltinMacro(*this, "__identifier");
Ident__pragma = RegisterBuiltinMacro(*this, "__pragma");
} else {
Ident__identifier = nullptr;
Ident__pragma = nullptr;
}
Ident__FILE_NAME__ = RegisterBuiltinMacro(*this, "__FILE_NAME__");
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");
if (!getLangOpts().CPlusPlus)
Ident__has_c_attribute = RegisterBuiltinMacro(*this, "__has_c_attribute");
else
Ident__has_c_attribute = nullptr;
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");
Ident__is_target_arch = RegisterBuiltinMacro(*this, "__is_target_arch");
Ident__is_target_vendor = RegisterBuiltinMacro(*this, "__is_target_vendor");
Ident__is_target_os = RegisterBuiltinMacro(*this, "__is_target_os");
Ident__is_target_environment =
RegisterBuiltinMacro(*this, "__is_target_environment");
Ident__is_target_variant_os =
RegisterBuiltinMacro(*this, "__is_target_variant_os");
Ident__is_target_variant_environment =
RegisterBuiltinMacro(*this, "__is_target_variant_environment");
Ident__building_module = RegisterBuiltinMacro(*this, "__building_module");
if (!getLangOpts().CurrentModule.empty())
Ident__MODULE__ = RegisterBuiltinMacro(*this, "__MODULE__");
else
Ident__MODULE__ = nullptr;
}
static bool isTrivialSingleTokenExpansion(const MacroInfo *MI,
const IdentifierInfo *MacroIdent,
Preprocessor &PP) {
IdentifierInfo *II = MI->getReplacementToken(0).getIdentifierInfo();
if (!II) return true;
if (II->isOutOfDate())
PP.getExternalSource()->updateOutOfDateIdentifier(*II);
if (auto *ExpansionMI = PP.getMacroInfo(II))
if (ExpansionMI->isEnabled() &&
II != MacroIdent)
return false;
if (MI->isObjectLike()) return true;
return !llvm::is_contained(MI->params(), II);
}
bool Preprocessor::isNextPPTokenLParen() {
unsigned Val;
if (CurLexer)
Val = CurLexer->isNextPPTokenLParen();
else
Val = CurTokenLexer->isNextTokenLParen();
if (Val == 2) {
if (CurPPLexer)
return false;
for (const IncludeStackInfo &Entry : llvm::reverse(IncludeMacroStack)) {
if (Entry.TheLexer)
Val = Entry.TheLexer->isNextPPTokenLParen();
else
Val = Entry.TheTokenLexer->isNextTokenLParen();
if (Val != 2)
break;
if (Entry.ThePPLexer)
return false;
}
}
return Val == 1;
}
bool Preprocessor::HandleMacroExpandedIdentifier(Token &Identifier,
const MacroDefinition &M) {
emitMacroExpansionWarnings(Identifier);
MacroInfo *MI = M.getMacroInfo();
if (CurPPLexer) CurPPLexer->MIOpt.ExpandedMacro();
if (MI->isBuiltinMacro()) {
if (Callbacks)
Callbacks->MacroExpands(Identifier, M, Identifier.getLocation(),
nullptr);
ExpandBuiltinMacro(Identifier);
return true;
}
MacroArgs *Args = nullptr;
SourceLocation ExpansionEnd = Identifier.getLocation();
if (MI->isFunctionLike()) {
InMacroArgs = true;
ArgMacro = &Identifier;
Args = ReadMacroCallArgumentList(Identifier, MI, ExpansionEnd);
InMacroArgs = false;
ArgMacro = nullptr;
if (!Args) return true;
++NumFnMacroExpanded;
} else {
++NumMacroExpanded;
}
markMacroAsUsed(MI);
SourceLocation ExpandLoc = Identifier.getLocation();
SourceRange ExpansionRange(ExpandLoc, ExpansionEnd);
if (Callbacks) {
if (InMacroArgs) {
DelayedMacroExpandsCallbacks.push_back(
MacroExpandsInfo(Identifier, M, ExpansionRange));
} else {
Callbacks->MacroExpands(Identifier, M, ExpansionRange, Args);
if (!DelayedMacroExpandsCallbacks.empty()) {
for (const MacroExpandsInfo &Info : DelayedMacroExpandsCallbacks) {
Callbacks->MacroExpands(Info.Tok, Info.MD, Info.Range,
nullptr);
}
DelayedMacroExpandsCallbacks.clear();
}
}
}
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 (MI->getNumTokens() == 0) {
if (Args) Args->destroy(*this);
Identifier.setFlag(Token::LeadingEmptyMacro);
PropagateLineStartLeadingSpaceInfo(Identifier);
++NumFastMacroExpanded;
return false;
} else if (MI->getNumTokens() == 1 &&
isTrivialSingleTokenExpansion(MI, Identifier.getIdentifierInfo(),
*this)) {
if (Args) Args->destroy(*this);
bool isAtStartOfLine = Identifier.isAtStartOfLine();
bool hasLeadingSpace = Identifier.hasLeadingSpace();
Identifier = MI->getReplacementToken(0);
Identifier.setFlagValue(Token::StartOfLine , isAtStartOfLine);
Identifier.setFlagValue(Token::LeadingSpace, hasLeadingSpace);
SourceLocation Loc =
SourceMgr.createExpansionLoc(Identifier.getLocation(), ExpandLoc,
ExpansionEnd,Identifier.getLength());
Identifier.setLocation(Loc);
if (IdentifierInfo *NewII = Identifier.getIdentifierInfo()) {
if (MacroInfo *NewMI = getMacroInfo(NewII))
if (!NewMI->isEnabled() || NewMI == MI) {
Identifier.setFlag(Token::DisableExpand);
if (NewMI != MI || MI->isFunctionLike())
Diag(Identifier, diag::pp_disabled_macro_expansion);
}
}
++NumFastMacroExpanded;
return true;
}
EnterMacro(Identifier, ExpansionEnd, MI, Args);
return false;
}
enum Bracket {
Brace,
Paren
};
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();
}
static bool GenerateNewArgTokens(Preprocessor &PP,
SmallVectorImpl<Token> &OldTokens,
SmallVectorImpl<Token> &NewTokens,
unsigned &NumArgs,
SmallVectorImpl<SourceRange> &ParenHints,
SmallVectorImpl<SourceRange> &InitLists) {
if (!CheckMatchedBrackets(OldTokens))
return false;
unsigned Braces = 0;
SmallVectorImpl<Token>::iterator ArgStartIterator = OldTokens.begin();
SmallVectorImpl<Token>::iterator ClosingBrace = OldTokens.end();
NumArgs = 0;
Token TempToken;
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)) {
if (Braces != 0) {
FoundSeparatorToken = true;
I->setKind(tok::comma);
I->setLength(1);
} else { ++NumArgs;
if (FoundSeparatorToken && ArgStartIterator->is(tok::l_brace)) {
InitLists.push_back(
SourceRange(ArgStartIterator->getLocation(),
PP.getLocForEndOfToken(ClosingBrace->getLocation())));
ClosingBrace = E;
}
if (FoundSeparatorToken) {
TempToken.startToken();
TempToken.setKind(tok::l_paren);
TempToken.setLocation(ArgStartIterator->getLocation());
TempToken.setLength(0);
NewTokens.push_back(TempToken);
}
NewTokens.insert(NewTokens.end(), ArgStartIterator, I);
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));
}
NewTokens.push_back(*I);
ArgStartIterator = I + 1;
FoundSeparatorToken = false;
}
}
}
return !ParenHints.empty() && InitLists.empty();
}
MacroArgs *Preprocessor::ReadMacroCallArgumentList(Token &MacroName,
MacroInfo *MI,
SourceLocation &MacroEnd) {
unsigned NumFixedArgsLeft = MI->getNumParams();
bool isVariadic = MI->isVariadic();
Token Tok;
LexUnexpandedToken(Tok);
assert(Tok.is(tok::l_paren) && "Error computing l-paren-ness?");
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();
unsigned NumParens = 0;
while (true) {
LexUnexpandedToken(Tok);
if (Tok.isOneOf(tok::eof, tok::eod)) { if (!ContainsCodeCompletionTok) {
Diag(MacroName, diag::err_unterm_macro_invoc);
Diag(MI->getDefinitionLoc(), diag::note_macro_here)
<< MacroName.getIdentifierInfo();
MacroName = Tok;
return nullptr;
}
auto Toks = std::make_unique<Token[]>(1);
Toks[0] = Tok;
EnterTokenStream(std::move(Toks), 1, true, false);
break;
} else if (Tok.is(tok::r_paren)) {
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)) {
if (Tok.getFlags() & Token::IgnoredComma) {
Tok.clearFlag(Token::IgnoredComma);
} else if (NumParens == 0) {
if (!isVariadic)
break;
if (NumFixedArgsLeft > 1)
break;
}
} else if (Tok.is(tok::comment) && !KeepMacroComments) {
continue;
} else if (!Tok.isAnnotation() && Tok.getIdentifierInfo() != nullptr) {
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);
}
ArgTokens.push_back(Tok);
}
if (ArgTokens.empty() && Tok.getKind() == tok::r_paren)
break;
if (!isVariadic && NumFixedArgsLeft == 0 && TooManyArgsLoc.isInvalid()) {
if (ArgTokens.size() != ArgTokenStart)
TooManyArgsLoc = ArgTokens[ArgTokenStart].getLocation();
else
TooManyArgsLoc = ArgStartLoc;
}
if (ArgTokens.size() == ArgTokenStart && !getLangOpts().C99)
Diag(Tok, getLangOpts().CPlusPlus11
? diag::warn_cxx98_compat_empty_fnmacro_arg
: diag::ext_empty_fnmacro_arg);
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;
}
unsigned MinArgsExpected = MI->getNumParams();
if (!isVariadic && NumActuals > MinArgsExpected &&
!ContainsCodeCompletionTok) {
Diag(TooManyArgsLoc, diag::err_too_many_args_in_macro_invoc);
Diag(MI->getDefinitionLoc(), diag::note_macro_here)
<< MacroName.getIdentifierInfo();
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;
}
bool isVarargsElided = false;
if (ContainsCodeCompletionTok) {
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) {
if (NumActuals == 0 && MinArgsExpected == 1) {
isVarargsElided = MI->isVariadic();
} else if ((FoundElidedComma || MI->isVariadic()) &&
(NumActuals+1 == MinArgsExpected || (NumActuals == 0 && MinArgsExpected == 2))) { if (!MI->hasCommaPasting()) {
Diag(Tok, getLangOpts().CPlusPlus20
? diag::warn_cxx17_compat_missing_varargs_arg
: diag::ext_missing_varargs_arg);
Diag(MI->getDefinitionLoc(), diag::note_macro_here)
<< MacroName.getIdentifierInfo();
}
isVarargsElided = true;
} else if (!ContainsCodeCompletionTok) {
Diag(Tok, diag::err_too_few_args_in_macro_invoc);
Diag(MI->getDefinitionLoc(), diag::note_macro_here)
<< MacroName.getIdentifierInfo();
return nullptr;
}
SourceLocation EndLoc = Tok.getLocation();
Tok.startToken();
Tok.setKind(tok::eof);
Tok.setLocation(EndLoc);
Tok.setLength(0);
ArgTokens.push_back(Tok);
if (NumActuals == 0 && MinArgsExpected == 2)
ArgTokens.push_back(Tok);
} else if (NumActuals > MinArgsExpected && !MI->isVariadic() &&
!ContainsCodeCompletionTok) {
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);
}
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) {
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());
MacroExpandedTokens.resize(tokIndex);
MacroExpandingLexersStack.pop_back();
}
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();
}
}
static bool HasFeature(const Preprocessor &PP, StringRef Feature) {
const LangOptions &LangOpts = PP.getLangOpts();
if (Feature.startswith("__") && Feature.endswith("__") && Feature.size() >= 4)
Feature = Feature.substr(2, Feature.size() - 4);
#define FEATURE(Name, Predicate) .Case(#Name, Predicate)
return llvm::StringSwitch<bool>(Feature)
#include "clang/Basic/Features.def"
.Default(false);
#undef FEATURE
}
static bool HasExtension(const Preprocessor &PP, StringRef Extension) {
if (HasFeature(PP, Extension))
return true;
if (PP.getDiagnostics().getExtensionHandlingBehavior() >=
diag::Severity::Error)
return false;
const LangOptions &LangOpts = PP.getLangOpts();
if (Extension.startswith("__") && Extension.endswith("__") &&
Extension.size() >= 4)
Extension = Extension.substr(2, Extension.size() - 4);
#define EXTENSION(Name, Predicate) .Case(#Name, Predicate)
return llvm::StringSwitch<bool>(Extension)
#include "clang/Basic/Features.def"
.Default(false);
#undef EXTENSION
}
static bool EvaluateHasIncludeCommon(Token &Tok, IdentifierInfo *II,
Preprocessor &PP,
ConstSearchDirIterator LookupFrom,
const FileEntry *LookupFromFile) {
SourceLocation LParenLoc = Tok.getLocation();
if (!PP.isParsingIfOrElifDirective()) {
PP.Diag(LParenLoc, diag::err_pp_directive_required) << II;
assert(Tok.is(tok::identifier));
Tok.setIdentifierInfo(II);
return false;
}
do {
if (PP.LexHeaderName(Tok))
return false;
} while (Tok.getKind() == tok::comment);
if (Tok.isNot(tok::l_paren)) {
LParenLoc = PP.getLocForEndOfToken(LParenLoc);
PP.Diag(LParenLoc, diag::err_pp_expected_after) << II << tok::l_paren;
if (Tok.isNot(tok::header_name))
return false;
} else {
LParenLoc = Tok.getLocation();
if (PP.LexHeaderName(Tok))
return false;
}
if (Tok.isNot(tok::header_name)) {
PP.Diag(Tok.getLocation(), diag::err_pp_expects_filename);
return false;
}
SmallString<128> FilenameBuffer;
bool Invalid = false;
StringRef Filename = PP.getSpelling(Tok, FilenameBuffer, &Invalid);
if (Invalid)
return false;
SourceLocation FilenameLoc = Tok.getLocation();
PP.LexNonComment(Tok);
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 (Filename.empty())
return false;
Optional<FileEntryRef> File =
PP.LookupFile(FilenameLoc, Filename, isAngled, LookupFrom, LookupFromFile,
nullptr, nullptr, nullptr, nullptr, nullptr, nullptr);
if (PPCallbacks *Callbacks = PP.getPPCallbacks()) {
SrcMgr::CharacteristicKind FileType = SrcMgr::C_User;
if (File)
FileType =
PP.getHeaderSearchInfo().getFileDirFlavor(&File->getFileEntry());
Callbacks->HasInclude(FilenameLoc, Filename, isAngled, File, FileType);
}
return File.has_value();
}
bool Preprocessor::EvaluateHasInclude(Token &Tok, IdentifierInfo *II) {
return EvaluateHasIncludeCommon(Tok, II, *this, nullptr, nullptr);
}
bool Preprocessor::EvaluateHasIncludeNext(Token &Tok, IdentifierInfo *II) {
ConstSearchDirIterator Lookup = nullptr;
const FileEntry *LookupFromFile;
std::tie(Lookup, LookupFromFile) = getIncludeNextStart(Tok);
return EvaluateHasIncludeCommon(Tok, II, *this, Lookup, LookupFromFile);
}
static void EvaluateFeatureLikeBuiltinMacro(llvm::raw_svector_ostream& OS,
Token &Tok, IdentifierInfo *II,
Preprocessor &PP, bool ExpandArgs,
llvm::function_ref<
int(Token &Tok,
bool &HasLexedNextTok)> Op) {
PP.LexUnexpandedToken(Tok);
if (Tok.isNot(tok::l_paren)) {
PP.Diag(Tok.getLocation(), diag::err_pp_expected_after) << II
<< tok::l_paren;
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) {
if (ExpandArgs)
PP.Lex(Tok);
else
PP.LexUnexpandedToken(Tok);
already_lexed:
switch (Tok.getKind()) {
case tok::eof:
case tok::eod:
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)
break;
if (!SuppressDiagnostic) {
PP.Diag(Tok.getLocation(), diag::err_pp_nested_paren) << II;
SuppressDiagnostic = true;
}
continue;
case tok::r_paren:
if (--ParenDepth > 0)
continue;
if (Result) {
OS << Result.value();
if (Result.value() > 1)
OS << 'L';
} else {
OS << 0;
if (!SuppressDiagnostic)
PP.Diag(Tok.getLocation(), diag::err_too_few_args_in_macro_invoc);
}
Tok.setKind(tok::numeric_constant);
return;
default: {
if (Result)
break;
bool HasLexedNextToken = false;
Result = Op(Tok, HasLexedNextToken);
ResultTok = Tok;
if (HasLexedNextToken)
goto already_lexed;
continue;
}
}
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;
}
}
}
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;
}
static bool isTargetArch(const TargetInfo &TI, const IdentifierInfo *II) {
std::string ArchName = II->getName().lower() + "--";
llvm::Triple Arch(ArchName);
const llvm::Triple &TT = TI.getTriple();
if (TT.isThumb()) {
if ((Arch.getSubArch() == llvm::Triple::NoSubArch ||
Arch.getSubArch() == TT.getSubArch()) &&
((TT.getArch() == llvm::Triple::thumb &&
Arch.getArch() == llvm::Triple::arm) ||
(TT.getArch() == llvm::Triple::thumbeb &&
Arch.getArch() == llvm::Triple::armeb)))
return true;
}
return (Arch.getSubArch() == llvm::Triple::NoSubArch ||
Arch.getSubArch() == TT.getSubArch()) &&
Arch.getArch() == TT.getArch();
}
static bool isTargetVendor(const TargetInfo &TI, const IdentifierInfo *II) {
StringRef VendorName = TI.getTriple().getVendorName();
if (VendorName.empty())
VendorName = "unknown";
return VendorName.equals_insensitive(II->getName());
}
static bool isTargetOS(const TargetInfo &TI, const IdentifierInfo *II) {
std::string OSName =
(llvm::Twine("unknown-unknown-") + II->getName().lower()).str();
llvm::Triple OS(OSName);
if (OS.getOS() == llvm::Triple::Darwin) {
return TI.getTriple().isOSDarwin();
}
return TI.getTriple().getOS() == OS.getOS();
}
static bool isTargetEnvironment(const TargetInfo &TI,
const IdentifierInfo *II) {
std::string EnvName = (llvm::Twine("---") + II->getName().lower()).str();
llvm::Triple Env(EnvName);
return TI.getTriple().getEnvironment() == Env.getEnvironment();
}
static bool isTargetVariantOS(const TargetInfo &TI, const IdentifierInfo *II) {
if (TI.getTriple().isOSDarwin()) {
const llvm::Triple *VariantTriple = TI.getDarwinTargetVariantTriple();
if (!VariantTriple)
return false;
std::string OSName =
(llvm::Twine("unknown-unknown-") + II->getName().lower()).str();
llvm::Triple OS(OSName);
if (OS.getOS() == llvm::Triple::Darwin) {
return VariantTriple->isOSDarwin();
}
return VariantTriple->getOS() == OS.getOS();
}
return false;
}
static bool isTargetVariantEnvironment(const TargetInfo &TI,
const IdentifierInfo *II) {
if (TI.getTriple().isOSDarwin()) {
const llvm::Triple *VariantTriple = TI.getDarwinTargetVariantTriple();
if (!VariantTriple)
return false;
std::string EnvName = (llvm::Twine("---") + II->getName().lower()).str();
llvm::Triple Env(EnvName);
return VariantTriple->getEnvironment() == Env.getEnvironment();
}
return false;
}
void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
IdentifierInfo *II = Tok.getIdentifierInfo();
assert(II && "Can't be a macro without id info!");
if (II == Ident_Pragma)
return Handle_Pragma(Tok);
else if (II == Ident__pragma) return HandleMicrosoft__pragma(Tok);
++NumBuiltinMacroExpanded;
SmallString<128> TmpBuffer;
llvm::raw_svector_ostream OS(TmpBuffer);
Tok.setIdentifierInfo(nullptr);
Tok.clearFlag(Token::NeedsCleaning);
bool IsAtStartOfLine = Tok.isAtStartOfLine();
bool HasLeadingSpace = Tok.hasLeadingSpace();
if (II == Ident__LINE__) {
SourceLocation Loc = Tok.getLocation();
Loc = AdvanceToTokenCharacter(Loc, 0);
Loc = SourceMgr.getExpansionRange(Loc).getEnd();
PresumedLoc PLoc = SourceMgr.getPresumedLoc(Loc);
OS << (PLoc.isValid()? PLoc.getLine() : 1);
Tok.setKind(tok::numeric_constant);
} else if (II == Ident__FILE__ || II == Ident__BASE_FILE__ ||
II == Ident__FILE_NAME__) {
PresumedLoc PLoc = SourceMgr.getPresumedLoc(Tok.getLocation());
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();
}
}
SmallString<256> FN;
if (PLoc.isValid()) {
if (II == Ident__FILE_NAME__) {
StringRef PLFileName = llvm::sys::path::filename(PLoc.getFilename());
if (PLFileName != "")
FN += PLFileName;
else
FN += PLoc.getFilename();
} else {
FN += PLoc.getFilename();
}
processPathForFileMacro(FN, getLangOpts(), getTargetInfo());
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__) {
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());
}
OS << Depth;
Tok.setKind(tok::numeric_constant);
} else if (II == Ident__TIMESTAMP__) {
Diag(Tok.getLocation(), diag::warn_pp_date_time);
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";
}
OS << '"' << StringRef(Result).drop_back() << '"';
Tok.setKind(tok::string_literal);
} else if (II == Ident__FLT_EVAL_METHOD__) {
if (getTUFPEvalMethod() ==
LangOptions::FPEvalMethodKind::FEM_Indeterminable) {
auto Toks = std::make_unique<Token[]>(1);
OS << "-";
Tok.setKind(tok::minus);
Token NumberToken;
NumberToken.startToken();
NumberToken.setKind(tok::numeric_constant);
NumberToken.setLiteralData("1");
NumberToken.setLength(1);
Toks[0] = NumberToken;
EnterTokenStream(std::move(Toks), 1, false,
false);
} else {
OS << getTUFPEvalMethod();
Tok.setKind(tok::numeric_constant);
if (getLastFPEvalPragmaLocation().isValid()) {
Diag(Tok, diag::err_illegal_use_of_flt_eval_macro);
Diag(getLastFPEvalPragmaLocation(), diag::note_pragma_entered_here);
}
}
} else if (II == Ident__COUNTER__) {
OS << CounterValue++;
Tok.setKind(tok::numeric_constant);
} else if (II == Ident__has_feature) {
EvaluateFeatureLikeBuiltinMacro(OS, Tok, II, *this, false,
[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, false,
[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, false,
[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) {
switch (II->getBuiltinID()) {
case Builtin::BI__builtin_operator_new:
case Builtin::BI__builtin_operator_delete:
return 201802;
default:
return Builtin::evaluateRequiredTargetFeatures(
getBuiltinInfo().getRequiredFeatures(II->getBuiltinID()),
getTargetInfo().getTargetOpts().FeatureMap);
}
return true;
} else if (II->getTokenID() != tok::identifier ||
II->hasRevertedTokenIDToIdentifier()) {
if (II->getName().startswith("__builtin_") ||
II->getName().startswith("__is_") ||
II->getName().startswith("__has_"))
return true;
return llvm::StringSwitch<bool>(II->getName())
.Case("__array_rank", true)
.Case("__array_extent", true)
.Case("__reference_binds_to_temporary", true)
.Case("__underlying_type", true)
.Default(false);
} else {
return llvm::StringSwitch<bool>(II->getName())
.Case("__make_integer_seq", getLangOpts().CPlusPlus)
.Case("__type_pack_element", getLangOpts().CPlusPlus)
.Case("__is_target_arch", true)
.Case("__is_target_vendor", true)
.Case("__is_target_os", true)
.Case("__is_target_environment", true)
.Case("__is_target_variant_os", true)
.Case("__is_target_variant_environment", true)
.Default(false);
}
});
} else if (II == Ident__is_identifier) {
EvaluateFeatureLikeBuiltinMacro(OS, Tok, II, *this, false,
[](Token &Tok, bool &HasLexedNextToken) -> int {
return Tok.is(tok::identifier);
});
} else if (II == Ident__has_attribute) {
EvaluateFeatureLikeBuiltinMacro(OS, Tok, II, *this, true,
[this](Token &Tok, bool &HasLexedNextToken) -> int {
IdentifierInfo *II = ExpectFeatureIdentifierInfo(Tok, *this,
diag::err_feature_check_malformed);
return II ? hasAttribute(AttributeCommonInfo::Syntax::AS_GNU, nullptr,
II, getTargetInfo(), getLangOpts())
: 0;
});
} else if (II == Ident__has_declspec) {
EvaluateFeatureLikeBuiltinMacro(OS, Tok, II, *this, true,
[this](Token &Tok, bool &HasLexedNextToken) -> int {
IdentifierInfo *II = ExpectFeatureIdentifierInfo(Tok, *this,
diag::err_feature_check_malformed);
if (II) {
const LangOptions &LangOpts = getLangOpts();
return LangOpts.DeclSpecKeyword &&
hasAttribute(AttributeCommonInfo::Syntax::AS_Declspec, nullptr,
II, getTargetInfo(), LangOpts);
}
return false;
});
} else if (II == Ident__has_cpp_attribute ||
II == Ident__has_c_attribute) {
bool IsCXX = II == Ident__has_cpp_attribute;
EvaluateFeatureLikeBuiltinMacro(OS, Tok, II, *this, true,
[&](Token &Tok, bool &HasLexedNextToken) -> int {
IdentifierInfo *ScopeII = nullptr;
IdentifierInfo *II = ExpectFeatureIdentifierInfo(
Tok, *this, diag::err_feature_check_malformed);
if (!II)
return false;
LexUnexpandedToken(Tok);
if (Tok.isNot(tok::coloncolon))
HasLexedNextToken = true;
else {
ScopeII = II;
Lex(Tok);
II = ExpectFeatureIdentifierInfo(Tok, *this,
diag::err_feature_check_malformed);
}
AttributeCommonInfo::Syntax Syntax =
IsCXX ? AttributeCommonInfo::Syntax::AS_CXX11
: AttributeCommonInfo::Syntax::AS_C2x;
return II ? hasAttribute(Syntax, ScopeII, II, getTargetInfo(),
getLangOpts())
: 0;
});
} else if (II == Ident__has_include ||
II == Ident__has_include_next) {
bool Value;
if (II == Ident__has_include)
Value = EvaluateHasInclude(Tok, II);
else
Value = EvaluateHasIncludeNext(Tok, II);
if (Tok.isNot(tok::r_paren))
return;
OS << (int)Value;
Tok.setKind(tok::numeric_constant);
} else if (II == Ident__has_warning) {
EvaluateFeatureLikeBuiltinMacro(OS, Tok, II, *this, false,
[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'",
false))
return false;
if (WarningName.size() < 3 || WarningName[0] != '-' ||
WarningName[1] != 'W') {
Diag(StrStartLoc, diag::warn_has_warning_invalid_option);
return false;
}
SmallVector<diag::kind, 10> Diags;
return !getDiagnostics().getDiagnosticIDs()->
getDiagnosticsInGroup(diag::Flavor::WarningOrError,
WarningName.substr(2), Diags);
});
} else if (II == Ident__building_module) {
EvaluateFeatureLikeBuiltinMacro(OS, Tok, II, *this, false,
[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__) {
OS << getLangOpts().CurrentModule;
IdentifierInfo *ModuleII = getIdentifierInfo(getLangOpts().CurrentModule);
Tok.setIdentifierInfo(ModuleII);
Tok.setKind(ModuleII->getTokenID());
} else if (II == Ident__identifier) {
SourceLocation Loc = Tok.getLocation();
LexNonComment(Tok);
if (Tok.isNot(tok::l_paren)) {
Diag(getLocForEndOfToken(Loc), diag::err_pp_expected_after)
<< II << tok::l_paren;
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 if (Tok.is(tok::string_literal) && !Tok.hasUDSuffix()) {
StringLiteralParser Literal(Tok, *this);
if (Literal.hadError)
return;
Tok.setIdentifierInfo(getIdentifierInfo(Literal.GetString()));
Tok.setKind(tok::identifier);
} else {
Diag(Tok.getLocation(), diag::err_pp_identifier_arg_not_identifier)
<< Tok.getKind();
if (Tok.isOneOf(tok::eof, tok::eod) || Tok.isAnnotation())
return;
}
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 if (II == Ident__is_target_arch) {
EvaluateFeatureLikeBuiltinMacro(
OS, Tok, II, *this, false,
[this](Token &Tok, bool &HasLexedNextToken) -> int {
IdentifierInfo *II = ExpectFeatureIdentifierInfo(
Tok, *this, diag::err_feature_check_malformed);
return II && isTargetArch(getTargetInfo(), II);
});
} else if (II == Ident__is_target_vendor) {
EvaluateFeatureLikeBuiltinMacro(
OS, Tok, II, *this, false,
[this](Token &Tok, bool &HasLexedNextToken) -> int {
IdentifierInfo *II = ExpectFeatureIdentifierInfo(
Tok, *this, diag::err_feature_check_malformed);
return II && isTargetVendor(getTargetInfo(), II);
});
} else if (II == Ident__is_target_os) {
EvaluateFeatureLikeBuiltinMacro(
OS, Tok, II, *this, false,
[this](Token &Tok, bool &HasLexedNextToken) -> int {
IdentifierInfo *II = ExpectFeatureIdentifierInfo(
Tok, *this, diag::err_feature_check_malformed);
return II && isTargetOS(getTargetInfo(), II);
});
} else if (II == Ident__is_target_environment) {
EvaluateFeatureLikeBuiltinMacro(
OS, Tok, II, *this, false,
[this](Token &Tok, bool &HasLexedNextToken) -> int {
IdentifierInfo *II = ExpectFeatureIdentifierInfo(
Tok, *this, diag::err_feature_check_malformed);
return II && isTargetEnvironment(getTargetInfo(), II);
});
} else if (II == Ident__is_target_variant_os) {
EvaluateFeatureLikeBuiltinMacro(
OS, Tok, II, *this, false,
[this](Token &Tok, bool &HasLexedNextToken) -> int {
IdentifierInfo *II = ExpectFeatureIdentifierInfo(
Tok, *this, diag::err_feature_check_malformed);
return II && isTargetVariantOS(getTargetInfo(), II);
});
} else if (II == Ident__is_target_variant_environment) {
EvaluateFeatureLikeBuiltinMacro(
OS, Tok, II, *this, false,
[this](Token &Tok, bool &HasLexedNextToken) -> int {
IdentifierInfo *II = ExpectFeatureIdentifierInfo(
Tok, *this, diag::err_feature_check_malformed);
return II && isTargetVariantEnvironment(getTargetInfo(), II);
});
} else {
llvm_unreachable("Unknown identifier!");
}
CreateString(OS.str(), Tok, Tok.getLocation(), Tok.getLocation());
Tok.setFlagValue(Token::StartOfLine, IsAtStartOfLine);
Tok.setFlagValue(Token::LeadingSpace, HasLeadingSpace);
}
void Preprocessor::markMacroAsUsed(MacroInfo *MI) {
if (MI->isWarnIfUnused() && !MI->isUsed())
WarnUnusedMacroLocs.erase(MI->getDefinitionLoc());
MI->setIsUsed(true);
}
void Preprocessor::processPathForFileMacro(SmallVectorImpl<char> &Path,
const LangOptions &LangOpts,
const TargetInfo &TI) {
LangOpts.remapPathPrefix(Path);
if (LangOpts.UseTargetPathSeparator) {
if (TI.getTriple().isOSWindows())
llvm::sys::path::remove_dots(Path, false,
llvm::sys::path::Style::windows_backslash);
else
llvm::sys::path::remove_dots(Path, false, llvm::sys::path::Style::posix);
}
}