[clang] [LifetimeSafety] Look through parentheses when reporting alias chain notes (PR #240104)
Zhijie Wang via cfe-commits
cfe-commits at lists.llvm.org
Sat Nov 7 23:53:52 PST 2026
https://github.com/aeft created https://github.com/llvm/llvm-project/pull/240104
None
>From 198748303e94d8d624f2f5dd257e146d2213d9ef 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
---
clang/lib/Sema/SemaLifetimeSafety.h | 16 +++++++++++++---
clang/test/Sema/LifetimeSafety/safety.cpp | 4 +---
2 files changed, 14 insertions(+), 6 deletions(-)
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}}
}
More information about the cfe-commits
mailing list