[clang-tools-extra] [clang-tidy] Improve `bugprone-exception-escape`: add stacktrace of escaped exception (PR #134375)
Congcong Cai via cfe-commits
cfe-commits at lists.llvm.org
Sun May 25 18:59:17 PDT 2025
================
@@ -80,13 +78,47 @@ 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;
+ }
+
+ const utils::ExceptionAnalyzer::CallStack &Stack = ThrowInfo.Stack;
+ diag(ThrowInfo.Loc,
+ "frame #0: unhandled exception may be thrown in function %0 here",
+ DiagnosticIDs::Note)
+ << 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)
----------------
HerrCai0907 wrote:
```suggestion
"frame #%0: function '%1' calls function '%2'", DiagnosticIDs::Note)
```
https://github.com/llvm/llvm-project/pull/134375
More information about the cfe-commits
mailing list