diff --git a/clang/lib/Analysis/FlowSensitive/DataflowEnvironment.cpp b/clang/lib/Analysis/FlowSensitive/DataflowEnvironment.cpp index 689f8abb51c8e..4a11c09a44f63 100644 --- a/clang/lib/Analysis/FlowSensitive/DataflowEnvironment.cpp +++ b/clang/lib/Analysis/FlowSensitive/DataflowEnvironment.cpp @@ -194,6 +194,8 @@ getFieldsGlobalsAndFuncs(const Stmt &S, for (auto *Child : S.children()) if (Child != nullptr) getFieldsGlobalsAndFuncs(*Child, Fields, Vars, Funcs); + if (const auto *DefaultInit = dyn_cast(&S)) + getFieldsGlobalsAndFuncs(*DefaultInit->getExpr(), Fields, Vars, Funcs); if (auto *DS = dyn_cast(&S)) { if (DS->isSingleDecl()) diff --git a/clang/unittests/Analysis/FlowSensitive/TransferTest.cpp b/clang/unittests/Analysis/FlowSensitive/TransferTest.cpp index 210b85f7ae8d6..f7210d29d06cc 100644 --- a/clang/unittests/Analysis/FlowSensitive/TransferTest.cpp +++ b/clang/unittests/Analysis/FlowSensitive/TransferTest.cpp @@ -5547,4 +5547,40 @@ TEST(TransferTest, AnonymousStructWithInitializer) { }); } +TEST(TransferTest, AnonymousStructWithReferenceField) { + std::string Code = R"( + int global_i = 0; + struct target { + target() { + (void)0; + // [[p]] + } + struct { + int &i = global_i; + }; + }; + )"; + runDataflow( + Code, + [](const llvm::StringMap> &Results, + ASTContext &ASTCtx) { + const Environment &Env = getEnvironmentAtAnnotation(Results, "p"); + const ValueDecl *GlobalIDecl = findValueDecl(ASTCtx, "global_i"); + const ValueDecl *IDecl = findValueDecl(ASTCtx, "i"); + const IndirectFieldDecl *IndirectField = + findIndirectFieldDecl(ASTCtx, "i"); + + auto *ThisLoc = + cast(Env.getThisPointeeStorageLocation()); + auto &AnonStruct = cast(ThisLoc->getChild( + *cast(IndirectField->chain().front()))); + + auto *RefVal = + cast(Env.getValue(AnonStruct.getChild(*IDecl))); + + ASSERT_EQ(&RefVal->getReferentLoc(), + Env.getStorageLocation(*GlobalIDecl)); + }); +} + } // namespace