Open morinmorin opened 11 years ago
mentioned in issue llvm/llvm-project#2702
Then, how about disallowing all of them?
In the real world, it's important to be able to handle //-style comments even in C89 mode. Giving a warning on:
f(ptr); // ptr gives you SegFault.
rather than an error seems better, since we know what you meant.
Yep, that's true.
Is there any way to (fully) enable C++ comments in C89 mode?
Not currently, no. Right now, you can choose between -std=gnu89 (which enables line comments and GNU extensions), and -std=c99 (which enables line comments and the rest of C99).
Replacing -std=c89 with -std=gnu89 works for me, thanks! (I used -Wall -Wno-unused-variable -Wno-return-type -Wreturn-type \ -std=c89 -pedantic -Wno-comment -Wno-declaration-after-statement to check source codes submitted by students in C language course.)
Then, how about disallowing all of them?
In the real world, it's important to be able to handle //-style comments even in C89 mode. Giving a warning on:
f(ptr); // ptr gives you SegFault.
rather than an error seems better, since we know what you meant.
Is there any way to (fully) enable C++ comments in C89 mode?
Not currently, no. Right now, you can choose between -std=gnu89 (which enables line comments and GNU extensions), and -std=c99 (which enables line comments and the rest of C99).
If you can provide a compelling reason that we should support c89-plus-line-comments as a language mode, that would be straightforward for us (but we do need a reason -- we don't want to add a feature and support it forever unless it's actually valuable).
Thanks for your reply, Richard.
Then, how about disallowing all of them? The current behavior
This is OK: f(ptr); // ptr gives you SegFault.
This is not OK: f(ptr); //ptr gives you SegFault.
seems a bit fragile.
Though it's not directly relevant to this PR, may I ask a question? Is there any way to (fully) enable C++ comments in C89 mode?
The bug is in our handling of (B), not (C).
int n = 1 // / 1;
is valid C89, and means
int n = 1 / 1;
Extended Description
I compiled three test codes with -std=c89 compiler option.
(A) int main(void) { // return 0; }
(B) int main(void) { // //* return 0; }
(C) int main(void) { //* return 0; }
Code (A) compiles fine with a warning a.c:3:5: warning: // comments are not allowed in this language [-Wcomment]
Code (B) behaves in the same way as (A).
Code (C) fails to compile with the following errors: c:3:5: error: expected expression // ^ c:3:6: error: unterminated / comment //* ^
The behavior of Code (C) is inconsistent with those of Codes (A) and (B). Tested on clang trunk.