[llvm-branch-commits] [clang] [analyzer] Only underline the exact parameter that is bound to the return value in UseAfterLifetimeEnd (PR #215651)

Benedek Kaibas via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Wed Aug 12 05:44:52 PDT 2026


================
@@ -43,20 +43,30 @@ class UseAfterLifetimeEndBRVisitor : public BugReporterVisitor {
 
 } // namespace
 
-static const Expr *getLifetimeBoundArg(const Expr *RetExpr) {
+static const Expr *getLifetimeBoundArg(const Expr *RetExpr,
+                                       const MemRegion *Region,
+                                       const ExplodedNode *N) {
   const CallExpr *Expr = dyn_cast_or_null<CallExpr>(RetExpr);
   if (!Expr)
     return nullptr;
+
   const FunctionDecl *FD = Expr->getDirectCallee();
   if (!FD)
     return nullptr;
 
+  const MemRegion *BaseReg = Region->getBaseRegion();
+
   for (const ParmVarDecl *PVD : FD->parameters()) {
-    if (PVD->hasAttr<LifetimeBoundAttr>()) {
-      unsigned Idx = PVD->getFunctionScopeIndex();
-      if (Idx < Expr->getNumArgs())
-        return Expr->getArg(Idx);
-    }
+    if (!PVD->hasAttr<LifetimeBoundAttr>())
+      continue;
+    unsigned Idx = PVD->getFunctionScopeIndex();
+
+    if (Idx >= Expr->getNumArgs())
+      continue;
+
+    const MemRegion *R = N->getSVal(Expr->getArg(Idx)).getAsRegion();
+    if (R && R->getBaseRegion() == BaseReg)
+      return Expr->getArg(Idx);
----------------
benedekaibas wrote:

 I have added the test cases here: [3e273b5](https://github.com/llvm/llvm-project/pull/215651/commits/3e273b5debb2c3a03b2cb131c0cb1c9c196fb2fe)
 
 I know we have agreed on using one test file for better locating the test files, but I think for this given test case it might make sense to create its own test file (and add multiple different tests) since it makes the current test file messy. Since there will be more work on the `BugReporterVisitor` for the checker the test file's size will increase as well.

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


More information about the llvm-branch-commits mailing list