Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 1, 2024

Conversation

Xazax-hun
Copy link
Collaborator

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

Since all callers of the API is called with the same type, we do not
actually need the pointer union.
@Xazax-hun Xazax-hun requested a review from hokein July 1, 2024 17:31
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Jul 1, 2024
@llvmbot
Copy link
Member

llvmbot commented Jul 1, 2024

@llvm/pr-subscribers-clang

Author: Gábor Horváth (Xazax-hun)

Changes

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


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

1 Files Affected:

  • (modified) clang/lib/Sema/CheckExprLifetime.cpp (+13-23)
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

Copy link
Collaborator

@hokein hokein left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks!

@Xazax-hun Xazax-hun merged commit ffca4ef into llvm:main Jul 1, 2024
10 checks passed
lravenclaw pushed a commit to lravenclaw/llvm-project that referenced this pull request Jul 3, 2024
…#97327)

Since all callers of the API is called with the same type, we do not
actually need the pointer union.
kbluck pushed a commit to kbluck/llvm-project that referenced this pull request Jul 6, 2024
…#97327)

Since all callers of the API is called with the same type, we do not
actually need the pointer union.
@Xazax-hun Xazax-hun deleted the remove-ptr-union branch August 15, 2024 13:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants