[PATCH] D154420: [clang][dataflow] Model variables / fields / funcs used in default initializers.
Martin Böhme via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Tue Jul 4 02:03:00 PDT 2023
mboehme created this revision.
Herald added subscribers: martong, xazax.hun.
Herald added a reviewer: NoQ.
Herald added a project: All.
mboehme requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.
The newly added test fails without the other changes in this patch.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D154420
Files:
clang/lib/Analysis/FlowSensitive/DataflowEnvironment.cpp
clang/unittests/Analysis/FlowSensitive/TransferTest.cpp
Index: clang/unittests/Analysis/FlowSensitive/TransferTest.cpp
===================================================================
--- clang/unittests/Analysis/FlowSensitive/TransferTest.cpp
+++ clang/unittests/Analysis/FlowSensitive/TransferTest.cpp
@@ -5547,4 +5547,40 @@
});
}
+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<DataflowAnalysisState<NoopLattice>> &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<AggregateStorageLocation>(Env.getThisPointeeStorageLocation());
+ auto &AnonStruct = cast<AggregateStorageLocation>(ThisLoc->getChild(
+ *cast<ValueDecl>(IndirectField->chain().front())));
+
+ auto *RefVal =
+ cast<ReferenceValue>(Env.getValue(AnonStruct.getChild(*IDecl)));
+
+ ASSERT_EQ(&RefVal->getReferentLoc(),
+ Env.getStorageLocation(*GlobalIDecl));
+ });
+}
+
} // namespace
Index: clang/lib/Analysis/FlowSensitive/DataflowEnvironment.cpp
===================================================================
--- clang/lib/Analysis/FlowSensitive/DataflowEnvironment.cpp
+++ clang/lib/Analysis/FlowSensitive/DataflowEnvironment.cpp
@@ -194,6 +194,8 @@
for (auto *Child : S.children())
if (Child != nullptr)
getFieldsGlobalsAndFuncs(*Child, Fields, Vars, Funcs);
+ if (const auto *DefaultInit = dyn_cast<CXXDefaultInitExpr>(&S))
+ getFieldsGlobalsAndFuncs(*DefaultInit->getExpr(), Fields, Vars, Funcs);
if (auto *DS = dyn_cast<DeclStmt>(&S)) {
if (DS->isSingleDecl())
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D154420.536994.patch
Type: text/x-patch
Size: 2112 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230704/6a529803/attachment-0001.bin>
More information about the cfe-commits
mailing list