[clang-tools-extra] [clang-tidy] Improve `bugprone-exception-escape`: add stacktrace of escaped exception (PR #134375)

Baranov Victor via cfe-commits cfe-commits at lists.llvm.org
Sun Apr 13 12:17:43 PDT 2025


================
@@ -80,13 +80,46 @@ void ExceptionEscapeCheck::check(const MatchFinder::MatchResult &Result) {
   if (!MatchedDecl)
     return;
 
-  if (Tracer.analyze(MatchedDecl).getBehaviour() ==
-      utils::ExceptionAnalyzer::State::Throwing)
-    // FIXME: We should provide more information about the exact location where
-    // the exception is thrown, maybe the full path the exception escapes
-    diag(MatchedDecl->getLocation(), "an exception may be thrown in function "
-                                     "%0 which should not throw exceptions")
-        << MatchedDecl;
+  const utils::ExceptionAnalyzer::ExceptionInfo Info =
+      Tracer.analyze(MatchedDecl);
+
+  if (Info.getBehaviour() != utils::ExceptionAnalyzer::State::Throwing) {
+    return;
+  }
+
+  diag(MatchedDecl->getLocation(), "an exception may be thrown in function "
+                                   "%0 which should not throw exceptions")
+      << MatchedDecl;
+
+  const utils::ExceptionAnalyzer::ExceptionInfo::ThrowInfo ThrowInfo =
+      Info.getExceptions().begin()->getSecond();
+
+  if (ThrowInfo.Loc.isInvalid()) {
+    return;
+  }
+
+  // FIXME: We should provide exact position of functions calls, not only call
+  // stack of thrown exception.
+  const utils::ExceptionAnalyzer::CallStack &Stack = ThrowInfo.Stack;
+  diag(Stack.front()->getLocation(),
+       "example of unhandled exception throw stack, starting from function %0",
----------------
vbvictor wrote:

Is this a better wording?
`throw stack of unhandled exception, starting from function %0`
Initially, I used `example` as a synonym to `may` word that was used in main diagnostic of this check.

https://github.com/llvm/llvm-project/pull/134375


More information about the cfe-commits mailing list