[clang] [analyzer] Remove getRegionName from LifetimeModeling and its dependent checkers (PR #214245)

Benedek Kaibas via cfe-commits cfe-commits at lists.llvm.org
Wed Aug 5 08:03:56 PDT 2026


https://github.com/benedekaibas created https://github.com/llvm/llvm-project/pull/214245

In #211552 `MemRegion::getDescriptiveName` got generalized and now has an `AllowFallback` functionality which makes it possible to remove `getRegionName()` from the modeling checker and from both of the dependent checkers (`UseAfterLifetimeEnd` and `DanglingPtrDeref`).

>From 4548ef138dfb6c1e2e0db3f43bfd947a64eb4f8a Mon Sep 17 00:00:00 2001
From: benedekaibas <kaibas01 at allegheny.edu>
Date: Wed, 5 Aug 2026 16:43:37 +0200
Subject: [PATCH] [analyzer] Remove getRegionName.

---
 .../Checkers/DanglingPtrDeref.cpp             |  6 ++++--
 .../Checkers/LifetimeModeling.cpp             |  8 --------
 .../Checkers/LifetimeModeling.h               |  4 ----
 .../Checkers/UseAfterLifetimeEnd.cpp          | 19 ++++++++++++-------
 4 files changed, 16 insertions(+), 21 deletions(-)

diff --git a/clang/lib/StaticAnalyzer/Checkers/DanglingPtrDeref.cpp b/clang/lib/StaticAnalyzer/Checkers/DanglingPtrDeref.cpp
index bd4cd864cb768..a34bf20eebafb 100644
--- a/clang/lib/StaticAnalyzer/Checkers/DanglingPtrDeref.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/DanglingPtrDeref.cpp
@@ -71,7 +71,8 @@ void DanglingPtrDeref::reportUseAfterScope(const MemRegion *Region,
                                            CheckerContext &C) const {
   auto BR = std::make_unique<PathSensitiveBugReport>(
       BugMsg,
-      (llvm::Twine("Use of ") + lifetime_modeling::getRegionName(Region) +
+      (llvm::Twine("Use of ") +
+       Region->getDescriptiveName(/*UseQuotes=*/true, /*AllowFallback=*/true) +
        " after its lifetime ended."),
       N);
   BR->addVisitor<DanglingPtrDerefBRVisitor>(Region);
@@ -103,7 +104,8 @@ DanglingPtrDerefBRVisitor::VisitNode(const ExplodedNode *N,
       S, BRC.getSourceManager(), N->getStackFrame());
   return std::make_shared<PathDiagnosticEventPiece>(
       Pos,
-      (lifetime_modeling::getRegionName(SourceRegion) +
+      (SourceRegion->getDescriptiveName(/*UseQuotes=*/true,
+                                        /*AllowFallback=*/true) +
        llvm::Twine(" is destroyed here"))
           .str(),
       true);
diff --git a/clang/lib/StaticAnalyzer/Checkers/LifetimeModeling.cpp b/clang/lib/StaticAnalyzer/Checkers/LifetimeModeling.cpp
index 7b9fb7acb21ab..85ea496cdfb0e 100644
--- a/clang/lib/StaticAnalyzer/Checkers/LifetimeModeling.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/LifetimeModeling.cpp
@@ -92,14 +92,6 @@ static ProgramStateRef bindSource(ProgramStateRef State, SVal RetVal,
   return State;
 }
 
-std::string lifetime_modeling::getRegionName(const MemRegion *Reg) {
-  // FIXME: Once the checker supports heap allocation, more region kinds
-  // should be handled to produce the correct descriptive name.
-  if (const std::string RegName = Reg->getDescriptiveName(); !RegName.empty())
-    return RegName;
-  return "the region";
-}
-
 void LifetimeModeling::checkPostCall(const CallEvent &Call,
                                      CheckerContext &C) const {
   ProgramStateRef State = C.getState();
diff --git a/clang/lib/StaticAnalyzer/Checkers/LifetimeModeling.h b/clang/lib/StaticAnalyzer/Checkers/LifetimeModeling.h
index 8d6c8e4882d1c..747bd8d6fd74c 100644
--- a/clang/lib/StaticAnalyzer/Checkers/LifetimeModeling.h
+++ b/clang/lib/StaticAnalyzer/Checkers/LifetimeModeling.h
@@ -18,10 +18,6 @@ bool isDeallocated(ProgramStateRef State, const MemRegion *Region);
 
 /// Returns true if \p Val is a key in the LifetimeBoundMap.
 bool isBoundToLifetimeSource(ProgramStateRef State, SVal Val);
-
-/// Returns the descriptive name of the memory region or a placeholder if a
-/// descriptive name cannot be constructed for it.
-std::string getRegionName(const MemRegion *Reg);
 } // namespace clang::ento::lifetime_modeling
 
 #endif // LLVM_CLANG_LIB_STATICANALYZER_CHECKERS_LIFETIMEMODELING_H
diff --git a/clang/lib/StaticAnalyzer/Checkers/UseAfterLifetimeEnd.cpp b/clang/lib/StaticAnalyzer/Checkers/UseAfterLifetimeEnd.cpp
index a9065352adae6..c36f986c5e3b8 100644
--- a/clang/lib/StaticAnalyzer/Checkers/UseAfterLifetimeEnd.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/UseAfterLifetimeEnd.cpp
@@ -99,7 +99,8 @@ void UseAfterLifetimeEnd::reportDanglingSource(const MemRegion *Source,
   auto BR = std::make_unique<PathSensitiveBugReport>(
       BugMsg,
       (llvm::Twine("Returning value bound to ") +
-       lifetime_modeling::getRegionName(Source) + " that will go out of scope"),
+       Source->getDescriptiveName(/*UseQuotes=*/true, /*AllowFallback=*/true) +
+       " that will go out of scope"),
       N);
 
   if (SourceRange Range = getRegionDeclRange(Source); Range.isValid())
@@ -146,7 +147,9 @@ UseAfterLifetimeEndBRVisitor::VisitNode(const ExplodedNode *N,
   auto Piece = createSourcePiece(
       N, BRC,
       (llvm::Twine("Value's lifetime bound to the lifetime of ") +
-       lifetime_modeling::getRegionName(SourceRegion) + " here")
+       SourceRegion->getDescriptiveName(/*UseQuotes=*/true,
+                                        /*AllowFallback=*/true) +
+       " here")
           .str());
   return Piece;
 }
@@ -155,11 +158,13 @@ PathDiagnosticPieceRef
 UseAfterLifetimeEndBRVisitor::getEndPath(const ExplodedNode *N,
                                          BugReporterContext &BRC,
                                          PathSensitiveBugReport &BR) {
-  auto Piece = createSourcePiece(
-      N, BRC,
-      (llvm::Twine("Lifetime of ") +
-       lifetime_modeling::getRegionName(SourceRegion) + " ended here")
-          .str());
+  auto Piece =
+      createSourcePiece(N, BRC,
+                        (llvm::Twine("Lifetime of ") +
+                         SourceRegion->getDescriptiveName(
+                             /*UseQuotes=*/true, /*AllowFallback=*/true) +
+                         " ended here")
+                            .str());
   return Piece;
 }
 



More information about the cfe-commits mailing list