diff --git a/clang/lib/Analysis/FlowSensitive/DataflowEnvironment.cpp b/clang/lib/Analysis/FlowSensitive/DataflowEnvironment.cpp index f734168e647bd9..3307981ce7e0f3 100644 --- a/clang/lib/Analysis/FlowSensitive/DataflowEnvironment.cpp +++ b/clang/lib/Analysis/FlowSensitive/DataflowEnvironment.cpp @@ -416,7 +416,7 @@ class ResultObjectVisitor : public AnalysisASTVisitor { // below them can initialize the same object (or part of it). if (isa(E) || isa(E) || isa(E) || isa(E) || isa(E) || - isa(E) || + isa(E) || isa(E) || // We treat `BuiltinBitCastExpr` as an "original initializer" too as // it may not even be casting from a record type -- and even if it is, // the two objects are in general of unrelated type. diff --git a/clang/unittests/Analysis/FlowSensitive/DataflowEnvironmentTest.cpp b/clang/unittests/Analysis/FlowSensitive/DataflowEnvironmentTest.cpp index a4ac597bb06d62..f09ee68ada1d54 100644 --- a/clang/unittests/Analysis/FlowSensitive/DataflowEnvironmentTest.cpp +++ b/clang/unittests/Analysis/FlowSensitive/DataflowEnvironmentTest.cpp @@ -442,6 +442,55 @@ TEST_F(EnvironmentTest, CXXDefaultInitExprResultObjIsWrappedExprResultObj) { &Env.getResultObjectLocation(*DefaultInit->getExpr())); } +// This test verifies the behavior of `getResultObjectLocation()` in +// scenarios involving inherited constructors. +// Since the specific AST node of interest `CXXConstructorDecl` is implicitly +// generated, we cannot annotate any statements inside of it as we do in tests +// within TransferTest. Thus, the only way to get the right `Environment` is by +// explicitly initializing it as we do in tests within EnvironmentTest. +// This is why this test is not inside TransferTest, where most of the tests for +// `getResultObjectLocation()` are located. +TEST_F(EnvironmentTest, ResultObjectLocationForInheritedCtorInitExpr) { + using namespace ast_matchers; + + std::string Code = R"( + struct Base { + Base(int b) {} + }; + struct Derived : Base { + using Base::Base; + }; + + Derived d = Derived(0); + )"; + + auto Unit = + tooling::buildASTFromCodeWithArgs(Code, {"-fsyntax-only", "-std=c++20"}); + auto &Context = Unit->getASTContext(); + + ASSERT_EQ(Context.getDiagnostics().getClient()->getNumErrors(), 0U); + + auto Results = + match(cxxConstructorDecl( + hasAnyConstructorInitializer(cxxCtorInitializer( + withInitializer(expr().bind("inherited_ctor_init_expr"))))) + .bind("ctor"), + Context); + const auto *Constructor = selectFirst("ctor", Results); + const auto *InheritedCtorInit = selectFirst( + "inherited_ctor_init_expr", Results); + + EXPECT_EQ(InheritedCtorInit->child_begin(), InheritedCtorInit->child_end()); + + Environment Env(DAContext, *Constructor); + Env.initialize(); + + RecordStorageLocation &Loc = Env.getResultObjectLocation(*InheritedCtorInit); + EXPECT_NE(&Loc, nullptr); + + EXPECT_EQ(&Loc, Env.getThisPointeeStorageLocation()); +} + TEST_F(EnvironmentTest, Stmt) { using namespace ast_matchers;