https://github.com/NagyDonat updated https://github.com/llvm/llvm-project/pull/244444
From 14eb9384cf630c1cc3066d2e2783df0f762c09de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Don=C3=A1t=20Nagy?= <[email protected]> Date: Thu, 19 Nov 2026 21:45:40 +0100 Subject: [PATCH 1/3] [analyzer] Regularize conjured sym for return area of entrypoint In C++ the function analyzed as the entrypoint may "return" by constructing an object at a memory area in the context of its caller (which is not part of the symbolic execution). To model this situation the analyzer conjures a symbolic region to represent the memory area where the object will be constructed. When a symbol is conjured this way, `SF` (passed to `conjureSymbolVal`) is the entry stack frame, which may be different from `getCurrStackFrame()` (the currently analyzed stack frame) in code like ``` Obj inner() { Obj o; return o; } Obj entrypoint() { return inner(); } ``` (Analysis of this code invokes `computeObjectUnderConstruction` at a point when `getCurrStackFrame()` is the frame of `inner()`.) Among the situations covered by LIT tests, this was the only one where a conjured symbol was initialized with a stack frame that is different from `getCurrStackFrame()`. This change eliminates this irregularity because it does not have any useful role: on a single path there should be only one "area where the return value of the entrypoint is constructed" symbol, and that symbol is distinguished by its unique tag, so other distinguishing features like the stack frame are practically irrelevant. (This change could theoretically prevent the merge of two execution paths where this "return from entrypoint" symbol was conjured in different stack frames, but while analyzing the same `CFGElement`, but this is irrelevant for the users.) --- .../lib/StaticAnalyzer/Core/ExprEngineCXX.cpp | 3 ++- clang/test/Analysis/temporaries.cpp | 25 +++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/clang/lib/StaticAnalyzer/Core/ExprEngineCXX.cpp b/clang/lib/StaticAnalyzer/Core/ExprEngineCXX.cpp index 609b3882e3867d..373ca4b5d1bab8 100644 --- a/clang/lib/StaticAnalyzer/Core/ExprEngineCXX.cpp +++ b/clang/lib/StaticAnalyzer/Core/ExprEngineCXX.cpp @@ -236,7 +236,8 @@ SVal ExprEngine::computeObjectUnderConstruction( QualType ReturnTy = RetE->getType(); QualType RegionTy = ACtx.getPointerType(ReturnTy); return SVB.conjureSymbolVal(&TopLevelSymRegionTag, getCFGElementRef(), - SF, RegionTy, getNumVisitedCurrent()); + getCurrStackFrame(), RegionTy, + getNumVisitedCurrent()); } llvm_unreachable("Unhandled return value construction context!"); } diff --git a/clang/test/Analysis/temporaries.cpp b/clang/test/Analysis/temporaries.cpp index 1c0ac38160c2d2..557b1830aa7af9 100644 --- a/clang/test/Analysis/temporaries.cpp +++ b/clang/test/Analysis/temporaries.cpp @@ -24,6 +24,7 @@ extern bool clang_analyzer_eval(bool); extern bool clang_analyzer_warnIfReached(); void clang_analyzer_checkInlined(bool); +void clang_analyzer_dump(void *); #include "Inputs/system-header-simulator-cxx.h" @@ -1239,3 +1240,27 @@ void test() { } } // namespace arguments_of_operators #endif // __cplusplus >= 201103L + +namespace stack_frame_of_return_from_top { +struct Obj { + int *p; + Obj() { p = new int; } + Obj(Obj &&s) : p(s.p) { + s.p = 0; + clang_analyzer_dump(this); + // expected-warning-re@-1 {{{{conj_\$[0-9]+{Obj \*, LC2, S[0-9]+, #[0-9]+}}}}} + } + ~Obj(); // Presumably releases 'p'. +}; + +Obj inner() { + // This is stack frame #2 (displayed as LC2 in the conjured symbol dump) + Obj o; + return o; +} + +Obj outer() { + // This is stack frame #1 (would be displayed as LC1) + return inner(); +} +} // namespace stack_frame_of_return_from_top From 3d5d8404709097af6a5015dd7d2d0e8354fd25a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Don=C3=A1t=20Nagy?= <[email protected]> Date: Fri, 20 Nov 2026 00:38:54 +0100 Subject: [PATCH 2/3] Add dots at end of sentences --- clang/test/Analysis/temporaries.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/clang/test/Analysis/temporaries.cpp b/clang/test/Analysis/temporaries.cpp index 557b1830aa7af9..977f17b381be98 100644 --- a/clang/test/Analysis/temporaries.cpp +++ b/clang/test/Analysis/temporaries.cpp @@ -1254,13 +1254,13 @@ struct Obj { }; Obj inner() { - // This is stack frame #2 (displayed as LC2 in the conjured symbol dump) + // This is stack frame #2 (displayed as LC2 in the conjured symbol dump). Obj o; return o; } Obj outer() { - // This is stack frame #1 (would be displayed as LC1) + // This is stack frame #1 (would be displayed as LC1). return inner(); } } // namespace stack_frame_of_return_from_top From a4cf3824a2c6feb9dfb55e27c8c3489ebcc74c02 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Don=C3=A1t=20Nagy?= <[email protected]> Date: Fri, 20 Nov 2026 10:07:35 +0100 Subject: [PATCH 3/3] Add an extra indirection layer to test This verifies that the discrepancy fixed in this PR is not caused by the known inconsistency of the "current" stack frame in and around the `BeginFunction` callback. --- clang/test/Analysis/temporaries.cpp | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/clang/test/Analysis/temporaries.cpp b/clang/test/Analysis/temporaries.cpp index 977f17b381be98..316a363a931fdd 100644 --- a/clang/test/Analysis/temporaries.cpp +++ b/clang/test/Analysis/temporaries.cpp @@ -1248,19 +1248,24 @@ struct Obj { Obj(Obj &&s) : p(s.p) { s.p = 0; clang_analyzer_dump(this); - // expected-warning-re@-1 {{{{conj_\$[0-9]+{Obj \*, LC2, S[0-9]+, #[0-9]+}}}}} + // expected-warning-re@-1 {{{{conj_\$[0-9]+{Obj \*, LC3, S[0-9]+, #[0-9]+}}}}} } ~Obj(); // Presumably releases 'p'. }; Obj inner() { - // This is stack frame #2 (displayed as LC2 in the conjured symbol dump). + // This is stack frame #3 (displayed as LC3 in the conjured symbol dump). Obj o; return o; } -Obj outer() { - // This is stack frame #1 (would be displayed as LC1). +Obj middle() { + // This is stack frame #2 (would be displayed as LC2). return inner(); } + +Obj entrypoint() { + // This is stack frame #1 (would be displayed as LC1). + return middle(); +} } // namespace stack_frame_of_return_from_top _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
