sgatev created this revision. sgatev added reviewers: ymandel, xazax.hun, gribozavr2. Herald added subscribers: martong, tschuett, rnkovacs. Herald added a project: All. sgatev requested review of this revision. Herald added a project: clang.
Handle `for` statements without conditions. Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D128833 Files: clang/lib/Analysis/FlowSensitive/TypeErasedDataflowAnalysis.cpp clang/unittests/Analysis/FlowSensitive/TransferTest.cpp Index: clang/unittests/Analysis/FlowSensitive/TransferTest.cpp =================================================================== --- clang/unittests/Analysis/FlowSensitive/TransferTest.cpp +++ clang/unittests/Analysis/FlowSensitive/TransferTest.cpp @@ -3827,4 +3827,30 @@ }); } +TEST_F(TransferTest, ForStmtBranchWithoutConditionDoesNotExtendFlowCondition) { + std::string Code = R"( + void target(bool Foo) { + for (;;) { + (void)0; + // [[loop_body]] + } + } + )"; + runDataflow( + Code, [](llvm::ArrayRef< + std::pair<std::string, DataflowAnalysisState<NoopLattice>>> + Results, + ASTContext &ASTCtx) { + ASSERT_THAT(Results, ElementsAre(Pair("loop_body", _))); + const Environment &LoopBodyEnv = Results[0].second.Env; + + const ValueDecl *FooDecl = findValueDecl(ASTCtx, "Foo"); + ASSERT_THAT(FooDecl, NotNull()); + + BoolValue &LoopBodyFooVal = + *cast<BoolValue>(LoopBodyEnv.getValue(*FooDecl, SkipPast::None)); + EXPECT_FALSE(LoopBodyEnv.flowConditionImplies(LoopBodyFooVal)); + }); +} + } // namespace Index: clang/lib/Analysis/FlowSensitive/TypeErasedDataflowAnalysis.cpp =================================================================== --- clang/lib/Analysis/FlowSensitive/TypeErasedDataflowAnalysis.cpp +++ clang/lib/Analysis/FlowSensitive/TypeErasedDataflowAnalysis.cpp @@ -97,8 +97,8 @@ void VisitForStmt(const ForStmt *S) { auto *Cond = S->getCond(); - assert(Cond != nullptr); - extendFlowCondition(*Cond); + if (Cond != nullptr) + extendFlowCondition(*Cond); } void VisitBinaryOperator(const BinaryOperator *S) {
Index: clang/unittests/Analysis/FlowSensitive/TransferTest.cpp =================================================================== --- clang/unittests/Analysis/FlowSensitive/TransferTest.cpp +++ clang/unittests/Analysis/FlowSensitive/TransferTest.cpp @@ -3827,4 +3827,30 @@ }); } +TEST_F(TransferTest, ForStmtBranchWithoutConditionDoesNotExtendFlowCondition) { + std::string Code = R"( + void target(bool Foo) { + for (;;) { + (void)0; + // [[loop_body]] + } + } + )"; + runDataflow( + Code, [](llvm::ArrayRef< + std::pair<std::string, DataflowAnalysisState<NoopLattice>>> + Results, + ASTContext &ASTCtx) { + ASSERT_THAT(Results, ElementsAre(Pair("loop_body", _))); + const Environment &LoopBodyEnv = Results[0].second.Env; + + const ValueDecl *FooDecl = findValueDecl(ASTCtx, "Foo"); + ASSERT_THAT(FooDecl, NotNull()); + + BoolValue &LoopBodyFooVal = + *cast<BoolValue>(LoopBodyEnv.getValue(*FooDecl, SkipPast::None)); + EXPECT_FALSE(LoopBodyEnv.flowConditionImplies(LoopBodyFooVal)); + }); +} + } // namespace Index: clang/lib/Analysis/FlowSensitive/TypeErasedDataflowAnalysis.cpp =================================================================== --- clang/lib/Analysis/FlowSensitive/TypeErasedDataflowAnalysis.cpp +++ clang/lib/Analysis/FlowSensitive/TypeErasedDataflowAnalysis.cpp @@ -97,8 +97,8 @@ void VisitForStmt(const ForStmt *S) { auto *Cond = S->getCond(); - assert(Cond != nullptr); - extendFlowCondition(*Cond); + if (Cond != nullptr) + extendFlowCondition(*Cond); } void VisitBinaryOperator(const BinaryOperator *S) {
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits