@@ -1668,13 +1668,13 @@ void ThreadSafetyAnalyzer::getEdgeLockset(FactSet& Result,
1668
1668
const CFGBlockInfo *PredBlockInfo = &BlockInfo[PredBlock->getBlockID ()];
1669
1669
const LocalVarContext &LVarCtx = PredBlockInfo->ExitContext ;
1670
1670
1671
- // Temporarily set the lookup context for SExprBuilder.
1672
- SxBuilder. setLookupLocalVarExpr ([&]( const NamedDecl *D) -> const Expr * {
1673
- if (!Handler. issueBetaWarnings ())
1674
- return nullptr ;
1675
- auto Ctx = LVarCtx ;
1676
- return LocalVarMap. lookupExpr (D, Ctx );
1677
- });
1671
+ if (Handler. issueBetaWarnings ()) {
1672
+ // Temporarily set the lookup context for SExprBuilder.
1673
+ SxBuilder. setLookupLocalVarExpr (
1674
+ [ this , Ctx = LVarCtx]( const NamedDecl *D) mutable -> const Expr * {
1675
+ return LocalVarMap. lookupExpr (D, Ctx) ;
1676
+ } );
1677
+ }
1678
1678
auto Cleanup = llvm::make_scope_exit (
1679
1679
[this ] { SxBuilder.setLookupLocalVarExpr (nullptr ); });
1680
1680
@@ -1722,6 +1722,19 @@ class BuildLockset : public ConstStmtVisitor<BuildLockset> {
1722
1722
LocalVariableMap::Context LVarCtx;
1723
1723
unsigned CtxIndex;
1724
1724
1725
+ // To update and adjust the context.
1726
+ void updateLocalVarMapCtx (const Stmt *S) {
1727
+ if (S)
1728
+ LVarCtx = Analyzer->LocalVarMap .getNextContext (CtxIndex, S, LVarCtx);
1729
+ if (!Analyzer->Handler .issueBetaWarnings ())
1730
+ return ;
1731
+ // The lookup closure needs to be reconstructed with the refreshed LVarCtx.
1732
+ Analyzer->SxBuilder .setLookupLocalVarExpr (
1733
+ [this , Ctx = LVarCtx](const NamedDecl *D) mutable -> const Expr * {
1734
+ return Analyzer->LocalVarMap .lookupExpr (D, Ctx);
1735
+ });
1736
+ }
1737
+
1725
1738
// helper functions
1726
1739
1727
1740
void checkAccess (const Expr *Exp, AccessKind AK,
@@ -1747,13 +1760,7 @@ class BuildLockset : public ConstStmtVisitor<BuildLockset> {
1747
1760
: ConstStmtVisitor<BuildLockset>(), Analyzer(Anlzr), FSet(Info.EntrySet),
1748
1761
FunctionExitFSet (FunctionExitFSet), LVarCtx(Info.EntryContext),
1749
1762
CtxIndex (Info.EntryIndex) {
1750
- Analyzer->SxBuilder .setLookupLocalVarExpr (
1751
- [this ](const NamedDecl *D) -> const Expr * {
1752
- if (!Analyzer->Handler .issueBetaWarnings ())
1753
- return nullptr ;
1754
- auto Ctx = LVarCtx;
1755
- return Analyzer->LocalVarMap .lookupExpr (D, Ctx);
1756
- });
1763
+ updateLocalVarMapCtx (nullptr );
1757
1764
}
1758
1765
1759
1766
~BuildLockset () { Analyzer->SxBuilder .setLookupLocalVarExpr (nullptr ); }
@@ -2259,9 +2266,7 @@ void BuildLockset::VisitBinaryOperator(const BinaryOperator *BO) {
2259
2266
if (!BO->isAssignmentOp ())
2260
2267
return ;
2261
2268
2262
- // adjust the context
2263
- LVarCtx = Analyzer->LocalVarMap .getNextContext (CtxIndex, BO, LVarCtx);
2264
-
2269
+ updateLocalVarMapCtx (BO);
2265
2270
checkAccess (BO->getLHS (), AK_Written);
2266
2271
}
2267
2272
@@ -2307,8 +2312,7 @@ void BuildLockset::examineArguments(const FunctionDecl *FD,
2307
2312
}
2308
2313
2309
2314
void BuildLockset::VisitCallExpr (const CallExpr *Exp) {
2310
- // adjust the context
2311
- LVarCtx = Analyzer->LocalVarMap .getNextContext (CtxIndex, Exp, LVarCtx);
2315
+ updateLocalVarMapCtx (Exp);
2312
2316
2313
2317
if (const auto *CE = dyn_cast<CXXMemberCallExpr>(Exp)) {
2314
2318
const auto *ME = dyn_cast<MemberExpr>(CE->getCallee ());
@@ -2404,8 +2408,7 @@ static const Expr *UnpackConstruction(const Expr *E) {
2404
2408
}
2405
2409
2406
2410
void BuildLockset::VisitDeclStmt (const DeclStmt *S) {
2407
- // adjust the context
2408
- LVarCtx = Analyzer->LocalVarMap .getNextContext (CtxIndex, S, LVarCtx);
2411
+ updateLocalVarMapCtx (S);
2409
2412
2410
2413
for (auto *D : S->getDeclGroup ()) {
2411
2414
if (auto *VD = dyn_cast_or_null<VarDecl>(D)) {
0 commit comments