Skip to content

Commit 9cbe0ef

Browse files
committed
[dataflow] CXXForRangeStmt should extend flow condition
This is needed in order to correctly assume implicit iterator validity in the iterator checker.
1 parent 1b87ebc commit 9cbe0ef

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

clang/lib/Analysis/FlowSensitive/TypeErasedDataflowAnalysis.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,12 @@ class TerminatorVisitor
103103
return {nullptr, false};
104104
}
105105

106-
TerminatorVisitorRetTy VisitCXXForRangeStmt(const CXXForRangeStmt *) {
107-
// Don't do anything special for CXXForRangeStmt, because the condition
108-
// (being implicitly generated) isn't visible from the loop body.
106+
TerminatorVisitorRetTy VisitCXXForRangeStmt(const CXXForRangeStmt *S) {
107+
// Even though the condition isn't visible from the loop body, analysis
108+
// might depend on the implicit implicit statements implied by the loop.
109+
auto *Cond = S->getCond();
110+
if (Cond != nullptr)
111+
return extendFlowCondition(*Cond);
109112
return {nullptr, false};
110113
}
111114

clang/unittests/Analysis/FlowSensitive/TypeErasedDataflowAnalysisTest.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1710,4 +1710,25 @@ TEST_F(TopTest, ForRangeStmtConverges) {
17101710
// analysis converged.
17111711
});
17121712
}
1713+
1714+
TEST_F(TopTest, ForRangeStmtHasFlowCondition) {
1715+
std::string Code = R"(
1716+
#include <array>
1717+
void target(bool Foo) {
1718+
std::array<int, 5> t;
1719+
for (auto& i : t) {
1720+
(void)0;
1721+
/*[[p1]]*/
1722+
}
1723+
}
1724+
)";
1725+
runDataflow(Code,
1726+
[](const llvm::StringMap<DataflowAnalysisState<NoopLattice>> &Results,
1727+
const AnalysisOutputs &AO) {
1728+
ASSERT_THAT(Results.keys(), UnorderedElementsAre("p1"));
1729+
const Environment &Env1 = getEnvironmentAtAnnotation(Results, "p1");
1730+
ASSERT_TRUE(Env1.proves(Env1.arena().makeAtomRef(Env1.getFlowConditionToken())));
1731+
});
1732+
}
1733+
17131734
} // namespace

0 commit comments

Comments
 (0)