[clang] [LifetimeSafety] Replace `PointerUnion` in `AccessPath` (PR #193520)
via cfe-commits
cfe-commits at lists.llvm.org
Wed Apr 22 11:50:32 PDT 2026
https://github.com/NeKon69 updated https://github.com/llvm/llvm-project/pull/193520
>From 970e6afa6a2ac751092593d32a719c92b406b93d Mon Sep 17 00:00:00 2001
From: NeKon69 <nobodqwe at gmail.com>
Date: Wed, 22 Apr 2026 18:30:52 +0300
Subject: [PATCH 1/4] placeholder commit
---
clang/include/clang/Analysis/Analyses/LifetimeSafety/Loans.h | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/clang/include/clang/Analysis/Analyses/LifetimeSafety/Loans.h b/clang/include/clang/Analysis/Analyses/LifetimeSafety/Loans.h
index aee6bf9eb69c9..3f1445540c730 100644
--- a/clang/include/clang/Analysis/Analyses/LifetimeSafety/Loans.h
+++ b/clang/include/clang/Analysis/Analyses/LifetimeSafety/Loans.h
@@ -52,8 +52,8 @@ class AccessPath {
private:
Kind K;
- const llvm::PointerUnion<const clang::ValueDecl *,
- const clang::MaterializeTemporaryExpr *,
+ const llvm::PointerUnion<const clang::MaterializeTemporaryExpr *,
+ const CXXNewExpr *, const clang::ValueDecl *,
const ParmVarDecl *, const CXXMethodDecl *>
Root;
>From 6f6d7f5054990d4e69e87a4209b4459358557980 Mon Sep 17 00:00:00 2001
From: NeKon69 <nobodqwe at gmail.com>
Date: Wed, 22 Apr 2026 20:49:32 +0300
Subject: [PATCH 2/4] implement draft
---
.../Analysis/Analyses/LifetimeSafety/Loans.h | 33 ++++++++++---------
1 file changed, 18 insertions(+), 15 deletions(-)
diff --git a/clang/include/clang/Analysis/Analyses/LifetimeSafety/Loans.h b/clang/include/clang/Analysis/Analyses/LifetimeSafety/Loans.h
index 3f1445540c730..9f5e636bb8316 100644
--- a/clang/include/clang/Analysis/Analyses/LifetimeSafety/Loans.h
+++ b/clang/include/clang/Analysis/Analyses/LifetimeSafety/Loans.h
@@ -52,15 +52,13 @@ class AccessPath {
private:
Kind K;
- const llvm::PointerUnion<const clang::MaterializeTemporaryExpr *,
- const CXXNewExpr *, const clang::ValueDecl *,
- const ParmVarDecl *, const CXXMethodDecl *>
- Root;
+ const uintptr_t Root;
public:
- AccessPath(const clang::ValueDecl *D) : K(Kind::ValueDecl), Root(D) {}
+ AccessPath(const clang::ValueDecl *D)
+ : K(Kind::ValueDecl), Root(reinterpret_cast<uintptr_t>(D)) {}
AccessPath(const clang::MaterializeTemporaryExpr *MTE)
- : K(Kind::MaterializeTemporary), Root(MTE) {}
+ : K(Kind::MaterializeTemporary), Root(reinterpret_cast<uintptr_t>(MTE)) {}
static AccessPath Placeholder(const ParmVarDecl *PVD) {
return AccessPath(Kind::PlaceholderParam, PVD);
}
@@ -72,21 +70,24 @@ class AccessPath {
Kind getKind() const { return K; }
const clang::ValueDecl *getAsValueDecl() const {
- return K == Kind::ValueDecl ? Root.dyn_cast<const clang::ValueDecl *>()
- : nullptr;
+ return K == Kind::ValueDecl
+ ? reinterpret_cast<const clang::ValueDecl *>(Root)
+ : nullptr;
}
const clang::MaterializeTemporaryExpr *getAsMaterializeTemporaryExpr() const {
return K == Kind::MaterializeTemporary
- ? Root.dyn_cast<const clang::MaterializeTemporaryExpr *>()
+ ? reinterpret_cast<const clang::MaterializeTemporaryExpr *>(Root)
: nullptr;
}
const ParmVarDecl *getAsPlaceholderParam() const {
- return K == Kind::PlaceholderParam ? Root.dyn_cast<const ParmVarDecl *>()
- : nullptr;
+ return K == Kind::PlaceholderParam
+ ? reinterpret_cast<const ParmVarDecl *>(Root)
+ : nullptr;
}
const CXXMethodDecl *getAsPlaceholderThis() const {
- return K == Kind::PlaceholderThis ? Root.dyn_cast<const CXXMethodDecl *>()
- : nullptr;
+ return K == Kind::PlaceholderThis
+ ? reinterpret_cast<const CXXMethodDecl *>(Root)
+ : nullptr;
}
bool operator==(const AccessPath &RHS) const {
@@ -96,8 +97,10 @@ class AccessPath {
void dump(llvm::raw_ostream &OS) const;
private:
- AccessPath(Kind K, const ParmVarDecl *PVD) : K(K), Root(PVD) {}
- AccessPath(Kind K, const CXXMethodDecl *MD) : K(K), Root(MD) {}
+ AccessPath(Kind K, const ParmVarDecl *PVD)
+ : K(K), Root(reinterpret_cast<uintptr_t>(PVD)) {}
+ AccessPath(Kind K, const CXXMethodDecl *MD)
+ : K(K), Root(reinterpret_cast<uintptr_t>(MD)) {}
};
/// Represents lending a storage location.
>From e2fad57c8c1cba92299ee1b2e08379c0dd263220 Mon Sep 17 00:00:00 2001
From: NeKon69 <nobodqwe at gmail.com>
Date: Wed, 22 Apr 2026 21:48:17 +0300
Subject: [PATCH 3/4] use PointerUnion with Expr* and Decl*
---
.../Analysis/Analyses/LifetimeSafety/Loans.h | 23 +++++++++----------
1 file changed, 11 insertions(+), 12 deletions(-)
diff --git a/clang/include/clang/Analysis/Analyses/LifetimeSafety/Loans.h b/clang/include/clang/Analysis/Analyses/LifetimeSafety/Loans.h
index 9f5e636bb8316..07c72ae251720 100644
--- a/clang/include/clang/Analysis/Analyses/LifetimeSafety/Loans.h
+++ b/clang/include/clang/Analysis/Analyses/LifetimeSafety/Loans.h
@@ -52,13 +52,12 @@ class AccessPath {
private:
Kind K;
- const uintptr_t Root;
+ llvm::PointerUnion<const Expr *, const Decl *> Root;
public:
- AccessPath(const clang::ValueDecl *D)
- : K(Kind::ValueDecl), Root(reinterpret_cast<uintptr_t>(D)) {}
+ AccessPath(const clang::ValueDecl *D) : K(Kind::ValueDecl), Root(D) {}
AccessPath(const clang::MaterializeTemporaryExpr *MTE)
- : K(Kind::MaterializeTemporary), Root(reinterpret_cast<uintptr_t>(MTE)) {}
+ : K(Kind::MaterializeTemporary), Root(MTE) {}
static AccessPath Placeholder(const ParmVarDecl *PVD) {
return AccessPath(Kind::PlaceholderParam, PVD);
}
@@ -71,22 +70,24 @@ class AccessPath {
const clang::ValueDecl *getAsValueDecl() const {
return K == Kind::ValueDecl
- ? reinterpret_cast<const clang::ValueDecl *>(Root)
+ ? cast<clang::ValueDecl>(Root.dyn_cast<const clang::Decl *>())
: nullptr;
}
const clang::MaterializeTemporaryExpr *getAsMaterializeTemporaryExpr() const {
return K == Kind::MaterializeTemporary
- ? reinterpret_cast<const clang::MaterializeTemporaryExpr *>(Root)
+ ? cast<clang::MaterializeTemporaryExpr>(
+ Root.dyn_cast<const clang::Expr *>())
: nullptr;
}
const ParmVarDecl *getAsPlaceholderParam() const {
return K == Kind::PlaceholderParam
- ? reinterpret_cast<const ParmVarDecl *>(Root)
+ ? cast<clang::ParmVarDecl>(Root.dyn_cast<const clang::Decl *>())
: nullptr;
}
const CXXMethodDecl *getAsPlaceholderThis() const {
return K == Kind::PlaceholderThis
- ? reinterpret_cast<const CXXMethodDecl *>(Root)
+ ? cast<clang::CXXMethodDecl>(
+ Root.dyn_cast<const clang::Decl *>())
: nullptr;
}
@@ -97,10 +98,8 @@ class AccessPath {
void dump(llvm::raw_ostream &OS) const;
private:
- AccessPath(Kind K, const ParmVarDecl *PVD)
- : K(K), Root(reinterpret_cast<uintptr_t>(PVD)) {}
- AccessPath(Kind K, const CXXMethodDecl *MD)
- : K(K), Root(reinterpret_cast<uintptr_t>(MD)) {}
+ AccessPath(Kind K, const ParmVarDecl *PVD) : K(K), Root(PVD) {}
+ AccessPath(Kind K, const CXXMethodDecl *MD) : K(K), Root(MD) {}
};
/// Represents lending a storage location.
>From eb2b456758e44a81e6fbabf568ca1fcda6e87e59 Mon Sep 17 00:00:00 2001
From: NeKon69 <nobodqwe at gmail.com>
Date: Wed, 22 Apr 2026 21:50:17 +0300
Subject: [PATCH 4/4] use dyn_cast instead of cast
---
.../clang/Analysis/Analyses/LifetimeSafety/Loans.h | 13 +++++++------
1 file changed, 7 insertions(+), 6 deletions(-)
diff --git a/clang/include/clang/Analysis/Analyses/LifetimeSafety/Loans.h b/clang/include/clang/Analysis/Analyses/LifetimeSafety/Loans.h
index 07c72ae251720..b5c95986e42d7 100644
--- a/clang/include/clang/Analysis/Analyses/LifetimeSafety/Loans.h
+++ b/clang/include/clang/Analysis/Analyses/LifetimeSafety/Loans.h
@@ -70,24 +70,25 @@ class AccessPath {
const clang::ValueDecl *getAsValueDecl() const {
return K == Kind::ValueDecl
- ? cast<clang::ValueDecl>(Root.dyn_cast<const clang::Decl *>())
+ ? dyn_cast<clang::ValueDecl>(Root.dyn_cast<const clang::Decl *>())
: nullptr;
}
const clang::MaterializeTemporaryExpr *getAsMaterializeTemporaryExpr() const {
return K == Kind::MaterializeTemporary
- ? cast<clang::MaterializeTemporaryExpr>(
- Root.dyn_cast<const clang::Expr *>())
+ ? dyn_cast<clang::MaterializeTemporaryExpr>(
+ Root.dyn_cast<const clang::Expr *>())
: nullptr;
}
const ParmVarDecl *getAsPlaceholderParam() const {
return K == Kind::PlaceholderParam
- ? cast<clang::ParmVarDecl>(Root.dyn_cast<const clang::Decl *>())
+ ? dyn_cast<clang::ParmVarDecl>(
+ Root.dyn_cast<const clang::Decl *>())
: nullptr;
}
const CXXMethodDecl *getAsPlaceholderThis() const {
return K == Kind::PlaceholderThis
- ? cast<clang::CXXMethodDecl>(
- Root.dyn_cast<const clang::Decl *>())
+ ? dyn_cast<clang::CXXMethodDecl>(
+ Root.dyn_cast<const clang::Decl *>())
: nullptr;
}
More information about the cfe-commits
mailing list