From: Anders Carlsson Date: Thu, 24 Mar 2011 01:01:41 +0000 (+0000) Subject: Add a new warning for exit-time destructors. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=2b32dad701ef3e666e3b9cea02e1d094a1078c6f;p=clang Add a new warning for exit-time destructors. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@128188 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Basic/DiagnosticGroups.td b/include/clang/Basic/DiagnosticGroups.td index 2b7c5de25c..3d2cbc486e 100644 --- a/include/clang/Basic/DiagnosticGroups.td +++ b/include/clang/Basic/DiagnosticGroups.td @@ -55,6 +55,7 @@ def CXXHexFloats : DiagGroup<"c++-hex-floats">; def : DiagGroup<"c++0x-compat", [CXXHexFloats]>; def : DiagGroup<"effc++">; +def ExitTimeDestructors : DiagGroup<"exit-time-destructors">; def FourByteMultiChar : DiagGroup<"four-char-constants">; def GlobalConstructors : DiagGroup<"global-constructors">; def : DiagGroup<"idiomatic-parentheses">; diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td index fa66d51369..dd82a46ba2 100644 --- a/include/clang/Basic/DiagnosticSemaKinds.td +++ b/include/clang/Basic/DiagnosticSemaKinds.td @@ -204,6 +204,9 @@ def warn_global_constructor : Warning< def warn_global_destructor : Warning< "declaration requires a global destructor">, InGroup, DefaultIgnore; +def warn_exit_time_destructor : Warning< + "declaration requires an exit-time destructor">, + InGroup, DefaultIgnore; def err_invalid_thread : Error< "'__thread' is only allowed on variable declarations">; diff --git a/lib/Sema/SemaDeclCXX.cpp b/lib/Sema/SemaDeclCXX.cpp index 220c22ad0f..edee86476b 100644 --- a/lib/Sema/SemaDeclCXX.cpp +++ b/lib/Sema/SemaDeclCXX.cpp @@ -6102,9 +6102,13 @@ void Sema::FinalizeVarWithDestructor(VarDecl *VD, const RecordType *Record) { << VD->getDeclName() << VD->getType()); - // TODO: this should be re-enabled for static locals by !CXAAtExit - if (!VD->isInvalidDecl() && VD->hasGlobalStorage() && !VD->isStaticLocal()) - Diag(VD->getLocation(), diag::warn_global_destructor); + if (!VD->isInvalidDecl() && VD->hasGlobalStorage()) { + // TODO: this should be re-enabled for static locals by !CXAAtExit + if (!VD->isStaticLocal()) + Diag(VD->getLocation(), diag::warn_global_destructor); + + Diag(VD->getLocation(), diag::warn_exit_time_destructor); + } } } diff --git a/test/SemaCXX/warn-exit-time-destructors.cpp b/test/SemaCXX/warn-exit-time-destructors.cpp new file mode 100644 index 0000000000..f49134b71c --- /dev/null +++ b/test/SemaCXX/warn-exit-time-destructors.cpp @@ -0,0 +1,27 @@ +// RUN: %clang_cc1 -fsyntax-only -Wexit-time-destructors %s -verify + +namespace test1 { + struct A { ~A(); }; + A a; // expected-warning {{declaration requires an exit-time destructor}} + A b[10]; // expected-warning {{declaration requires an exit-time destructor}} + A c[10][10]; // expected-warning {{declaration requires an exit-time destructor}} + + A &d = a; + A &e = b[5]; + A &f = c[5][7]; +} + +namespace test2 { +void f() { + struct A { ~A() { } }; + + static A a; // expected-warning {{declaration requires an exit-time destructor}} + static A b[10]; // expected-warning {{declaration requires an exit-time destructor}} + static A c[10][10]; // expected-warning {{declaration requires an exit-time destructor}} + + static A &d = a; + static A &e = b[5]; + static A &f = c[5][7]; +} + +}