r189515 - Fix "//" comments with -traditional-cpp in C++.

Eli Friedman eli.friedman at gmail.com
Wed Aug 28 13:53:32 PDT 2013


Author: efriedma
Date: Wed Aug 28 15:53:32 2013
New Revision: 189515

URL: http://llvm.org/viewvc/llvm-project?rev=189515&view=rev
Log:
Fix "//" comments with -traditional-cpp in C++.

Apparently, gcc's -traditional-cpp behaves slightly differently in C++ mode;
specifically, it discards "//" comments.  Match gcc's behavior.

<rdar://problem/14808126>

Modified:
    cfe/trunk/lib/Lex/Lexer.cpp
    cfe/trunk/test/Preprocessor/traditional-cpp.c

Modified: cfe/trunk/lib/Lex/Lexer.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Lex/Lexer.cpp?rev=189515&r1=189514&r2=189515&view=diff
==============================================================================
--- cfe/trunk/lib/Lex/Lexer.cpp (original)
+++ cfe/trunk/lib/Lex/Lexer.cpp Wed Aug 28 15:53:32 2013
@@ -2868,7 +2868,8 @@ LexNextToken:
     // 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() &&
-        LangOpts.LineComment && !LangOpts.TraditionalCPP) {
+        LangOpts.LineComment &&
+        (LangOpts.CPlusPlus || !LangOpts.TraditionalCPP)) {
       if (SkipLineComment(Result, CurPtr+2))
         return; // There is a token to return.
       goto SkipIgnoredUnits;
@@ -3165,7 +3166,8 @@ LexNextToken:
       // "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.TraditionalCPP;
+      bool TreatAsComment = LangOpts.LineComment &&
+                            (LangOpts.CPlusPlus || !LangOpts.TraditionalCPP);
       if (!TreatAsComment)
         if (!(PP && PP->isPreprocessedOutput()))
           TreatAsComment = getCharAndSize(CurPtr+SizeTmp, SizeTmp2) != '*';

Modified: cfe/trunk/test/Preprocessor/traditional-cpp.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Preprocessor/traditional-cpp.c?rev=189515&r1=189514&r2=189515&view=diff
==============================================================================
--- cfe/trunk/test/Preprocessor/traditional-cpp.c (original)
+++ cfe/trunk/test/Preprocessor/traditional-cpp.c Wed Aug 28 15:53:32 2013
@@ -3,9 +3,9 @@
  * things like using /usr/bin/cpp to preprocess non-source files. */
 
 /*
- RUN: %clang_cc1 -traditional-cpp %s -E -o %t
- RUN: FileCheck -strict-whitespace < %t %s
+ RUN: %clang_cc1 -traditional-cpp %s -E | FileCheck -strict-whitespace %s
  RUN: %clang_cc1 -traditional-cpp %s -E -C | FileCheck -check-prefix=CHECK-COMMENTS %s
+ RUN: %clang_cc1 -traditional-cpp -x c++ %s -E | FileCheck -check-prefix=CHECK-CXX %s
 */
 
 /* -traditional-cpp should eliminate all C89 comments. */
@@ -13,7 +13,9 @@
  * CHECK-COMMENTS: {{^}}/* -traditional-cpp should eliminate all C89 comments. *{{/$}}
  */
 
+/* -traditional-cpp should only eliminate "//" comments in C++ mode. */
 /* CHECK: {{^}}foo // bar{{$}}
+ * CHECK-CXX: {{^}}foo {{$}}
  */
 foo // bar
 





More information about the cfe-commits mailing list