[clang] [Clang] Extend lifetime bound analysis to support assignments (PR #96475)

Utkarsh Saxena via cfe-commits cfe-commits at lists.llvm.org
Thu Jun 27 15:49:06 PDT 2024


================
@@ -964,11 +966,26 @@ static bool pathOnlyInitializesGslPointer(IndirectLocalPath &Path) {
   return false;
 }
 
-void checkExprLifetime(Sema &SemaRef, const InitializedEntity &Entity,
+void checkExprLifetime(Sema &SemaRef, const CheckingEntity &CEntity,
                        Expr *Init) {
-  LifetimeResult LR = getEntityLifetime(&Entity);
-  LifetimeKind LK = LR.getInt();
-  const InitializedEntity *ExtendingEntity = LR.getPointer();
+  LifetimeKind LK = LK_FullExpression;
+
+  const AssignedEntity *AEntity = nullptr;
+  // Local variables for initialized entity.
+  const InitializedEntity *InitEntity = nullptr;
+  const InitializedEntity *ExtendingEntity = nullptr;
+  if (auto IEntityP = std::get_if<const InitializedEntity *>(&CEntity)) {
+    InitEntity = *IEntityP;
+    auto LTResult = getEntityLifetime(InitEntity);
+    LK = LTResult.getInt();
+    ExtendingEntity = LTResult.getPointer();
+  } else if (auto AEntityP = std::get_if<const AssignedEntity *>(&CEntity)) {
+    AEntity = *AEntityP;
+    if (AEntity->LHS->getType()->isPointerType()) // builtin pointer type
+      LK = LK_Extended;
+  } else {
+    llvm_unreachable("unexpected kind");
+  }
 
   // If this entity doesn't have an interesting lifetime, don't bother looking
   // for temporaries within its initializer.
----------------
usx95 wrote:

Another good refactoring would be to make `TemporaryVisitor`, `visitLocalsRetainedByReferenceBinding`, `visitLocalsRetainedByInitializer` as member functions of a separate class, say, `CheckLifetimeVisitor`. There share a lot of data like `IndirectLocalPath &Path`, `LocalVisitor Visit`. `Path` and `LocalVisitor` could be member var and member function respectively.

(This is again not relevant and could be a separate patch).

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


More information about the cfe-commits mailing list