Open henrickarlsson opened 5 years ago
ok I see, so it's by design clang allows more VLA variants than the C99 standard. Maybe this should be documented as a clang LanguageExtension then? (I couldn't find it as a gnuc extension, but maybe it was part of gcc 4.2)
But how about typedef int garr[xx]; void fn0() { typedef int larr[xx]; static garr arr1; static larr arr2; }
This results in: vla_tst.c:9:15: error: variable length array declaration cannot have 'static' storage duration static larr arr2;
So arr1 is ok, but arr2 is not, it feels like both or none should be allowed.
Oh, sorry, you were asking about the behavior with -std=c99, specifically.
With -std=c99, you basically get the bits of the -std=gnu99 behavior that we can support without breaking valid code.
You get a warning with -pedantic because none of these uses are valid C, as you might expect. We implemented this extension for compatibility with an equivalent gcc extension.
It's possible clang's version of the extension is a little more generous than what gcc implements, or that "what gcc implements" has changed since gcc 4.2 (the compiler clang's C support was originally developed against).
Extended Description
It seems like clang is at least not consistent with how VLAs are allowed, and I think clang is a bit too permissive. A VLA should not be allowed as static variable. reproducer command: clang -S -std=c99 vla_tst.c
Code below is the same as in attached file vla_tst.c Consider the following:
const int xx = 1;
gcc gives an error for arrx, and global VLAs sounds strange, but clang accepts this, possibly because xx is known at compiletime? But is not a "constant expression" so should still be a VLA. int arrx[xx];
here clang gives an error for arr2 but not arr1. gcc gives an error for the garr typedef (and arr2). typedef int garr[xx]; void fn0() { typedef int larr[xx]; static garr arr1; static larr arr2; }
clang accepts arr3, but a static storage should not be allowed for a VLA. void fn1() { const int a = 1; static int arr3[a]; }
Here arr4 correctly results in an error with clang. So a little confusing why clang allows some cases but not all. void fn2() { const int a = 1; typedef int at[a]; static at arr4; }
With -Wpedantic you get warnings for these (from C++ extension? )