[clang] [analyzer] Compute the statement crash trace location lazily (PR #245071)

via cfe-commits cfe-commits at lists.llvm.org
Sun Nov 22 11:10:20 PST 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang-static-analyzer-1

Author: Gábor Horváth (Xazax-hun)

<details>
<summary>Changes</summary>

ProcessStmt, Visit, processBranch, ProcessLoopExit and ProcessLifetimeEnd pushed a PrettyStackTraceLoc for every statement they evaluate, which called the out-of-line Stmt::getBeginLoc eagerly although the location is only needed if a crash trace is printed.

Use a small PrettyStackTraceEntry subclass that keeps the statement and computes its location in print(). The printed text is unchanged.

On some LLVM TUs (GVN, LiveVariables, RegionStore, SemaExpr) this reduces the instructions of the static analyzer by 0.16-0.19%. The cycles (-0.3% to -1.1%) and peak memory changes are within noise.

Assisted-by: Claude Opus 5.5

---
Full diff: https://github.com/llvm/llvm-project/pull/245071.diff


1 Files Affected:

- (modified) clang/lib/StaticAnalyzer/Core/ExprEngine.cpp (+34-14) 


``````````diff
diff --git a/clang/lib/StaticAnalyzer/Core/ExprEngine.cpp b/clang/lib/StaticAnalyzer/Core/ExprEngine.cpp
index e19be8c3a7b376..f0c5c691e3d6da 100644
--- a/clang/lib/StaticAnalyzer/Core/ExprEngine.cpp
+++ b/clang/lib/StaticAnalyzer/Core/ExprEngine.cpp
@@ -1245,13 +1245,36 @@ static bool shouldJustCallCheckers(const Stmt *S, VisitKind K) {
   }
 }
 
+namespace {
+/// Like PrettyStackTraceLoc, but computes the location of the statement only
+/// when a crash trace is printed.
+class PrettyStackTraceStmt : public llvm::PrettyStackTraceEntry {
+  const SourceManager &SM;
+  const Stmt *S;
+  const char *Message;
+
+public:
+  PrettyStackTraceStmt(const SourceManager &SM, const Stmt *S,
+                       const char *Message)
+      : SM(SM), S(S), Message(Message) {}
+
+  void print(raw_ostream &OS) const override {
+    SourceLocation Loc = S->getBeginLoc();
+    if (Loc.isValid()) {
+      Loc.print(OS, SM);
+      OS << ": ";
+    }
+    OS << Message << '\n';
+  }
+};
+} // namespace
+
 void ExprEngine::ProcessStmt(const Stmt *currStmt, ExplodedNode *Pred) {
   // Reclaim any unnecessary nodes in the ExplodedGraph.
   G.reclaimRecentlyAllocatedNodes();
 
-  PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(),
-                                currStmt->getBeginLoc(),
-                                "Error evaluating statement");
+  PrettyStackTraceStmt CrashInfo(getContext().getSourceManager(), currStmt,
+                                 "Error evaluating statement");
 
   // Remove dead bindings and symbols.
   ExplodedNodeSet CleanedStates;
@@ -1286,9 +1309,8 @@ void ExprEngine::ProcessStmt(const Stmt *currStmt, ExplodedNode *Pred) {
 }
 
 void ExprEngine::ProcessLoopExit(const Stmt* S, ExplodedNode *Pred) {
-  PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(),
-                                S->getBeginLoc(),
-                                "Error evaluating end of the loop");
+  PrettyStackTraceStmt CrashInfo(getContext().getSourceManager(), S,
+                                 "Error evaluating end of the loop");
   ProgramStateRef NewState = Pred->getState();
 
   if(AMgr.options.ShouldUnrollLoops)
@@ -1301,9 +1323,8 @@ void ExprEngine::ProcessLoopExit(const Stmt* S, ExplodedNode *Pred) {
 
 void ExprEngine::ProcessLifetimeEnd(const Stmt *S, const VarDecl *D,
                                     ExplodedNode *Pred) {
-  PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(),
-                                S->getBeginLoc(),
-                                "Error evaluating end of a lifetime");
+  PrettyStackTraceStmt CrashInfo(getContext().getSourceManager(), S,
+                                 "Error evaluating end of a lifetime");
   LifetimeEnd PP(S, D, Pred->getStackFrame());
   ExplodedNode *Src = Engine.makeNode(PP, Pred->getState(), Pred);
 
@@ -1833,8 +1854,8 @@ ProgramStateRef ExprEngine::escapeValues(ProgramStateRef State,
 
 void ExprEngine::Visit(const Stmt *S, ExplodedNode *Pred,
                        ExplodedNodeSet &Dst) {
-  PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(),
-                                S->getBeginLoc(), "Error evaluating statement");
+  PrettyStackTraceStmt CrashInfo(getContext().getSourceManager(), S,
+                                 "Error evaluating statement");
 
   assert(!isa<Expr>(S) || S == cast<Expr>(S)->IgnoreParens());
 
@@ -2731,9 +2752,8 @@ void ExprEngine::processBranch(
     Condition = Ex->IgnoreParens();
 
   Condition = ResolveCondition(Condition, getCurrBlock());
-  PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(),
-                                Condition->getBeginLoc(),
-                                "Error evaluating branch");
+  PrettyStackTraceStmt CrashInfo(getContext().getSourceManager(), Condition,
+                                 "Error evaluating branch");
 
   ExplodedNodeSet CheckersOutSet;
   getCheckerManager().runCheckersForBranchCondition(Condition, CheckersOutSet,

``````````

</details>


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


More information about the cfe-commits mailing list