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

via cfe-commits cfe-commits at lists.llvm.org
Mon Nov 23 01:15:55 PST 2026


Author: Gábor Horváth
Date: 2026-11-23T09:15:50Z
New Revision: e046dda7080e3a7f4154ed8b1b700e5df5a99e78

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

LOG: [analyzer] Compute the statement crash trace location lazily (#245071)

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

Co-authored-by: Gabor Horvath <gaborh at apple.com>

Added: 
    

Modified: 
    clang/lib/StaticAnalyzer/Core/ExprEngine.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/StaticAnalyzer/Core/ExprEngine.cpp b/clang/lib/StaticAnalyzer/Core/ExprEngine.cpp
index 4293e175229634..87ff7efcb389b3 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());
 
@@ -2744,9 +2765,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,


        


More information about the cfe-commits mailing list