llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
29.41k stars 12.15k forks source link

Inconsistent handling of C++ comments in C89 mode #18415

Open morinmorin opened 11 years ago

morinmorin commented 11 years ago
Bugzilla Link 18041
Version trunk
OS All
CC @zygoloid

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.

efriedma-quic commented 3 years ago

mentioned in issue llvm/llvm-project#2702

morinmorin commented 11 years ago

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.)

ec04fc15-fa35-46f2-80e1-5d271f2ef708 commented 11 years ago

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).

morinmorin commented 11 years ago

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?

ec04fc15-fa35-46f2-80e1-5d271f2ef708 commented 11 years ago

The bug is in our handling of (B), not (C).

int n = 1 // / 1;

is valid C89, and means

int n = 1 / 1;