Array may have zero size
| 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_tand its value shall be greater than zero.
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.
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.
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.
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.
CC @AaronBallman
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.
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!