[clang] [llvm] [clang] Implement lifetime analysis for lifetime_capture_by(X) (PR #115921)

Boaz Brickner via cfe-commits cfe-commits at lists.llvm.org
Mon Nov 18 05:38:00 PST 2024


================
@@ -1110,13 +1117,14 @@ static bool shouldRunGSLAssignmentAnalysis(const Sema &SemaRef,
            isAssignmentOperatorLifetimeBound(Entity.AssignmentOperator)));
 }
 
-static void checkExprLifetimeImpl(Sema &SemaRef,
-                                  const InitializedEntity *InitEntity,
-                                  const InitializedEntity *ExtendingEntity,
-                                  LifetimeKind LK,
-                                  const AssignedEntity *AEntity, Expr *Init) {
-  assert((AEntity && LK == LK_Assignment) ||
-         (InitEntity && LK != LK_Assignment));
+static void
+checkExprLifetimeImpl(Sema &SemaRef, const InitializedEntity *InitEntity,
+                      const InitializedEntity *ExtendingEntity, LifetimeKind LK,
+                      const AssignedEntity *AEntity,
+                      const CapturingEntity *CapEntity, Expr *Init) {
+  assert(!AEntity || LK == LK_Assignment);
+  assert(!CapEntity || LK == LK_LifetimeCapture);
+  assert(!InitEntity || (LK != LK_Assignment && LK != LK_LifetimeCapture));
----------------
bricknerb wrote:

Extracting the TemporaryVisitor and visitLocalsRetained* functions to a dedicated class would only partially help.
I think the logic that checks [[lifetimebound]] should not have access to data that is [[lifetime_capture_by]] specific and vice versa.
We could still reuse the common logic, but checkExprLifetimeImpl() API that tries to handle all cases would make things complex and a single class that tries to handle all cases would be somewhat similar in that sense.

We could do the refactoring in a separate PR before this change to avoid making the old issue bigger.
If you still want to push this change first, are you committed to this refactoring as a follow up?

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


More information about the cfe-commits mailing list