[PATCH] D154965: [clang][dataflow] Fix initializing a reference field with an `InitListExpr`.
Martin Böhme via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Tue Jul 11 21:53:25 PDT 2023
This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rGbd9b57de4ff7: [clang][dataflow] Fix initializing a reference field with an `InitListExpr`. (authored by mboehme).
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D154965/new/
https://reviews.llvm.org/D154965
Files:
clang/lib/Analysis/FlowSensitive/Transfer.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
@@ -2912,6 +2912,34 @@
}
}
+TEST(TransferTest, AggregateInitializationReferenceField) {
+ std::string Code = R"(
+ struct S {
+ int &RefField;
+ };
+
+ void target(int i) {
+ S s = { i };
+ /*[[p]]*/
+ }
+ )";
+ runDataflow(
+ Code,
+ [](const llvm::StringMap<DataflowAnalysisState<NoopLattice>> &Results,
+ ASTContext &ASTCtx) {
+ const Environment &Env = getEnvironmentAtAnnotation(Results, "p");
+
+ const ValueDecl *RefFieldDecl = findValueDecl(ASTCtx, "RefField");
+
+ auto &ILoc = getLocForDecl<StorageLocation>(ASTCtx, Env, "i");
+ auto &SLoc = getLocForDecl<AggregateStorageLocation>(ASTCtx, Env, "s");
+
+ auto &RefValue =
+ *cast<ReferenceValue>(getFieldValue(&SLoc, *RefFieldDecl, Env));
+ EXPECT_EQ(&RefValue.getReferentLoc(), &ILoc);
+ });
+}
+
TEST(TransferTest, AssignToUnionMember) {
std::string Code = R"(
union A {
Index: clang/lib/Analysis/FlowSensitive/Transfer.cpp
===================================================================
--- clang/lib/Analysis/FlowSensitive/Transfer.cpp
+++ clang/lib/Analysis/FlowSensitive/Transfer.cpp
@@ -717,14 +717,15 @@
if (Type->isStructureOrClassType()) {
std::vector<FieldDecl *> Fields =
getFieldsForInitListExpr(Type->getAsRecordDecl());
- for (auto It : llvm::zip(Fields, S->inits())) {
- const FieldDecl *Field = std::get<0>(It);
+ for (auto [Field, Init] : llvm::zip(Fields, S->inits())) {
assert(Field != nullptr);
-
- const Expr *Init = std::get<1>(It);
assert(Init != nullptr);
- if (Value *InitVal = Env.getValue(*Init, SkipPast::None))
+ if (Field->getType()->isReferenceType()) {
+ if (StorageLocation *Loc = Env.getStorageLocationStrict(*Init))
+ cast<StructValue>(Val)->setChild(*Field,
+ Env.create<ReferenceValue>(*Loc));
+ } else if (Value *InitVal = Env.getValue(*Init, SkipPast::None))
cast<StructValue>(Val)->setChild(*Field, *InitVal);
}
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D154965.539372.patch
Type: text/x-patch
Size: 2366 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230712/d166df25/attachment.bin>
More information about the cfe-commits
mailing list