[cfe-commits] r136748 - in /cfe/trunk: include/clang/Lex/Preprocessor.h lib/Lex/PPExpressions.cpp test/Preprocessor/comment_save_if.c test/Preprocessor/expr_define_expansion.c

Eli Friedman eli.friedman at gmail.com
Tue Aug 2 17:04:13 PDT 2011


Author: efriedma
Date: Tue Aug  2 19:04:13 2011
New Revision: 136748

URL: http://llvm.org/viewvc/llvm-project?rev=136748&view=rev
Log:
A couple fixes for preprocessor expressions:

1. Be more tolerant of comments in -CC (comment-preserving) mode.  We were missing a few cases.

2. Make sure to expand the second FOO in "#if defined FOO FOO".  (See also
r97253, which addressed the case of "#if defined(FOO FOO".)

Fixes PR10286.


Added:
    cfe/trunk/test/Preprocessor/expr_define_expansion.c
Modified:
    cfe/trunk/include/clang/Lex/Preprocessor.h
    cfe/trunk/lib/Lex/PPExpressions.cpp
    cfe/trunk/test/Preprocessor/comment_save_if.c

Modified: cfe/trunk/include/clang/Lex/Preprocessor.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Lex/Preprocessor.h?rev=136748&r1=136747&r2=136748&view=diff
==============================================================================
--- cfe/trunk/include/clang/Lex/Preprocessor.h (original)
+++ cfe/trunk/include/clang/Lex/Preprocessor.h Tue Aug  2 19:04:13 2011
@@ -556,6 +556,14 @@
     DisableMacroExpansion = OldVal;
   }
 
+  /// LexUnexpandedNonComment - Like LexNonComment, but this disables macro
+  /// expansion of identifier tokens.
+  void LexUnexpandedNonComment(Token &Result) {
+    do
+      LexUnexpandedToken(Result);
+    while (Result.getKind() == tok::comment);
+  }
+
   /// LookAhead - This peeks ahead N tokens and returns that token without
   /// consuming any tokens.  LookAhead(0) returns the next token that would be
   /// returned by Lex(), LookAhead(1) returns the token after it, etc.  This

Modified: cfe/trunk/lib/Lex/PPExpressions.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Lex/PPExpressions.cpp?rev=136748&r1=136747&r2=136748&view=diff
==============================================================================
--- cfe/trunk/lib/Lex/PPExpressions.cpp (original)
+++ cfe/trunk/lib/Lex/PPExpressions.cpp Tue Aug  2 19:04:13 2011
@@ -83,20 +83,20 @@
   Result.setBegin(PeekTok.getLocation());
 
   // Get the next token, don't expand it.
-  PP.LexUnexpandedToken(PeekTok);
+  PP.LexUnexpandedNonComment(PeekTok);
 
   // Two options, it can either be a pp-identifier or a (.
   SourceLocation LParenLoc;
   if (PeekTok.is(tok::l_paren)) {
     // Found a paren, remember we saw it and skip it.
     LParenLoc = PeekTok.getLocation();
-    PP.LexUnexpandedToken(PeekTok);
+    PP.LexUnexpandedNonComment(PeekTok);
   }
 
   if (PeekTok.is(tok::code_completion)) {
     if (PP.getCodeCompletionHandler())
       PP.getCodeCompletionHandler()->CodeCompleteMacroName(false);
-    PP.LexUnexpandedToken(PeekTok);
+    PP.LexUnexpandedNonComment(PeekTok);
   }
   
   // If we don't have a pp-identifier now, this is an error.
@@ -115,12 +115,12 @@
     PP.markMacroAsUsed(Macro);
   }
 
-  // Consume identifier.
-  Result.setEnd(PeekTok.getLocation());
-  PP.LexUnexpandedToken(PeekTok);
-
   // If we are in parens, ensure we have a trailing ).
   if (LParenLoc.isValid()) {
+    // Consume identifier.
+    Result.setEnd(PeekTok.getLocation());
+    PP.LexUnexpandedNonComment(PeekTok);
+
     if (PeekTok.isNot(tok::r_paren)) {
       PP.Diag(PeekTok.getLocation(), diag::err_pp_missing_rparen) << "defined";
       PP.Diag(LParenLoc, diag::note_matching) << "(";
@@ -129,6 +129,10 @@
     // Consume the ).
     Result.setEnd(PeekTok.getLocation());
     PP.LexNonComment(PeekTok);
+  } else {
+    // Consume identifier.
+    Result.setEnd(PeekTok.getLocation());
+    PP.LexNonComment(PeekTok);
   }
 
   // Success, remember that we saw defined(X).
@@ -152,7 +156,7 @@
   if (PeekTok.is(tok::code_completion)) {
     if (PP.getCodeCompletionHandler())
       PP.getCodeCompletionHandler()->CodeCompletePreprocessorExpression();
-    PP.LexUnexpandedToken(PeekTok);
+    PP.LexNonComment(PeekTok);
   }
       
   // If this token's spelling is a pp-identifier, check to see if it is
@@ -712,7 +716,7 @@
   
   // Peek ahead one token.
   Token Tok;
-  Lex(Tok);
+  LexNonComment(Tok);
   
   // C99 6.10.1p3 - All expressions are evaluated as intmax_t or uintmax_t.
   unsigned BitWidth = getTargetInfo().getIntMaxTWidth();

Modified: cfe/trunk/test/Preprocessor/comment_save_if.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Preprocessor/comment_save_if.c?rev=136748&r1=136747&r2=136748&view=diff
==============================================================================
--- cfe/trunk/test/Preprocessor/comment_save_if.c (original)
+++ cfe/trunk/test/Preprocessor/comment_save_if.c Tue Aug  2 19:04:13 2011
@@ -1,6 +1,11 @@
-// RUN: %clang_cc1 %s -E -CC -pedantic 2>&1 | grep -v '^/' | not grep warning
+// RUN: %clang_cc1 %s -E -CC -pedantic -verify
 
 #if 1 /*bar */
 
 #endif /*foo*/
 
+#if /*foo*/ defined /*foo*/ FOO /*foo*/
+#if /*foo*/ defined /*foo*/ ( /*foo*/ FOO /*foo*/ ) /*foo*/
+#endif
+#endif
+

Added: cfe/trunk/test/Preprocessor/expr_define_expansion.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Preprocessor/expr_define_expansion.c?rev=136748&view=auto
==============================================================================
--- cfe/trunk/test/Preprocessor/expr_define_expansion.c (added)
+++ cfe/trunk/test/Preprocessor/expr_define_expansion.c Tue Aug  2 19:04:13 2011
@@ -0,0 +1,5 @@
+// RUN: %clang_cc1 %s -E -CC -pedantic -verify
+
+#define FOO && 1
+#if defined FOO FOO
+#endif





More information about the cfe-commits mailing list