Skip to content

Commit 7cf20f1

Browse files
authored
[clang][dataflow] Eliminate RecordValue::getChild(). (#65586)
We want to eliminate the `RecordStorageLocation` from `RecordValue` and, ultimately, eliminate `RecordValue` entirely (see the discussion linked in the `RecordValue` class comment). This is one step in that direction. To eliminate `RecordValue::getChild()`, we also eliminate the last remaining caller, namely the `getFieldValue(const RecordValue *, ...)` overload. Calls to this overload have been rewritten to use the `getFieldValue(const RecordStorageLocation *, ...)` overload. Note that this also makes the code slightly simpler in many cases.
1 parent 2126a18 commit 7cf20f1

File tree

4 files changed

+27
-46
lines changed

4 files changed

+27
-46
lines changed

clang/include/clang/Analysis/FlowSensitive/Value.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -225,12 +225,6 @@ class RecordValue final : public Value {
225225
/// Returns the storage location that this `RecordValue` is associated with.
226226
RecordStorageLocation &getLoc() const { return Loc; }
227227

228-
/// Convenience function that returns the child storage location for `Field`.
229-
/// See also the documentation for `RecordStorageLocation::getChild()`.
230-
StorageLocation *getChild(const ValueDecl &Field) const {
231-
return Loc.getChild(Field);
232-
}
233-
234228
private:
235229
RecordStorageLocation &Loc;
236230
};

clang/unittests/Analysis/FlowSensitive/DataflowEnvironmentTest.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ TEST_F(EnvironmentTest, CreateValueRecursiveType) {
9191
// Verify that the struct and the field (`R`) with first appearance of the
9292
// type is created successfully.
9393
Environment Env(DAContext, *Fun);
94-
RecordValue *SVal = cast<RecordValue>(Env.createValue(Ty));
95-
PointerValue *PV = cast_or_null<PointerValue>(getFieldValue(SVal, *R, Env));
94+
auto &SLoc = cast<RecordStorageLocation>(Env.createObject(Ty));
95+
PointerValue *PV = cast_or_null<PointerValue>(getFieldValue(&SLoc, *R, Env));
9696
EXPECT_THAT(PV, NotNull());
9797
}
9898

@@ -240,8 +240,8 @@ TEST_F(EnvironmentTest, IncludeFieldsFromDefaultInitializers) {
240240
// Verify that the `X` field of `S` is populated when analyzing the
241241
// constructor, even though it is not referenced directly in the constructor.
242242
Environment Env(DAContext, *Constructor);
243-
auto *Val = cast<RecordValue>(Env.createValue(QTy));
244-
EXPECT_THAT(getFieldValue(Val, *XDecl, Env), NotNull());
243+
auto &Loc = cast<RecordStorageLocation>(Env.createObject(QTy));
244+
EXPECT_THAT(getFieldValue(&Loc, *XDecl, Env), NotNull());
245245
}
246246

247247
TEST_F(EnvironmentTest, InitGlobalVarsFieldFun) {
@@ -285,8 +285,7 @@ TEST_F(EnvironmentTest, InitGlobalVarsFieldFun) {
285285
Environment Env(DAContext, *Fun);
286286
const auto *GlobalLoc =
287287
cast<RecordStorageLocation>(Env.getStorageLocation(*GlobalDecl));
288-
const auto *GlobalVal = cast<RecordValue>(Env.getValue(*GlobalLoc));
289-
auto *BarVal = getFieldValue(GlobalVal, *BarDecl, Env);
288+
auto *BarVal = getFieldValue(GlobalLoc, *BarDecl, Env);
290289
EXPECT_TRUE(isa<IntegerValue>(BarVal));
291290
}
292291

clang/unittests/Analysis/FlowSensitive/TestingSupport.h

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -463,18 +463,6 @@ inline Value *getFieldValue(const RecordStorageLocation *Loc,
463463
return Env.getValue(*FieldLoc);
464464
}
465465

466-
/// Returns the value of a `Field` on a `Struct.
467-
/// Returns null if `Struct` is null.
468-
inline Value *getFieldValue(const RecordValue *Struct, const ValueDecl &Field,
469-
const Environment &Env) {
470-
if (Struct == nullptr)
471-
return nullptr;
472-
StorageLocation *FieldLoc = Struct->getChild(Field);
473-
if (FieldLoc == nullptr)
474-
return nullptr;
475-
return Env.getValue(*FieldLoc);
476-
}
477-
478466
/// Creates and owns constraints which are boolean values.
479467
class ConstraintContext {
480468
unsigned NextAtom = 0;

clang/unittests/Analysis/FlowSensitive/TransferTest.cpp

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -647,26 +647,26 @@ TEST(TransferTest, SelfReferentialPointerVarDecl) {
647647
const auto &FooLoc =
648648
*cast<ScalarStorageLocation>(Env.getStorageLocation(*FooDecl));
649649
const auto &FooVal = *cast<PointerValue>(Env.getValue(FooLoc));
650-
const auto &FooPointeeVal =
651-
*cast<RecordValue>(Env.getValue(FooVal.getPointeeLoc()));
650+
const auto &FooPointeeLoc =
651+
cast<RecordStorageLocation>(FooVal.getPointeeLoc());
652652

653653
const auto &BarVal =
654-
*cast<PointerValue>(getFieldValue(&FooPointeeVal, *BarDecl, Env));
655-
const auto &BarPointeeVal =
656-
*cast<RecordValue>(Env.getValue(BarVal.getPointeeLoc()));
654+
*cast<PointerValue>(getFieldValue(&FooPointeeLoc, *BarDecl, Env));
655+
const auto &BarPointeeLoc =
656+
cast<RecordStorageLocation>(BarVal.getPointeeLoc());
657657

658-
EXPECT_THAT(getFieldValue(&BarPointeeVal, *FooRefDecl, Env), NotNull());
658+
EXPECT_THAT(getFieldValue(&BarPointeeLoc, *FooRefDecl, Env), NotNull());
659659

660660
const auto &FooPtrVal = *cast<PointerValue>(
661-
getFieldValue(&BarPointeeVal, *FooPtrDecl, Env));
661+
getFieldValue(&BarPointeeLoc, *FooPtrDecl, Env));
662662
const auto &FooPtrPointeeLoc =
663663
cast<RecordStorageLocation>(FooPtrVal.getPointeeLoc());
664664
EXPECT_THAT(Env.getValue(FooPtrPointeeLoc), IsNull());
665665

666-
EXPECT_THAT(getFieldValue(&BarPointeeVal, *BazRefDecl, Env), NotNull());
666+
EXPECT_THAT(getFieldValue(&BarPointeeLoc, *BazRefDecl, Env), NotNull());
667667

668668
const auto &BazPtrVal = *cast<PointerValue>(
669-
getFieldValue(&BarPointeeVal, *BazPtrDecl, Env));
669+
getFieldValue(&BarPointeeLoc, *BazPtrDecl, Env));
670670
const StorageLocation &BazPtrPointeeLoc = BazPtrVal.getPointeeLoc();
671671
EXPECT_THAT(Env.getValue(BazPtrPointeeLoc), NotNull());
672672
});
@@ -2478,9 +2478,10 @@ TEST(TransferTest, BindTemporary) {
24782478
const ValueDecl *BazDecl = findValueDecl(ASTCtx, "Baz");
24792479
ASSERT_THAT(BazDecl, NotNull());
24802480

2481-
const auto &FooVal = *cast<RecordValue>(Env.getValue(*FooDecl));
2481+
const auto &FooLoc =
2482+
*cast<RecordStorageLocation>(Env.getStorageLocation(*FooDecl));
24822483
const auto *BarVal = cast<IntegerValue>(Env.getValue(*BarDecl));
2483-
EXPECT_EQ(BarVal, getFieldValue(&FooVal, *BazDecl, Env));
2484+
EXPECT_EQ(BarVal, getFieldValue(&FooLoc, *BazDecl, Env));
24842485
});
24852486
}
24862487

