diff --git a/clang/unittests/Analysis/FlowSensitive/TransferTest.cpp b/clang/unittests/Analysis/FlowSensitive/TransferTest.cpp index baadea57e4375..3ae3106bca467 100644 --- a/clang/unittests/Analysis/FlowSensitive/TransferTest.cpp +++ b/clang/unittests/Analysis/FlowSensitive/TransferTest.cpp @@ -262,7 +262,8 @@ TEST(TransferTest, StructVarDecl) { cast(&FooLoc->getChild(*BarDecl)); const auto *FooVal = cast(Env.getValue(*FooLoc)); - const auto *BarVal = cast(FooVal->getChild(*BarDecl)); + const auto *BarVal = + cast(getFieldValue(FooVal, *BarDecl, Env)); EXPECT_EQ(Env.getValue(*BarLoc), BarVal); }); } @@ -310,7 +311,8 @@ TEST(TransferTest, StructVarDeclWithInit) { cast(&FooLoc->getChild(*BarDecl)); const auto *FooVal = cast(Env.getValue(*FooLoc)); - const auto *BarVal = cast(FooVal->getChild(*BarDecl)); + const auto *BarVal = + cast(getFieldValue(FooVal, *BarDecl, Env)); EXPECT_EQ(Env.getValue(*BarLoc), BarVal); }); } @@ -357,7 +359,8 @@ TEST(TransferTest, ClassVarDecl) { cast(&FooLoc->getChild(*BarDecl)); const auto *FooVal = cast(Env.getValue(*FooLoc)); - const auto *BarVal = cast(FooVal->getChild(*BarDecl)); + const auto *BarVal = + cast(getFieldValue(FooVal, *BarDecl, Env)); EXPECT_EQ(Env.getValue(*BarLoc), BarVal); }); } @@ -470,37 +473,37 @@ TEST(TransferTest, SelfReferentialReferenceVarDecl) { ASSERT_THAT(BazRefDecl, NotNull()); ASSERT_THAT(BazPtrDecl, NotNull()); - const auto *FooLoc = - cast(Env.getStorageLocation(*FooDecl)); - const auto *FooReferentVal = cast(Env.getValue(*FooLoc)); + const auto &FooLoc = + *cast(Env.getStorageLocation(*FooDecl)); + const auto &FooReferentVal = *cast(Env.getValue(FooLoc)); - const auto *BarVal = - cast(FooReferentVal->getChild(*BarDecl)); - const auto *BarReferentVal = - cast(Env.getValue(BarVal->getReferentLoc())); + const auto &BarVal = + *cast(FooReferentVal.getChild(*BarDecl)); + const auto &BarReferentVal = + *cast(Env.getValue(BarVal.getReferentLoc())); - const auto *FooRefVal = - cast(BarReferentVal->getChild(*FooRefDecl)); + const auto &FooRefVal = + *cast(getFieldValue(&BarReferentVal, *FooRefDecl, Env)); const auto &FooReferentLoc = - cast(FooRefVal->getReferentLoc()); + cast(FooRefVal.getReferentLoc()); EXPECT_THAT(Env.getValue(FooReferentLoc), NotNull()); - EXPECT_THAT(Env.getValue(FooReferentLoc.getChild(*BarDecl)), IsNull()); + EXPECT_THAT(getFieldValue(&FooReferentLoc, *BarDecl, Env), IsNull()); - const auto *FooPtrVal = - cast(BarReferentVal->getChild(*FooPtrDecl)); + const auto &FooPtrVal = + *cast(getFieldValue(&BarReferentVal, *FooPtrDecl, Env)); const auto &FooPtrPointeeLoc = - cast(FooPtrVal->getPointeeLoc()); + cast(FooPtrVal.getPointeeLoc()); EXPECT_THAT(Env.getValue(FooPtrPointeeLoc), NotNull()); - EXPECT_THAT(Env.getValue(FooPtrPointeeLoc.getChild(*BarDecl)), IsNull()); + EXPECT_THAT(getFieldValue(&FooPtrPointeeLoc, *BarDecl, Env), IsNull()); - const auto *BazRefVal = - cast(BarReferentVal->getChild(*BazRefDecl)); - const StorageLocation &BazReferentLoc = BazRefVal->getReferentLoc(); + const auto &BazRefVal = + *cast(getFieldValue(&BarReferentVal, *BazRefDecl, Env)); + const StorageLocation &BazReferentLoc = BazRefVal.getReferentLoc(); EXPECT_THAT(Env.getValue(BazReferentLoc), NotNull()); - const auto *BazPtrVal = - cast(BarReferentVal->getChild(*BazPtrDecl)); - const StorageLocation &BazPtrPointeeLoc = BazPtrVal->getPointeeLoc(); + const auto &BazPtrVal = + *cast(getFieldValue(&BarReferentVal, *BazPtrDecl, Env)); + const StorageLocation &BazPtrPointeeLoc = BazPtrVal.getPointeeLoc(); EXPECT_THAT(Env.getValue(BazPtrPointeeLoc), NotNull()); }); } @@ -630,35 +633,36 @@ TEST(TransferTest, SelfReferentialPointerVarDecl) { ASSERT_THAT(BazRefDecl, NotNull()); ASSERT_THAT(BazPtrDecl, NotNull()); - const auto *FooLoc = - cast(Env.getStorageLocation(*FooDecl)); - const auto *FooVal = cast(Env.getValue(*FooLoc)); - const auto *FooPointeeVal = - cast(Env.getValue(FooVal->getPointeeLoc())); - - const auto *BarVal = - cast(FooPointeeVal->getChild(*BarDecl)); - const auto *BarPointeeVal = - cast(Env.getValue(BarVal->getPointeeLoc())); - - const auto *FooRefVal = - cast(BarPointeeVal->getChild(*FooRefDecl)); - const StorageLocation &FooReferentLoc = FooRefVal->getReferentLoc(); + const auto &FooLoc = + *cast(Env.getStorageLocation(*FooDecl)); + const auto &FooVal = *cast(Env.getValue(FooLoc)); + const auto &FooPointeeVal = + *cast(Env.getValue(FooVal.getPointeeLoc())); + + const auto &BarVal = + *cast(getFieldValue(&FooPointeeVal, *BarDecl, Env)); + const auto &BarPointeeVal = + *cast(Env.getValue(BarVal.getPointeeLoc())); + + const auto &FooRefVal = *cast( + getFieldValue(&BarPointeeVal, *FooRefDecl, Env)); + const StorageLocation &FooReferentLoc = FooRefVal.getReferentLoc(); EXPECT_THAT(Env.getValue(FooReferentLoc), IsNull()); - const auto *FooPtrVal = - cast(BarPointeeVal->getChild(*FooPtrDecl)); - const StorageLocation &FooPtrPointeeLoc = FooPtrVal->getPointeeLoc(); + const auto &FooPtrVal = *cast( + getFieldValue(&BarPointeeVal, *FooPtrDecl, Env)); + const auto &FooPtrPointeeLoc = + cast(FooPtrVal.getPointeeLoc()); EXPECT_THAT(Env.getValue(FooPtrPointeeLoc), IsNull()); - const auto *BazRefVal = - cast(BarPointeeVal->getChild(*BazRefDecl)); - const StorageLocation &BazReferentLoc = BazRefVal->getReferentLoc(); + const auto &BazRefVal = *cast( + getFieldValue(&BarPointeeVal, *BazRefDecl, Env)); + const StorageLocation &BazReferentLoc = BazRefVal.getReferentLoc(); EXPECT_THAT(Env.getValue(BazReferentLoc), NotNull()); - const auto *BazPtrVal = - cast(BarPointeeVal->getChild(*BazPtrDecl)); - const StorageLocation &BazPtrPointeeLoc = BazPtrVal->getPointeeLoc(); + const auto &BazPtrVal = *cast( + getFieldValue(&BarPointeeVal, *BazPtrDecl, Env)); + const StorageLocation &BazPtrPointeeLoc = BazPtrVal.getPointeeLoc(); EXPECT_THAT(Env.getValue(BazPtrPointeeLoc), NotNull()); }); } @@ -1052,7 +1056,8 @@ TEST(TransferTest, StructParamDecl) { cast(&FooLoc->getChild(*BarDecl)); const auto *FooVal = cast(Env.getValue(*FooLoc)); - const auto *BarVal = cast(FooVal->getChild(*BarDecl)); + const auto *BarVal = + cast(getFieldValue(FooVal, *BarDecl, Env)); EXPECT_EQ(Env.getValue(*BarLoc), BarVal); }); } @@ -1152,7 +1157,8 @@ TEST(TransferTest, StructMember) { const auto *FooLoc = cast(Env.getStorageLocation(*FooDecl)); const auto *FooVal = cast(Env.getValue(*FooLoc)); - const auto *BarVal = cast(FooVal->getChild(*BarDecl)); + const auto *BarVal = + cast(getFieldValue(FooVal, *BarDecl, Env)); const ValueDecl *BazDecl = findValueDecl(ASTCtx, "Baz"); ASSERT_THAT(BazDecl, NotNull()); @@ -1422,7 +1428,8 @@ TEST(TransferTest, ClassMember) { const auto *FooLoc = cast(Env.getStorageLocation(*FooDecl)); const auto *FooVal = cast(Env.getValue(*FooLoc)); - const auto *BarVal = cast(FooVal->getChild(*BarDecl)); + const auto *BarVal = + cast(getFieldValue(FooVal, *BarDecl, Env)); const ValueDecl *BazDecl = findValueDecl(ASTCtx, "Baz"); ASSERT_THAT(BazDecl, NotNull()); @@ -1588,7 +1595,8 @@ TEST(TransferTest, StructThisMember) { const auto *BazLoc = cast(&QuxLoc->getChild(*BazDecl)); - const auto *BazVal = cast(QuxVal->getChild(*BazDecl)); + const auto *BazVal = + cast(getFieldValue(QuxVal, *BazDecl, Env)); EXPECT_EQ(Env.getValue(*BazLoc), BazVal); const ValueDecl *QuuxDecl = findValueDecl(ASTCtx, "Quux"); @@ -1662,7 +1670,8 @@ TEST(TransferTest, ClassThisMember) { const auto *BazLoc = cast(&QuxLoc->getChild(*BazDecl)); - const auto *BazVal = cast(QuxVal->getChild(*BazDecl)); + const auto *BazVal = + cast(getFieldValue(QuxVal, *BazDecl, Env)); EXPECT_EQ(Env.getValue(*BazLoc), BazVal); const ValueDecl *QuuxDecl = findValueDecl(ASTCtx, "Quux"); @@ -1947,7 +1956,8 @@ TEST(TransferTest, TemporaryObject) { cast(&FooLoc->getChild(*BarDecl)); const auto *FooVal = cast(Env.getValue(*FooLoc)); - const auto *BarVal = cast(FooVal->getChild(*BarDecl)); + const auto *BarVal = + cast(getFieldValue(FooVal, *BarDecl, Env)); EXPECT_EQ(Env.getValue(*BarLoc), BarVal); }); } @@ -1985,7 +1995,8 @@ TEST(TransferTest, ElidableConstructor) { cast(&FooLoc->getChild(*BarDecl)); const auto *FooVal = cast(Env.getValue(*FooLoc)); - const auto *BarVal = cast(FooVal->getChild(*BarDecl)); + const auto *BarVal = + cast(getFieldValue(FooVal, *BarDecl, Env)); EXPECT_EQ(Env.getValue(*BarLoc), BarVal); }, LangStandard::lang_cxx14); @@ -2031,9 +2042,9 @@ TEST(TransferTest, AssignmentOperator) { EXPECT_FALSE(recordsEqual(*FooLoc1, *BarLoc1, Env1)); const auto *FooBazVal1 = - cast(Env1.getValue(FooLoc1->getChild(*BazDecl))); + cast(getFieldValue(FooLoc1, *BazDecl, Env1)); const auto *BarBazVal1 = - cast(Env1.getValue(BarLoc1->getChild(*BazDecl))); + cast(getFieldValue(BarLoc1, *BazDecl, Env1)); EXPECT_NE(FooBazVal1, BarBazVal1); } @@ -2053,9 +2064,9 @@ TEST(TransferTest, AssignmentOperator) { EXPECT_TRUE(recordsEqual(*FooLoc2, *BarLoc2, Env2)); const auto *FooBazVal2 = - cast(Env2.getValue(FooLoc2->getChild(*BazDecl))); + cast(getFieldValue(FooLoc2, *BazDecl, Env2)); const auto *BarBazVal2 = - cast(Env2.getValue(BarLoc2->getChild(*BazDecl))); + cast(getFieldValue(BarLoc2, *BazDecl, Env2)); EXPECT_EQ(FooBazVal2, BarBazVal2); } @@ -2070,9 +2081,9 @@ TEST(TransferTest, AssignmentOperator) { EXPECT_FALSE(recordsEqual(*FooLoc3, *BarLoc3, Env3)); const auto *FooBazVal3 = - cast(Env3.getValue(FooLoc3->getChild(*BazDecl))); + cast(getFieldValue(FooLoc3, *BazDecl, Env3)); const auto *BarBazVal3 = - cast(Env3.getValue(BarLoc3->getChild(*BazDecl))); + cast(getFieldValue(BarLoc3, *BazDecl, Env3)); EXPECT_NE(FooBazVal3, BarBazVal3); } }); @@ -2145,9 +2156,9 @@ TEST(TransferTest, CopyConstructor) { // In particular, the value of `Baz` in both records is the same. const auto *FooBazVal = - cast(Env.getValue(FooLoc->getChild(*BazDecl))); + cast(getFieldValue(FooLoc, *BazDecl, Env)); const auto *BarBazVal = - cast(Env.getValue(BarLoc->getChild(*BazDecl))); + cast(getFieldValue(BarLoc, *BazDecl, Env)); EXPECT_EQ(FooBazVal, BarBazVal); } @@ -2164,9 +2175,9 @@ TEST(TransferTest, CopyConstructor) { EXPECT_FALSE(recordsEqual(*FooLoc, *BarLoc, Env)); const auto *FooBazVal = - cast(Env.getValue(FooLoc->getChild(*BazDecl))); + cast(getFieldValue(FooLoc, *BazDecl, Env)); const auto *BarBazVal = - cast(Env.getValue(BarLoc->getChild(*BazDecl))); + cast(getFieldValue(BarLoc, *BazDecl, Env)); EXPECT_NE(FooBazVal, BarBazVal); } }); @@ -2210,9 +2221,9 @@ TEST(TransferTest, CopyConstructorWithDefaultArgument) { EXPECT_TRUE(recordsEqual(*FooLoc, *BarLoc, Env)); const auto *FooBazVal = - cast(Env.getValue(FooLoc->getChild(*BazDecl))); + cast(getFieldValue(FooLoc, *BazDecl, Env)); const auto *BarBazVal = - cast(Env.getValue(BarLoc->getChild(*BazDecl))); + cast(getFieldValue(BarLoc, *BazDecl, Env)); EXPECT_EQ(FooBazVal, BarBazVal); }); } @@ -2253,9 +2264,9 @@ TEST(TransferTest, CopyConstructorWithParens) { EXPECT_TRUE(recordsEqual(*FooLoc, *BarLoc, Env)); const auto *FooBazVal = - cast(Env.getValue(FooLoc->getChild(*BazDecl))); + cast(getFieldValue(FooLoc, *BazDecl, Env)); const auto *BarBazVal = - cast(Env.getValue(BarLoc->getChild(*BazDecl))); + cast(getFieldValue(BarLoc, *BazDecl, Env)); EXPECT_EQ(FooBazVal, BarBazVal); }); } @@ -2333,9 +2344,9 @@ TEST(TransferTest, MoveConstructor) { EXPECT_FALSE(recordsEqual(*FooLoc1, *BarLoc1, Env1)); const auto *FooBazVal1 = - cast(Env1.getValue(FooLoc1->getChild(*BazDecl))); + cast(getFieldValue(FooLoc1, *BazDecl, Env1)); const auto *BarBazVal1 = - cast(Env1.getValue(BarLoc1->getChild(*BazDecl))); + cast(getFieldValue(BarLoc1, *BazDecl, Env1)); EXPECT_NE(FooBazVal1, BarBazVal1); const auto *FooLoc2 = @@ -2345,7 +2356,7 @@ TEST(TransferTest, MoveConstructor) { EXPECT_TRUE(recordsEqual(*FooLoc2, Env2, *BarLoc1, Env1)); const auto *FooBazVal2 = - cast(Env2.getValue(FooLoc1->getChild(*BazDecl))); + cast(getFieldValue(FooLoc1, *BazDecl, Env2)); EXPECT_EQ(FooBazVal2, BarBazVal1); }); } @@ -2381,7 +2392,7 @@ TEST(TransferTest, BindTemporary) { const auto &FooVal = *cast(Env.getValue(*FooDecl)); const auto *BarVal = cast(Env.getValue(*BarDecl)); - EXPECT_EQ(BarVal, FooVal.getChild(*BazDecl)); + EXPECT_EQ(BarVal, getFieldValue(&FooVal, *BazDecl, Env)); }); } @@ -2825,7 +2836,7 @@ TEST(TransferTest, AggregateInitialization) { /*[[p]]*/ } )"; - std::string BraceEllisionCode = R"( + std::string BraceElisionCode = R"( struct A { int Foo; }; @@ -2841,7 +2852,7 @@ TEST(TransferTest, AggregateInitialization) { /*[[p]]*/ } )"; - for (const std::string &Code : {BracesCode, BraceEllisionCode}) { + for (const std::string &Code : {BracesCode, BraceElisionCode}) { runDataflow( Code, [](const llvm::StringMap> &Results, @@ -2880,12 +2891,13 @@ TEST(TransferTest, AggregateInitialization) { const auto *QuuxVal = cast(Env.getValue(*QuuxDecl)); ASSERT_THAT(QuuxVal, NotNull()); - const auto *BazVal = cast(QuuxVal->getChild(*BazDecl)); + const auto *BazVal = + cast(getFieldValue(QuuxVal, *BazDecl, Env)); ASSERT_THAT(BazVal, NotNull()); - EXPECT_EQ(QuuxVal->getChild(*BarDecl), BarArgVal); - EXPECT_EQ(BazVal->getChild(*FooDecl), FooArgVal); - EXPECT_EQ(QuuxVal->getChild(*QuxDecl), QuxArgVal); + EXPECT_EQ(getFieldValue(QuuxVal, *BarDecl, Env), BarArgVal); + EXPECT_EQ(getFieldValue(BazVal, *FooDecl, Env), FooArgVal); + EXPECT_EQ(getFieldValue(QuuxVal, *QuxDecl, Env), QuxArgVal); }); } } @@ -2930,8 +2942,10 @@ TEST(TransferTest, AssignToUnionMember) { ASSERT_THAT(Env.getValue(*BazLoc), NotNull()); const auto *BazVal = cast(Env.getValue(*BazLoc)); - const auto *FooValFromBazVal = cast(BazVal->getChild(*FooDecl)); - const auto *FooValFromBazLoc = cast(Env.getValue(BazLoc->getChild(*FooDecl))); + const auto *FooValFromBazVal = + cast(getFieldValue(BazVal, *FooDecl, Env)); + const auto *FooValFromBazLoc = + cast(getFieldValue(BazLoc, *FooDecl, Env)); EXPECT_EQ(FooValFromBazLoc, FooValFromBazVal); const ValueDecl *BarDecl = findValueDecl(ASTCtx, "Bar"); @@ -3496,7 +3510,7 @@ TEST(TransferTest, AssignMemberBeforeCopy) { const auto *BarVal = cast(Env.getValue(*BarDecl)); const auto *A2Val = cast(Env.getValue(*A2Decl)); - EXPECT_EQ(A2Val->getChild(*FooDecl), BarVal); + EXPECT_EQ(getFieldValue(A2Val, *FooDecl, Env), BarVal); }); } @@ -5534,7 +5548,7 @@ TEST(TransferTest, AnonymousStruct) { auto &AnonStruct = cast( S->getChild(*cast(IndirectField->chain().front()))); - auto *B = cast(Env.getValue(AnonStruct.getChild(*BDecl))); + auto *B = cast(getFieldValue(&AnonStruct, *BDecl, Env)); ASSERT_TRUE(Env.flowConditionImplies(*B)); }); } @@ -5565,7 +5579,7 @@ TEST(TransferTest, AnonymousStructWithInitializer) { auto &AnonStruct = cast(ThisLoc->getChild( *cast(IndirectField->chain().front()))); - auto *B = cast(Env.getValue(AnonStruct.getChild(*BDecl))); + auto *B = cast(getFieldValue(&AnonStruct, *BDecl, Env)); ASSERT_TRUE(Env.flowConditionImplies(*B)); }); }