llvm-project/clang/test/SemaCXX/exceptions.cpp
Aaron Ballman 84a3aadf0f Diagnose use of VLAs in C++ by default
Reapplication of 7339c0f782d5c70e0928f8991b0c05338a90c84c 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

295 lines
7.2 KiB
C++

// RUN: %clang_cc1 -fcxx-exceptions -fexceptions -fsyntax-only -verify=expected,precxx17 %std_cxx98-14 %s
// RUN: %clang_cc1 -fcxx-exceptions -fexceptions -fsyntax-only -verify %std_cxx17- %s
struct A; // expected-note 4 {{forward declaration of 'A'}}
struct Abstract { virtual void f() = 0; }; // expected-note {{unimplemented pure virtual method 'f'}}
void trys() {
int k = 42;
try {
} catch(int i) { // expected-note {{previous definition}}
int j = i;
int i; // expected-error {{redefinition of 'i'}}
} catch(float i) {
} catch(void v) { // expected-error {{cannot catch incomplete type 'void'}}
} catch(A a) { // expected-error {{cannot catch incomplete type 'A'}}
} catch(A *a) { // expected-error {{cannot catch pointer to incomplete type 'A'}}
} catch(A &a) { // expected-error {{cannot catch reference to incomplete type 'A'}}
} catch(Abstract) { // expected-error {{variable type 'Abstract' is an abstract class}}
} catch(...) {
int ref = k;
{
int ref = k;
}
int j = i; // expected-error {{use of undeclared identifier 'i'}}
}
try {
} catch(...) { // expected-error {{catch-all handler must come last}}
} catch(int) {
}
}
void throws() {
throw;
throw 0;
throw throw; // expected-error {{cannot throw object of incomplete type 'void'}}
throw (A*)0; // expected-error {{cannot throw pointer to object of incomplete type 'A'}}
}
void jumps() {
l1:
goto l5;
goto l4; // expected-error {{cannot jump}}
goto l3; // expected-error {{cannot jump}}
goto l2; // expected-error {{cannot jump}}
goto l1;
try { // expected-note 4 {{jump bypasses initialization of try block}}
l2:
goto l5;
goto l4; // expected-error {{cannot jump}}
goto l3; // expected-error {{cannot jump}}
goto l2;
goto l1;
} catch(int) { // expected-note 4 {{jump bypasses initialization of catch block}}
l3:
goto l5;
goto l4; // expected-error {{cannot jump}}
goto l3;
goto l2; // expected-error {{cannot jump}}
goto l1;
} catch(...) { // expected-note 4 {{jump bypasses initialization of catch block}}
l4:
goto l5;
goto l4;
goto l3; // expected-error {{cannot jump}}
goto l2; // expected-error {{cannot jump}}
goto l1;
}
l5:
goto l5;
goto l4; // expected-error {{cannot jump}}
goto l3; // expected-error {{cannot jump}}
goto l2; // expected-error {{cannot jump}}
goto l1;
}
struct BadReturn {
BadReturn() try {
} catch(...) {
// Try to hide
try {
} catch(...) {
{
if (0)
return; // expected-error {{return in the catch of a function try block of a constructor is illegal}}
}
}
}
BadReturn(int);
};
BadReturn::BadReturn(int) try {
} catch(...) {
// Try to hide
try {
} catch(int) {
return; // expected-error {{return in the catch of a function try block of a constructor is illegal}}
} catch(...) {
{
if (0)
return; // expected-error {{return in the catch of a function try block of a constructor is illegal}}
}
}
}
// Cannot throw an abstract type.
class foo {
public:
foo() {}
void bar () {
throw *this; // expected-error{{cannot throw an object of abstract type 'foo'}}
}
virtual void test () = 0; // expected-note{{unimplemented pure virtual method 'test'}}
};
namespace PR6831 {
namespace NA { struct S; }
namespace NB { struct S; }
void f() {
using namespace NA;
using namespace NB;
try {
} catch (int S) {
}
}
}
#if __cplusplus < 201703L
namespace Decay {
struct A {
void f() throw (A[10]);
};
template<typename T> struct B {
void f() throw (B[10]);
};
template struct B<int>;
void f() throw (int[10], int(*)());
void f() throw (int*, int());
template<typename T> struct C {
void f() throw (T);
#if __cplusplus <= 199711L
// expected-error@-2 {{pointer to incomplete type 'Decay::E' is not allowed in exception specification}}
#endif
};
struct D {
C<D[10]> c;
};
struct E;
#if __cplusplus <= 199711L
// expected-note@-2 {{forward declaration of 'Decay::E'}}
#endif
C<E[10]> e;
#if __cplusplus <= 199711L
// expected-note@-2 {{in instantiation of template class 'Decay::C<Decay::E[10]>' requested here}}
#endif
}
void rval_ref() throw (int &&); // expected-error {{rvalue reference type 'int &&' is not allowed in exception specification}}
#if __cplusplus <= 199711L
// expected-warning@-2 {{rvalue references are a C++11 extension}}
#endif
#endif
namespace HandlerInversion {
struct B {};
struct D : B {};
struct D2 : D {};
void f1() {
try {
} catch (B &b) { // expected-note {{for type 'B &'}}
} catch (D &d) { // expected-warning {{exception of type 'D &' will be caught by earlier handler}}
}
}
void f2() {
try {
} catch (B *b) { // expected-note {{for type 'B *'}}
} catch (D *d) { // expected-warning {{exception of type 'D *' will be caught by earlier handler}}
}
}
void f3() {
try {
} catch (D &d) { // Ok
} catch (B &b) {
}
}
void f4() {
try {
} catch (B &b) { // Ok
}
}
void f5() {
try {
} catch (int) {
} catch (float) {
}
}
void f6() {
try {
} catch (B &b) { // expected-note {{for type 'B &'}}
} catch (D2 &d) { // expected-warning {{exception of type 'D2 &' will be caught by earlier handler}}
}
}
void f7() {
try {
} catch (B *b) { // Ok
} catch (D &d) { // Ok
}
try {
} catch (B b) { // Ok
} catch (D *d) { // Ok
}
}
void f8() {
try {
} catch (const B &b) { // expected-note {{for type 'const B &'}}
} catch (D2 &d) { // expected-warning {{exception of type 'D2 &' will be caught by earlier handler}}
}
try {
} catch (B &b) { // expected-note {{for type 'B &'}}
} catch (const D2 &d) { // expected-warning {{exception of type 'const D2 &' will be caught by earlier handler}}
}
try {
} catch (B b) { // expected-note {{for type 'B'}}
} catch (D &d) { // expected-warning {{exception of type 'D &' will be caught by earlier handler}}
}
}
}
namespace ConstVolatileThrow {
struct S {
S() {} // precxx17-note{{candidate constructor not viable}}
S(const S &s); // precxx17-note{{candidate constructor not viable}}
};
typedef const volatile S CVS;
void f() {
throw CVS(); // precxx17-error{{no matching constructor for initialization}}
}
}
namespace ConstVolatileCatch {
struct S {
S() {}
S(const volatile S &s);
private:
S(const S &s); // expected-note {{declared private here}}
};
void f();
void g() {
try {
f();
} catch (volatile S s) { // expected-error {{calling a private constructor}}
}
}
}
namespace PR28047 {
void test1(int i) { // expected-note {{declared here}}
try {
} catch (int(*)[i]) { // expected-error{{cannot catch variably modified type}} \
expected-warning {{variable length arrays in C++ are a Clang extension}} \
expected-note {{function parameter 'i' with unknown value cannot be used in a constant expression}}
}
}
void test2() {
int i; // expected-note {{declared here}}
try {
} catch (int(*)[i]) { // expected-error{{cannot catch variably modified type}} \
expected-warning {{variable length arrays in C++ are a Clang extension}} \
expected-note {{read of non-const variable 'i' is not allowed in a constant expression}}
}
}
}