Author: aaronballman Date: Wed Nov 28 03:57:13 2018 New Revision: 347745 URL: http://llvm.org/viewvc/llvm-project?rev=347745&view=rev Log: Fix a false-positive with cert-err58-cpp.
If a variable is declared constexpr then its initializer needs to be a constant expression, and thus, cannot throw. This check is about not throwing exceptions before main() runs, and so it doesn't apply if the initializer cannot throw. This silences the diagnostic when initializing a constexpr variable and fixes PR35457. Modified: clang-tools-extra/trunk/clang-tidy/cert/StaticObjectExceptionCheck.cpp clang-tools-extra/trunk/test/clang-tidy/cert-static-object-exception.cpp Modified: clang-tools-extra/trunk/clang-tidy/cert/StaticObjectExceptionCheck.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/cert/StaticObjectExceptionCheck.cpp?rev=347745&r1=347744&r2=347745&view=diff ============================================================================== --- clang-tools-extra/trunk/clang-tidy/cert/StaticObjectExceptionCheck.cpp (original) +++ clang-tools-extra/trunk/clang-tidy/cert/StaticObjectExceptionCheck.cpp Wed Nov 28 03:57:13 2018 @@ -26,7 +26,7 @@ void StaticObjectExceptionCheck::registe // initializer that can throw. Finder->addMatcher( varDecl(anyOf(hasThreadStorageDuration(), hasStaticStorageDuration()), - unless(hasAncestor(functionDecl())), + unless(anyOf(isConstexpr(), hasAncestor(functionDecl()))), anyOf(hasDescendant(cxxConstructExpr(hasDeclaration( cxxConstructorDecl(unless(isNoThrow())).bind("func")))), hasDescendant(cxxNewExpr(hasDeclaration( Modified: clang-tools-extra/trunk/test/clang-tidy/cert-static-object-exception.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/cert-static-object-exception.cpp?rev=347745&r1=347744&r2=347745&view=diff ============================================================================== --- clang-tools-extra/trunk/test/clang-tidy/cert-static-object-exception.cpp (original) +++ clang-tools-extra/trunk/test/clang-tidy/cert-static-object-exception.cpp Wed Nov 28 03:57:13 2018 @@ -1,7 +1,7 @@ // RUN: clang-tidy %s -checks="-*,cert-err58-cpp" -- -std=c++11 -target x86_64-pc-linux-gnu \ // RUN: | FileCheck %s -check-prefix=CHECK-EXCEPTIONS \ // RUN: -implicit-check-not="{{warning|error}}:" -// RUN: clang-tidy %s -checks="-*,cert-err58-cpp" -- -fno-exceptions -std=c++11 -target x86_64-pc-linux-gnu \ +// RUN: clang-tidy %s -checks="-*,cert-err58-cpp" -- -DNONEXCEPTIONS -fno-exceptions -std=c++11 -target x86_64-pc-linux-gnu \ // RUN: | FileCheck %s -allow-empty -check-prefix=CHECK-NONEXCEPTIONS \ // RUN: -implicit-check-not="{{warning|error}}:" @@ -223,3 +223,16 @@ W Statics::w; // CHECK-EXCEPTIONS: :[[@LINE-1]]:12: warning: initialization of 'w' with static storage duration may throw an exception that cannot be caught // CHECK-EXCEPTIONS: 29:3: note: possibly throwing constructor declared here // CHECK-NONEXCEPTIONS-NOT: warning: + +#ifndef NONEXCEPTIONS +namespace pr35457 { +constexpr int foo(int x) { if (x <= 0) throw 12; return x; } + +constexpr int bar = foo(1); // OK +// CHECK-EXCEPTIONS-NOT: warning: initialization of 'bar' with static storage +int baz = foo(0); // Not OK; throws at runtime when exceptions are enabled. +// CHECK-EXCEPTIONS: :[[@LINE-1]]:5: warning: initialization of 'baz' with static storage duration may throw an exception that cannot be caught [cert-err58-cpp] +// CHECK-EXCEPTIONS: :[[@LINE-6]]:15: note: possibly throwing function declared here +} // namespace pr35457 +#endif // NONEXCEPTIONS + _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits