summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChris Lattner <sabre@nondot.org>2006-07-30 08:40:43 +0000
committerChris Lattner <sabre@nondot.org>2006-07-30 08:40:43 +0000
commit95a06b34f79e7c27e25b20123cadfe6081038373 (patch)
tree9310c1a47ea52cf53d1c152703cf2a25e9262ffe
parentb10969b9b2cc6f98de74352d482fb19b63deda76 (diff)
downloadbcm5719-llvm-95a06b34f79e7c27e25b20123cadfe6081038373.tar.gz
bcm5719-llvm-95a06b34f79e7c27e25b20123cadfe6081038373.zip
Simplify implementation of varargs macros by adding the __VA_ARGS__ token
to the formal argument list of a C99 varargs macro. llvm-svn: 38800
-rw-r--r--clang/Lex/MacroExpander.cpp35
-rw-r--r--clang/Lex/Preprocessor.cpp22
-rw-r--r--clang/include/clang/Lex/MacroInfo.h3
-rw-r--r--clang/include/clang/Lex/Preprocessor.h6
4 files changed, 17 insertions, 49 deletions
diff --git a/clang/Lex/MacroExpander.cpp b/clang/Lex/MacroExpander.cpp
index cdf148ca648..423eb9ba360 100644
--- a/clang/Lex/MacroExpander.cpp
+++ b/clang/Lex/MacroExpander.cpp
@@ -246,7 +246,7 @@ MacroExpander::MacroExpander(LexerToken &Tok, MacroArgs *Actuals,
// If this is a function-like macro, expand the arguments and change
// MacroTokens to point to the expanded tokens.
- if (Macro->isFunctionLike() && (Macro->getNumArgs() || Macro->isC99Varargs()))
+ if (Macro->isFunctionLike() && Macro->getNumArgs())
ExpandFunctionArguments();
// Mark the macro as currently disabled, so that it is not recursively
@@ -288,8 +288,6 @@ MacroExpander::~MacroExpander() {
void MacroExpander::ExpandFunctionArguments() {
SmallVector<LexerToken, 128> ResultToks;
- IdentifierInfo *VAARGSii = PP.get__VA_ARGS__Identifier();
-
// Loop through the MacroTokens tokens, expanding them into ResultToks. Keep
// track of whether we change anything. If not, no need to keep them. If so,
// we install the newly expanded sequence as MacroTokens.
@@ -307,14 +305,8 @@ void MacroExpander::ExpandFunctionArguments() {
const LexerToken &CurTok = MacroTokens[i];
if (CurTok.getKind() == tok::hash || CurTok.getKind() == tok::hashat) {
int ArgNo = Macro->getArgumentNum(MacroTokens[i+1].getIdentifierInfo());
- if (ArgNo == -1) {
- // Otherwise, this must be #__VA_ARGS__.
- assert(MacroTokens[i+1].getIdentifierInfo() ==
- PP.get__VA_ARGS__Identifier() &&
- "Token following # is not an argument?");
- ArgNo = Macro->getNumArgs();
- }
-
+ assert(ArgNo != -1 && "Token following # is not an argument?");
+
LexerToken Res;
if (CurTok.getKind() == tok::hash) // Stringify
Res = ActualArgs->getStringifiedArgument(ArgNo, PP);
@@ -340,19 +332,14 @@ void MacroExpander::ExpandFunctionArguments() {
IdentifierInfo *II = CurTok.getIdentifierInfo();
int ArgNo = II ? Macro->getArgumentNum(II) : -1;
if (ArgNo == -1) {
- if (II != VAARGSii || !Macro->isC99Varargs()) {
- // This isn't an argument and isn't __VA_ARGS__. Just add it.
- ResultToks.push_back(CurTok);
-
- if (NextTokGetsSpace) {
- ResultToks.back().SetFlag(LexerToken::LeadingSpace);
- NextTokGetsSpace = false;
- }
- continue;
+ // This isn't an argument, just add it.
+ ResultToks.push_back(CurTok);
+
+ if (NextTokGetsSpace) {
+ ResultToks.back().SetFlag(LexerToken::LeadingSpace);
+ NextTokGetsSpace = false;
}
-
- // Otherwise, this *is* __VA_ARGS__. Set ArgNo to the last argument.
- ArgNo = Macro->getNumArgs();
+ continue;
}
// An argument is expanded somehow, the result is different than the
@@ -438,7 +425,7 @@ void MacroExpander::ExpandFunctionArguments() {
// If this is the __VA_ARGS__ token, and if the argument wasn't provided,
// and if the macro had at least one real argument, and if the token before
// the ## was a comma, remove the comma.
- if ((unsigned)ArgNo == Macro->getNumArgs() && // is __VA_ARGS__
+ if ((unsigned)ArgNo == Macro->getNumArgs()-1 && // is __VA_ARGS__
ActualArgs->isVarargsElidedUse() && // Argument elided.
!ResultToks.empty() && ResultToks.back().getKind() == tok::comma) {
// Never add a space, even if the comma, ##, or arg had a space.
diff --git a/clang/Lex/Preprocessor.cpp b/clang/Lex/Preprocessor.cpp
index 5dc2c925a9a..b3eb07e7172 100644
--- a/clang/Lex/Preprocessor.cpp
+++ b/clang/Lex/Preprocessor.cpp
@@ -13,7 +13,6 @@
//
// Options to support:
// -H - Print the name of each header file used.
-// -C -CC - Do not discard comments for cpp.
// -d[MDNI] - Dump various things.
// -fworking-directory - #line's with preprocessor's working dir.
// -fpreprocessed
@@ -543,10 +542,6 @@ static bool isTrivialSingleTokenExpansion(const MacroInfo *MI,
if (*I == II)
return false; // Identifier is a macro argument.
- // If the argument is the __VA_ARGS__ token, we can't fast expand it.
- if (!strcmp(II->getName(), "__VA_ARGS__"))
- return false; // Identifier is macro argument.
-
return true;
}
@@ -711,11 +706,6 @@ MacroArgs *Preprocessor::ReadFunctionLikeMacroArgs(LexerToken &MacroName,
unsigned NumFixedArgsLeft = MI->getNumArgs();
bool isVariadic = MI->isVariadic();
- // If this is a C99-style varargs macro invocation, add an extra expected
- // argument, which will catch all of the vararg args in one argument.
- if (MI->isC99Varargs())
- ++NumFixedArgsLeft;
-
// Outer loop, while there are more arguments, keep reading them.
LexerToken Tok;
Tok.SetKind(tok::comma);
@@ -789,11 +779,6 @@ MacroArgs *Preprocessor::ReadFunctionLikeMacroArgs(LexerToken &MacroName,
// arguments.
unsigned MinArgsExpected = MI->getNumArgs();
- // C99 expects us to pass at least one vararg arg (but as an extension, we
- // don't require this). GNU-style varargs already include the 'rest' name in
- // the count.
- MinArgsExpected += MI->isC99Varargs();
-
// See MacroArgs instance var for description of this.
bool isVarargsElided = false;
@@ -807,7 +792,7 @@ MacroArgs *Preprocessor::ReadFunctionLikeMacroArgs(LexerToken &MacroName,
// Remember this occurred if this is a C99 macro invocation with at least
// one actual argument.
- isVarargsElided = (MI->isC99Varargs() && MI->getNumArgs());
+ isVarargsElided = MI->isC99Varargs() && MI->getNumArgs() > 1;
} else if (MI->getNumArgs() == 1) {
// #define A(x)
// A()
@@ -1654,6 +1639,8 @@ bool Preprocessor::ReadMacroDefinitionArgList(MacroInfo *MI) {
Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
return true;
}
+ // Add the __VA_ARGS__ identifier as an argument.
+ MI->addArgument(Ident__VA_ARGS__);
MI->setIsC99Varargs();
return false;
case tok::eom: // #define X(
@@ -1786,8 +1773,7 @@ void Preprocessor::HandleDefineDirective(LexerToken &DefineTok) {
// Not a macro arg identifier?
if (!Tok.getIdentifierInfo() ||
- (MI->getArgumentNum(Tok.getIdentifierInfo()) == -1 &&
- Tok.getIdentifierInfo() != Ident__VA_ARGS__)) {
+ MI->getArgumentNum(Tok.getIdentifierInfo()) == -1) {
Diag(Tok, diag::err_pp_stringize_not_parameter);
delete MI;
diff --git a/clang/include/clang/Lex/MacroInfo.h b/clang/include/clang/Lex/MacroInfo.h
index c12e064abbb..68067b6d7da 100644
--- a/clang/include/clang/Lex/MacroInfo.h
+++ b/clang/include/clang/Lex/MacroInfo.h
@@ -31,7 +31,8 @@ class MacroInfo {
SourceLocation Location;
/// Arguments - The list of arguments for a function-like macro. This can be
- /// empty, for, e.g. "#define X()".
+ /// empty, for, e.g. "#define X()". In a C99-style variadic macro, this
+ /// includes the __VA_ARGS__ identifier on the list.
std::vector<IdentifierInfo*> Arguments;
/// ReplacementTokens - This is the list of tokens that the macro is defined
diff --git a/clang/include/clang/Lex/Preprocessor.h b/clang/include/clang/Lex/Preprocessor.h
index 2d8e74bd1cb..0ebd9c41c1b 100644
--- a/clang/include/clang/Lex/Preprocessor.h
+++ b/clang/include/clang/Lex/Preprocessor.h
@@ -387,12 +387,6 @@ public:
SourceLocation CreateString(const char *Buf, unsigned Len,
SourceLocation SourceLoc = SourceLocation());
- /// get__VA_ARGS__Identifier - Return the identifier info for the __VA_ARGS__
- /// identifier.
- IdentifierInfo *get__VA_ARGS__Identifier() const {
- return Ident__VA_ARGS__;
- }
-
/// DumpToken - Print the token to stderr, used for debugging.
///
void DumpToken(const LexerToken &Tok, bool DumpFlags = false) const;
OpenPOWER on IntegriCloud