Quuxplusone / LLVMBugzillaTest

0 stars 0 forks source link

clang incorrectly parses array declarators in sizeof operand. #20633

Closed Quuxplusone closed 10 years ago

Quuxplusone commented 10 years ago
Bugzilla Link PR20634
Status RESOLVED FIXED
Importance P normal
Reported by Nick Bowler (nbowler@draconx.ca)
Reported on 2014-08-12 12:11:45 -0700
Last modified on 2014-08-12 21:18:33 -0700
Version trunk
Hardware PC Linux
CC llvm-bugs@lists.llvm.org, richard-llvm@metafoo.co.uk
Fixed by commit(s)
Attachments
Blocks
Blocked by
See also
Consider the following C program:

  #include <stdio.h>

  int main(void)
  {
    printf("%zu\n", sizeof (struct { int a; } [2]));
    return 0;
  }

While unusual, there is no problem with this type name from the C
grammar productions (from n1570, but I don't believe these are
changed significantly from earlier revisions):

  type-name
    -> specifier-qualifier-list abstract-declarator
    -> type-specifier abstract-declarator
    -> struct-or-union-specifier abstract-declarator
    -> struct-or-union { struct-declaration-list } abstract-declarator
    -> struct { struct-declaration-list } abstract-declarator
    -> struct { struct-declaration-list } direct-abstract-declarator
    -> struct { struct-declaration-list } [ assignment-expression ]

and so on.

The problem appears to be specific to array abstract declarators used in
this manner, sizeof expressions with more ridiculous type names such as:

  sizeof (struct { int a; } (*[2])(struct { int a; }))

appear to work just fine in clang.  With clang, the program is rejected
with this pile of error messages:

  % clang -O -std=c11 -Wall -pedantic test.c
  test.c:5:46: error: expected ';' after struct
      printf("%zu\n", sizeof (struct { int a; } [2]));
                                               ^
                                               ;
  test.c:5:47: error: expected ')'
      printf("%zu\n", sizeof (struct { int a; } [2]));
                                                ^
  test.c:5:28: note: to match this '('
      printf("%zu\n", sizeof (struct { int a; } [2]));
                             ^
  test.c:5:47: error: expected ')'
      printf("%zu\n", sizeof (struct { int a; } [2]));
                                                ^
  test.c:5:11: note: to match this '('
      printf("%zu\n", sizeof (struct { int a; } [2]));
            ^
  test.c:5:47: error: expected expression
      printf("%zu\n", sizeof (struct { int a; } [2]));
                                                ^
  4 errors generated.

None of the error messages are helpful.  GCC accepts the program and
works correctly.
Quuxplusone commented 10 years ago

Thanks for the report, fixed in r215520.