[clang] 3b19369 - [analyzer] Regularize conjured sym for return area of entrypoint (#244444)

via cfe-commits cfe-commits at lists.llvm.org
Sun Nov 29 09:37:06 PST 2026


Author: DonĂ¡t Nagy
Date: 2026-11-29T18:37:01+01:00
New Revision: 3b19369594ee51e93dfe6dd53eb1117e24dae830

URL: https://github.com/llvm/llvm-project/commit/3b19369594ee51e93dfe6dd53eb1117e24dae830
DIFF: https://github.com/llvm/llvm-project/commit/3b19369594ee51e93dfe6dd53eb1117e24dae830.diff

LOG: [analyzer] Regularize conjured sym for return area of entrypoint (#244444)

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 did not serve any
useful purpose: 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 is almost completely NFC: the only theoretically possible
functional effect is that it could prevent the merge of two execution
paths that only differ in the stack frame where their "return from
entrypoint" function was conjured. This is an extremely unlikely
situation (all other state components must be identical, and the
different stack frames must correspond to different calls of the same
recursive function, because otherwise the `CFGElement` would also
distinguish the conjured symbols) and even in this situation the only
effect is that the separate but equivalent paths consume the analysis
budget twice (which cannot introduce false positives and often wouldn't
have any visible effect).

This change prepares the ground for the simplification of the interface
for conjuring symbols.

Added: 
    

Modified: 
    clang/lib/StaticAnalyzer/Core/ExprEngineCXX.cpp
    clang/test/Analysis/temporaries.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/StaticAnalyzer/Core/ExprEngineCXX.cpp b/clang/lib/StaticAnalyzer/Core/ExprEngineCXX.cpp
index 0817489fd25276..09b515a13a71e7 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..0eaaa50adca3f7 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,32 @@ void test() {
 }
 } // namespace arguments_of_operators
 #endif // __cplusplus >= 201103L
+
+namespace stack_frame_of_return_from_entrypoint {
+struct Obj {
+  int *p;
+  Obj() { p = new int; }
+  Obj(Obj &&s) : p(s.p) {
+    s.p = 0;
+    clang_analyzer_dump(this); // Note: We match LC3 - see later.
+    // expected-warning-re at -1 {{{{conj_\$[0-9]+{Obj \*, LC3, S[0-9]+, #[0-9]+}}}}}
+  }
+  ~Obj();  // Presumably releases 'p'.
+};
+
+Obj inner() {
+  // This is stack frame #3 (displayed as LC3 in the conjured symbol dump).
+  Obj o;
+  return o;
+}
+
+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_entrypoint


        


More information about the cfe-commits mailing list