jhjourdan / C11parser

A correct C89/C90/C99/C11/C18 parser written using Menhir and OCaml
Other
192 stars 17 forks source link

function-decls.c uses sizeof on function type (constraint error) #24

Closed michaelforney closed 6 months ago

michaelforney commented 7 months ago

I noticed that function-decls.c contains the following line:

X = sizeof(void (*(int arga, void (*argb)(double Y)))(void* Z));

Unless I'm mistaken, the typename void (*(int arga, void (*argb)(double Y)))(void* Z) specifies a function type with arguments int and pointer to function with argument double returning void, returning pointer to function with argument pointer to void returning void.

This is a function type, but C17 6.5.3.4p1 says (in the constraints section)

The sizeof operator shall not be applied to an expression that has function type or an incomplete type, to the parenthesized name of such a type, or to an expression that designates a bit-field member.

gcc correctly produces a warning when compiling with -Wpedantic:

$ gcc -std=c17 -Wpedantic -c function-decls.c
function-decls.c: In function 'foo':
function-decls.c:5:14: warning: invalid application of 'sizeof' to a function type [-Wpointer-arith]
    5 |   X = sizeof(void (*(int arga, void (*argb)(double Y)))(void* Z));
      |              ^~~~

I know these tests are mainly about syntax, but since the repository states that these are all valid C18 fragments, I think the test should be tweaked to avoid the constraint error.

fpottier commented 6 months ago

Hello! Thanks for this report. To me, it seems preferable to avoid the warning, indeed. I will let Jacques-Henri, who is currently on leave, reply once he comes back.

jhjourdan commented 6 months ago

Thanks, well spotted. This is fixed in 7560d4f.