baloghadamsoftware created this revision. baloghadamsoftware added reviewers: JonasToth, alexfh, aaron.ballman. baloghadamsoftware added a project: clang-tools-extra. Herald added subscribers: Szelethus, dkrupp, rnkovacs, xazax.hun, whisperity.
Checking whether a functions throws indirectly may be very expensive because it needs to visit its whole call graph. Therefore we should first check whether the function is forbidden to throw and only check whether it throws afterward. This also seems to solve bug bug 39167 <https://bugs.llvm.org/show_bug.cgi?id=39167> where the execution time is so long that it seems to hang. Repository: rCTE Clang Tools Extra https://reviews.llvm.org/D53187 Files: clang-tidy/bugprone/ExceptionEscapeCheck.cpp test/clang-tidy/bugprone-exception-escape.cpp Index: test/clang-tidy/bugprone-exception-escape.cpp =================================================================== --- test/clang-tidy/bugprone-exception-escape.cpp +++ test/clang-tidy/bugprone-exception-escape.cpp @@ -258,6 +258,31 @@ throw ignored1(); } +void thrower(int n) { + throw n; +} + +int directly_recursive(int n) noexcept { + // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: an exception may be thrown in funcion 'directly_recursive' which should not throw exceptions + if (n == 0) + thrower(n); + return directly_recursive(n); +} + +int indirectly_recursive(int n) noexcept; +// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: an exception may be thrown in functin 'indirectly_recursive' which should not throw exceptions + +int recursion_helper(int n) { + indirectly_recursive(n); +} + +int indirectly_recursive(int n) noexcept { + // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: an exception may be thrown in funcion 'indirectly_recursive' which should not throw exceptions + if (n == 0) + thrower(n); + return recursion_helper(n); +} + int main() { // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: an exception may be thrown in function 'main' which should not throw exceptions throw 1; Index: clang-tidy/bugprone/ExceptionEscapeCheck.cpp =================================================================== --- clang-tidy/bugprone/ExceptionEscapeCheck.cpp +++ clang-tidy/bugprone/ExceptionEscapeCheck.cpp @@ -190,12 +190,12 @@ return; Finder->addMatcher( - functionDecl(allOf(throws(unless(isIgnored(IgnoredExceptions))), - anyOf(isNoThrow(), cxxDestructorDecl(), + functionDecl(allOf(anyOf(isNoThrow(), cxxDestructorDecl(), cxxConstructorDecl(isMoveConstructor()), cxxMethodDecl(isMoveAssignmentOperator()), hasName("main"), hasName("swap"), - isEnabled(FunctionsThatShouldNotThrow)))) + isEnabled(FunctionsThatShouldNotThrow)), + throws(unless(isIgnored(IgnoredExceptions))))) .bind("thrower"), this); }
Index: test/clang-tidy/bugprone-exception-escape.cpp =================================================================== --- test/clang-tidy/bugprone-exception-escape.cpp +++ test/clang-tidy/bugprone-exception-escape.cpp @@ -258,6 +258,31 @@ throw ignored1(); } +void thrower(int n) { + throw n; +} + +int directly_recursive(int n) noexcept { + // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: an exception may be thrown in funcion 'directly_recursive' which should not throw exceptions + if (n == 0) + thrower(n); + return directly_recursive(n); +} + +int indirectly_recursive(int n) noexcept; +// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: an exception may be thrown in functin 'indirectly_recursive' which should not throw exceptions + +int recursion_helper(int n) { + indirectly_recursive(n); +} + +int indirectly_recursive(int n) noexcept { + // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: an exception may be thrown in funcion 'indirectly_recursive' which should not throw exceptions + if (n == 0) + thrower(n); + return recursion_helper(n); +} + int main() { // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: an exception may be thrown in function 'main' which should not throw exceptions throw 1; Index: clang-tidy/bugprone/ExceptionEscapeCheck.cpp =================================================================== --- clang-tidy/bugprone/ExceptionEscapeCheck.cpp +++ clang-tidy/bugprone/ExceptionEscapeCheck.cpp @@ -190,12 +190,12 @@ return; Finder->addMatcher( - functionDecl(allOf(throws(unless(isIgnored(IgnoredExceptions))), - anyOf(isNoThrow(), cxxDestructorDecl(), + functionDecl(allOf(anyOf(isNoThrow(), cxxDestructorDecl(), cxxConstructorDecl(isMoveConstructor()), cxxMethodDecl(isMoveAssignmentOperator()), hasName("main"), hasName("swap"), - isEnabled(FunctionsThatShouldNotThrow)))) + isEnabled(FunctionsThatShouldNotThrow)), + throws(unless(isIgnored(IgnoredExceptions))))) .bind("thrower"), this); }
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits