(filed as https://llvm.org/bugs/show_bug.cgi?id=23356, sent here for
patch review)

In general clang cannot parse line comments when they are not enabled,
otherwise valid code is seen as invalid. I've attached patch to fix that.

$ cat p.c
#define str_(x) #x
#define str(x) str_(x)
char *s = str(//);
$ clang-3.5 -std=c89 -c p.c
p.c:3:11: error: unterminated function-like macro invocation
char *s = str(//);
          ^
p.c:2:9: note: macro 'str' defined here
#define str(x) str_(x)
        ^
p.c:3:19: error: expected expression
char *s = str(//);
                  ^
p.c:3:10: error: expected ';' after top level declarator
char *s = str(//);
         ^
         ;
3 errors generated.
$ gcc -std=c89 -c p.c
$


-- 
Abramo Bagnara

BUGSENG srl - http://bugseng.com
mailto:[email protected]
Index: lib/Lex/Lexer.cpp
===================================================================
--- lib/Lex/Lexer.cpp	(revisione 224986)
+++ lib/Lex/Lexer.cpp	(copia locale)
@@ -3310,20 +3316,8 @@
     // 6.4.9: Comments
     Char = getCharAndSize(CurPtr, SizeTmp);
     if (Char == '/') {         // Line comment.
-      // Even if Line 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 Line 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.
-      // However, we never do this if we are just preprocessing.
       bool TreatAsComment = LangOpts.LineComment &&
                             (LangOpts.CPlusPlus || !LangOpts.TraditionalCPP);
-      if (!TreatAsComment)
-        if (!(PP && PP->isPreprocessedOutput()))
-          TreatAsComment = getCharAndSize(CurPtr+SizeTmp, SizeTmp2) != '*';
-
       if (TreatAsComment) {
         if (SkipLineComment(Result, ConsumeChar(CurPtr, SizeTmp, Result),
                             TokAtPhysicalStartOfLine))
_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits

Reply via email to