diff --git a/clang/lib/Analysis/FlowSensitive/Transfer.cpp b/clang/lib/Analysis/FlowSensitive/Transfer.cpp --- a/clang/lib/Analysis/FlowSensitive/Transfer.cpp +++ b/clang/lib/Analysis/FlowSensitive/Transfer.cpp @@ -571,14 +571,18 @@ return *Val; } - // Sub-expressions that are logic operators are not added in basic blocks - // (e.g. see CFG for `bool d = a && (b || c);`). If `SubExpr` is a logic - // operator, it isn't evaluated and assigned a value yet. In that case, we - // need to first visit `SubExpr` and then try to get the value that gets - // assigned to it. - Visit(&SubExpr); - if (auto *Val = dyn_cast_or_null( - Env.getValue(SubExpr, SkipPast::Reference))) + auto *SubExprLoc = Env.getStorageLocation(SubExpr, SkipPast::Reference); + if (SubExprLoc == nullptr) { + // Sub-expressions that are logic operators are not added in basic blocks + // (e.g. see CFG for `bool d = a && (b || c);`). If `SubExpr` is a logic + // operator, it may not have been evaluated and assigned a value yet. In + // that case, we need to first visit `SubExpr` and then try to get the + // value that gets assigned to it. + Visit(&SubExpr); + SubExprLoc = Env.getStorageLocation(SubExpr, SkipPast::Reference); + assert(SubExprLoc != nullptr); + } + if (auto *Val = dyn_cast_or_null(Env.getValue(*SubExprLoc))) return *Val; // If the value of `SubExpr` is still unknown, we create a fresh symbolic 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 @@ -2467,6 +2467,67 @@ EXPECT_EQ(&BazVal->getRightSubValue(), BarVal); }); } + + { + std::string Code = R"( + void target(bool A, bool B, bool C, bool D) { + bool Foo = ((A && B) && C) && D; + // [[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 *ADecl = findValueDecl(ASTCtx, "A"); + ASSERT_THAT(ADecl, NotNull()); + + const auto *AVal = + dyn_cast_or_null(Env.getValue(*ADecl, SkipPast::None)); + ASSERT_THAT(AVal, NotNull()); + + const ValueDecl *BDecl = findValueDecl(ASTCtx, "B"); + ASSERT_THAT(BDecl, NotNull()); + + const auto *BVal = + dyn_cast_or_null(Env.getValue(*BDecl, SkipPast::None)); + ASSERT_THAT(BVal, NotNull()); + + const ValueDecl *CDecl = findValueDecl(ASTCtx, "C"); + ASSERT_THAT(CDecl, NotNull()); + + const auto *CVal = + dyn_cast_or_null(Env.getValue(*CDecl, SkipPast::None)); + ASSERT_THAT(CVal, NotNull()); + + const ValueDecl *DDecl = findValueDecl(ASTCtx, "D"); + ASSERT_THAT(DDecl, NotNull()); + + const auto *DVal = + dyn_cast_or_null(Env.getValue(*DDecl, SkipPast::None)); + ASSERT_THAT(DVal, NotNull()); + + const ValueDecl *FooDecl = findValueDecl(ASTCtx, "Foo"); + ASSERT_THAT(FooDecl, NotNull()); + + const auto *FooVal = dyn_cast_or_null( + Env.getValue(*FooDecl, SkipPast::None)); + ASSERT_THAT(FooVal, NotNull()); + + const auto &FooLeftSubVal = + cast(FooVal->getLeftSubValue()); + const auto &FooLeftLeftSubVal = + cast(FooLeftSubVal.getLeftSubValue()); + EXPECT_EQ(&FooLeftLeftSubVal.getLeftSubValue(), AVal); + EXPECT_EQ(&FooLeftLeftSubVal.getRightSubValue(), BVal); + EXPECT_EQ(&FooLeftSubVal.getRightSubValue(), CVal); + EXPECT_EQ(&FooVal->getRightSubValue(), DVal); + }); + } } TEST_F(TransferTest, AssignFromBoolNegation) {