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 @@ -97,7 +97,8 @@ void VisitForStmt(const ForStmt *S) { auto *Cond = S->getCond(); - assert(Cond != nullptr); + if (Cond == nullptr) + return; extendFlowCondition(*Cond); } 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 @@ -10,7 +10,6 @@ #include "TestingSupport.h" #include "clang/AST/ASTContext.h" #include "clang/AST/Decl.h" -#include "clang/ASTMatchers/ASTMatchFinder.h" #include "clang/ASTMatchers/ASTMatchers.h" #include "clang/Analysis/FlowSensitive/DataflowEnvironment.h" #include "clang/Analysis/FlowSensitive/StorageLocation.h" @@ -3827,4 +3826,42 @@ }); } +TEST_F(TransferTest, ForStmtEmptyBranchExtendsFlowCondition) { + std::string Code = R"cc( + void target(bool Foo) { + for (;;) { + if (!Foo) { + break; + } + (void)0; + // [[loop_body]] + } + (void)0; + // [[after_loop]] + } + )cc"; + runDataflow( + Code, [](llvm::ArrayRef< + std::pair>> + Results, + ASTContext &ASTCtx) { + ASSERT_THAT(Results, + ElementsAre(Pair("after_loop", _), Pair("loop_body", _))); + const Environment &LoopBodyEnv = Results[1].second.Env; + const Environment &AfterLoopEnv = Results[0].second.Env; + + const ValueDecl *FooDecl = findValueDecl(ASTCtx, "Foo"); + ASSERT_THAT(FooDecl, NotNull()); + + BoolValue &LoopBodyFooVal = + *cast(LoopBodyEnv.getValue(*FooDecl, SkipPast::None)); + EXPECT_TRUE(LoopBodyEnv.flowConditionImplies(LoopBodyFooVal)); + + BoolValue &AfterLoopFooVal = + *cast(AfterLoopEnv.getValue(*FooDecl, SkipPast::None)); + EXPECT_TRUE(AfterLoopEnv.flowConditionImplies( + AfterLoopEnv.makeNot(AfterLoopFooVal))); + }); +} + } // namespace