[clang-tools-extra] r347761 - Fix false positive with lambda assignments in cert-err58-cpp.

Aaron Ballman via cfe-commits cfe-commits at lists.llvm.org
Wed Nov 28 07:04:38 PST 2018


Author: aaronballman
Date: Wed Nov 28 07:04:38 2018
New Revision: 347761

URL: http://llvm.org/viewvc/llvm-project?rev=347761&view=rev
Log:
Fix false positive with lambda assignments in cert-err58-cpp.

This check is about preventing exceptions from being thrown before main() executes, and assigning a lambda (rather than calling it) to a global object cannot throw any exceptions.

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=347761&r1=347760&r2=347761&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/cert/StaticObjectExceptionCheck.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/cert/StaticObjectExceptionCheck.cpp Wed Nov 28 07:04:38 2018
@@ -26,7 +26,8 @@ void StaticObjectExceptionCheck::registe
   // initializer that can throw.
   Finder->addMatcher(
       varDecl(anyOf(hasThreadStorageDuration(), hasStaticStorageDuration()),
-              unless(anyOf(isConstexpr(), hasAncestor(functionDecl()))),
+              unless(anyOf(isConstexpr(), hasType(cxxRecordDecl(isLambda())),
+                           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=347761&r1=347760&r2=347761&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 07:04:38 2018
@@ -1,7 +1,7 @@
-// RUN: clang-tidy %s -checks="-*,cert-err58-cpp" -- -std=c++11 -target x86_64-pc-linux-gnu \
+// RUN: clang-tidy %s -checks="-*,cert-err58-cpp" -- -std=c++17 -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" -- -DNONEXCEPTIONS -fno-exceptions -std=c++11 -target x86_64-pc-linux-gnu \
+// RUN: clang-tidy %s -checks="-*,cert-err58-cpp" -- -DNONEXCEPTIONS -fno-exceptions -std=c++17 -target x86_64-pc-linux-gnu \
 // RUN:   | FileCheck %s -allow-empty -check-prefix=CHECK-NONEXCEPTIONS \
 // RUN:   -implicit-check-not="{{warning|error}}:"
 
@@ -236,3 +236,28 @@ int baz = foo(0); // Not OK; throws at r
 } // namespace pr35457
 #endif // NONEXCEPTIONS
 
+namespace pr39777 {
+struct S { S(); };
+struct T { T() noexcept; };
+
+auto Okay1 = []{ S s; };
+auto Okay2 = []{ (void)new int; };
+auto NotOkay1 = []{ S s; return 12; }(); // Because the lambda call is not noexcept
+// CHECK-EXCEPTIONS: :[[@LINE-1]]:6: warning: initialization of 'NotOkay1' with static storage duration may throw an exception that cannot be caught [cert-err58-cpp]
+// CHECK-EXCEPTIONS: :[[@LINE-7]]:12: note: possibly throwing constructor declared here
+auto NotOkay2 = []() noexcept { S s; return 12; }(); // Because S::S() is not noexcept
+// CHECK-EXCEPTIONS: :[[@LINE-1]]:6: warning: initialization of 'NotOkay2' with static storage duration may throw an exception that cannot be caught [cert-err58-cpp]
+// CHECK-EXCEPTIONS: :[[@LINE-10]]:12: note: possibly throwing constructor declared here
+auto Okay3 = []() noexcept { T t; return t; }();
+
+struct U {
+  U() noexcept;
+  auto getBadLambda() const noexcept {
+    return []{ S s; return s; };
+  }
+};
+auto Okay4 = []{ U u; return u.getBadLambda(); }();
+auto NotOkay3 = []() noexcept { U u; return u.getBadLambda(); }()(); // Because the lambda returned and called is not noexcept
+// CHECK-EXCEPTIONS: :[[@LINE-1]]:6: warning: initialization of 'NotOkay3' with static storage duration may throw an exception that cannot be caught [cert-err58-cpp]
+// CHECK-EXCEPTIONS: :[[@LINE-6]]:12: note: possibly throwing function declared here
+}




More information about the cfe-commits mailing list