Create a file test.c
, containing the following code:
auto;
Compile it with clang 6.0: clang -c test.c
. It will successfully generate an object file test.o
, albeit one with no actual content (other than the object file headers). It prints a warning, but nonetheless accepts this as valid code:
test.c:1:1: warning: declaration does not declare anything [-Wmissing-declarations]
auto;
^~~~
1 warning generated.
By contrast, gcc 4.9 refuses to compile test.c
, generating an error:
test.c:1:1: error: 'auto' in file-scope empty declaration
auto;
^
Why does clang generate a warning yet accept this translation unit as valid, while gcc generates an error and refuses to compile it? Whose behaviour is more conforming to the C standards? What is the point of allowing an auto declaration which declares nothing?
It is not valid code. But the implementation is free to give it any meaning it wants after issuing a diagnostic message.
5.1.1.3 Diagnostics
1 A conforming implementation shall produce at least one diagnostic message (identified in an implementation-defined manner) if a preprocessing translation unit or translation unit contains a violation of any syntax rule or constraint, even if the behavior is also explicitly specified as undefined or implementation-defined. Diagnostic messages need not be produced in other circumstances.9)
[...]
6.7 Declarations
Constraints
2 A declaration other than astatic_assert
declaration shall declare at least a declarator (other than the parameters of a function or the members of a structure or union), a tag, or the members of an enumeration.
[...]
Quotes from C99+Amendments (C11, n1570)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With