From: Eli Friedman Date: Wed, 28 Aug 2013 20:53:32 +0000 (+0000) Subject: Fix "//" comments with -traditional-cpp in C++. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=a601579fa0c87b742797c1249e39cb54ec83083f;p=clang 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. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@189515 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Lex/Lexer.cpp b/lib/Lex/Lexer.cpp index e3e01da841..4b6852ba90 100644 --- a/lib/Lex/Lexer.cpp +++ b/lib/Lex/Lexer.cpp @@ -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) != '*'; diff --git a/test/Preprocessor/traditional-cpp.c b/test/Preprocessor/traditional-cpp.c index b8bb99d93c..aa9f0f146e 100644 --- a/test/Preprocessor/traditional-cpp.c +++ b/test/Preprocessor/traditional-cpp.c @@ -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