[clang] [Thread Analysis] Fix a bug in context update in alias-analysis (PR #178952)

Marco Elver via cfe-commits cfe-commits at lists.llvm.org
Fri Jan 30 12:46:23 PST 2026


================
@@ -1726,20 +1725,27 @@ class BuildLockset : public ConstStmtVisitor<BuildLockset> {
   FactSet FSet;
   // The fact set for the function on exit.
   const FactSet &FunctionExitFSet;
+  // Use `LVarCtx` to keep track of the context at each program point, which
+  // will be used to translate DREs (by `SExprBuilder::translateDeclRefExpr`)
+  // to their canonical definitions:
   LocalVariableMap::Context LVarCtx;
   unsigned CtxIndex;
 
   // To update and adjust the context.
   void updateLocalVarMapCtx(const Stmt *S) {
-    if (S)
-      LVarCtx = Analyzer->LocalVarMap.getNextContext(CtxIndex, S, LVarCtx);
     if (!Analyzer->Handler.issueBetaWarnings())
       return;
     // The lookup closure needs to be reconstructed with the refreshed LVarCtx.
     Analyzer->SxBuilder.setLookupLocalVarExpr(
         [this, Ctx = LVarCtx](const NamedDecl *D) mutable -> const Expr * {
           return Analyzer->LocalVarMap.lookupExpr(D, Ctx);
         });
+    // The `setLookupLocalVarExpr` call above copies the context immediately
+    // before the statement `S`.  That is the context that should be used during
+    // visiting `S`.  Then we update `LVarCtx` to hold the context immediately
+    // after `S` for the next `Visit*` method.
+    if (S)
+      LVarCtx = Analyzer->LocalVarMap.getNextContext(CtxIndex, S, LVarCtx);
----------------
melver wrote:

This needs to happen with and without issueBetaWarnings(). So I think for the time being you need to just copy the LVarCtx before and then move it to the lambda.

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


More information about the cfe-commits mailing list