[clang] 188d28f - [clang][dataflow] Assign aggregate storage locations to union stmts
Stanislav Gatev via cfe-commits
cfe-commits at lists.llvm.org
Wed Jan 26 02:38:19 PST 2022
Author: Stanislav Gatev
Date: 2022-01-26T10:36:49Z
New Revision: 188d28f73cc7b7891c182a85fdb6e274123b5b69
URL: https://github.com/llvm/llvm-project/commit/188d28f73cc7b7891c182a85fdb6e274123b5b69
DIFF: https://github.com/llvm/llvm-project/commit/188d28f73cc7b7891c182a85fdb6e274123b5b69.diff
LOG: [clang][dataflow] Assign aggregate storage locations to union stmts
This patch ensures that the dataflow analysis framework does not crash
when it encounters access to members of union types.
This is part of the implementation of the dataflow analysis framework.
See "[RFC] A dataflow analysis framework for Clang AST" on cfe-dev.
Reviewed-by: xazax.hun
Differential Revision: https://reviews.llvm.org/D118226
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 6fbc3ec42465c..512e3d991df20 100644
--- a/clang/lib/Analysis/FlowSensitive/DataflowEnvironment.cpp
+++ b/clang/lib/Analysis/FlowSensitive/DataflowEnvironment.cpp
@@ -100,7 +100,7 @@ LatticeJoinEffect Environment::join(const Environment &Other) {
StorageLocation &Environment::createStorageLocation(QualType Type) {
assert(!Type.isNull());
- if (Type->isStructureOrClassType()) {
+ if (Type->isStructureOrClassType() || Type->isUnionType()) {
// FIXME: Explore options to avoid eager initialization of fields as some of
// them might not be needed for a particular analysis.
llvm::DenseMap<const ValueDecl *, StorageLocation *> FieldLocs;
diff --git a/clang/unittests/Analysis/FlowSensitive/TransferTest.cpp b/clang/unittests/Analysis/FlowSensitive/TransferTest.cpp
index cd3e58207680a..0f7ac3af74edd 100644
--- a/clang/unittests/Analysis/FlowSensitive/TransferTest.cpp
+++ b/clang/unittests/Analysis/FlowSensitive/TransferTest.cpp
@@ -1952,4 +1952,37 @@ TEST_F(TransferTest, AggregateInitialization) {
}
}
+TEST_F(TransferTest, AssignToUnionMember) {
+ std::string Code = R"(
+ union A {
+ int Foo;
+ };
+
+ void target(int Bar) {
+ A Baz;
+ Baz.Foo = Bar;
+ // [[p]]
+ }
+ )";
+ runDataflow(Code,
+ [](llvm::ArrayRef<
+ std::pair<std::string, DataflowAnalysisState<NoopLattice>>>
+ Results,
+ ASTContext &ASTCtx) {
+ ASSERT_THAT(Results, ElementsAre(Pair("p", _)));
+ const Environment &Env = Results[0].second.Env;
+
+ const ValueDecl *BazDecl = findValueDecl(ASTCtx, "Baz");
+ ASSERT_THAT(BazDecl, NotNull());
+ ASSERT_TRUE(BazDecl->getType()->isUnionType());
+
+ const auto *BazLoc = dyn_cast_or_null<AggregateStorageLocation>(
+ Env.getStorageLocation(*BazDecl, SkipPast::None));
+ ASSERT_THAT(BazLoc, NotNull());
+
+ // FIXME: Add support for union types.
+ EXPECT_THAT(Env.getValue(*BazLoc), IsNull());
+ });
+}
+
} // namespace
More information about the cfe-commits
mailing list