[PATCH] D153409: BEGIN_PUBLIC [clang][dataflow] Treat fields of anonymous records as being part of their parent.
Martin Böhme via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Wed Jun 21 04:09:31 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.
See https://en.cppreference.com/w/c/language/struct.
Adds a test that fails without the other changes in the patch.
END_PUBLIC
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D153409
Files:
clang/lib/Analysis/FlowSensitive/DataflowAnalysisContext.cpp
clang/lib/Analysis/FlowSensitive/Transfer.cpp
clang/unittests/Analysis/FlowSensitive/TestingSupport.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
@@ -5403,4 +5403,35 @@
});
}
+// Check that fields of anonymous records are modeled and treated as children
+// of the enclosing record.
+TEST(TransferTest, AnonymousStruct) {
+ std::string Code = R"(
+ struct S {
+ struct {
+ int i;
+ };
+ };
+ void target() {
+ S s;
+ (void)s.i;
+ // [[p]]
+ }
+ )";
+ runDataflow(
+ Code,
+ [](const llvm::StringMap<DataflowAnalysisState<NoopLattice>> &Results,
+ ASTContext &ASTCtx) {
+ const Environment &Env = getEnvironmentAtAnnotation(Results, "p");
+ const ValueDecl *SDecl = findValueDecl(ASTCtx, "s");
+ const ValueDecl *IDecl = findValueDecl(ASTCtx, "i");
+
+ auto *S =
+ cast<AggregateStorageLocation>(Env.getStorageLocation(*SDecl));
+ // If we can call getChild() without an assertion failure, it means
+ // that `i` is modeled.
+ S->getChild(*IDecl);
+ });
+}
+
} // namespace
Index: clang/unittests/Analysis/FlowSensitive/TestingSupport.cpp
===================================================================
--- clang/unittests/Analysis/FlowSensitive/TestingSupport.cpp
+++ clang/unittests/Analysis/FlowSensitive/TestingSupport.cpp
@@ -154,7 +154,8 @@
}
const ValueDecl *test::findValueDecl(ASTContext &ASTCtx, llvm::StringRef Name) {
- auto TargetNodes = match(valueDecl(hasName(Name)).bind("v"), ASTCtx);
+ auto TargetNodes = match(
+ valueDecl(unless(indirectFieldDecl()), hasName(Name)).bind("v"), ASTCtx);
assert(TargetNodes.size() == 1 && "Name must be unique");
auto *const Result = selectFirst<ValueDecl>("v", TargetNodes);
assert(Result != nullptr);
Index: clang/lib/Analysis/FlowSensitive/Transfer.cpp
===================================================================
--- clang/lib/Analysis/FlowSensitive/Transfer.cpp
+++ clang/lib/Analysis/FlowSensitive/Transfer.cpp
@@ -543,6 +543,16 @@
if (BaseLoc == nullptr)
return;
+ // Fields on anonymous records are treated as being part of the enclosing
+ // records. So if we see a member access for an anonymous record, just pass
+ // through the storage location of the base object.
+ if (auto *Field = dyn_cast<FieldDecl>(Member)) {
+ if (Field->isAnonymousStructOrUnion()) {
+ Env.setStorageLocation(*S, *BaseLoc);
+ return;
+ }
+ }
+
auto &MemberLoc = BaseLoc->getChild(*Member);
if (MemberLoc.getType()->isReferenceType()) {
// Based on its type, `MemberLoc` must be mapped either to nothing or to a
Index: clang/lib/Analysis/FlowSensitive/DataflowAnalysisContext.cpp
===================================================================
--- clang/lib/Analysis/FlowSensitive/DataflowAnalysisContext.cpp
+++ clang/lib/Analysis/FlowSensitive/DataflowAnalysisContext.cpp
@@ -300,8 +300,12 @@
!Type->isRecordType())
return;
- for (const FieldDecl *Field : Type->getAsRecordDecl()->fields())
- Fields.insert(Field);
+ for (const FieldDecl *Field : Type->getAsRecordDecl()->fields()) {
+ if (Field->isAnonymousStructOrUnion())
+ getFieldsFromClassHierarchy(Field->getType(), Fields);
+ else
+ Fields.insert(Field);
+ }
if (auto *CXXRecord = Type->getAsCXXRecordDecl())
for (const CXXBaseSpecifier &Base : CXXRecord->bases())
getFieldsFromClassHierarchy(Base.getType(), Fields);
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D153409.533212.patch
Type: text/x-patch
Size: 3617 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230621/e667639b/attachment.bin>
More information about the cfe-commits
mailing list