[clang] [clang] Remove a pointer union from the lifetime bound analysis (PR #97327)

Gábor Horváth via cfe-commits cfe-commits at lists.llvm.org
Mon Jul 1 10:32:00 PDT 2024


https://github.com/Xazax-hun created https://github.com/llvm/llvm-project/pull/97327

Since all callers of the API is called with the same type, we do not actually need the pointer union.

>From 48f51032cb9228ea8704e4e2352b18ea182e1024 Mon Sep 17 00:00:00 2001
From: Gabor Horvath <gaborh at apple.com>
Date: Mon, 1 Jul 2024 18:29:34 +0100
Subject: [PATCH] [clang] Remove a pointer union from the lifetime bound
 analysis

Since all callers of the API is called with the same type, we do not
actually need the pointer union.
---
 clang/lib/Sema/CheckExprLifetime.cpp | 36 ++++++++++------------------
 1 file changed, 13 insertions(+), 23 deletions(-)

diff --git a/clang/lib/Sema/CheckExprLifetime.cpp b/clang/lib/Sema/CheckExprLifetime.cpp
index be77949e8b547..fea0239f7bc36 100644
--- a/clang/lib/Sema/CheckExprLifetime.cpp
+++ b/clang/lib/Sema/CheckExprLifetime.cpp
@@ -966,27 +966,11 @@ static bool pathOnlyInitializesGslPointer(IndirectLocalPath &Path) {
   return false;
 }
 
-static void checkExprLifetimeImpl(
-    Sema &SemaRef,
-    llvm::PointerUnion<const InitializedEntity *, const AssignedEntity *>
-        CEntity,
-    Expr *Init) {
-  LifetimeKind LK = LK_FullExpression;
-
-  const AssignedEntity *AEntity = nullptr;
-  // Local variables for initialized entity.
-  const InitializedEntity *InitEntity = nullptr;
-  const InitializedEntity *ExtendingEntity = nullptr;
-  if (CEntity.is<const InitializedEntity *>()) {
-    InitEntity = CEntity.get<const InitializedEntity *>();
-    auto LTResult = getEntityLifetime(InitEntity);
-    LK = LTResult.getInt();
-    ExtendingEntity = LTResult.getPointer();
-  } else {
-    AEntity = CEntity.get<const AssignedEntity *>();
-    if (AEntity->LHS->getType()->isPointerType()) // builtin pointer type
-      LK = LK_Extended;
-  }
+static void checkExprLifetimeImpl(Sema &SemaRef,
+                                  const InitializedEntity *InitEntity,
+                                  const InitializedEntity *ExtendingEntity,
+                                  LifetimeKind LK,
+                                  const AssignedEntity *AEntity, Expr *Init) {
   // If this entity doesn't have an interesting lifetime, don't bother looking
   // for temporaries within its initializer.
   if (LK == LK_FullExpression)
@@ -1292,12 +1276,18 @@ static void checkExprLifetimeImpl(
 
 void checkExprLifetime(Sema &SemaRef, const InitializedEntity &Entity,
                        Expr *Init) {
-  checkExprLifetimeImpl(SemaRef, &Entity, Init);
+  auto LTResult = getEntityLifetime(&Entity);
+  LifetimeKind LK = LTResult.getInt();
+  const InitializedEntity *ExtendingEntity = LTResult.getPointer();
+  checkExprLifetimeImpl(SemaRef, &Entity, ExtendingEntity, LK, nullptr, Init);
 }
 
 void checkExprLifetime(Sema &SemaRef, const AssignedEntity &Entity,
                        Expr *Init) {
-  checkExprLifetimeImpl(SemaRef, &Entity, Init);
+  LifetimeKind LK = LK_FullExpression;
+  if (Entity.LHS->getType()->isPointerType()) // builtin pointer type
+    LK = LK_Extended;
+  checkExprLifetimeImpl(SemaRef, nullptr, nullptr, LK, &Entity, Init);
 }
 
 } // namespace clang::sema



More information about the cfe-commits mailing list