Author: Benedek Kaibas Date: 2026-11-20T10:29:37+01:00 New Revision: ca8bc84b579c1c4b90081bd930e97abd88243b50
URL: https://github.com/llvm/llvm-project/commit/ca8bc84b579c1c4b90081bd930e97abd88243b50 DIFF: https://github.com/llvm/llvm-project/commit/ca8bc84b579c1c4b90081bd930e97abd88243b50.diff LOG: [analyzer] Fix CFG elements order when AddLifetime is enabled (#243630) When `AddLifetime` is enabled the the order of CFG elements are in a mixed, not reversed order for a scope that mixes trivially and non-trivially destructible locals. Non-trivially destructible locals were constructed in a reverse order in `addAutomaticObjDestruction`, but the trivially destructible locals were constructed in a reverse order in `addScopeExitHandling`. Even though in each function the reverse order of declaration was correctly implemented at the call site of the functions (`addAutomaticObjHandling`) the order was mixed. This PR resolves this issue by removing `appendLifetimeEnds` function from the `addScopeExitHandling` function and adding `needsAutomaticDestruction` as a guard only for destructors marked as no return. This PR fixes https://github.com/llvm/llvm-project/issues/233815 Added: clang/test/Analysis/correct-order-of-CFG-elements.cpp Modified: clang/lib/Analysis/CFG.cpp clang/test/Analysis/lifetime-cfg-output.cpp clang/test/Sema/LifetimeSafety/lifetime-facts.cpp Removed: ################################################################################ diff --git a/clang/lib/Analysis/CFG.cpp b/clang/lib/Analysis/CFG.cpp index 1c69f5d71583f5..6025cf202fb1f1 100644 --- a/clang/lib/Analysis/CFG.cpp +++ b/clang/lib/Analysis/CFG.cpp @@ -805,8 +805,9 @@ class CFGBuilder { void addLoopExit(const Stmt *LoopStmt); void addAutomaticObjHandling(LocalScope::const_iterator B, LocalScope::const_iterator E, Stmt *S); - void addAutomaticObjDestruction(LocalScope::const_iterator B, - LocalScope::const_iterator E, Stmt *S); + void + addAutomaticObjDestruction(llvm::iterator_range<LocalScope::const_iterator> R, + Stmt *S); void addScopeExitHandling(LocalScope::const_iterator B, LocalScope::const_iterator E, Stmt *S); void addImplicitDtorsForDestructor(const CXXDestructorDecl *DD); @@ -1955,7 +1956,7 @@ void CFGBuilder::addAutomaticObjHandling(LocalScope::const_iterator B, // Not leaving the scope, only need to handle destruction and lifetime if (B.inSameLocalScope(E)) { - addAutomaticObjDestruction(B, E, S); + addAutomaticObjDestruction(llvm::iterator_range(B, E), S); return; } @@ -1976,31 +1977,25 @@ void CFGBuilder::addAutomaticObjHandling(LocalScope::const_iterator B, for (auto [E, B] : Pairwise) { if (!B.inSameLocalScope(E)) addScopeExitHandling(B, E, S); - addAutomaticObjDestruction(B, E, S); + addAutomaticObjDestruction(llvm::iterator_range(B, E), S); } } /// 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. -void CFGBuilder::addAutomaticObjDestruction(LocalScope::const_iterator B, - LocalScope::const_iterator E, - Stmt *S) { +/// of all automatic variables with trivial and non-trivial destructors in +/// range [B, E). This includes AutomaticObjectDtor and LifetimeEnds elements. +void CFGBuilder::addAutomaticObjDestruction( + llvm::iterator_range<LocalScope::const_iterator> R, Stmt *S) { if (!BuildOpts.AddImplicitDtors && !BuildOpts.AddLifetime) return; - if (B == E) - 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); + SmallVector<VarDecl *, 10> LocalVarDecls; + LocalVarDecls.reserve(R.begin().distance(R.end())); + for (VarDecl *VD : R) + LocalVarDecls.push_back(VD); - for (VarDecl *VD : llvm::reverse(DeclsNeedDestruction)) { - if (BuildOpts.AddImplicitDtors) { + for (VarDecl *VD : llvm::reverse(LocalVarDecls)) { + 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 @@ -2017,9 +2012,9 @@ void CFGBuilder::addAutomaticObjDestruction(LocalScope::const_iterator B, autoCreateBlock(); - // 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. + // LifetimeEnds is emitted for every variable leaving the scope in + // reverse order of declaration, covering automatic objects with + // non-trivial destructors and trivial objects. if (BuildOpts.AddLifetime) appendLifetimeEnds(Block, VD, S); if (BuildOpts.AddImplicitDtors && !hasTrivialDestructor(VD)) @@ -2029,42 +2024,15 @@ void CFGBuilder::addAutomaticObjDestruction(LocalScope::const_iterator B, } } -/// Add CFG elements corresponding to leaving a scope. +/// Add ScopeEnd CFG elements for the local scope that is being left. /// 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 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); } - - 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..94e1b511e4be5d --- /dev/null +++ b/clang/test/Analysis/correct-order-of-CFG-elements.cpp @@ -0,0 +1,84 @@ +// 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 enables cfg-scopes to ensure that +// none of the elements appear as a duplicate in the given basic block. + +struct NonTrivial { + ~NonTrivial(); +}; + +struct Trivial { + int x; +}; + +// CHECK-LABEL: void test_trivial_and_nontrivial_order() +// CHECK-NEXT: [B2 (ENTRY)] +// CHECK-NEXT: Succs (1): B1 +// CHECK-EMPTY: +// CHECK-NEXT: [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-EMPTY: +// CHECK-NEXT: [B0 (EXIT)] +// CHECK-NEXT: Preds (1): B1 +void test_trivial_and_nontrivial_order() { + NonTrivial a; + int i = 0; + NonTrivial b; + Trivial u; +} + +// CHECK-LABEL: void test_nested_scope() +// CHECK-NEXT: [B2 (ENTRY)] +// CHECK-NEXT: Succs (1): B1 +// CHECK-EMPTY: +// CHECK-NEXT: [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-EMPTY: +// CHECK-NEXT: [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)] diff --git a/clang/test/Sema/LifetimeSafety/lifetime-facts.cpp b/clang/test/Sema/LifetimeSafety/lifetime-facts.cpp index 302d3d59589379..214dbcced10b6c 100644 --- a/clang/test/Sema/LifetimeSafety/lifetime-facts.cpp +++ b/clang/test/Sema/LifetimeSafety/lifetime-facts.cpp @@ -24,8 +24,8 @@ MyObj* return_local_addr() { // CHECK: OriginFlow: // CHECK-NEXT: Dest: [[O_RET_VAL:[0-9]+]] (Expr: ImplicitCastExpr, Type : MyObj *) has loans to { x } // CHECK-NEXT: Src: [[O_P]] (Decl: p, Type : MyObj *) -// CHECK: Expire (x) // CHECK: Expire (p, Origin: [[O_P]] (Decl: p, Type : MyObj *)) +// CHECK: Expire (x) // CHECK: OriginEscapes ([[O_RET_VAL]] (Expr: ImplicitCastExpr, Type : MyObj *), via Return) } _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
