diff --git a/clang/lib/Analysis/FlowSensitive/DataflowEnvironment.cpp b/clang/lib/Analysis/FlowSensitive/DataflowEnvironment.cpp --- a/clang/lib/Analysis/FlowSensitive/DataflowEnvironment.cpp +++ b/clang/lib/Analysis/FlowSensitive/DataflowEnvironment.cpp @@ -83,6 +83,11 @@ if (DeclToLocSizeBefore != DeclToLoc.size()) Effect = LatticeJoinEffect::Changed; + const unsigned ExprToLocSizeBefore = ExprToLoc.size(); + ExprToLoc = intersectDenseMaps(ExprToLoc, Other.ExprToLoc); + if (ExprToLocSizeBefore != ExprToLoc.size()) + Effect = LatticeJoinEffect::Changed; + // FIXME: Add support for joining distinct values that are assigned to the // same storage locations in `LocToVal` and `Other.LocToVal`. const unsigned LocToValSizeBefore = LocToVal.size(); 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 @@ -12,6 +12,7 @@ //===----------------------------------------------------------------------===// #include +#include #include #include @@ -30,6 +31,16 @@ namespace clang { namespace dataflow { +struct CFGBlockPtrCompare { + bool operator()(const CFGBlock *LHS, const CFGBlock *RHS) const { + if (LHS == nullptr) + return true; + if (RHS == nullptr) + return false; + return LHS->getBlockID() < RHS->getBlockID(); + } +}; + /// Computes the input state for a given basic block by joining the output /// states of its predecessors. /// @@ -43,7 +54,9 @@ std::vector> &BlockStates, const CFGBlock &Block, const Environment &InitEnv, TypeErasedDataflowAnalysis &Analysis) { - llvm::DenseSet Preds; + // Ensure the order of predecessor blocks is deterministic to avoid flakiness + // in tests. + std::set Preds; Preds.insert(Block.pred_begin(), Block.pred_end()); if (Block.getTerminator().isTemporaryDtorsBranch()) { // This handles a special case where the code that produced the CFG includes 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 @@ -1828,4 +1828,41 @@ }); } +TEST_F(TransferTest, VarDeclInDoWhile) { + std::string Code = R"( + void target(int *Foo) { + do { + int Bar = *Foo; + } while (true); + (void)0; + /*[[p]]*/ + } + )"; + runDataflow(Code, + [](llvm::ArrayRef< + std::pair>> + Results, + ASTContext &ASTCtx) { + ASSERT_THAT(Results, ElementsAre(Pair("p", _))); + const Environment &Env = Results[0].second.Env; + + const ValueDecl *FooDecl = findValueDecl(ASTCtx, "Foo"); + ASSERT_THAT(FooDecl, NotNull()); + + const ValueDecl *BarDecl = findValueDecl(ASTCtx, "Bar"); + ASSERT_THAT(BarDecl, NotNull()); + + const auto *FooVal = + cast(Env.getValue(*FooDecl, SkipPast::None)); + const auto *FooPointeeVal = + cast(Env.getValue(FooVal->getPointeeLoc())); + + const auto *BarVal = dyn_cast_or_null( + Env.getValue(*BarDecl, SkipPast::None)); + ASSERT_THAT(BarVal, NotNull()); + + EXPECT_EQ(BarVal, FooPointeeVal); + }); +} + } // namespace