@@ -2970,16 +2971,14 @@ TEST(TransferTest, AggregateInitialization) {
29702971
const auto *BarArgVal = cast<IntegerValue>(Env.getValue(*BarArgDecl));
29712972
const auto *QuxArgVal = cast<IntegerValue>(Env.getValue(*QuxArgDecl));
29722973

2973-
const auto *QuuxVal = cast<RecordValue>(Env.getValue(*QuuxDecl));
2974-
ASSERT_THAT(QuuxVal, NotNull());
2975-
2976-
const auto *BazVal =
2977-
cast<RecordValue>(getFieldValue(QuuxVal, *BazDecl, Env));
2978-
ASSERT_THAT(BazVal, NotNull());
2974+
const auto &QuuxLoc =
2975+
*cast<RecordStorageLocation>(Env.getStorageLocation(*QuuxDecl));
2976+
const auto &BazLoc =
2977+
*cast<RecordStorageLocation>(QuuxLoc.getChild(*BazDecl));
29792978

2980-
EXPECT_EQ(getFieldValue(QuuxVal, *BarDecl, Env), BarArgVal);
2981-
EXPECT_EQ(getFieldValue(BazVal, *FooDecl, Env), FooArgVal);
2982-
EXPECT_EQ(getFieldValue(QuuxVal, *QuxDecl, Env), QuxArgVal);
2979+
EXPECT_EQ(getFieldValue(&QuuxLoc, *BarDecl, Env), BarArgVal);
2980+
EXPECT_EQ(getFieldValue(&BazLoc, *FooDecl, Env), FooArgVal);
2981+
EXPECT_EQ(getFieldValue(&QuuxLoc, *QuxDecl, Env), QuxArgVal);
29832982

29842983
// Check that fields initialized in an initializer list are always
29852984
// modeled in other instances of the same type.
@@ -3653,8 +3652,9 @@ TEST(TransferTest, AssignMemberBeforeCopy) {
36533652

36543653
const auto *BarVal = cast<IntegerValue>(Env.getValue(*BarDecl));
36553654

3656-
const auto *A2Val = cast<RecordValue>(Env.getValue(*A2Decl));
3657-
EXPECT_EQ(getFieldValue(A2Val, *FooDecl, Env), BarVal);
3655+
const auto &A2Loc =
3656+
*cast<RecordStorageLocation>(Env.getStorageLocation(*A2Decl));
3657+
EXPECT_EQ(getFieldValue(&A2Loc, *FooDecl, Env), BarVal);
36583658
});
36593659
}
36603660

0 commit comments

Comments
 (0)