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.
Extend flow condition in the body of a for loop. Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D128060 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 @@ -3557,4 +3557,87 @@ }); } +TEST_F(TransferTest, BranchesExtendFlowCondition) { + std::string IfBranchCode = R"( + void target(bool Foo) { + if (Foo) { + (void)0; + // [[if_branch_0]] + } else { + (void)0; + // [[if_branch_1]] + } + } + )"; + runDataflow(IfBranchCode, + [](llvm::ArrayRef< + std::pair<std::string, DataflowAnalysisState<NoopLattice>>> + Results, + ASTContext &ASTCtx) { + ASSERT_THAT(Results, ElementsAre(Pair("if_branch_1", _), + Pair("if_branch_0", _))); + const Environment &Env0 = Results[1].second.Env; + const Environment &Env1 = Results[0].second.Env; + + const ValueDecl *FooDecl = findValueDecl(ASTCtx, "Foo"); + ASSERT_THAT(FooDecl, NotNull()); + + BoolValue &FooVal0 = + *cast<BoolValue>(Env0.getValue(*FooDecl, SkipPast::None)); + EXPECT_TRUE(Env0.flowConditionImplies(FooVal0)); + + BoolValue &FooVal1 = + *cast<BoolValue>(Env1.getValue(*FooDecl, SkipPast::None)); + EXPECT_TRUE(Env1.flowConditionImplies(Env1.makeNot(FooVal1))); + }); + + std::string WhileBranchCode = R"( + void target(bool Foo) { + while (Foo) { + (void)0; + // [[while_branch]] + } + } + )"; + runDataflow(WhileBranchCode, + [](llvm::ArrayRef< + std::pair<std::string, DataflowAnalysisState<NoopLattice>>> + Results, + ASTContext &ASTCtx) { + ASSERT_THAT(Results, ElementsAre(Pair("while_branch", _))); + const Environment &Env = Results[0].second.Env; + + const ValueDecl *FooDecl = findValueDecl(ASTCtx, "Foo"); + ASSERT_THAT(FooDecl, NotNull()); + + BoolValue &FooVal = + *cast<BoolValue>(Env.getValue(*FooDecl, SkipPast::None)); + EXPECT_TRUE(Env.flowConditionImplies(FooVal)); + }); + + std::string ForBranchCode = R"( + void target(bool Foo) { + for (; Foo;) { + (void)0; + // [[for_branch]] + } + } + )"; + runDataflow(ForBranchCode, + [](llvm::ArrayRef< + std::pair<std::string, DataflowAnalysisState<NoopLattice>>> + Results, + ASTContext &ASTCtx) { + ASSERT_THAT(Results, ElementsAre(Pair("for_branch", _))); + const Environment &Env = Results[0].second.Env; + + const ValueDecl *FooDecl = findValueDecl(ASTCtx, "Foo"); + ASSERT_THAT(FooDecl, NotNull()); + + BoolValue &FooVal = + *cast<BoolValue>(Env.getValue(*FooDecl, SkipPast::None)); + EXPECT_TRUE(Env.flowConditionImplies(FooVal)); + }); +} + } // namespace Index: clang/lib/Analysis/FlowSensitive/TypeErasedDataflowAnalysis.cpp =================================================================== --- clang/lib/Analysis/FlowSensitive/TypeErasedDataflowAnalysis.cpp +++ clang/lib/Analysis/FlowSensitive/TypeErasedDataflowAnalysis.cpp @@ -89,6 +89,12 @@ extendFlowCondition(*Cond); } + void VisitForStmt(const ForStmt *S) { + auto *Cond = S->getCond(); + assert(Cond != nullptr); + extendFlowCondition(*Cond); + } + void VisitBinaryOperator(const BinaryOperator *S) { assert(S->getOpcode() == BO_LAnd || S->getOpcode() == BO_LOr); auto *LHS = S->getLHS();
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits