================
@@ -38,21 +61,83 @@ void UseAfterLifetimeEnd::checkEndFunction(const ReturnStmt 
*RS,
   if (ExplodedNode *N =
           C.generateNonFatalErrorNode(State, C.getPredecessor())) {
     for (const MemRegion *R : RetValRegion)
-      reportDanglingSource(R, N, C);
+      reportDanglingSource(R, RetVal, N, C);
   }
 }
 
+static SourceRange getRegionDeclRange(const MemRegion *Source) {
+  if (const auto *VR = dyn_cast<VarRegion>(Source))
+    return VR->getDecl()->getSourceRange();
+  return SourceRange();
+}
+
 void UseAfterLifetimeEnd::reportDanglingSource(const MemRegion *Source,
-                                               ExplodedNode *N,
+                                               SVal Val, ExplodedNode *N,
                                                CheckerContext &C) const {
   auto BR = std::make_unique<PathSensitiveBugReport>(
       BugMsg,
       (llvm::Twine("Returning value bound to '") + Source->getString() +
        "' that will go out of scope"),
       N);
+
+  if (SourceRange Range = getRegionDeclRange(Source); Range.isValid())
+    BR->addRange(Range);
+
+  BR->addVisitor<UseAfterLifetimeEndBRVisitor>(Val, Source);
   C.emitReport(std::move(BR));
 }
 
+PathDiagnosticPieceRef
+UseAfterLifetimeEndBRVisitor::VisitNode(const ExplodedNode *N,
+                                        BugReporterContext &BRC,
+                                        PathSensitiveBugReport &BR) {
+  const ExplodedNode *Pred = N->getFirstPred();
+  if (!Pred)
+    return nullptr;
+
+  if (!lifetime_modeling::isBoundToLifetimeSource(N->getState(), BoundVal) ||
+      lifetime_modeling::isBoundToLifetimeSource(Pred->getState(), BoundVal))
+    return nullptr;
+
+  const Stmt *S = N->getStmtForDiagnostics();
+  if (!S)
+    return nullptr;
+
+  PathDiagnosticLocation Pos(S, BRC.getSourceManager(), N->getStackFrame());
+  auto Piece = std::make_shared<PathDiagnosticEventPiece>(
+      Pos,
+      (llvm::Twine("Value bound to '") + SourceRegion->getString() + "' here")
----------------
benedekaibas wrote:

Resolved here: 
[a761c5a](https://github.com/llvm/llvm-project/pull/207052/commits/a761c5ab469d8878cb93f7e9d064fc6256dca057)

https://github.com/llvm/llvm-project/pull/207052
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to