Skip to content

Commit 5342456

Browse files
committed
Fix loop widening element ref
1 parent cdcee13 commit 5342456

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

clang/lib/StaticAnalyzer/Core/ExprEngine.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2556,10 +2556,19 @@ void ExprEngine::processCFGBlockEntrance(const BlockEdge &L,
25562556
const Stmt *Term = nodeBuilder.getContext().getBlock()->getTerminatorStmt();
25572557
if (!isa_and_nonnull<ForStmt, WhileStmt, DoStmt, CXXForRangeStmt>(Term))
25582558
return;
2559+
2560+
// FIXME:
2561+
// We cannot use the CFG element from the via `ExprEngine::getCFGElementRef`
2562+
// since we are currently at the block entrance and the current reference
2563+
// would be stale. Ideally, we should pass on the terminator of the CFG
2564+
// block, but the terminator cannot be referred as a CFG element.
2565+
// As a workaround, we pass on the first element of the block that we are
2566+
// processing.
2567+
ConstCFGElementRef Elem = *nodeBuilder.getContext().getBlock()->ref_begin();
25592568
// Widen.
25602569
const LocationContext *LCtx = Pred->getLocationContext();
2561-
ProgramStateRef WidenedState = getWidenedLoopState(
2562-
Pred->getState(), LCtx, BlockCount, getCFGElementRef());
2570+
ProgramStateRef WidenedState =
2571+
getWidenedLoopState(Pred->getState(), LCtx, BlockCount, Elem);
25632572
nodeBuilder.generateNode(WidenedState, Pred);
25642573
return;
25652574
}

clang/lib/StaticAnalyzer/Core/LoopWidening.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ ProgramStateRef getWidenedLoopState(ProgramStateRef PrevState,
3131
const LocationContext *LCtx,
3232
unsigned BlockCount,
3333
ConstCFGElementRef Elem) {
34+
if (Elem.getParent()) {
35+
const Stmt *TermStmt = Elem.getParent()->getTerminatorStmt();
36+
assert((isa<ForStmt, WhileStmt, DoStmt, CXXForRangeStmt>(TermStmt)) &&
37+
"Terminator must be a loop statement");
38+
}
3439
// Invalidate values in the current state.
3540
// TODO Make this more conservative by only invalidating values that might
3641
// be modified by the body of the loop.

0 commit comments

Comments
 (0)