diff --git a/clang/lib/Analysis/FlowSensitive/TypeErasedDataflowAnalysis.cpp b/clang/lib/Analysis/FlowSensitive/TypeErasedDataflowAnalysis.cpp --- a/clang/lib/Analysis/FlowSensitive/TypeErasedDataflowAnalysis.cpp +++ b/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(); diff --git a/clang/unittests/Analysis/FlowSensitive/TransferTest.cpp b/clang/unittests/Analysis/FlowSensitive/TransferTest.cpp --- a/clang/unittests/Analysis/FlowSensitive/TransferTest.cpp +++ b/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>> + 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(Env0.getValue(*FooDecl, SkipPast::None)); + EXPECT_TRUE(Env0.flowConditionImplies(FooVal0)); + + BoolValue &FooVal1 = + *cast(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>> + 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(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>> + 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(Env.getValue(*FooDecl, SkipPast::None)); + EXPECT_TRUE(Env.flowConditionImplies(FooVal)); + }); +} + } // namespace