diff --git a/clang/lib/StaticAnalyzer/Core/ExprEngine.cpp b/clang/lib/StaticAnalyzer/Core/ExprEngine.cpp index 451ee91b94533..2e67fb953e456 100644 --- a/clang/lib/StaticAnalyzer/Core/ExprEngine.cpp +++ b/clang/lib/StaticAnalyzer/Core/ExprEngine.cpp @@ -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(Term)) + if (!isa_and_nonnull(Term)) return; // Widen. const LocationContext *LCtx = Pred->getLocationContext(); diff --git a/clang/lib/StaticAnalyzer/Core/LoopWidening.cpp b/clang/lib/StaticAnalyzer/Core/LoopWidening.cpp index a3b29ff487e4e..9e42801760622 100644 --- a/clang/lib/StaticAnalyzer/Core/LoopWidening.cpp +++ b/clang/lib/StaticAnalyzer/Core/LoopWidening.cpp @@ -35,6 +35,8 @@ static const Expr *getLoopCondition(const Stmt *LoopStmt) { return cast(LoopStmt)->getCond(); case Stmt::DoStmtClass: return cast(LoopStmt)->getCond(); + case Stmt::CXXForRangeStmtClass: + return cast(LoopStmt)->getCond(); } } @@ -45,7 +47,7 @@ ProgramStateRef getWidenedLoopState(ProgramStateRef PrevState, const LocationContext *LCtx, unsigned BlockCount, const Stmt *LoopStmt) { - assert((isa(LoopStmt))); + assert((isa(LoopStmt))); // Invalidate values in the current state. // TODO Make this more conservative by only invalidating values that might diff --git a/clang/test/Analysis/loop-widening-notes.cpp b/clang/test/Analysis/loop-widening-notes.cpp index 0ba71d030d058..a3f030dfe9882 100644 --- a/clang/test/Analysis/loop-widening-notes.cpp +++ b/clang/test/Analysis/loop-widening-notes.cpp @@ -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; +}