[clang] [LifetimeSafety] Look through parentheses when reporting alias chain notes (PR #240104)

Zhijie Wang via cfe-commits cfe-commits at lists.llvm.org
Sun Nov 8 10:37:57 PST 2026


https://github.com/aeft updated https://github.com/llvm/llvm-project/pull/240104

>From 95af030ef8c85e5207a65d1eabba7feef1c0cb11 Mon Sep 17 00:00:00 2001
From: Zhijie Wang <yesterda9 at gmail.com>
Date: Sat, 7 Nov 2026 23:52:50 -0800
Subject: [PATCH] [LifetimeSafety] Look through parentheses when reporting
 alias chain notes

---
 .../Analysis/Analyses/LifetimeSafety/LifetimeAnnotations.h  | 4 ++++
 clang/lib/Analysis/LifetimeSafety/LifetimeAnnotations.cpp   | 6 ++++++
 clang/lib/Sema/SemaLifetimeSafety.h                         | 6 +++---
 clang/test/Sema/LifetimeSafety/safety.cpp                   | 4 +---
 4 files changed, 14 insertions(+), 6 deletions(-)

diff --git a/clang/include/clang/Analysis/Analyses/LifetimeSafety/LifetimeAnnotations.h b/clang/include/clang/Analysis/Analyses/LifetimeSafety/LifetimeAnnotations.h
index 96e502b60c489b..6f9255311d9496 100644
--- a/clang/include/clang/Analysis/Analyses/LifetimeSafety/LifetimeAnnotations.h
+++ b/clang/include/clang/Analysis/Analyses/LifetimeSafety/LifetimeAnnotations.h
@@ -162,6 +162,10 @@ bool isStdCallableWrapperType(const CXXRecordDecl *RD);
 /// argument to result.
 bool isStdReferenceCast(const FunctionDecl *FD);
 
+/// Skips parentheses and implicit casts. Unlike IgnoreParenImpCasts(), stops at
+/// a MaterializeTemporaryExpr, which owns the storage of a temporary.
+const Expr *ignoreParensAndImpCasts(const Expr *E);
+
 } // namespace clang::lifetimes
 
 #endif // LLVM_CLANG_ANALYSIS_ANALYSES_LIFETIMEANNOTATIONS_H
diff --git a/clang/lib/Analysis/LifetimeSafety/LifetimeAnnotations.cpp b/clang/lib/Analysis/LifetimeSafety/LifetimeAnnotations.cpp
index 99edabb20d6182..dd00ff4f5ccb61 100644
--- a/clang/lib/Analysis/LifetimeSafety/LifetimeAnnotations.cpp
+++ b/clang/lib/Analysis/LifetimeSafety/LifetimeAnnotations.cpp
@@ -13,6 +13,7 @@
 #include "clang/AST/DeclTemplate.h"
 #include "clang/AST/Expr.h"
 #include "clang/AST/ExprCXX.h"
+#include "clang/AST/IgnoreExpr.h"
 #include "clang/AST/Type.h"
 #include "clang/AST/TypeLoc.h"
 #include "clang/Analysis/AnyCall.h"
@@ -595,4 +596,9 @@ bool isStdReferenceCast(const FunctionDecl *FD) {
   }
 }
 
+const Expr *ignoreParensAndImpCasts(const Expr *E) {
+  return IgnoreExprNodes(E, IgnoreParensSingleStep,
+                         IgnoreImplicitCastsSingleStep);
+}
+
 } // namespace clang::lifetimes
diff --git a/clang/lib/Sema/SemaLifetimeSafety.h b/clang/lib/Sema/SemaLifetimeSafety.h
index c2f336bc204178..3495fc63e790be 100644
--- a/clang/lib/Sema/SemaLifetimeSafety.h
+++ b/clang/lib/Sema/SemaLifetimeSafety.h
@@ -761,7 +761,7 @@ class LifetimeSafetySemaHelperImpl final : public LifetimeSafetySemaHelper {
   }
 
   std::string getDiagSubjectDescription(const Expr *E) {
-    E = E->IgnoreImpCasts();
+    E = ignoreParensAndImpCasts(E);
     if (const auto *DAE = dyn_cast<CXXDefaultArgExpr>(E))
       return getDiagSubjectDescription(DAE->getExpr()->IgnoreParenBaseCasts());
     if (isa<MaterializeTemporaryExpr>(E))
@@ -792,8 +792,8 @@ class LifetimeSafetySemaHelperImpl final : public LifetimeSafetySemaHelper {
   }
 
   bool shouldShowInAliasChain(const Expr *CurrExpr, const Expr *LastExpr) {
-    CurrExpr = CurrExpr->IgnoreImpCasts();
-    LastExpr = LastExpr->IgnoreImpCasts();
+    CurrExpr = ignoreParensAndImpCasts(CurrExpr);
+    LastExpr = ignoreParensAndImpCasts(LastExpr);
 
     if (!isa<CallExpr, DeclRefExpr>(CurrExpr))
       return false;
diff --git a/clang/test/Sema/LifetimeSafety/safety.cpp b/clang/test/Sema/LifetimeSafety/safety.cpp
index cd6578c0796656..e3e228233f7988 100644
--- a/clang/test/Sema/LifetimeSafety/safety.cpp
+++ b/clang/test/Sema/LifetimeSafety/safety.cpp
@@ -1453,14 +1453,12 @@ int **return_multilevel() {
 }
 } // namespace unary_plus
 
-// FIXME: Diagnostic output does not handle ParenExpr correctly, causing alias
-// information to be missed (local variable 'p' aliases the storage of local variable 'b').
 void simpleparen() {
   MyObj* p;
   {
     MyObj a;
     MyObj* b = &a;  // expected-warning {{local variable 'a' does not live long enough}}
-    p = (((b)));
+    p = (((b)));    // expected-note {{local variable 'b' aliases the storage of local variable 'a'}}
   }                 // expected-note {{local variable 'a' is destroyed here}}
   use(*p);          // expected-note {{later used here}}
 }



More information about the cfe-commits mailing list