[clang-tools-extra] [clang-tidy] Add options to throw unannotated functions in `bugprone-exception-escape` (PR #168324)

Baranov Victor via cfe-commits cfe-commits at lists.llvm.org
Tue Nov 18 11:28:26 PST 2025


================
@@ -103,41 +109,53 @@ void ExceptionEscapeCheck::check(const MatchFinder::MatchResult &Result) {
   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 auto Behaviour = Info.getBehaviour();
+  const bool IsThrowing =
+      Behaviour == utils::ExceptionAnalyzer::State::Throwing;
+  const bool IsUnknown = Behaviour == utils::ExceptionAnalyzer::State::Unknown;
 
-  const auto &[ThrowType, ThrowInfo] = *Info.getExceptions().begin();
+  const bool ReportUnknown =
+      IsUnknown &&
+      ((KnownUnannotatedAsThrowing && Info.hasUnknownFromKnownUnannotated()) ||
+       (UnknownAsThrowing && Info.hasUnknownFromMissingDefinition()));
 
-  if (ThrowInfo.Loc.isInvalid())
+  if (!(IsThrowing || ReportUnknown))
     return;
 
-  const utils::ExceptionAnalyzer::CallStack &Stack = ThrowInfo.Stack;
-  diag(ThrowInfo.Loc,
-       "frame #0: unhandled exception of type %0 may be thrown in function %1 "
-       "here",
-       DiagnosticIDs::Note)
-      << QualType(ThrowType, 0U) << Stack.back().first;
-
-  size_t FrameNo = 1;
-  for (auto CurrIt = ++Stack.rbegin(), PrevIt = Stack.rbegin();
-       CurrIt != Stack.rend(); ++CurrIt, ++PrevIt) {
-    const FunctionDecl *CurrFunction = CurrIt->first;
-    const FunctionDecl *PrevFunction = PrevIt->first;
-    const SourceLocation PrevLocation = PrevIt->second;
-    if (PrevLocation.isValid()) {
-      diag(PrevLocation, "frame #%0: function %1 calls function %2 here",
-           DiagnosticIDs::Note)
-          << FrameNo << CurrFunction << PrevFunction;
-    } else {
-      diag(CurrFunction->getLocation(),
-           "frame #%0: function %1 calls function %2", DiagnosticIDs::Note)
-          << FrameNo << CurrFunction << PrevFunction;
+  diag(MatchedDecl->getLocation(), "an exception may be thrown in function %0 "
+                                   "which should not throw exceptions")
+      << MatchedDecl;
+
+  if (!Info.getExceptions().empty()) {
----------------
vbvictor wrote:

Filed https://github.com/llvm/llvm-project/issues/168599 for it.

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


More information about the cfe-commits mailing list