[clang] [analyzer] Implement BugReporterVisitor for UseAfterLifetimeEnd to trace lifetime source binding (PR #207052)

Benedek Kaibas via cfe-commits cfe-commits at lists.llvm.org
Sat Aug 1 04:24:33 PDT 2026


================
@@ -51,10 +101,71 @@ void UseAfterLifetimeEnd::reportDanglingSource(const MemRegion *Source,
       (llvm::Twine("Returning value bound to ") +
        lifetime_modeling::getRegionName(Source) + " that will go out of scope"),
       N);
+
+  if (SourceRange Range = getRegionDeclRange(Source); Range.isValid())
+    BR->addRange(Range);
+
+  BR->addVisitor<UseAfterLifetimeEndBRVisitor>(RetVal, Source);
   bugreporter::trackStoredValue(RetVal, Source, *BR);
   C.emitReport(std::move(BR));
 }
 
+PathDiagnosticPieceRef UseAfterLifetimeEndBRVisitor::createSourcePiece(
+    const ExplodedNode *N, BugReporterContext &BRC, StringRef Message) const {
+  const Stmt *S = N->getStmtForDiagnostics();
+  if (!S)
+    return nullptr;
+
+  const Expr *RetExpr = dyn_cast_or_null<Expr>(S);
+  const Expr *Arg = getLifetimeBoundArg(RetExpr);
+
+  PathDiagnosticLocation Pos;
+
+  if (Arg != nullptr)
+    Pos =
+        PathDiagnosticLocation(Arg, BRC.getSourceManager(), N->getStackFrame());
+  else
+    Pos = PathDiagnosticLocation(S, BRC.getSourceManager(), N->getStackFrame());
----------------
benedekaibas wrote:

I agree. Applied changes here: [a442cb5](https://github.com/llvm/llvm-project/pull/207052/commits/a442cb559fc7cf0ea7edb50c5a65e2d625310099)

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


More information about the cfe-commits mailing list