Files
clang-p2996/clang/test/SemaCXX/vla-ext-diag.cpp
Aaron Ballman 84a3aadf0f Diagnose use of VLAs in C++ by default
Reapplication of 7339c0f782 with a fix
for a crash involving arrays without a size expression.

Clang supports VLAs in C++ as an extension, but we currently only warn
on their use when you pass -Wvla, -Wvla-extension, or -pedantic.
However, VLAs as they're expressed in C have been considered by WG21
and rejected, are easy to use accidentally to the surprise of users
(e.g., https://ddanilov.me/default-non-standard-features/), and they
have potential security implications beyond constant-size arrays
(https://wiki.sei.cmu.edu/confluence/display/c/ARR32-C.+Ensure+size+arguments+for+variable+length+arrays+are+in+a+valid+range).
C++ users should strongly consider using other functionality such as
std::vector instead.

This seems like sufficiently compelling evidence to warn users about
VLA use by default in C++ modes. This patch enables the -Wvla-extension
diagnostic group in C++ language modes by default, and adds the warning
group to -Wall in GNU++ language modes. The warning is still opt-in in
C language modes, where support for VLAs is somewhat less surprising to
users.

RFC: https://discourse.llvm.org/t/rfc-diagnosing-use-of-vlas-in-c/73109
Fixes https://github.com/llvm/llvm-project/issues/62836
Differential Revision: https://reviews.llvm.org/D156565
2023-10-20 13:10:03 -04:00

41 lines
3.3 KiB
C++

// RUN: %clang_cc1 -verify=gnu -std=gnu++11 %s
// RUN: %clang_cc1 -verify=expected,cxx11 -Wvla -std=gnu++11 %s
// RUN: %clang_cc1 -verify=expected,cxx11 -std=c++11 %s
// RUN: %clang_cc1 -verify=expected,cxx98 -std=c++98 %s
// RUN: %clang_cc1 -verify=expected,off -std=c++11 -Wno-vla-extension-static-assert %s
// gnu-no-diagnostics
// Demonstrate that we do not diagnose use of VLAs by default in GNU mode, but
// we do diagnose them in C++ mode. Also note that we suggest use of
// static_assert, but only in C++11 and later and only if the warning group is
// not disabled.
// C++98 mode does not emit the same notes as C++11 mode because in C++98,
// we're looking for an integer constant expression, whereas in C++11 and later,
// we're looking for a constant expression that is of integer type (these are
// different operations; ICE looks at the syntactic form of the expression, but
// C++11 constant expressions require calculating the expression value).
void func(int n) { // cxx11-note {{declared here}} off-note {{declared here}}
int vla[n]; // expected-warning {{variable length arrays in C++ are a Clang extension}} \
cxx11-note {{function parameter 'n' with unknown value cannot be used in a constant expression}} \
off-note {{function parameter 'n' with unknown value cannot be used in a constant expression}}
}
void old_style_static_assert(int n) { // cxx11-note 5 {{declared here}} off-note 2 {{declared here}}
int array1[n != 12 ? 1 : -1]; // cxx11-warning {{variable length arrays in C++ are a Clang extension; did you mean to use 'static_assert'?}} \
cxx98-warning {{variable length arrays in C++ are a Clang extension}} \
cxx11-note {{function parameter 'n' with unknown value cannot be used in a constant expression}}
int array2[n != 12 ? -1 : 1]; // cxx11-warning {{variable length arrays in C++ are a Clang extension; did you mean to use 'static_assert'?}} \
cxx98-warning {{variable length arrays in C++ are a Clang extension}} \
cxx11-note {{function parameter 'n' with unknown value cannot be used in a constant expression}}
int array3[n != 12 ? 1 : n]; // expected-warning {{variable length arrays in C++ are a Clang extension}} \
cxx11-note {{function parameter 'n' with unknown value cannot be used in a constant expression}} \
off-note {{function parameter 'n' with unknown value cannot be used in a constant expression}}
int array4[(n ? 1 : -1)]; // cxx11-warning {{variable length arrays in C++ are a Clang extension; did you mean to use 'static_assert'?}} \
cxx98-warning {{variable length arrays in C++ are a Clang extension}} \
cxx11-note {{function parameter 'n' with unknown value cannot be used in a constant expression}}
int array5[n ? 1 : 0]; // expected-warning {{variable length arrays in C++ are a Clang extension}} \
cxx11-note {{function parameter 'n' with unknown value cannot be used in a constant expression}} \
off-note {{function parameter 'n' with unknown value cannot be used in a constant expression}}
}