[llvm-bugs] [Bug 48079] New: switch implicit block should not be there in -std=c89

via llvm-bugs llvm-bugs at lists.llvm.org
Wed Nov 4 16:11:15 PST 2020


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: unassignedclangbugs at nondot.org
          Reporter: cuoq at trust-in-soft.com
                CC: blitzrakete at gmail.com, dgregor at apple.com,
                    erik.pilkington at gmail.com, llvm-bugs at lists.llvm.org,
                    richard-llvm at 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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20201105/3367bd9e/attachment.html>


More information about the llvm-bugs mailing list