llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

Array may have zero size

Open KarstenH opened this issue 9 years ago • 7 comments

Bugzilla Link 32389
Version trunk
OS Windows XP
CC @DougGregor

Extended Description

The following is NO error in clang 4.0.0 (and 5.0.0 based upon LLVM 5.0.0-r295586) / Visual Studio 2017:

int i[0];

But the C++14 standard says in 8.3.4.1:

If the constant-expression (5.19) is present, it shall be a converted constant expression of type std::size_t and its value shall be greater than zero.

KarstenH avatar Mar 23 '17 09:03 KarstenH

Thanks for this information, Ismail. I didn't know about it. Anyway, do you have an idea why this extension cannot be turned off? Even Microsoft's VC always generates an error, with or without Microsoft extensions. I think clang should offer a mode which is in true compliance to the chosen C++ standard, that is, all non-compliant extensions are turned off. Or (even better) this true compliance mode is on by default and non-compliant extensions must be turned on manually.

KarstenH avatar Mar 24 '17 05:03 KarstenH

Yes, I know. Since I always compile with -Weverything I got that warning. For C++14 compliant code however there should be a way to turn it into an error. This is not possible because the extension cannot be turned off. Or can it? If so please let me know.

You cannot turn off this extension; it's always on. You can only convert the soft warning into an error with -Werror=zero-length-array switch. You can also use -pedantic-error, which promote pedantic warnings to errors.

% clang -x c++ -fsyntax-only -Werror=zero-length-array -std=c++14 -
int i[0];
<stdin>:1:7: error: zero size arrays are an extension [-Werror,-Wzero-length-array]
int i[0];
      ^
1 error generated.
% clang -x c -fsyntax-only -pedantic-errors -
int i[0];
<stdin>:1:7: error: zero size arrays are an extension [-Werror,-Wzero-length-array]
int i[0];
      ^
1 error generated.

llvmbot avatar Mar 23 '17 18:03 llvmbot

Yes, I know. Since I always compile with -Weverything I got that warning. For C++14 compliant code however there should be a way to turn it into an error. This is not possible because the extension cannot be turned off. Or can it? If so please let me know.

KarstenH avatar Mar 23 '17 18:03 KarstenH

It's a compiler extension. If you compile with -pedantic, you'll get a warning:

% clang -x c -fsyntax-only -pedantic -
int i[0];
<stdin>:1:7: warning: zero size arrays are an extension [-Wzero-length-array]
int i[0];
      ^
1 warning generated.

llvmbot avatar Mar 23 '17 13:03 llvmbot

CC @AaronBallman

Endilll avatar Mar 10 '25 22:03 Endilll

Related discussions:

https://github.com/llvm/llvm-project/issues/121398 https://github.com/llvm/llvm-project/issues/54705 https://github.com/llvm/llvm-project/issues/54120

There are some tidbits in them work looking at.

shafik avatar Mar 11 '25 01:03 shafik

Anyway, do you have an idea why this extension cannot be turned off?

It can be. -Werror=zero-length-array turns off just this extension.

I think clang should offer a mode which is in true compliance to the chosen C++ standard, that is, all non-compliant extensions are turned off.

This is a conforming extension. If you want to disable all extensions, that's what -pedantic-errors does.

Closing this issue as it is not a bug, however, if you have further questions, feel free to reopen and ask them!

AaronBallman avatar Mar 11 '25 12:03 AaronBallman