]> granicus.if.org Git - clang/commitdiff
Fix "//" comments with -traditional-cpp in C++.
authorEli Friedman <eli.friedman@gmail.com>
Wed, 28 Aug 2013 20:53:32 +0000 (20:53 +0000)
committerEli Friedman <eli.friedman@gmail.com>
Wed, 28 Aug 2013 20:53:32 +0000 (20:53 +0000)
Apparently, gcc's -traditional-cpp behaves slightly differently in C++ mode;
specifically, it discards "//" comments.  Match gcc's behavior.

<rdar://problem/14808126>

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@189515 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Lex/Lexer.cpp
test/Preprocessor/traditional-cpp.c

index e3e01da8416f3f8309f6380c54b76cbb1714d3bb..4b6852ba90bd732de8ff560906be6668d5b5477b 100644 (file)
@@ -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) != '*';
index b8bb99d93cbb96216ccf97827ccbcd71f2a4dd9c..aa9f0f146e4cea0540d1f8a6ef0ed33bdb37b9b7 100644 (file)
@@ -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