[cfe-commits] r38737 - in /cfe/cfe/trunk: Lex/MacroExpander.cpp test/Preprocessor/macro_paste_c_block_comment.c

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


Author: sabre
Date: Wed Jul 11 11:24:13 2007
New Revision: 38737

URL: http://llvm.org/viewvc/llvm-project?rev=38737&view=rev
Log:
Avoid testing / ## * in the lexer.  This will cause an unhelpful error message
to be emitted from the lexer.  This fixes macro_paste_c_block_comment.c

Added:
    cfe/cfe/trunk/test/Preprocessor/macro_paste_c_block_comment.c   (with props)
Modified:
    cfe/cfe/trunk/Lex/MacroExpander.cpp

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

==============================================================================
--- cfe/cfe/trunk/Lex/MacroExpander.cpp (original)
+++ cfe/cfe/trunk/Lex/MacroExpander.cpp Wed Jul 11 11:24:13 2007
@@ -426,9 +426,6 @@
   
     bool isInvalid = false;
 
-    // TODO: Avoid // and /*, as the lexer would think it is the start of a
-    // comment and emit warnings that don't make sense.
-    
     // Allocate space for the result token.  This is guaranteed to be enough for
     // the two tokens and a null terminator.
     char *Buffer = (char*)alloca(Tok.getLength() + RHS.getLength() + 1);
@@ -454,36 +451,44 @@
     // Lex the resultant pasted token into Result.
     LexerToken Result;
     
-    // FIXME: Handle common cases: ident+ident, ident+simplenumber here.
-
-    // Make a lexer to lex this string from.
-    SourceManager &SourceMgr = PP.getSourceManager();
-    const char *ResultStrData = SourceMgr.getCharacterData(ResultTokLoc);
-    
-    unsigned FileID = ResultTokLoc.getFileID();
-    assert(FileID && "Could not get FileID for paste?");
-    
-    // Make and enter a lexer object so that we lex and expand the paste result.
-    Lexer *TL = new Lexer(SourceMgr.getBuffer(FileID), FileID, PP,
-                          ResultStrData, 
-                          ResultStrData+LHSLen+RHSLen /*don't include null*/);
-    
-    // Lex a token in raw mode.  This way it won't look up identifiers
-    // automatically, lexing off the end will return an eof token, and warnings
-    // are disabled.  This returns true if the result token is the entire
-    // buffer.
-    bool IsComplete = TL->LexRawToken(Result);
-    
-    // If we got an EOF token, we didn't form even ONE token.  For example, we
-    // did "/ ## /" to get "//".
-    IsComplete &= Result.getKind() != tok::eof;
+    // Avoid testing /*, as the lexer would think it is the start of a comment
+    // and emit an error that it is unterminated.
+    if (Tok.getKind() == tok::slash && RHS.getKind() == tok::star) {
+      isInvalid = true;
+    } else {
+      // FIXME: Handle common cases: ident+ident, ident+simplenumber here.
 
-    // We're now done with the temporary lexer.
-    delete TL;
+      // Make a lexer to lex this string from.
+      SourceManager &SourceMgr = PP.getSourceManager();
+      const char *ResultStrData = SourceMgr.getCharacterData(ResultTokLoc);
+      
+      unsigned FileID = ResultTokLoc.getFileID();
+      assert(FileID && "Could not get FileID for paste?");
+      
+      // Make a lexer object so that we lex and expand the paste result.
+      Lexer *TL = new Lexer(SourceMgr.getBuffer(FileID), FileID, PP,
+                            ResultStrData, 
+                            ResultStrData+LHSLen+RHSLen /*don't include null*/);
+      
+      // Lex a token in raw mode.  This way it won't look up identifiers
+      // automatically, lexing off the end will return an eof token, and
+      // warnings are disabled.  This returns true if the result token is the
+      // entire buffer.
+      bool IsComplete = TL->LexRawToken(Result);
+      
+      // If we got an EOF token, we didn't form even ONE token.  For example, we
+      // did "/ ## /" to get "//".
+      IsComplete &= Result.getKind() != tok::eof;
+      isInvalid = !IsComplete;
+      
+      // We're now done with the temporary lexer.
+      delete TL;
+    }
     
     // If pasting the two tokens didn't form a full new token, this is an error.
-    // This occurs with "x ## +"  and other stuff.
-    if (!IsComplete) {
+    // This occurs with "x ## +"  and other stuff.  Return with Tok unmodified
+    // and with RHS as the next token to lex.
+    if (isInvalid) {
       // If not in assembler language mode.
       PP.Diag(PasteOpLoc, diag::err_pp_bad_paste, 
               std::string(Buffer, Buffer+LHSLen+RHSLen));

Added: cfe/cfe/trunk/test/Preprocessor/macro_paste_c_block_comment.c
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/test/Preprocessor/macro_paste_c_block_comment.c?rev=38737&view=auto

==============================================================================
--- cfe/cfe/trunk/test/Preprocessor/macro_paste_c_block_comment.c (added)
+++ cfe/cfe/trunk/test/Preprocessor/macro_paste_c_block_comment.c Wed Jul 11 11:24:13 2007
@@ -0,0 +1,7 @@
+// RUN: clang %s -Eonly 2>&1 | grep error &&
+// RUN: clang %s -Eonly 2>&1 | not grep unterminated &&
+// RUN: clang %s -Eonly 2>&1 | not grep scratch
+
+#define COMM / ## *
+COMM
+

Propchange: cfe/cfe/trunk/test/Preprocessor/macro_paste_c_block_comment.c

------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cfe/cfe/trunk/test/Preprocessor/macro_paste_c_block_comment.c

------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision





More information about the cfe-commits mailing list