llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
28.69k stars 11.87k forks source link

clang allows static VLAs for C99 #39948

Open henrickarlsson opened 5 years ago

henrickarlsson commented 5 years ago
Bugzilla Link 40602
Version trunk
OS Linux
Attachments example code
CC @efriedma-quic,@hfinkel,@zygoloid

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;

With -Wpedantic you get warnings for these (from C++ extension? )

henrickarlsson commented 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.

efriedma-quic commented 5 years ago

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.

efriedma-quic commented 5 years ago

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).