PPMacroExpansion.cpp [plain text]
#include "clang/Lex/Preprocessor.h"
#include "MacroArgs.h"
#include "clang/Lex/MacroInfo.h"
#include "clang/Basic/SourceManager.h"
#include "clang/Basic/FileManager.h"
#include "clang/Basic/TargetInfo.h"
#include "clang/Lex/LexDiagnostic.h"
#include "clang/Lex/CodeCompletionHandler.h"
#include "clang/Lex/ExternalPreprocessorSource.h"
#include "clang/Lex/LiteralSupport.h"
#include "llvm/ADT/StringSwitch.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/Config/llvm-config.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/Format.h"
#include <cstdio>
#include <ctime>
using namespace clang;
MacroInfo *Preprocessor::getMacroInfoHistory(IdentifierInfo *II) const {
assert(II->hadMacroDefinition() && "Identifier has not been not a macro!");
macro_iterator Pos = Macros.find(II);
if (Pos == Macros.end()) {
getExternalSource()->LoadMacroDefinition(II);
Pos = Macros.find(II);
}
assert(Pos != Macros.end() && "Identifier macro info is missing!");
return Pos->second;
}
void Preprocessor::setMacroInfo(IdentifierInfo *II, MacroInfo *MI,
bool LoadedFromAST) {
assert(MI && "MacroInfo should be non-zero!");
assert((LoadedFromAST || MI->getUndefLoc().isInvalid()) &&
"Undefined macros can only be registered when just LoadedFromAST");
MI->setPreviousDefinition(Macros[II]);
Macros[II] = MI;
II->setHasMacroDefinition(MI->getUndefLoc().isInvalid());
if (II->isFromAST() && !LoadedFromAST)
II->setChangedSinceDeserialization();
}
void Preprocessor::clearMacroInfo(IdentifierInfo *II) {
assert(II->hasMacroDefinition() && "Macro is not defined!");
assert(Macros[II]->getUndefLoc().isValid() && "Macro is still defined!");
II->setHasMacroDefinition(false);
if (II->isFromAST())
II->setChangedSinceDeserialization();
}
static IdentifierInfo *RegisterBuiltinMacro(Preprocessor &PP, const char *Name){
IdentifierInfo *Id = PP.getIdentifierInfo(Name);
MacroInfo *MI = PP.AllocateMacroInfo(SourceLocation());
MI->setIsBuiltinMacro();
PP.setMacroInfo(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__BASE_FILE__ = RegisterBuiltinMacro(*this, "__BASE_FILE__");
Ident__INCLUDE_LEVEL__ = RegisterBuiltinMacro(*this, "__INCLUDE_LEVEL__");
Ident__TIMESTAMP__ = RegisterBuiltinMacro(*this, "__TIMESTAMP__");
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_include = RegisterBuiltinMacro(*this, "__has_include");
Ident__has_include_next = RegisterBuiltinMacro(*this, "__has_include_next");
Ident__has_warning = RegisterBuiltinMacro(*this, "__has_warning");
if (LangOpts.Modules) {
Ident__building_module = RegisterBuiltinMacro(*this, "__building_module");
if (!LangOpts.CurrentModule.empty())
Ident__MODULE__ = RegisterBuiltinMacro(*this, "__MODULE__");
else
Ident__MODULE__ = 0;
} else {
Ident__building_module = 0;
Ident__MODULE__ = 0;
}
if (LangOpts.MicrosoftExt)
Ident__pragma = RegisterBuiltinMacro(*this, "__pragma");
else
Ident__pragma = 0;
}
static bool isTrivialSingleTokenExpansion(const MacroInfo *MI,
const IdentifierInfo *MacroIdent,
Preprocessor &PP) {
IdentifierInfo *II = MI->getReplacementToken(0).getIdentifierInfo();
if (II == 0) return true;
if (II->isOutOfDate())
PP.getExternalSource()->updateOutOfDateIdentifier(*II);
if (II->hasMacroDefinition() && PP.getMacroInfo(II)->isEnabled() &&
II != MacroIdent)
return false;
if (MI->isObjectLike()) return true;
for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
I != E; ++I)
if (*I == II)
return false;
return true;
}
bool Preprocessor::isNextPPTokenLParen() {
unsigned Val;
if (CurLexer)
Val = CurLexer->isNextPPTokenLParen();
else if (CurPTHLexer)
Val = CurPTHLexer->isNextPPTokenLParen();
else
Val = CurTokenLexer->isNextTokenLParen();
if (Val == 2) {
if (CurPPLexer)
return false;
for (unsigned i = IncludeMacroStack.size(); i != 0; --i) {
IncludeStackInfo &Entry = IncludeMacroStack[i-1];
if (Entry.TheLexer)
Val = Entry.TheLexer->isNextPPTokenLParen();
else if (Entry.ThePTHLexer)
Val = Entry.ThePTHLexer->isNextPPTokenLParen();
else
Val = Entry.TheTokenLexer->isNextTokenLParen();
if (Val != 2)
break;
if (Entry.ThePPLexer)
return false;
}
}
return Val == 1;
}
bool Preprocessor::HandleMacroExpandedIdentifier(Token &Identifier,
MacroInfo *MI) {
if (CurPPLexer) CurPPLexer->MIOpt.ExpandedMacro();
if (MI->isBuiltinMacro()) {
if (Callbacks) Callbacks->MacroExpands(Identifier, MI,
Identifier.getLocation());
ExpandBuiltinMacro(Identifier);
return false;
}
MacroArgs *Args = 0;
SourceLocation ExpansionEnd = Identifier.getLocation();
if (MI->isFunctionLike()) {
if (!isNextPPTokenLParen())
return true;
InMacroArgs = true;
Args = ReadFunctionLikeMacroArgs(Identifier, MI, ExpansionEnd);
InMacroArgs = false;
if (Args == 0) return false;
++NumFnMacroExpanded;
} else {
++NumMacroExpanded;
}
markMacroAsUsed(MI);
SourceLocation ExpandLoc = Identifier.getLocation();
SourceRange ExpansionRange(ExpandLoc, ExpansionEnd);
if (Callbacks) {
if (InMacroArgs) {
DelayedMacroExpandsCallbacks.push_back(
MacroExpandsInfo(Identifier, MI, ExpansionRange));
} else {
Callbacks->MacroExpands(Identifier, MI, ExpansionRange);
if (!DelayedMacroExpandsCallbacks.empty()) {
for (unsigned i=0, e = DelayedMacroExpandsCallbacks.size(); i!=e; ++i) {
MacroExpandsInfo &Info = DelayedMacroExpandsCallbacks[i];
Callbacks->MacroExpands(Info.Tok, Info.MI, Info.Range);
}
DelayedMacroExpandsCallbacks.clear();
}
}
}
if (MI->getNumTokens() == 0) {
if (Args) Args->destroy(*this);
bool HadLeadingSpace = Identifier.hasLeadingSpace();
bool IsAtStartOfLine = Identifier.isAtStartOfLine();
Lex(Identifier);
if (!Identifier.isAtStartOfLine()) {
if (IsAtStartOfLine) Identifier.setFlag(Token::StartOfLine);
if (HadLeadingSpace) Identifier.setFlag(Token::LeadingSpace);
}
Identifier.setFlag(Token::LeadingEmptyMacro);
++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);
Diag(Identifier, diag::pp_disabled_macro_expansion);
}
}
++NumFastMacroExpanded;
return false;
}
EnterMacro(Identifier, ExpansionEnd, MI, Args);
Lex(Identifier);
return false;
}
MacroArgs *Preprocessor::ReadFunctionLikeMacroArgs(Token &MacroName,
MacroInfo *MI,
SourceLocation &MacroEnd) {
unsigned NumFixedArgsLeft = MI->getNumArgs();
bool isVariadic = MI->isVariadic();
Token Tok;
LexUnexpandedToken(Tok);
assert(Tok.is(tok::l_paren) && "Error computing l-paren-ness?");
SmallVector<Token, 64> ArgTokens;
unsigned NumActuals = 0;
while (Tok.isNot(tok::r_paren)) {
assert((Tok.is(tok::l_paren) || Tok.is(tok::comma)) &&
"only expect argument separators here");
unsigned ArgTokenStart = ArgTokens.size();
SourceLocation ArgStartLoc = Tok.getLocation();
unsigned NumParens = 0;
while (1) {
LexUnexpandedToken(Tok);
if (Tok.is(tok::eof) || Tok.is(tok::eod)) { Diag(MacroName, diag::err_unterm_macro_invoc);
MacroName = Tok;
return 0;
} else if (Tok.is(tok::r_paren)) {
if (NumParens-- == 0) {
MacroEnd = Tok.getLocation();
break;
}
} else if (Tok.is(tok::l_paren)) {
++NumParens;
} else if (Tok.is(tok::comma) && NumParens == 0) {
if (!isVariadic) break;
if (NumFixedArgsLeft > 1)
break;
} else if (Tok.is(tok::comment) && !KeepMacroComments) {
continue;
} else if (Tok.getIdentifierInfo() != 0) {
if (MacroInfo *MI = getMacroInfo(Tok.getIdentifierInfo()))
if (!MI->isEnabled())
Tok.setFlag(Token::DisableExpand);
} else if (Tok.is(tok::code_completion)) {
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) {
if (ArgTokens.size() != ArgTokenStart)
ArgStartLoc = ArgTokens[ArgTokenStart].getLocation();
Diag(ArgStartLoc, diag::err_too_many_args_in_macro_invoc);
return 0;
}
if (ArgTokens.size() == ArgTokenStart && !LangOpts.C99)
Diag(Tok, LangOpts.CPlusPlus0x ?
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;
assert(NumFixedArgsLeft != 0 && "Too many arguments parsed");
--NumFixedArgsLeft;
}
unsigned MinArgsExpected = MI->getNumArgs();
bool isVarargsElided = false;
if (NumActuals < MinArgsExpected) {
if (NumActuals == 0 && MinArgsExpected == 1) {
isVarargsElided = MI->isVariadic();
} else if (MI->isVariadic() &&
(NumActuals+1 == MinArgsExpected || (NumActuals == 0 && MinArgsExpected == 2))) { if (!MI->hasCommaPasting()) {
Diag(Tok, diag::ext_missing_varargs_arg);
Diag(MI->getDefinitionLoc(), diag::note_macro_here)
<< MacroName.getIdentifierInfo();
}
isVarargsElided = true;
} else {
Diag(Tok, diag::err_too_few_args_in_macro_invoc);
return 0;
}
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()) {
Diag(MacroName, diag::err_too_many_args_in_macro_invoc);
return 0;
}
return MacroArgs::create(MI, ArgTokens, isVarargsElided, *this);
}
Token *Preprocessor::cacheMacroExpandedTokens(TokenLexer *tokLexer,
ArrayRef<Token> tokens) {
assert(tokLexer);
if (tokens.empty())
return 0;
size_t newIndex = MacroExpandedTokens.size();
bool cacheNeedsToGrow = tokens.size() >
MacroExpandedTokens.capacity()-MacroExpandedTokens.size();
MacroExpandedTokens.append(tokens.begin(), tokens.end());
if (cacheNeedsToGrow) {
for (unsigned i = 0, e = MacroExpandingLexersStack.size(); i != e; ++i) {
TokenLexer *prevLexer;
size_t tokIndex;
llvm::tie(prevLexer, tokIndex) = MacroExpandingLexersStack[i];
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(0);
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, const IdentifierInfo *II) {
const LangOptions &LangOpts = PP.getLangOpts();
StringRef Feature = II->getName();
if (Feature.startswith("__") && Feature.endswith("__") && Feature.size() >= 4)
Feature = Feature.substr(2, Feature.size() - 4);
return llvm::StringSwitch<bool>(Feature)
.Case("address_sanitizer", LangOpts.AddressSanitizer)
.Case("attribute_analyzer_noreturn", true)
.Case("attribute_availability", true)
.Case("attribute_availability_with_message", true)
.Case("attribute_cf_returns_not_retained", true)
.Case("attribute_cf_returns_retained", true)
.Case("attribute_deprecated_with_message", 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("blocks", LangOpts.Blocks)
.Case("cxx_exceptions", LangOpts.Exceptions)
.Case("cxx_rtti", LangOpts.RTTI)
.Case("enumerator_attributes", true)
.Case("objc_arr", LangOpts.ObjCAutoRefCount) .Case("objc_arc", LangOpts.ObjCAutoRefCount)
.Case("objc_arc_weak", LangOpts.ObjCARCWeak)
.Case("objc_default_synthesize_properties", LangOpts.ObjC2)
.Case("objc_fixed_enum", LangOpts.ObjC2)
.Case("objc_instancetype", LangOpts.ObjC2)
.Case("objc_modules", LangOpts.ObjC2 && LangOpts.Modules)
.Case("objc_nonfragile_abi", LangOpts.ObjCRuntime.isNonFragile())
.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("arc_cf_code_audited", true)
.Case("c_alignas", LangOpts.C11)
.Case("c_atomic", LangOpts.C11)
.Case("c_generic_selections", LangOpts.C11)
.Case("c_static_assert", LangOpts.C11)
.Case("cxx_access_control_sfinae", LangOpts.CPlusPlus0x)
.Case("cxx_alias_templates", LangOpts.CPlusPlus0x)
.Case("cxx_alignas", LangOpts.CPlusPlus0x)
.Case("cxx_atomic", LangOpts.CPlusPlus0x)
.Case("cxx_attributes", LangOpts.CPlusPlus0x)
.Case("cxx_auto_type", LangOpts.CPlusPlus0x)
.Case("cxx_constexpr", LangOpts.CPlusPlus0x)
.Case("cxx_decltype", LangOpts.CPlusPlus0x)
.Case("cxx_decltype_incomplete_return_types", LangOpts.CPlusPlus0x)
.Case("cxx_default_function_template_args", LangOpts.CPlusPlus0x)
.Case("cxx_defaulted_functions", LangOpts.CPlusPlus0x)
.Case("cxx_delegating_constructors", LangOpts.CPlusPlus0x)
.Case("cxx_deleted_functions", LangOpts.CPlusPlus0x)
.Case("cxx_explicit_conversions", LangOpts.CPlusPlus0x)
.Case("cxx_generalized_initializers", LangOpts.CPlusPlus0x)
.Case("cxx_implicit_moves", LangOpts.CPlusPlus0x)
.Case("cxx_inline_namespaces", LangOpts.CPlusPlus0x)
.Case("cxx_lambdas", LangOpts.CPlusPlus0x)
.Case("cxx_local_type_template_args", LangOpts.CPlusPlus0x)
.Case("cxx_nonstatic_member_init", LangOpts.CPlusPlus0x)
.Case("cxx_noexcept", LangOpts.CPlusPlus0x)
.Case("cxx_nullptr", LangOpts.CPlusPlus0x)
.Case("cxx_override_control", LangOpts.CPlusPlus0x)
.Case("cxx_range_for", LangOpts.CPlusPlus0x)
.Case("cxx_raw_string_literals", LangOpts.CPlusPlus0x)
.Case("cxx_reference_qualified_functions", LangOpts.CPlusPlus0x)
.Case("cxx_rvalue_references", LangOpts.CPlusPlus0x)
.Case("cxx_strong_enums", LangOpts.CPlusPlus0x)
.Case("cxx_static_assert", LangOpts.CPlusPlus0x)
.Case("cxx_trailing_return", LangOpts.CPlusPlus0x)
.Case("cxx_unicode_literals", LangOpts.CPlusPlus0x)
.Case("cxx_unrestricted_unions", LangOpts.CPlusPlus0x)
.Case("cxx_user_literals", LangOpts.CPlusPlus0x)
.Case("cxx_variadic_templates", LangOpts.CPlusPlus0x)
.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_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_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("tls", PP.getTargetInfo().isTLSSupported())
.Case("underlying_type", LangOpts.CPlusPlus)
.Default(false);
}
static bool HasExtension(const Preprocessor &PP, const IdentifierInfo *II) {
if (HasFeature(PP, II))
return true;
if (PP.getDiagnostics().getExtensionHandlingBehavior() ==
DiagnosticsEngine::Ext_Error)
return false;
const LangOptions &LangOpts = PP.getLangOpts();
StringRef Extension = II->getName();
if (Extension.startswith("__") && Extension.endswith("__") &&
Extension.size() >= 4)
Extension = Extension.substr(2, Extension.size() - 4);
return llvm::StringSwitch<bool>(Extension)
.Case("c_alignas", true)
.Case("c_atomic", true)
.Case("c_generic_selections", true)
.Case("c_static_assert", true)
.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)
.Default(false);
}
static bool HasAttribute(const IdentifierInfo *II) {
StringRef Name = II->getName();
if (Name.startswith("__") && Name.endswith("__") && Name.size() >= 4)
Name = Name.substr(2, Name.size() - 4);
return llvm::StringSwitch<bool>(Name)
#include "clang/Lex/AttrSpellings.inc"
.Default(false);
}
static bool EvaluateHasIncludeCommon(Token &Tok,
IdentifierInfo *II, Preprocessor &PP,
const DirectoryLookup *LookupFrom) {
SourceLocation LParenLoc;
PP.LexNonComment(Tok);
if (Tok.isNot(tok::l_paren)) {
PP.Diag(Tok.getLocation(), diag::err_pp_missing_lparen) << II->getName();
return false;
}
LParenLoc = Tok.getLocation();
PP.getCurrentLexer()->LexIncludeFilename(Tok);
SmallString<128> FilenameBuffer;
StringRef Filename;
SourceLocation EndLoc;
switch (Tok.getKind()) {
case tok::eod:
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:
FilenameBuffer.push_back('<');
if (PP.ConcatenateIncludeName(FilenameBuffer, EndLoc))
return false; Filename = FilenameBuffer.str();
break;
default:
PP.Diag(Tok.getLocation(), diag::err_pp_expects_filename);
return false;
}
PP.LexNonComment(Tok);
if (Tok.isNot(tok::r_paren)) {
PP.Diag(Tok.getLocation(), diag::err_pp_missing_rparen) << II->getName();
PP.Diag(LParenLoc, diag::note_matching) << "(";
return false;
}
bool isAngled = PP.GetIncludeFilenameSpelling(Tok.getLocation(), Filename);
if (Filename.empty())
return false;
const DirectoryLookup *CurDir;
const FileEntry *File =
PP.LookupFile(Filename, isAngled, LookupFrom, CurDir, NULL, NULL, NULL);
return File != 0;
}
static bool EvaluateHasInclude(Token &Tok, IdentifierInfo *II,
Preprocessor &PP) {
return EvaluateHasIncludeCommon(Tok, II, PP, NULL);
}
static bool EvaluateHasIncludeNext(Token &Tok,
IdentifierInfo *II, Preprocessor &PP) {
const DirectoryLookup *Lookup = PP.GetCurDirLookup();
if (PP.isInPrimaryFile()) {
Lookup = 0;
PP.Diag(Tok, diag::pp_include_next_in_primary);
} else if (Lookup == 0) {
PP.Diag(Tok, diag::pp_include_next_absolute_path);
} else {
++Lookup;
}
return EvaluateHasIncludeCommon(Tok, II, PP, Lookup);
}
static bool EvaluateBuildingModule(Token &Tok,
IdentifierInfo *II, Preprocessor &PP) {
PP.LexNonComment(Tok);
if (Tok.isNot(tok::l_paren)) {
PP.Diag(Tok.getLocation(), diag::err_pp_missing_lparen) << II->getName();
return false;
}
SourceLocation LParenLoc = Tok.getLocation();
PP.LexNonComment(Tok);
if (Tok.isNot(tok::identifier)) {
PP.Diag(Tok.getLocation(), diag::err_expected_id_building_module);
return false;
}
bool Result
= Tok.getIdentifierInfo()->getName() == PP.getLangOpts().CurrentModule;
PP.LexNonComment(Tok);
if (Tok.isNot(tok::r_paren)) {
PP.Diag(Tok.getLocation(), diag::err_pp_missing_rparen) << II->getName();
PP.Diag(LParenLoc, diag::note_matching) << "(";
return false;
}
return Result;
}
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(0);
Tok.clearFlag(Token::NeedsCleaning);
if (II == Ident__LINE__) {
SourceLocation Loc = Tok.getLocation();
Loc = AdvanceToTokenCharacter(Loc, 0);
Loc = SourceMgr.getExpansionRange(Loc).second;
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__) {
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<128> FN;
if (PLoc.isValid()) {
FN += PLoc.getFilename();
Lexer::Stringify(FN);
OS << '"' << FN.str() << '"';
}
Tok.setKind(tok::string_literal);
} else if (II == Ident__DATE__) {
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__) {
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__) {
const FileEntry *CurFile = 0;
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, strlen(Result)-1) << '"';
Tok.setKind(tok::string_literal);
} else if (II == Ident__COUNTER__) {
OS << CounterValue++;
Tok.setKind(tok::numeric_constant);
} else if (II == Ident__has_feature ||
II == Ident__has_extension ||
II == Ident__has_builtin ||
II == Ident__has_attribute) {
SourceLocation StartLoc = Tok.getLocation();
bool IsValid = false;
IdentifierInfo *FeatureII = 0;
Lex(Tok);
if (Tok.is(tok::l_paren)) {
Lex(Tok);
if (Tok.is(tok::identifier) || Tok.is(tok::kw_const)) {
FeatureII = Tok.getIdentifierInfo();
Lex(Tok);
if (Tok.is(tok::r_paren))
IsValid = true;
}
}
bool Value = false;
if (!IsValid)
Diag(StartLoc, diag::err_feature_check_malformed);
else if (II == Ident__has_builtin) {
Value = FeatureII->getBuiltinID() != 0;
} else if (II == Ident__has_attribute)
Value = HasAttribute(FeatureII);
else if (II == Ident__has_extension)
Value = HasExtension(*this, FeatureII);
else {
assert(II == Ident__has_feature && "Must be feature check");
Value = HasFeature(*this, FeatureII);
}
OS << (int)Value;
if (IsValid)
Tok.setKind(tok::numeric_constant);
} else if (II == Ident__has_include ||
II == Ident__has_include_next) {
bool Value;
if (II == Ident__has_include)
Value = EvaluateHasInclude(Tok, II, *this);
else
Value = EvaluateHasIncludeNext(Tok, II, *this);
OS << (int)Value;
Tok.setKind(tok::numeric_constant);
} else if (II == Ident__has_warning) {
SourceLocation StartLoc = Tok.getLocation();
bool IsValid = false;
bool Value = false;
Lex(Tok);
do {
if (Tok.is(tok::l_paren)) {
Lex(Tok);
if (!Tok.is(tok::string_literal)) {
StartLoc = Tok.getLocation();
IsValid = false;
do Lex(Tok); while (!(Tok.is(tok::r_paren) || Tok.is(tok::eod)));
break;
}
SmallVector<Token, 4> StrToks;
while (Tok.is(tok::string_literal)) {
if (Tok.hasUDSuffix())
Diag(Tok, diag::err_invalid_string_udl);
StrToks.push_back(Tok);
LexUnexpandedToken(Tok);
}
if (!(IsValid = Tok.is(tok::r_paren)))
break;
StringLiteralParser Literal(&StrToks[0], StrToks.size(), *this);
assert(Literal.isAscii() && "Didn't allow wide strings in");
if (Literal.hadError)
break;
if (Literal.Pascal) {
Diag(Tok, diag::warn_pragma_diagnostic_invalid);
break;
}
StringRef WarningName(Literal.GetString());
if (WarningName.size() < 3 || WarningName[0] != '-' ||
WarningName[1] != 'W') {
Diag(StrToks[0].getLocation(), diag::warn_has_warning_invalid_option);
break;
}
llvm::SmallVector<diag::kind, 10> Diags;
Value = !getDiagnostics().getDiagnosticIDs()->
getDiagnosticsInGroup(WarningName.substr(2), Diags);
}
} while (false);
if (!IsValid)
Diag(StartLoc, diag::err_warning_check_malformed);
OS << (int)Value;
Tok.setKind(tok::numeric_constant);
} else if (II == Ident__building_module) {
OS << (int)EvaluateBuildingModule(Tok, II, *this);
Tok.setKind(tok::numeric_constant);
} else if (II == Ident__MODULE__) {
OS << getLangOpts().CurrentModule;
IdentifierInfo *ModuleII = getIdentifierInfo(getLangOpts().CurrentModule);
Tok.setIdentifierInfo(ModuleII);
Tok.setKind(ModuleII->getTokenID());
} else {
llvm_unreachable("Unknown identifier!");
}
CreateString(OS.str(), Tok, Tok.getLocation(), Tok.getLocation());
}
void Preprocessor::markMacroAsUsed(MacroInfo *MI) {
if (MI->isWarnIfUnused() && !MI->isUsed())
WarnUnusedMacroLocs.erase(MI->getDefinitionLoc());
MI->setIsUsed(true);
}