https://bugs.llvm.org/show_bug.cgi?id=48079
Bug ID: 48079
Summary: switch implicit block should not be there in -std=c89
Product: clang
Version: 11.0
Hardware: PC
OS: All
Status: NEW
Severity: normal
Priority: P
Component: C
Assignee: unassignedclangb...@nondot.org
Reporter: c...@trust-in-soft.com
CC: blitzrak...@gmail.com, dgre...@apple.com,
erik.pilking...@gmail.com, llvm-bugs@lists.llvm.org,
richard-l...@metafoo.co.uk
Consider the following:
int X;
int main(void) {
switch (sizeof(struct s { char a;}))
case 1: X = sizeof (struct s { float b;});
return 0;
}
This is a correct C99 program, because in C99 and later standards, the switch
implicitly defines a block for its substatement (clause 6.8.4:3 in C17):
A selection statement is a block whose scope is a strict subset of the scope of
its enclosing block. Each
associated substatement is also a block whose scope is a strict subset of the
scope of the selection
statement.
No such clause exists in the C89 standard, and accordingly, the text above is
not a valid C89 program, because the struct s is defined twice in the same
scope.
GCC 10.2 correctly accepts the program when invoked with “gcc -std=c99” and
kindly rejects it when invoked with “gcc -std=c89”. Compiler Explorer link:
https://gcc.godbolt.org/z/P6qevb
However, Clang 11.0 accepts the program when invoked with “clang -std=c89“.
Compiler Explorer link: https://gcc.godbolt.org/z/jeKfMs
It is possible that C89 says that this program is UB (the relevant clause
appears to be 3.5.2.3 Tags but I am not an expert at C89 and its
interpretations), in which case Clang's behavior would be acceptable. But
please note that Clang generally aims at kindly rejecting such programs instead
of silently doing something meaningless, as seen when compiling this program,
which is rejected for this very reason:
int X;
int main(void) {
switch (sizeof(struct s { char a;}))
case 1:;
X = sizeof (struct s { float b;});
return 0;
}
Compiler Explorer link: https://gcc.godbolt.org/z/9eqds9
It is therefore still possible to consider this Clang behavior when compiling
C89 code as a Quality of Implementation issue.
--
You are receiving this mail because:
You are on the CC list for the bug.
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs