https://github.com/benedekaibas updated https://github.com/llvm/llvm-project/pull/243630
>From eb7281bd085d9b2dd4139d6f34c164784813ba18 Mon Sep 17 00:00:00 2001 From: benedekaibas <[email protected]> Date: Tue, 17 Nov 2026 21:33:09 +0100 Subject: [PATCH 1/5] [clang] Fix CFG elements order when AddLifetime is enabled --- clang/lib/Analysis/CFG.cpp | 42 ++++------ .../correct-order-of-CFG-elements.cpp | 78 +++++++++++++++++++ clang/test/Analysis/lifetime-cfg-output.cpp | 20 ++--- 3 files changed, 102 insertions(+), 38 deletions(-) create mode 100644 clang/test/Analysis/correct-order-of-CFG-elements.cpp diff --git a/clang/lib/Analysis/CFG.cpp b/clang/lib/Analysis/CFG.cpp index 1c69f5d71583f5..4e67ba27cd6f93 100644 --- a/clang/lib/Analysis/CFG.cpp +++ b/clang/lib/Analysis/CFG.cpp @@ -1993,14 +1993,12 @@ void CFGBuilder::addAutomaticObjDestruction(LocalScope::const_iterator B, return; SmallVector<VarDecl *, 10> DeclsNeedDestruction; - DeclsNeedDestruction.reserve(B.distance(E)); - - for (VarDecl* D : llvm::make_range(B, E)) - if (needsAutomaticDestruction(D)) - DeclsNeedDestruction.push_back(D); + for (VarDecl *VD : llvm::make_range(B, E)) + // if (needsAutomaticDestruction(VD)) + DeclsNeedDestruction.push_back(VD); for (VarDecl *VD : llvm::reverse(DeclsNeedDestruction)) { - if (BuildOpts.AddImplicitDtors) { + if (BuildOpts.AddImplicitDtors && needsAutomaticDestruction(VD)) { // If this destructor is marked as a no-return destructor, we need to // create a new block for the destructor which does not have as a // successor anything built thus far: control won't flow out of this @@ -2019,7 +2017,16 @@ void CFGBuilder::addAutomaticObjDestruction(LocalScope::const_iterator B, // Add LifetimeEnd after automatic obj with non-trivial destructors, // as they end their lifetime when the destructor returns. For trivial - // objects, we end lifetime with scope end. + // objects, we end lifetime with scope end. Since the SCOPE_END CFG + // marker has the value of LifetimeEnds, the LifetimeEnds marker also + // covers trivial destructors and local variables. See #201123. + // Egyelore legyen ez a komment, de meg kene nezni, h nem e helyesebb + // ha a trivialis destruktorokat kulon az exit handling-es fuggvenyben + // kezelem. Logikatlan, hogy a trivialis destruktort is egy olyan fuggv + // kezel, aminek feladata az automatic object destruction. Viszont ha + // kulon akarom kezelni akkor megint kell a helper fuggveny, ami megadja + // az osszes VarDecl-t forditott sorrendben a ket fuggveny szamara. + // Kitorolni es a PR-ban ezt leirni!!!!! if (BuildOpts.AddLifetime) appendLifetimeEnds(Block, VD, S); if (BuildOpts.AddImplicitDtors && !hasTrivialDestructor(VD)) @@ -2044,27 +2051,6 @@ void CFGBuilder::addScopeExitHandling(LocalScope::const_iterator B, autoCreateBlock(); appendScopeEnd(Block, B.getFirstVarInScope(), S); } - - if (!BuildOpts.AddLifetime) - return; - - // We need to perform the scope leaving in reverse order - SmallVector<VarDecl *, 10> DeclsTrivial; - DeclsTrivial.reserve(B.distance(E)); - - // Objects with trivial destructor ends their lifetime when their storage - // is destroyed, for automatic variables, this happens when the end of the - // scope is added. - for (VarDecl* D : llvm::make_range(B, E)) - if (!needsAutomaticDestruction(D)) - DeclsTrivial.push_back(D); - - if (DeclsTrivial.empty()) - return; - - autoCreateBlock(); - for (VarDecl *VD : llvm::reverse(DeclsTrivial)) - appendLifetimeEnds(Block, VD, S); } /// addScopeChangesHandling - appends information about destruction, lifetime diff --git a/clang/test/Analysis/correct-order-of-CFG-elements.cpp b/clang/test/Analysis/correct-order-of-CFG-elements.cpp new file mode 100644 index 00000000000000..d0ba8b79bc9ac4 --- /dev/null +++ b/clang/test/Analysis/correct-order-of-CFG-elements.cpp @@ -0,0 +1,78 @@ +// RUN: %clang_analyze_cc1 -fcxx-exceptions -fexceptions -analyzer-checker=debug.DumpCFG -analyzer-config cfg-lifetime=true,cfg-scopes=true %s > %t 2>&1 +// RUN: FileCheck --input-file=%t %s + +// Check the correct order of the CFG elements when AddLifetime is enabled. +// The CFG elements in a basic block should appear in reverse order of +// declaration. This test file also enabled cfg-scopes to ensure that +// none of the elements appear as a duplicate in the give basic block. + +struct NonTrivial { + ~NonTrivial(); +}; + +struct Trivial { + int x; +}; + +// CHECK: [B2 (ENTRY)] +// CHECK-NEXT: Succs (1): B1 +// CHECK: [B1] +// CHECK-NEXT: 1: CFGScopeBegin(a) +// CHECK-NEXT: 2: (CXXConstructExpr, [B1.3], NonTrivial) +// CHECK-NEXT: 3: NonTrivial a; +// CHECK-NEXT: 4: 0 +// CHECK-NEXT: 5: int i = 0; +// CHECK-NEXT: 6: (CXXConstructExpr, [B1.7], NonTrivial) +// CHECK-NEXT: 7: NonTrivial b; +// CHECK-NEXT: 8: (CXXConstructExpr, [B1.9], Trivial) +// CHECK-NEXT: 9: Trivial u; +// CHECK-NEXT: 10: [B1.9] (Lifetime ends) +// CHECK-NEXT: 11: [B1.7].~NonTrivial() (Implicit destructor) +// CHECK-NEXT: 12: [B1.7] (Lifetime ends) +// CHECK-NEXT: 13: [B1.5] (Lifetime ends) +// CHECK-NEXT: 14: [B1.3].~NonTrivial() (Implicit destructor) +// CHECK-NEXT: 15: [B1.3] (Lifetime ends) +// CHECK-NEXT: 16: CFGScopeEnd(a) +// CHECK-NEXT: Preds (1): B2 +// CHECK-NEXT: Succs (1): B0 +// CHECK: [B0 (EXIT)] +// CHECK-NEXT: Preds (1): B1 +void test_trivial_and_nontrivial_order() { + NonTrivial a; + int i = 0; + NonTrivial b; + Trivial u; +} + +// CHECK: [B2 (ENTRY)] +// CHECK-NEXT: Succs (1): B1 +// CHECK: [B1] +// CHECK-NEXT: 1: CFGScopeBegin(a) +// CHECK-NEXT: 2: (CXXConstructExpr, [B1.3], A) +// CHECK-NEXT: 3: A a; +// CHECK-NEXT: 4: CFGScopeBegin(c) +// CHECK-NEXT: 5: (CXXConstructExpr, [B1.6], A) +// CHECK-NEXT: 6: A c; +// CHECK-NEXT: 7: (CXXConstructExpr, [B1.8], A) +// CHECK-NEXT: 8: A d; +// CHECK-NEXT: 9: [B1.8] (Lifetime ends) +// CHECK-NEXT: 10: [B1.6] (Lifetime ends) +// CHECK-NEXT: 11: CFGScopeEnd(c) +// CHECK-NEXT: 12: (CXXConstructExpr, [B1.13], A) +// CHECK-NEXT: 13: A b; +// CHECK-NEXT: 14: [B1.13] (Lifetime ends) +// CHECK-NEXT: 15: [B1.3] (Lifetime ends) +// CHECK-NEXT: 16: CFGScopeEnd(a) +// CHECK-NEXT: Preds (1): B2 +// CHECK-NEXT: Succs (1): B0 +// CHECK: [B0 (EXIT)] +// CHECK-NEXT: Preds (1): B1 +struct A {}; +void test_nested_scope() { + A a; + { + A c; + A d; + } + A b; +} diff --git a/clang/test/Analysis/lifetime-cfg-output.cpp b/clang/test/Analysis/lifetime-cfg-output.cpp index 0a75c5bcc0bcc9..df8d7549a5de7f 100644 --- a/clang/test/Analysis/lifetime-cfg-output.cpp +++ b/clang/test/Analysis/lifetime-cfg-output.cpp @@ -77,9 +77,9 @@ class A { // CHECK-NEXT: 10: const A &c = A(); // CHECK-NEXT: 11: [B1.10].~A() (Implicit destructor) // CHECK-NEXT: 12: [B1.10] (Lifetime ends) -// CHECK-NEXT: 13: [B1.2].~A() (Implicit destructor) -// CHECK-NEXT: 14: [B1.2] (Lifetime ends) -// CHECK-NEXT: 15: [B1.5] (Lifetime ends) +// CHECK-NEXT: 13: [B1.5] (Lifetime ends) +// CHECK-NEXT: 14: [B1.2].~A() (Implicit destructor) +// CHECK-NEXT: 15: [B1.2] (Lifetime ends) // CHECK-NEXT: Preds (1): B2 // CHECK-NEXT: Succs (1): B0 // CHECK: [B0 (EXIT)] @@ -97,11 +97,11 @@ void test_const_ref() { // CHECK-NEXT: 2: A a[2]; // CHECK-NEXT: 3: (CXXConstructExpr, [B1.4], A[0]) // CHECK-NEXT: 4: A b[0]; -// CHECK-NEXT: 5: [B1.2].~A[2]() (Implicit destructor) -// lifetime of a ends when its destructors are run -// CHECK-NEXT: 6: [B1.2] (Lifetime ends) // lifetime of b ends when its storage duration ends -// CHECK-NEXT: 7: [B1.4] (Lifetime ends) +// CHECK-NEXT: 5: [B1.4] (Lifetime ends) +// CHECK-NEXT: 6: [B1.2].~A[2]() (Implicit destructor) +// lifetime of a ends when its destructors are run +// CHECK-NEXT: 7: [B1.2] (Lifetime ends) // CHECK-NEXT: Preds (1): B2 // CHECK-NEXT: Succs (1): B0 // CHECK: [B0 (EXIT)] @@ -814,9 +814,9 @@ void test_for_inc_conditional() { // CHECK-NEXT: 6: a // CHECK-NEXT: 7: [B1.6].p // CHECK-NEXT: 8: [B1.7] = [B1.5] -// CHECK-NEXT: 9: [B1.2].~A() (Implicit destructor) -// CHECK-NEXT: 10: [B1.2] (Lifetime ends) -// CHECK-NEXT: 11: [B1.3] (Lifetime ends) +// CHECK-NEXT: 9: [B1.3] (Lifetime ends) +// CHECK-NEXT: 10: [B1.2].~A() (Implicit destructor) +// CHECK-NEXT: 11: [B1.2] (Lifetime ends) // CHECK-NEXT: Preds (1): B2 // CHECK-NEXT: Succs (1): B0 // CHECK: [B0 (EXIT)] >From c526c8ed02ae061f83a6c6f08af60ec63c353849 Mon Sep 17 00:00:00 2001 From: benedekaibas <[email protected]> Date: Tue, 17 Nov 2026 23:06:12 +0100 Subject: [PATCH 2/5] Remove comment that was left from implementation --- clang/lib/Analysis/CFG.cpp | 8 -------- 1 file changed, 8 deletions(-) diff --git a/clang/lib/Analysis/CFG.cpp b/clang/lib/Analysis/CFG.cpp index 4e67ba27cd6f93..94126730b689e3 100644 --- a/clang/lib/Analysis/CFG.cpp +++ b/clang/lib/Analysis/CFG.cpp @@ -1994,7 +1994,6 @@ void CFGBuilder::addAutomaticObjDestruction(LocalScope::const_iterator B, SmallVector<VarDecl *, 10> DeclsNeedDestruction; for (VarDecl *VD : llvm::make_range(B, E)) - // if (needsAutomaticDestruction(VD)) DeclsNeedDestruction.push_back(VD); for (VarDecl *VD : llvm::reverse(DeclsNeedDestruction)) { @@ -2020,13 +2019,6 @@ void CFGBuilder::addAutomaticObjDestruction(LocalScope::const_iterator B, // objects, we end lifetime with scope end. Since the SCOPE_END CFG // marker has the value of LifetimeEnds, the LifetimeEnds marker also // covers trivial destructors and local variables. See #201123. - // Egyelore legyen ez a komment, de meg kene nezni, h nem e helyesebb - // ha a trivialis destruktorokat kulon az exit handling-es fuggvenyben - // kezelem. Logikatlan, hogy a trivialis destruktort is egy olyan fuggv - // kezel, aminek feladata az automatic object destruction. Viszont ha - // kulon akarom kezelni akkor megint kell a helper fuggveny, ami megadja - // az osszes VarDecl-t forditott sorrendben a ket fuggveny szamara. - // Kitorolni es a PR-ban ezt leirni!!!!! if (BuildOpts.AddLifetime) appendLifetimeEnds(Block, VD, S); if (BuildOpts.AddImplicitDtors && !hasTrivialDestructor(VD)) >From 45af3968f00b4bd6160640134cd448ce4de67fdb Mon Sep 17 00:00:00 2001 From: benedekaibas <[email protected]> Date: Tue, 17 Nov 2026 23:17:23 +0100 Subject: [PATCH 3/5] Update the doc comment of the functions. --- clang/lib/Analysis/CFG.cpp | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/clang/lib/Analysis/CFG.cpp b/clang/lib/Analysis/CFG.cpp index 94126730b689e3..703350f2cf4b90 100644 --- a/clang/lib/Analysis/CFG.cpp +++ b/clang/lib/Analysis/CFG.cpp @@ -1981,8 +1981,8 @@ void CFGBuilder::addAutomaticObjHandling(LocalScope::const_iterator B, } /// Add CFG elements corresponding to call destructor and end of lifetime -/// of all automatic variables with non-trivial destructor in range [B, E). -/// This include AutomaticObjectDtor and LifetimeEnds elements. +/// of all automatic variables with trivial and non-trivial destructors in +/// range [B, E). This includes AutomaticObjectDtor and LifetimeEnds elements. void CFGBuilder::addAutomaticObjDestruction(LocalScope::const_iterator B, LocalScope::const_iterator E, Stmt *S) { @@ -2028,11 +2028,8 @@ void CFGBuilder::addAutomaticObjDestruction(LocalScope::const_iterator B, } } -/// Add CFG elements corresponding to leaving a scope. -/// Assumes that range [B, E) corresponds to single scope. -/// This add following elements: -/// * LifetimeEnds for all variables with non-trivial destructor -/// * ScopeEnd for each scope left +/// Add ScopeEnd CFG elements for each element in the CFG leaving a +/// local scope. Assumes that range [B, E) corresponds to single scope. void CFGBuilder::addScopeExitHandling(LocalScope::const_iterator B, LocalScope::const_iterator E, Stmt *S) { assert(!B.inSameLocalScope(E)); >From ccb0f28d1b46a0b7a7662b6eeb2c4421cb686c89 Mon Sep 17 00:00:00 2001 From: benedekaibas <[email protected]> Date: Tue, 17 Nov 2026 23:19:29 +0100 Subject: [PATCH 4/5] Update the doc comment of the functions. --- clang/lib/Analysis/CFG.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/clang/lib/Analysis/CFG.cpp b/clang/lib/Analysis/CFG.cpp index 703350f2cf4b90..cd4f3ac968412e 100644 --- a/clang/lib/Analysis/CFG.cpp +++ b/clang/lib/Analysis/CFG.cpp @@ -2028,8 +2028,8 @@ void CFGBuilder::addAutomaticObjDestruction(LocalScope::const_iterator B, } } -/// Add ScopeEnd CFG elements for each element in the CFG leaving a -/// local scope. Assumes that range [B, E) corresponds to single scope. +/// Add ScopeEnd CFG elements for the local scope that is being left. +/// Assumes that range [B, E) corresponds to single scope. void CFGBuilder::addScopeExitHandling(LocalScope::const_iterator B, LocalScope::const_iterator E, Stmt *S) { assert(!B.inSameLocalScope(E)); >From 75b6a48c63fcfd586ef6dc94ec6c42ccd7a055bd Mon Sep 17 00:00:00 2001 From: benedekaibas <[email protected]> Date: Tue, 17 Nov 2026 23:25:30 +0100 Subject: [PATCH 5/5] Further simplify addScopeExitHandling. --- clang/lib/Analysis/CFG.cpp | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/clang/lib/Analysis/CFG.cpp b/clang/lib/Analysis/CFG.cpp index cd4f3ac968412e..e8f3407f5728fb 100644 --- a/clang/lib/Analysis/CFG.cpp +++ b/clang/lib/Analysis/CFG.cpp @@ -2028,14 +2028,11 @@ void CFGBuilder::addAutomaticObjDestruction(LocalScope::const_iterator B, } } -/// Add ScopeEnd CFG elements for the local scope that is being left. +/// Add ScopeEnd CFG elements for the local scope that is being left. /// Assumes that range [B, E) corresponds to single scope. void CFGBuilder::addScopeExitHandling(LocalScope::const_iterator B, LocalScope::const_iterator E, Stmt *S) { assert(!B.inSameLocalScope(E)); - if (!BuildOpts.AddLifetime && !BuildOpts.AddScopes) - return; - if (BuildOpts.AddScopes) { autoCreateBlock(); appendScopeEnd(Block, B.getFirstVarInScope(), S); _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
