2008-03-09 11:13:06 +08:00
|
|
|
//===--- 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 expasion for the
|
|
|
|
// preprocessor.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/Lex/Preprocessor.h"
|
|
|
|
#include "MacroArgs.h"
|
|
|
|
#include "clang/Lex/MacroInfo.h"
|
|
|
|
#include "clang/Basic/SourceManager.h"
|
|
|
|
#include "clang/Basic/FileManager.h"
|
2009-01-29 13:15:15 +08:00
|
|
|
#include "clang/Lex/LexDiagnostic.h"
|
2009-03-03 06:20:04 +08:00
|
|
|
#include <cstdio>
|
2008-03-18 13:59:11 +08:00
|
|
|
#include <ctime>
|
2008-03-09 11:13:06 +08:00
|
|
|
using namespace clang;
|
|
|
|
|
|
|
|
/// setMacroInfo - Specify a macro for this identifier.
|
|
|
|
///
|
|
|
|
void Preprocessor::setMacroInfo(IdentifierInfo *II, MacroInfo *MI) {
|
2009-04-11 05:17:07 +08:00
|
|
|
if (MI) {
|
2008-03-09 11:13:06 +08:00
|
|
|
Macros[II] = MI;
|
|
|
|
II->setHasMacroDefinition(true);
|
2009-04-11 05:17:07 +08:00
|
|
|
} else if (II->hasMacroDefinition()) {
|
|
|
|
Macros.erase(II);
|
|
|
|
II->setHasMacroDefinition(false);
|
2008-03-09 11:13:06 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// RegisterBuiltinMacro - Register the specified identifier in the identifier
|
|
|
|
/// table and mark it as a builtin macro to be expanded.
|
2009-06-13 15:13:28 +08:00
|
|
|
static IdentifierInfo *RegisterBuiltinMacro(Preprocessor &PP, const char *Name){
|
2008-03-09 11:13:06 +08:00
|
|
|
// Get the identifier.
|
2009-06-13 15:13:28 +08:00
|
|
|
IdentifierInfo *Id = PP.getIdentifierInfo(Name);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 11:13:06 +08:00
|
|
|
// Mark it as being a macro that is builtin.
|
2009-06-13 15:13:28 +08:00
|
|
|
MacroInfo *MI = PP.AllocateMacroInfo(SourceLocation());
|
2008-03-09 11:13:06 +08:00
|
|
|
MI->setIsBuiltinMacro();
|
2009-06-13 15:13:28 +08:00
|
|
|
PP.setMacroInfo(Id, MI);
|
2008-03-09 11:13:06 +08:00
|
|
|
return Id;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// RegisterBuiltinMacros - Register builtin macros, such as __LINE__ with the
|
|
|
|
/// identifier table.
|
|
|
|
void Preprocessor::RegisterBuiltinMacros() {
|
2009-06-13 15:13:28 +08:00
|
|
|
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");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 11:13:06 +08:00
|
|
|
// GCC Extensions.
|
2009-06-13 15:13:28 +08:00
|
|
|
Ident__BASE_FILE__ = RegisterBuiltinMacro(*this, "__BASE_FILE__");
|
|
|
|
Ident__INCLUDE_LEVEL__ = RegisterBuiltinMacro(*this, "__INCLUDE_LEVEL__");
|
|
|
|
Ident__TIMESTAMP__ = RegisterBuiltinMacro(*this, "__TIMESTAMP__");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-06-13 15:13:28 +08:00
|
|
|
// Clang Extensions.
|
|
|
|
Ident__has_feature = RegisterBuiltinMacro(*this, "__has_feature");
|
|
|
|
Ident__has_builtin = RegisterBuiltinMacro(*this, "__has_builtin");
|
2008-03-09 11:13:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// 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 == 0) return true;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 11:13:06 +08:00
|
|
|
// If the identifier is a macro, and if that macro is enabled, it may be
|
|
|
|
// expanded so it's not a trivial expansion.
|
|
|
|
if (II->hasMacroDefinition() && PP.getMacroInfo(II)->isEnabled() &&
|
|
|
|
// Fast expanding "#define X X" is ok, because X would be disabled.
|
|
|
|
II != MacroIdent)
|
|
|
|
return false;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 11:13:06 +08:00
|
|
|
// 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.
|
|
|
|
for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
|
|
|
|
I != E; ++I)
|
|
|
|
if (*I == II)
|
|
|
|
return false; // Identifier is a macro argument.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 11:13:06 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// 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();
|
2008-11-20 06:43:49 +08:00
|
|
|
else if (CurPTHLexer)
|
|
|
|
Val = CurPTHLexer->isNextPPTokenLParen();
|
2008-03-09 11:13:06 +08:00
|
|
|
else
|
|
|
|
Val = CurTokenLexer->isNextTokenLParen();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 11:13:06 +08:00
|
|
|
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.
|
2008-11-20 06:21:33 +08:00
|
|
|
if (CurPPLexer)
|
2008-03-09 11:13:06 +08:00
|
|
|
return false;
|
|
|
|
for (unsigned i = IncludeMacroStack.size(); i != 0; --i) {
|
|
|
|
IncludeStackInfo &Entry = IncludeMacroStack[i-1];
|
|
|
|
if (Entry.TheLexer)
|
|
|
|
Val = Entry.TheLexer->isNextPPTokenLParen();
|
2008-11-21 00:46:54 +08:00
|
|
|
else if (Entry.ThePTHLexer)
|
|
|
|
Val = Entry.ThePTHLexer->isNextPPTokenLParen();
|
2008-03-09 11:13:06 +08:00
|
|
|
else
|
|
|
|
Val = Entry.TheTokenLexer->isNextTokenLParen();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 11:13:06 +08:00
|
|
|
if (Val != 2)
|
|
|
|
break;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 11:13:06 +08:00
|
|
|
// Ran off the end of a source file?
|
2008-11-21 00:46:54 +08:00
|
|
|
if (Entry.ThePPLexer)
|
2008-03-09 11:13:06 +08:00
|
|
|
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.
|
2009-04-18 09:13:56 +08:00
|
|
|
return Val == 1;
|
2008-03-09 11:13:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// HandleMacroExpandedIdentifier - If an identifier token is read that is to be
|
|
|
|
/// expanded as a macro, handle it and return the next token as 'Identifier'.
|
2009-09-09 23:08:12 +08:00
|
|
|
bool Preprocessor::HandleMacroExpandedIdentifier(Token &Identifier,
|
2008-03-09 11:13:06 +08:00
|
|
|
MacroInfo *MI) {
|
2009-03-13 01:31:43 +08:00
|
|
|
if (Callbacks) Callbacks->MacroExpands(Identifier, MI);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 11:13:06 +08:00
|
|
|
// If this is a macro exapnsion 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.
|
2008-11-18 09:12:54 +08:00
|
|
|
if (CurPPLexer) CurPPLexer->MIOpt.ExpandedMacro();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 11:13:06 +08:00
|
|
|
// If this is a builtin macro, like __LINE__ or _Pragma, handle it specially.
|
|
|
|
if (MI->isBuiltinMacro()) {
|
|
|
|
ExpandBuiltinMacro(Identifier);
|
|
|
|
return false;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 11:13:06 +08:00
|
|
|
/// 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 = 0;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-16 04:52:18 +08:00
|
|
|
// Remember where the end of the instantiation occurred. For an object-like
|
|
|
|
// macro, this is the identifier. For a function-like macro, this is the ')'.
|
|
|
|
SourceLocation InstantiationEnd = Identifier.getLocation();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 11:13:06 +08:00
|
|
|
// If this is a function-like macro, read the arguments.
|
|
|
|
if (MI->isFunctionLike()) {
|
|
|
|
// C99 6.10.3p10: If the preprocessing token immediately after the the macro
|
2009-04-18 09:13:56 +08:00
|
|
|
// name isn't a '(', this macro should not be expanded.
|
2008-03-09 11:13:06 +08:00
|
|
|
if (!isNextPPTokenLParen())
|
|
|
|
return true;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 11:13:06 +08:00
|
|
|
// 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;
|
2009-02-16 04:52:18 +08:00
|
|
|
Args = ReadFunctionLikeMacroArgs(Identifier, MI, InstantiationEnd);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 11:13:06 +08:00
|
|
|
// Finished parsing args.
|
|
|
|
InMacroArgs = false;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 11:13:06 +08:00
|
|
|
// If there was an error parsing the arguments, bail out.
|
|
|
|
if (Args == 0) return false;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 11:13:06 +08:00
|
|
|
++NumFnMacroExpanded;
|
|
|
|
} else {
|
|
|
|
++NumMacroExpanded;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 11:13:06 +08:00
|
|
|
// Notice that this macro has been used.
|
|
|
|
MI->setIsUsed(true);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 11:13:06 +08:00
|
|
|
// If we started lexing a macro, enter the macro expansion body.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 11:13:06 +08:00
|
|
|
// 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();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 11:13:06 +08:00
|
|
|
// Ignore this macro use, just return the next token in the current
|
|
|
|
// buffer.
|
|
|
|
bool HadLeadingSpace = Identifier.hasLeadingSpace();
|
|
|
|
bool IsAtStartOfLine = Identifier.isAtStartOfLine();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 11:13:06 +08:00
|
|
|
Lex(Identifier);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 11:13:06 +08:00
|
|
|
// If the identifier isn't on some OTHER line, inherit the leading
|
|
|
|
// whitespace/first-on-a-line property of this token. This handles
|
|
|
|
// stuff like "! XX," -> "! ," and " XX," -> " ,", when XX is
|
|
|
|
// empty.
|
|
|
|
if (!Identifier.isAtStartOfLine()) {
|
|
|
|
if (IsAtStartOfLine) Identifier.setFlag(Token::StartOfLine);
|
|
|
|
if (HadLeadingSpace) Identifier.setFlag(Token::LeadingSpace);
|
|
|
|
}
|
|
|
|
++NumFastMacroExpanded;
|
|
|
|
return false;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 11:13:06 +08:00
|
|
|
} else if (MI->getNumTokens() == 1 &&
|
|
|
|
isTrivialSingleTokenExpansion(MI, Identifier.getIdentifierInfo(),
|
2009-01-26 08:43:02 +08:00
|
|
|
*this)) {
|
2008-03-09 11:13:06 +08:00
|
|
|
// Otherwise, if this macro expands into a single trivially-expanded
|
2009-09-09 23:08:12 +08:00
|
|
|
// token: expand it now. This handles common cases like
|
2008-03-09 11:13:06 +08:00
|
|
|
// "#define VAL 42".
|
2008-03-21 15:13:02 +08:00
|
|
|
|
|
|
|
// No need for arg info.
|
|
|
|
if (Args) Args->destroy();
|
|
|
|
|
2008-03-09 11:13:06 +08:00
|
|
|
// Propagate the isAtStartOfLine/hasLeadingSpace markers of the macro
|
|
|
|
// identifier to the expanded token.
|
|
|
|
bool isAtStartOfLine = Identifier.isAtStartOfLine();
|
|
|
|
bool hasLeadingSpace = Identifier.hasLeadingSpace();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 11:13:06 +08:00
|
|
|
// Remember where the token is instantiated.
|
|
|
|
SourceLocation InstantiateLoc = Identifier.getLocation();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 11:13:06 +08:00
|
|
|
// Replace the result token.
|
|
|
|
Identifier = MI->getReplacementToken(0);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 11:13:06 +08:00
|
|
|
// Restore the StartOfLine/LeadingSpace markers.
|
|
|
|
Identifier.setFlagValue(Token::StartOfLine , isAtStartOfLine);
|
|
|
|
Identifier.setFlagValue(Token::LeadingSpace, hasLeadingSpace);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-01-16 15:36:28 +08:00
|
|
|
// Update the tokens location to include both its instantiation and physical
|
2008-03-09 11:13:06 +08:00
|
|
|
// locations.
|
|
|
|
SourceLocation Loc =
|
2009-01-26 08:43:02 +08:00
|
|
|
SourceMgr.createInstantiationLoc(Identifier.getLocation(), InstantiateLoc,
|
2009-02-16 04:52:18 +08:00
|
|
|
InstantiationEnd,Identifier.getLength());
|
2008-03-09 11:13:06 +08:00
|
|
|
Identifier.setLocation(Loc);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 11:13:06 +08:00
|
|
|
// If this is #define X X, we must mark the result as unexpandible.
|
|
|
|
if (IdentifierInfo *NewII = Identifier.getIdentifierInfo())
|
|
|
|
if (getMacroInfo(NewII) == MI)
|
|
|
|
Identifier.setFlag(Token::DisableExpand);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 11:13:06 +08:00
|
|
|
// Since this is not an identifier token, it can't be macro expanded, so
|
|
|
|
// we're done.
|
|
|
|
++NumFastMacroExpanded;
|
|
|
|
return false;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 11:13:06 +08:00
|
|
|
// Start expanding the macro.
|
2009-02-16 04:52:18 +08:00
|
|
|
EnterMacro(Identifier, InstantiationEnd, Args);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 11:13:06 +08:00
|
|
|
// Now that the macro is at the top of the include stack, ask the
|
|
|
|
// preprocessor to read the next token from it.
|
|
|
|
Lex(Identifier);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-04-18 09:13:56 +08:00
|
|
|
/// 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.
|
2008-03-09 11:13:06 +08:00
|
|
|
MacroArgs *Preprocessor::ReadFunctionLikeMacroArgs(Token &MacroName,
|
2009-02-16 04:52:18 +08:00
|
|
|
MacroInfo *MI,
|
|
|
|
SourceLocation &MacroEnd) {
|
2008-03-09 11:13:06 +08:00
|
|
|
// The number of fixed arguments to parse.
|
|
|
|
unsigned NumFixedArgsLeft = MI->getNumArgs();
|
|
|
|
bool isVariadic = MI->isVariadic();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 11:13:06 +08:00
|
|
|
// Outer loop, while there are more arguments, keep reading them.
|
|
|
|
Token Tok;
|
|
|
|
|
2009-04-18 09:13:56 +08:00
|
|
|
// 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?");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 11:13:06 +08:00
|
|
|
// 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.
|
|
|
|
llvm::SmallVector<Token, 64> ArgTokens;
|
|
|
|
|
|
|
|
unsigned NumActuals = 0;
|
2009-04-18 09:13:56 +08:00
|
|
|
while (Tok.isNot(tok::r_paren)) {
|
|
|
|
assert((Tok.is(tok::l_paren) || Tok.is(tok::comma)) &&
|
|
|
|
"only expect argument separators here");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-18 09:13:56 +08:00
|
|
|
unsigned ArgTokenStart = ArgTokens.size();
|
|
|
|
SourceLocation ArgStartLoc = Tok.getLocation();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 11:13:06 +08:00
|
|
|
// 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;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 11:13:06 +08:00
|
|
|
while (1) {
|
|
|
|
// Read arguments as unexpanded tokens. This avoids issues, e.g., where
|
|
|
|
// an argument value in a macro could expand to ',' or '(' or ')'.
|
|
|
|
LexUnexpandedToken(Tok);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 11:13:06 +08:00
|
|
|
if (Tok.is(tok::eof) || Tok.is(tok::eom)) { // "#if f(<eof>" & "#if f(\n"
|
|
|
|
Diag(MacroName, diag::err_unterm_macro_invoc);
|
|
|
|
// Do not lose the EOF/EOM. Return it to the client.
|
|
|
|
MacroName = Tok;
|
|
|
|
return 0;
|
|
|
|
} else if (Tok.is(tok::r_paren)) {
|
|
|
|
// If we found the ) token, the macro arg list is done.
|
2009-02-16 04:52:18 +08:00
|
|
|
if (NumParens-- == 0) {
|
|
|
|
MacroEnd = Tok.getLocation();
|
2008-03-09 11:13:06 +08:00
|
|
|
break;
|
2009-02-16 04:52:18 +08:00
|
|
|
}
|
2008-03-09 11:13:06 +08:00
|
|
|
} else if (Tok.is(tok::l_paren)) {
|
|
|
|
++NumParens;
|
|
|
|
} else if (Tok.is(tok::comma) && NumParens == 0) {
|
|
|
|
// Comma ends this argument if there are more fixed arguments expected.
|
2009-04-18 09:13:56 +08:00
|
|
|
// However, if this is a variadic macro, and this is part of the
|
2009-09-09 23:08:12 +08:00
|
|
|
// variadic part, then the comma is just an argument token.
|
2009-04-18 09:13:56 +08:00
|
|
|
if (!isVariadic) break;
|
|
|
|
if (NumFixedArgsLeft > 1)
|
2008-03-09 11:13:06 +08:00
|
|
|
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;
|
2009-04-18 14:44:18 +08:00
|
|
|
} else if (Tok.getIdentifierInfo() != 0) {
|
2008-03-09 11:13:06 +08:00
|
|
|
// 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.
|
2009-09-09 23:08:12 +08:00
|
|
|
// If so, we mark the token as noexpand. This is a subtle aspect of
|
2008-03-09 11:13:06 +08:00
|
|
|
// C99 6.10.3.4p2.
|
|
|
|
if (MacroInfo *MI = getMacroInfo(Tok.getIdentifierInfo()))
|
|
|
|
if (!MI->isEnabled())
|
|
|
|
Tok.setFlag(Token::DisableExpand);
|
|
|
|
}
|
|
|
|
ArgTokens.push_back(Tok);
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-18 09:13:56 +08:00
|
|
|
// 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;
|
2008-03-09 11:13:06 +08:00
|
|
|
|
2009-04-18 09:13:56 +08:00
|
|
|
// If this is not a variadic macro, and too many args were specified, emit
|
|
|
|
// an error.
|
|
|
|
if (!isVariadic && NumFixedArgsLeft == 0) {
|
|
|
|
if (ArgTokens.size() != ArgTokenStart)
|
|
|
|
ArgStartLoc = ArgTokens[ArgTokenStart].getLocation();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-18 09:13:56 +08:00
|
|
|
// 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(ArgStartLoc, diag::err_too_many_args_in_macro_invoc);
|
|
|
|
return 0;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 11:13:06 +08:00
|
|
|
// Empty arguments are standard in C99 and supported as an extension in
|
|
|
|
// other modes.
|
2009-04-18 09:13:56 +08:00
|
|
|
if (ArgTokens.size() == ArgTokenStart && !Features.C99)
|
2008-03-09 11:13:06 +08:00
|
|
|
Diag(Tok, diag::ext_empty_fnmacro_arg);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 11:13:06 +08:00
|
|
|
// Add a marker EOF token to the end of the token list for this argument.
|
|
|
|
Token EOFTok;
|
|
|
|
EOFTok.startToken();
|
|
|
|
EOFTok.setKind(tok::eof);
|
2009-01-27 04:24:53 +08:00
|
|
|
EOFTok.setLocation(Tok.getLocation());
|
2008-03-09 11:13:06 +08:00
|
|
|
EOFTok.setLength(0);
|
|
|
|
ArgTokens.push_back(EOFTok);
|
|
|
|
++NumActuals;
|
2009-04-18 09:13:56 +08:00
|
|
|
assert(NumFixedArgsLeft != 0 && "Too many arguments parsed");
|
2008-03-09 11:13:06 +08:00
|
|
|
--NumFixedArgsLeft;
|
2009-02-16 04:52:18 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 11:13:06 +08:00
|
|
|
// Okay, we either found the r_paren. Check to see if we parsed too few
|
|
|
|
// arguments.
|
|
|
|
unsigned MinArgsExpected = MI->getNumArgs();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 11:13:06 +08:00
|
|
|
// See MacroArgs instance var for description of this.
|
|
|
|
bool isVarargsElided = false;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 11:13:06 +08:00
|
|
|
if (NumActuals < MinArgsExpected) {
|
|
|
|
// There are several cases where too few arguments is ok, handle them now.
|
2009-04-21 05:08:10 +08:00
|
|
|
if (NumActuals == 0 && MinArgsExpected == 1) {
|
|
|
|
// #define A(X) or #define A(...) ---> A()
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-21 05:08:10 +08:00
|
|
|
// 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 (MI->isVariadic() &&
|
|
|
|
(NumActuals+1 == MinArgsExpected || // A(x, ...) -> A(X)
|
|
|
|
(NumActuals == 0 && MinArgsExpected == 2))) {// A(x,...) -> A()
|
2008-03-09 11:13:06 +08:00
|
|
|
// Varargs where the named vararg parameter is missing: ok as extension.
|
|
|
|
// #define A(x, ...)
|
|
|
|
// A("blah")
|
|
|
|
Diag(Tok, diag::ext_missing_varargs_arg);
|
|
|
|
|
2009-04-18 09:13:56 +08:00
|
|
|
// Remember this occurred, allowing us to elide the comma when used for
|
2008-05-08 13:10:33 +08:00
|
|
|
// cases like:
|
2009-09-09 23:08:12 +08:00
|
|
|
// #define A(x, foo...) blah(a, ## foo)
|
|
|
|
// #define B(x, ...) blah(a, ## __VA_ARGS__)
|
|
|
|
// #define C(...) blah(a, ## __VA_ARGS__)
|
2009-04-18 09:13:56 +08:00
|
|
|
// A(x) B(x) C()
|
2009-04-21 05:08:10 +08:00
|
|
|
isVarargsElided = true;
|
2008-03-09 11:13:06 +08:00
|
|
|
} else {
|
|
|
|
// Otherwise, emit the error.
|
|
|
|
Diag(Tok, diag::err_too_few_args_in_macro_invoc);
|
|
|
|
return 0;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 11:13:06 +08:00
|
|
|
// 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);
|
2009-05-13 08:55:26 +08:00
|
|
|
|
|
|
|
// If we expect two arguments, add both as empty.
|
|
|
|
if (NumActuals == 0 && MinArgsExpected == 2)
|
|
|
|
ArgTokens.push_back(Tok);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-18 09:13:56 +08:00
|
|
|
} else if (NumActuals > MinArgsExpected && !MI->isVariadic()) {
|
|
|
|
// 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);
|
|
|
|
return 0;
|
2008-03-09 11:13:06 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-05-21 17:52:38 +08:00
|
|
|
return MacroArgs::create(MI, ArgTokens.data(), ArgTokens.size(),
|
|
|
|
isVarargsElided);
|
2008-03-09 11:13:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// 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(0);
|
|
|
|
struct tm *TM = localtime(&TT);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 11:13:06 +08:00
|
|
|
static const char * const Months[] = {
|
|
|
|
"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"
|
|
|
|
};
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 11:13:06 +08:00
|
|
|
char TmpBuffer[100];
|
2009-09-09 23:08:12 +08:00
|
|
|
sprintf(TmpBuffer, "\"%s %2d %4d\"", Months[TM->tm_mon], TM->tm_mday,
|
2008-03-09 11:13:06 +08:00
|
|
|
TM->tm_year+1900);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-01-27 03:29:26 +08:00
|
|
|
Token TmpTok;
|
|
|
|
TmpTok.startToken();
|
|
|
|
PP.CreateString(TmpBuffer, strlen(TmpBuffer), TmpTok);
|
|
|
|
DATELoc = TmpTok.getLocation();
|
2008-03-09 11:13:06 +08:00
|
|
|
|
|
|
|
sprintf(TmpBuffer, "\"%02d:%02d:%02d\"", TM->tm_hour, TM->tm_min, TM->tm_sec);
|
2009-01-27 03:29:26 +08:00
|
|
|
PP.CreateString(TmpBuffer, strlen(TmpBuffer), TmpTok);
|
|
|
|
TIMELoc = TmpTok.getLocation();
|
2008-03-09 11:13:06 +08:00
|
|
|
}
|
|
|
|
|
2009-06-13 15:13:28 +08:00
|
|
|
|
|
|
|
/// HasFeature - Return true if we recognize and implement the specified feature
|
|
|
|
/// specified by the identifier.
|
|
|
|
static bool HasFeature(const Preprocessor &PP, const IdentifierInfo *II) {
|
|
|
|
const LangOptions &LangOpts = PP.getLangOptions();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-06-13 15:13:28 +08:00
|
|
|
switch (II->getLength()) {
|
|
|
|
default: return false;
|
|
|
|
case 6:
|
|
|
|
if (II->isStr("blocks")) return LangOpts.Blocks;
|
|
|
|
return false;
|
2009-09-01 00:41:57 +08:00
|
|
|
case 19:
|
|
|
|
if (II->isStr("objc_nonfragile_abi")) return LangOpts.ObjCNonFragileABI;
|
|
|
|
return false;
|
2009-06-13 15:13:28 +08:00
|
|
|
case 22:
|
|
|
|
if (II->isStr("attribute_overloadable")) return true;
|
|
|
|
return false;
|
|
|
|
case 25:
|
|
|
|
if (II->isStr("attribute_ext_vector_type")) return true;
|
|
|
|
return false;
|
|
|
|
case 27:
|
|
|
|
if (II->isStr("attribute_analyzer_noreturn")) return true;
|
|
|
|
return false;
|
|
|
|
case 29:
|
|
|
|
if (II->isStr("attribute_ns_returns_retained")) return true;
|
|
|
|
if (II->isStr("attribute_cf_returns_retained")) return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-03-09 11:13:06 +08:00
|
|
|
/// 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!");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 11:13:06 +08:00
|
|
|
// If this is an _Pragma directive, expand it, invoke the pragma handler, then
|
|
|
|
// lex the token after it.
|
|
|
|
if (II == Ident_Pragma)
|
|
|
|
return Handle_Pragma(Tok);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 11:13:06 +08:00
|
|
|
++NumBuiltinMacroExpanded;
|
|
|
|
|
|
|
|
char TmpBuffer[100];
|
|
|
|
|
|
|
|
// Set up the return result.
|
|
|
|
Tok.setIdentifierInfo(0);
|
|
|
|
Tok.clearFlag(Token::NeedsCleaning);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 11:13:06 +08:00
|
|
|
if (II == Ident__LINE__) {
|
2009-01-27 15:57:44 +08:00
|
|
|
// 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.
|
2009-02-16 05:06:39 +08:00
|
|
|
SourceLocation Loc = Tok.getLocation();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-19 06:29:33 +08:00
|
|
|
// 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);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-16 05:06:39 +08:00
|
|
|
// One wrinkle here is that GCC expands __LINE__ to location of the *end* of
|
|
|
|
// a macro instantiation. This doesn't matter for object-like macros, but
|
|
|
|
// can matter for a function-like macro that expands to contain __LINE__.
|
|
|
|
// Skip down through instantiation points until we find a file loc for the
|
|
|
|
// end of the instantiation history.
|
2009-02-16 05:26:50 +08:00
|
|
|
Loc = SourceMgr.getInstantiationRange(Loc).second;
|
2009-02-16 05:06:39 +08:00
|
|
|
PresumedLoc PLoc = SourceMgr.getPresumedLoc(Loc);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-08 16:08:45 +08:00
|
|
|
// __LINE__ expands to a simple numeric value.
|
|
|
|
sprintf(TmpBuffer, "%u", PLoc.getLine());
|
2008-03-09 11:13:06 +08:00
|
|
|
Tok.setKind(tok::numeric_constant);
|
2009-03-08 16:08:45 +08:00
|
|
|
CreateString(TmpBuffer, strlen(TmpBuffer), Tok, Tok.getLocation());
|
2008-03-09 11:13:06 +08:00
|
|
|
} else if (II == Ident__FILE__ || II == Ident__BASE_FILE__) {
|
2009-01-27 15:57:44 +08:00
|
|
|
// 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.
|
2008-03-09 11:13:06 +08:00
|
|
|
if (II == Ident__BASE_FILE__) {
|
|
|
|
Diag(Tok, diag::ext_pp_base_file);
|
2009-01-27 15:57:44 +08:00
|
|
|
SourceLocation NextLoc = PLoc.getIncludeLoc();
|
2008-03-09 11:13:06 +08:00
|
|
|
while (NextLoc.isValid()) {
|
2009-01-27 15:57:44 +08:00
|
|
|
PLoc = SourceMgr.getPresumedLoc(NextLoc);
|
|
|
|
NextLoc = PLoc.getIncludeLoc();
|
2008-03-09 11:13:06 +08:00
|
|
|
}
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 11:13:06 +08:00
|
|
|
// Escape this filename. Turn '\' -> '\\' '"' -> '\"'
|
2009-01-27 15:57:44 +08:00
|
|
|
std::string FN = PLoc.getFilename();
|
2008-03-09 11:13:06 +08:00
|
|
|
FN = '"' + Lexer::Stringify(FN) + '"';
|
|
|
|
Tok.setKind(tok::string_literal);
|
2009-01-27 03:29:26 +08:00
|
|
|
CreateString(&FN[0], FN.size(), Tok, Tok.getLocation());
|
2008-03-09 11:13:06 +08:00
|
|
|
} else if (II == Ident__DATE__) {
|
|
|
|
if (!DATELoc.isValid())
|
|
|
|
ComputeDATE_TIME(DATELoc, TIMELoc, *this);
|
|
|
|
Tok.setKind(tok::string_literal);
|
|
|
|
Tok.setLength(strlen("\"Mmm dd yyyy\""));
|
2009-01-26 08:43:02 +08:00
|
|
|
Tok.setLocation(SourceMgr.createInstantiationLoc(DATELoc, Tok.getLocation(),
|
2009-02-16 04:52:18 +08:00
|
|
|
Tok.getLocation(),
|
2009-01-26 08:43:02 +08:00
|
|
|
Tok.getLength()));
|
2008-03-09 11:13:06 +08:00
|
|
|
} else if (II == Ident__TIME__) {
|
|
|
|
if (!TIMELoc.isValid())
|
|
|
|
ComputeDATE_TIME(DATELoc, TIMELoc, *this);
|
|
|
|
Tok.setKind(tok::string_literal);
|
|
|
|
Tok.setLength(strlen("\"hh:mm:ss\""));
|
2009-01-26 08:43:02 +08:00
|
|
|
Tok.setLocation(SourceMgr.createInstantiationLoc(TIMELoc, Tok.getLocation(),
|
2009-02-16 04:52:18 +08:00
|
|
|
Tok.getLocation(),
|
2009-01-26 08:43:02 +08:00
|
|
|
Tok.getLength()));
|
2008-03-09 11:13:06 +08:00
|
|
|
} else if (II == Ident__INCLUDE_LEVEL__) {
|
|
|
|
Diag(Tok, diag::ext_pp_include_level);
|
|
|
|
|
2009-01-27 15:57:44 +08:00
|
|
|
// Compute the presumed include depth of this token. This can be affected
|
|
|
|
// by GNU line markers.
|
2008-03-09 11:13:06 +08:00
|
|
|
unsigned Depth = 0;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-01-27 15:57:44 +08:00
|
|
|
PresumedLoc PLoc = SourceMgr.getPresumedLoc(Tok.getLocation());
|
|
|
|
PLoc = SourceMgr.getPresumedLoc(PLoc.getIncludeLoc());
|
|
|
|
for (; PLoc.isValid(); ++Depth)
|
|
|
|
PLoc = SourceMgr.getPresumedLoc(PLoc.getIncludeLoc());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-08 16:08:45 +08:00
|
|
|
// __INCLUDE_LEVEL__ expands to a simple numeric value.
|
|
|
|
sprintf(TmpBuffer, "%u", Depth);
|
2008-03-09 11:13:06 +08:00
|
|
|
Tok.setKind(tok::numeric_constant);
|
2009-03-08 16:08:45 +08:00
|
|
|
CreateString(TmpBuffer, strlen(TmpBuffer), Tok, Tok.getLocation());
|
2008-03-09 11:13:06 +08:00
|
|
|
} else if (II == Ident__TIMESTAMP__) {
|
|
|
|
// 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.
|
|
|
|
Diag(Tok, diag::ext_pp_timestamp);
|
|
|
|
|
|
|
|
// 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 = 0;
|
2008-11-20 09:35:24 +08:00
|
|
|
PreprocessorLexer *TheLexer = getCurrentFileLexer();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 11:13:06 +08:00
|
|
|
if (TheLexer)
|
2008-11-20 06:55:25 +08:00
|
|
|
CurFile = SourceMgr.getFileEntryForID(TheLexer->getFileID());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 11:13:06 +08:00
|
|
|
// If this file is older than the file it depends on, emit a diagnostic.
|
|
|
|
const char *Result;
|
|
|
|
if (CurFile) {
|
|
|
|
time_t TT = CurFile->getModificationTime();
|
|
|
|
struct tm *TM = localtime(&TT);
|
|
|
|
Result = asctime(TM);
|
|
|
|
} else {
|
|
|
|
Result = "??? ??? ?? ??:??:?? ????\n";
|
|
|
|
}
|
|
|
|
TmpBuffer[0] = '"';
|
2009-09-16 21:10:04 +08:00
|
|
|
unsigned Len = strlen(Result);
|
|
|
|
memcpy(TmpBuffer+1, Result, Len-1); // Copy string without the newline.
|
|
|
|
TmpBuffer[Len] = '"';
|
2008-03-09 11:13:06 +08:00
|
|
|
Tok.setKind(tok::string_literal);
|
2009-01-27 03:29:26 +08:00
|
|
|
CreateString(TmpBuffer, Len+1, Tok, Tok.getLocation());
|
2009-04-13 09:29:17 +08:00
|
|
|
} else if (II == Ident__COUNTER__) {
|
|
|
|
Diag(Tok, diag::ext_pp_counter);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-13 09:29:17 +08:00
|
|
|
// __COUNTER__ expands to a simple numeric value.
|
|
|
|
sprintf(TmpBuffer, "%u", CounterValue++);
|
|
|
|
Tok.setKind(tok::numeric_constant);
|
|
|
|
CreateString(TmpBuffer, strlen(TmpBuffer), Tok, Tok.getLocation());
|
2009-06-13 15:13:28 +08:00
|
|
|
} else if (II == Ident__has_feature ||
|
|
|
|
II == Ident__has_builtin) {
|
|
|
|
// The argument to these two builtins should be a parenthesized identifier.
|
|
|
|
SourceLocation StartLoc = Tok.getLocation();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-06-13 15:13:28 +08:00
|
|
|
bool IsValid = false;
|
|
|
|
IdentifierInfo *FeatureII = 0;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-06-13 15:13:28 +08:00
|
|
|
// Read the '('.
|
|
|
|
Lex(Tok);
|
|
|
|
if (Tok.is(tok::l_paren)) {
|
|
|
|
// Read the identifier
|
|
|
|
Lex(Tok);
|
|
|
|
if (Tok.is(tok::identifier)) {
|
|
|
|
FeatureII = Tok.getIdentifierInfo();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-06-13 15:13:28 +08:00
|
|
|
// Read the ')'.
|
|
|
|
Lex(Tok);
|
|
|
|
if (Tok.is(tok::r_paren))
|
|
|
|
IsValid = true;
|
|
|
|
}
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-06-13 15:13:28 +08:00
|
|
|
bool Value = false;
|
|
|
|
if (!IsValid)
|
|
|
|
Diag(StartLoc, diag::err_feature_check_malformed);
|
|
|
|
else if (II == Ident__has_builtin) {
|
2009-09-09 23:08:12 +08:00
|
|
|
// Check for a builtin is trivial.
|
2009-06-13 15:13:28 +08:00
|
|
|
Value = FeatureII->getBuiltinID() != 0;
|
|
|
|
} else {
|
|
|
|
assert(II == Ident__has_feature && "Must be feature check");
|
|
|
|
Value = HasFeature(*this, FeatureII);
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-06-13 15:13:28 +08:00
|
|
|
sprintf(TmpBuffer, "%d", (int)Value);
|
|
|
|
Tok.setKind(tok::numeric_constant);
|
|
|
|
CreateString(TmpBuffer, strlen(TmpBuffer), Tok, Tok.getLocation());
|
2008-03-09 11:13:06 +08:00
|
|
|
} else {
|
|
|
|
assert(0 && "Unknown identifier!");
|
|
|
|
}
|
|
|
|
}
|