[cfe-commits] r62368 - in /cfe/trunk: lib/Lex/Lexer.cpp test/Lexer/c90.c

Chris Lattner sabre at nondot.org
Fri Jan 16 14:39:25 PST 2009


Author: lattner
Date: Fri Jan 16 16:39:25 2009
New Revision: 62368

URL: http://llvm.org/viewvc/llvm-project?rev=62368&view=rev
Log:
Fix PR2477 - clang misparses "//*" in C89 mode


Modified:
    cfe/trunk/lib/Lex/Lexer.cpp
    cfe/trunk/test/Lexer/c90.c

Modified: cfe/trunk/lib/Lex/Lexer.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Lex/Lexer.cpp?rev=62368&r1=62367&r2=62368&view=diff

==============================================================================
--- cfe/trunk/lib/Lex/Lexer.cpp (original)
+++ cfe/trunk/lib/Lex/Lexer.cpp Fri Jan 16 16:39:25 2009
@@ -1300,7 +1300,8 @@
     
     // If the next token is obviously a // or /* */ comment, skip it efficiently
     // too (without going through the big switch stmt).
-    if (CurPtr[0] == '/' && CurPtr[1] == '/' && !inKeepCommentMode()) {
+    if (CurPtr[0] == '/' && CurPtr[1] == '/' && !inKeepCommentMode() &&
+        Features.BCPLComment) {
       SkipBCPLComment(Result, CurPtr+2);
       goto SkipIgnoredUnits;
     } else if (CurPtr[0] == '/' && CurPtr[1] == '*' && !inKeepCommentMode()) {
@@ -1480,18 +1481,32 @@
     // 6.4.9: Comments
     Char = getCharAndSize(CurPtr, SizeTmp);
     if (Char == '/') {         // BCPL comment.
-      if (SkipBCPLComment(Result, ConsumeChar(CurPtr, SizeTmp, Result)))
-        return; // KeepCommentMode
+      // Even if BCPL comments are disabled (e.g. in C89 mode), we generally
+      // want to lex this as a comment.  There is one problem with this though,
+      // that in one particular corner case, this can change the behavior of the
+      // resultant program.  For example, In  "foo //**/ bar", C89 would lex
+      // this as "foo / bar" and langauges with BCPL comments would lex it as
+      // "foo".  Check to see if the character after the second slash is a '*'.
+      // If so, we will lex that as a "/" instead of the start of a comment.
+      if (Features.BCPLComment ||
+          getCharAndSize(CurPtr+SizeTmp, SizeTmp2) != '*') {
+        if (SkipBCPLComment(Result, ConsumeChar(CurPtr, SizeTmp, Result)))
+          return; // KeepCommentMode
       
-      // It is common for the tokens immediately after a // comment to be
-      // whitespace (indentation for the next line).  Instead of going through
-      // the big switch, handle it efficiently now.
-      goto SkipIgnoredUnits;
-    } else if (Char == '*') {  // /**/ comment.
+        // It is common for the tokens immediately after a // comment to be
+        // whitespace (indentation for the next line).  Instead of going through
+        // the big switch, handle it efficiently now.
+        goto SkipIgnoredUnits;
+      }
+    }
+      
+    if (Char == '*') {  // /**/ comment.
       if (SkipBlockComment(Result, ConsumeChar(CurPtr, SizeTmp, Result)))
         return; // KeepCommentMode
       goto LexNextToken;   // GCC isn't tail call eliminating.
-    } else if (Char == '=') {
+    }
+      
+    if (Char == '=') {
       CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
       Kind = tok::slashequal;
     } else {

Modified: cfe/trunk/test/Lexer/c90.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Lexer/c90.c?rev=62368&r1=62367&r2=62368&view=diff

==============================================================================
--- cfe/trunk/test/Lexer/c90.c (original)
+++ cfe/trunk/test/Lexer/c90.c Fri Jan 16 16:39:25 2009
@@ -4,3 +4,10 @@
 enum { cast_hex = (long) (
       0x0p-1   /* expected-error {{hexadecimal floating constants are a C99 feature}} */
      ) };
+
+/* PR2477 */
+int test1(int a,int b) {return a//* This is a divide followed by block comment in c89 mode */
+b;}
+
+// comment accepted as extension    /* expected-error {{// comments are not allowed in this language}}
+





More information about the cfe-commits mailing list