[cfe-commits] r39069 - in /cfe/cfe/trunk: Lex/PPExpressions.cpp Lex/Preprocessor.cpp include/clang/Lex/Preprocessor.h

sabre at cs.uiuc.edu sabre at cs.uiuc.edu
Wed Jul 11 09:27:17 PDT 2007


Author: sabre
Date: Wed Jul 11 11:27:17 2007
New Revision: 39069

URL: http://llvm.org/viewvc/llvm-project?rev=39069&view=rev
Log:
Implement test/Preprocessor/comment_save_if.c

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

Modified: cfe/cfe/trunk/Lex/PPExpressions.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/Lex/PPExpressions.cpp?rev=39069&r1=39068&r2=39069&view=diff

==============================================================================
--- cfe/cfe/trunk/Lex/PPExpressions.cpp (original)
+++ cfe/cfe/trunk/Lex/PPExpressions.cpp Wed Jul 11 11:27:17 2007
@@ -69,7 +69,7 @@
     // into a simple 0.
     if (strcmp(II->getName(), "defined")) {
       Result = 0;
-      PP.Lex(PeekTok);
+      PP.LexNonComment(PeekTok);
       return false;
     }
 
@@ -116,7 +116,7 @@
     }
 
     // Consume identifier.
-    PP.Lex(PeekTok);
+    PP.LexNonComment(PeekTok);
 
     // If we are in parens, ensure we have a trailing ).
     if (InParens) {
@@ -125,7 +125,7 @@
         return true;
       }
       // Consume the ).
-      PP.Lex(PeekTok);
+      PP.LexNonComment(PeekTok);
     }
     
     // Success, remember that we saw defined(X).
@@ -148,11 +148,11 @@
     std::string Spell = PP.getSpelling(PeekTok);
     // FIXME: COMPUTE integer constants CORRECTLY.
     Result = atoi(Spell.c_str());
-    PP.Lex(PeekTok);
+    PP.LexNonComment(PeekTok);
     return false;
   }
   case tok::l_paren:
-    PP.Lex(PeekTok);  // Eat the (.
+    PP.LexNonComment(PeekTok);  // Eat the (.
     // Parse the value and if there are any binary operators involved, parse
     // them.
     if (EvaluateValue(Result, PeekTok, DT, PP)) return true;
@@ -170,29 +170,29 @@
       }
       DT.State = DefinedTracker::Unknown;
     }
-    PP.Lex(PeekTok);  // Eat the ).
+    PP.LexNonComment(PeekTok);  // Eat the ).
     return false;
  
   case tok::plus:
     // Unary plus doesn't modify the value.
-    PP.Lex(PeekTok);
+    PP.LexNonComment(PeekTok);
     return EvaluateValue(Result, PeekTok, DT, PP);
   case tok::minus:
-    PP.Lex(PeekTok);
+    PP.LexNonComment(PeekTok);
     if (EvaluateValue(Result, PeekTok, DT, PP)) return true;
     Result = -Result;
     DT.State = DefinedTracker::Unknown;
     return false;
     
   case tok::tilde:
-    PP.Lex(PeekTok);
+    PP.LexNonComment(PeekTok);
     if (EvaluateValue(Result, PeekTok, DT, PP)) return true;
     Result = ~Result;
     DT.State = DefinedTracker::Unknown;
     return false;
     
   case tok::exclaim:
-    PP.Lex(PeekTok);
+    PP.LexNonComment(PeekTok);
     if (EvaluateValue(Result, PeekTok, DT, PP)) return true;
     Result = !Result;
     
@@ -275,7 +275,7 @@
 
     // Consume the operator, saving the operator token for error reporting.
     LexerToken OpToken = PeekTok;
-    PP.Lex(PeekTok);
+    PP.LexNonComment(PeekTok);
 
     int RHS;
     // Parse the RHS of the operator.
@@ -348,7 +348,7 @@
         return true;
       }
       // Consume the :.
-      PP.Lex(PeekTok);
+      PP.LexNonComment(PeekTok);
 
       // Evaluate the value after the :.
       int AfterColonVal = 0;

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

==============================================================================
--- cfe/cfe/trunk/Lex/Preprocessor.cpp (original)
+++ cfe/cfe/trunk/Lex/Preprocessor.cpp Wed Jul 11 11:27:17 2007
@@ -1104,6 +1104,9 @@
   Lex(Tmp);
   // There should be no tokens after the directive, but we allow them as an
   // extension.
+  while (Tmp.getKind() == tok::comment)  // Skip comments in -C mode.
+    Lex(Tmp);
+  
   if (Tmp.getKind() != tok::eom) {
     Diag(Tmp, diag::ext_pp_extra_tokens_at_eol, DirType);
     DiscardUntilEndOfDirective();
@@ -1331,9 +1334,14 @@
   if (InMacroArgs)
     Diag(Result, diag::ext_embedded_directive);
   
+TryAgain:
   switch (Result.getKind()) {
   case tok::eom:
     return;   // null directive.
+  case tok::comment:
+    // Handle stuff like "# /*foo*/ define X" in -E -C mode.
+    LexUnexpandedToken(Result);
+    goto TryAgain;
 
   case tok::numeric_constant:
     // FIXME: implement # 7 line numbers!

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=39069&r1=39068&r2=39069&view=diff

==============================================================================
--- cfe/cfe/trunk/include/clang/Lex/Preprocessor.h (original)
+++ cfe/cfe/trunk/include/clang/Lex/Preprocessor.h Wed Jul 11 11:27:17 2007
@@ -222,6 +222,15 @@
       CurMacroExpander->Lex(Result);
   }
   
+  /// LexNonComment - Lex a token.  If it's a comment, keep lexing until we get
+  /// something not a comment.  This is useful in -E -C mode where comments
+  /// would foul up preprocessor directive handling.
+  void LexNonComment(LexerToken &Result) {
+    do
+      Lex(Result);
+    while (Result.getKind() == tok::comment);
+  }
+  
   /// LexUnexpandedToken - This is just like Lex, but this disables macro
   /// expansion of identifier tokens.
   void LexUnexpandedToken(LexerToken &Result) {





More information about the cfe-commits mailing list