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

via cfe-commits cfe-commits at lists.llvm.org
Sun Nov 8 00:10:41 PST 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang-temporal-safety

Author: Zhijie Wang (aeft)

<details>
<summary>Changes</summary>



---
Full diff: https://github.com/llvm/llvm-project/pull/240104.diff


2 Files Affected:

- (modified) clang/lib/Sema/SemaLifetimeSafety.h (+13-3) 
- (modified) clang/test/Sema/LifetimeSafety/safety.cpp (+1-3) 


``````````diff
diff --git a/clang/lib/Sema/SemaLifetimeSafety.h b/clang/lib/Sema/SemaLifetimeSafety.h
index c2f336bc204178..9a46c5f6f092b7 100644
--- a/clang/lib/Sema/SemaLifetimeSafety.h
+++ b/clang/lib/Sema/SemaLifetimeSafety.h
@@ -15,6 +15,7 @@
 #ifndef LLVM_CLANG_LIB_SEMA_SEMALIFETIMESAFETY_H
 #define LLVM_CLANG_LIB_SEMA_SEMALIFETIMESAFETY_H
 
+#include "clang/AST/IgnoreExpr.h"
 #include "clang/Analysis/Analyses/LifetimeSafety/LifetimeAnnotations.h"
 #include "clang/Analysis/Analyses/LifetimeSafety/LifetimeSafety.h"
 #include "clang/Basic/DiagnosticSema.h"
@@ -760,8 +761,17 @@ class LifetimeSafetySemaHelperImpl final : public LifetimeSafetySemaHelper {
     return Res;
   }
 
+  // Parentheses share origins with their sub-expression (see
+  // OriginManager::getOrCreateList). Unlike IgnoreParenImpCasts(), this keeps
+  // MaterializeTemporaryExpr, which is needed to describe the subject as a
+  // temporary object.
+  static const Expr *ignoreParensAndImpCasts(const Expr *E) {
+    return IgnoreExprNodes(E, IgnoreParensSingleStep,
+                           IgnoreImplicitCastsSingleStep);
+  }
+
   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 +802,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}}
 }

``````````

</details>


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


More information about the cfe-commits mailing list