Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[analyzer] Loop should contain CXXForRangeStmt #70190

Merged
merged 1 commit into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion clang/lib/StaticAnalyzer/Core/ExprEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2509,7 +2509,7 @@ void ExprEngine::processCFGBlockEntrance(const BlockEdge &L,
if (BlockCount == AMgr.options.maxBlockVisitOnPath - 1 &&
AMgr.options.ShouldWidenLoops) {
const Stmt *Term = nodeBuilder.getContext().getBlock()->getTerminatorStmt();
if (!isa_and_nonnull<ForStmt, WhileStmt, DoStmt>(Term))
if (!isa_and_nonnull<ForStmt, WhileStmt, DoStmt, CXXForRangeStmt>(Term))
return;
// Widen.
const LocationContext *LCtx = Pred->getLocationContext();
Expand Down
4 changes: 3 additions & 1 deletion clang/lib/StaticAnalyzer/Core/LoopWidening.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ static const Expr *getLoopCondition(const Stmt *LoopStmt) {
return cast<WhileStmt>(LoopStmt)->getCond();
case Stmt::DoStmtClass:
return cast<DoStmt>(LoopStmt)->getCond();
case Stmt::CXXForRangeStmtClass:
return cast<CXXForRangeStmt>(LoopStmt)->getCond();
}
}

Expand All @@ -45,7 +47,7 @@ ProgramStateRef getWidenedLoopState(ProgramStateRef PrevState,
const LocationContext *LCtx,
unsigned BlockCount, const Stmt *LoopStmt) {

assert((isa<ForStmt, WhileStmt, DoStmt>(LoopStmt)));
assert((isa<ForStmt, WhileStmt, DoStmt, CXXForRangeStmt>(LoopStmt)));

// Invalidate values in the current state.
// TODO Make this more conservative by only invalidating values that might
Expand Down
12 changes: 12 additions & 0 deletions clang/test/Analysis/loop-widening-notes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,15 @@ int test_for_loop() {
return flag_d / num; // no-crash expected-warning {{Division by zero}}
// expected-note@-1 {{Division by zero}}
}

int test_for_range_loop() {
int arr[10] = {0};
for(auto x : arr) { // expected-note {{Assigning value}}
++x;
}
if (arr[0] == 0) // expected-note {{Assuming the condition is true}}
// expected-note@-1 {{Taking true branch}}
return 1/arr[0]; // expected-warning {{Division by zero}}
// expected-note@-1 {{Division by zero}}
return 0;
}