[cfe-commits] r38800 - in /cfe/cfe/trunk: Lex/MacroExpander.cpp Lex/Preprocessor.cpp include/clang/Lex/MacroInfo.h include/clang/Lex/Preprocessor.h

sabre at cs.uiuc.edu sabre at cs.uiuc.edu
Wed Jul 11 09:24:45 PDT 2007


Author: sabre
Date: Wed Jul 11 11:24:44 2007
New Revision: 38800

URL: http://llvm.org/viewvc/llvm-project?rev=38800&view=rev
Log:
Simplify implementation of varargs macros by adding the __VA_ARGS__ token
to the formal argument list of a C99 varargs macro.

Modified:
    cfe/cfe/trunk/Lex/MacroExpander.cpp
    cfe/cfe/trunk/Lex/Preprocessor.cpp
    cfe/cfe/trunk/include/clang/Lex/MacroInfo.h
    cfe/cfe/trunk/include/clang/Lex/Preprocessor.h

Modified: cfe/cfe/trunk/Lex/MacroExpander.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/Lex/MacroExpander.cpp?rev=38800&r1=38799&r2=38800&view=diff

==============================================================================
--- cfe/cfe/trunk/Lex/MacroExpander.cpp (original)
+++ cfe/cfe/trunk/Lex/MacroExpander.cpp Wed Jul 11 11:24:44 2007
@@ -246,7 +246,7 @@
 
   // 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 @@
 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 @@
     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 @@
     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 @@
     // 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.

Modified: cfe/cfe/trunk/Lex/Preprocessor.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/Lex/Preprocessor.cpp?rev=38800&r1=38799&r2=38800&view=diff

==============================================================================
--- cfe/cfe/trunk/Lex/Preprocessor.cpp (original)
+++ cfe/cfe/trunk/Lex/Preprocessor.cpp Wed Jul 11 11:24:44 2007
@@ -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 @@
     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 @@
   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 @@
   // 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 @@
 
       // 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 @@
         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 @@
    
     // 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;
       

Modified: cfe/cfe/trunk/include/clang/Lex/MacroInfo.h
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/include/clang/Lex/MacroInfo.h?rev=38800&r1=38799&r2=38800&view=diff

==============================================================================
--- cfe/cfe/trunk/include/clang/Lex/MacroInfo.h (original)
+++ cfe/cfe/trunk/include/clang/Lex/MacroInfo.h Wed Jul 11 11:24:44 2007
@@ -31,7 +31,8 @@
   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

Modified: cfe/cfe/trunk/include/clang/Lex/Preprocessor.h
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/include/clang/Lex/Preprocessor.h?rev=38800&r1=38799&r2=38800&view=diff

==============================================================================
--- cfe/cfe/trunk/include/clang/Lex/Preprocessor.h (original)
+++ cfe/cfe/trunk/include/clang/Lex/Preprocessor.h Wed Jul 11 11:24:44 2007
@@ -387,12 +387,6 @@
   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;





More information about the cfe-commits mailing list