r176526 - Preprocessor: don't consider // to be a line comment in -E -std=c89 mode.
Jordan Rose
jordan_rose at apple.com
Tue Mar 5 14:51:05 PST 2013
Author: jrose
Date: Tue Mar 5 16:51:04 2013
New Revision: 176526
URL: http://llvm.org/viewvc/llvm-project?rev=176526&view=rev
Log:
Preprocessor: don't consider // to be a line comment in -E -std=c89 mode.
It's beneficial when compiling to treat // as the start of a line
comment even in -std=c89 mode, since it's not valid C code (with a few
rare exceptions) and is usually intended as such. We emit a pedantic
warning and then continue on as if line comments were enabled.
This has been our behavior for quite some time.
However, people use the preprocessor for things besides C source files.
In today's prompting example, the input contains (unquoted) URLs, which
contain // but should still be preserved.
This change instructs the lexer to treat // as a plain token if Clang is
in C90 mode and generating preprocessed output rather than actually compiling.
<rdar://problem/13338743>
Modified:
cfe/trunk/lib/Lex/Lexer.cpp
cfe/trunk/test/Preprocessor/c90.c
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=176526&r1=176525&r2=176526&view=diff
==============================================================================
--- cfe/trunk/lib/Lex/Lexer.cpp (original)
+++ cfe/trunk/lib/Lex/Lexer.cpp Tue Mar 5 16:51:04 2013
@@ -3121,10 +3121,13 @@ LexNextToken:
// 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 in -traditional-cpp mode.
- if ((LangOpts.LineComment ||
- getCharAndSize(CurPtr+SizeTmp, SizeTmp2) != '*') &&
- !LangOpts.TraditionalCPP) {
+ // However, we never do this if we are just preprocessing.
+ bool TreatAsComment = LangOpts.LineComment && !LangOpts.TraditionalCPP;
+ if (!TreatAsComment)
+ if (!(PP && PP->isPreprocessedOutput()))
+ TreatAsComment = getCharAndSize(CurPtr+SizeTmp, SizeTmp2) != '*';
+
+ if (TreatAsComment) {
if (SkipLineComment(Result, ConsumeChar(CurPtr, SizeTmp, Result)))
return; // There is a token to return.
Modified: cfe/trunk/test/Preprocessor/c90.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Preprocessor/c90.c?rev=176526&r1=176525&r2=176526&view=diff
==============================================================================
--- cfe/trunk/test/Preprocessor/c90.c (original)
+++ cfe/trunk/test/Preprocessor/c90.c Tue Mar 5 16:51:04 2013
@@ -1,4 +1,5 @@
/* RUN: %clang_cc1 %s -std=c89 -Eonly -verify -pedantic-errors
+ * RUN: %clang_cc1 %s -std=c89 -E | FileCheck %s
*/
/* PR3919 */
@@ -8,3 +9,7 @@
#define foo3$bar /* expected-error {{'$' in identifier}} */
+/* CHECK-NOT: this comment should be missing
+ * CHECK: {{^}}// this comment should be present{{$}}
+ */
+// this comment should be present
Modified: cfe/trunk/test/Preprocessor/traditional-cpp.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Preprocessor/traditional-cpp.c?rev=176526&r1=176525&r2=176526&view=diff
==============================================================================
--- cfe/trunk/test/Preprocessor/traditional-cpp.c (original)
+++ cfe/trunk/test/Preprocessor/traditional-cpp.c Tue Mar 5 16:51:04 2013
@@ -78,3 +78,7 @@ a b c in skipped block
/* CHECK-NOT: {{^}}a b c in skipped block{{$}}
* CHECK-NOT: {{^}}/* Comments are whitespace too
*/
+
+Preserve URLs: http://clang.llvm.org
+/* CHECK: {{^}}Preserve URLs: http://clang.llvm.org{{$}}
+ */
More information about the cfe-commits
mailing list