[clang] 1e7329c - [clang][dataflow] Model variables / fields / funcs used in default initializers.
Martin Braenne via cfe-commits
cfe-commits at lists.llvm.org
Tue Jul 4 05:06:22 PDT 2023
Author: Martin Braenne
Date: 2023-07-04T12:06:10Z
New Revision: 1e7329cd79c53165f113edfe6a2ff06d12899632
URL: https://github.com/llvm/llvm-project/commit/1e7329cd79c53165f113edfe6a2ff06d12899632
DIFF: https://github.com/llvm/llvm-project/commit/1e7329cd79c53165f113edfe6a2ff06d12899632.diff
LOG: [clang][dataflow] Model variables / fields / funcs used in default initializers.
The newly added test fails without the other changes in this patch.
Reviewed By: sammccall, gribozavr2
Differential Revision: https://reviews.llvm.org/D154420
Added:
Modified:
clang/lib/Analysis/FlowSensitive/DataflowEnvironment.cpp
clang/unittests/Analysis/FlowSensitive/TransferTest.cpp
Removed:
################################################################################
diff --git a/clang/lib/Analysis/FlowSensitive/DataflowEnvironment.cpp b/clang/lib/Analysis/FlowSensitive/DataflowEnvironment.cpp
index 689f8abb51c8e0..4a11c09a44f63b 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<CXXDefaultInitExpr>(&S))
+ getFieldsGlobalsAndFuncs(*DefaultInit->getExpr(), Fields, Vars, Funcs);
if (auto *DS = dyn_cast<DeclStmt>(&S)) {
if (DS->isSingleDecl())
diff --git a/clang/unittests/Analysis/FlowSensitive/TransferTest.cpp b/clang/unittests/Analysis/FlowSensitive/TransferTest.cpp
index 210b85f7ae8d67..f7210d29d06cc9 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<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
More information about the cfe-commits
mailing list