[clang-tools-extra] r344444 - [clang-tidy] Optimize query in bugprone-exception-escape

Adam Balogh via cfe-commits cfe-commits at lists.llvm.org
Sat Oct 13 03:34:52 PDT 2018


Author: baloghadamsoftware
Date: Sat Oct 13 03:34:52 2018
New Revision: 344444

URL: http://llvm.org/viewvc/llvm-project?rev=344444&view=rev
Log:
[clang-tidy] Optimize query in bugprone-exception-escape

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 https://bugs.llvm.org/show_bug.cgi?id=39167 where the
execution time is so long that it seems to hang.

Differential Revision: https://reviews.llvm.org/D53187


Modified:
    clang-tools-extra/trunk/clang-tidy/bugprone/ExceptionEscapeCheck.cpp
    clang-tools-extra/trunk/docs/clang-tidy/checks/bugprone-exception-escape.rst
    clang-tools-extra/trunk/test/clang-tidy/bugprone-exception-escape.cpp

Modified: clang-tools-extra/trunk/clang-tidy/bugprone/ExceptionEscapeCheck.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/bugprone/ExceptionEscapeCheck.cpp?rev=344444&r1=344443&r2=344444&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/bugprone/ExceptionEscapeCheck.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/bugprone/ExceptionEscapeCheck.cpp Sat Oct 13 03:34:52 2018
@@ -190,12 +190,12 @@ void ExceptionEscapeCheck::registerMatch
     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);
 }

Modified: clang-tools-extra/trunk/docs/clang-tidy/checks/bugprone-exception-escape.rst
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/docs/clang-tidy/checks/bugprone-exception-escape.rst?rev=344444&r1=344443&r2=344444&view=diff
==============================================================================
--- clang-tools-extra/trunk/docs/clang-tidy/checks/bugprone-exception-escape.rst (original)
+++ clang-tools-extra/trunk/docs/clang-tidy/checks/bugprone-exception-escape.rst Sat Oct 13 03:34:52 2018
@@ -21,6 +21,8 @@ are always possible to implement in a no
 operations are also used to create move operations. A throwing ``main()``
 function also results in unexpected termination.
 
+WARNING! This check may be expensive on large source files.
+
 Options
 -------
 

Modified: clang-tools-extra/trunk/test/clang-tidy/bugprone-exception-escape.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/bugprone-exception-escape.cpp?rev=344444&r1=344443&r2=344444&view=diff
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/bugprone-exception-escape.cpp (original)
+++ clang-tools-extra/trunk/test/clang-tidy/bugprone-exception-escape.cpp Sat Oct 13 03:34:52 2018
@@ -258,6 +258,31 @@ void this_counts(int n) noexcept {
   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;




More information about the cfe-commits mailing list