1 //===- unittests/Analysis/FlowSensitive/ValueTest.cpp ---===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "clang/Analysis/FlowSensitive/Value.h"
10 #include "clang/Analysis/FlowSensitive/Arena.h"
11 #include "clang/Analysis/FlowSensitive/StorageLocation.h"
12 #include "gmock/gmock.h"
13 #include "gtest/gtest.h"
18 using namespace clang
;
19 using namespace dataflow
;
21 TEST(ValueTest
, EquivalenceReflexive
) {
23 EXPECT_TRUE(areEquivalentValues(V
, V
));
26 TEST(ValueTest
, DifferentIntegerValuesNotEquivalent
) {
29 EXPECT_FALSE(areEquivalentValues(V1
, V2
));
32 TEST(ValueTest
, AliasedPointersEquivalent
) {
33 auto L
= ScalarStorageLocation(QualType());
36 EXPECT_TRUE(areEquivalentValues(V1
, V2
));
37 EXPECT_TRUE(areEquivalentValues(V2
, V1
));
40 TEST(ValueTest
, TopsEquivalent
) {
42 TopBoolValue
V1(A
.makeAtomRef(Atom(0)));
43 TopBoolValue
V2(A
.makeAtomRef(Atom(1)));
44 EXPECT_TRUE(areEquivalentValues(V1
, V2
));
45 EXPECT_TRUE(areEquivalentValues(V2
, V1
));
48 // The framework does not (currently) consider equivalence for values with
49 // properties, leaving such to individual analyses.
50 TEST(ValueTest
, ValuesWithSamePropsDifferent
) {
52 TopBoolValue
Prop(A
.makeAtomRef(Atom(0)));
53 TopBoolValue
V1(A
.makeAtomRef(Atom(2)));
54 TopBoolValue
V2(A
.makeAtomRef(Atom(3)));
55 V1
.setProperty("foo", Prop
);
56 V2
.setProperty("foo", Prop
);
57 EXPECT_FALSE(areEquivalentValues(V1
, V2
));
58 EXPECT_FALSE(areEquivalentValues(V2
, V1
));
61 TEST(ValueTest
, ValuesWithDifferentPropsDifferent
) {
63 TopBoolValue
Prop1(A
.makeAtomRef(Atom(0)));
64 TopBoolValue
Prop2(A
.makeAtomRef(Atom(1)));
65 TopBoolValue
V1(A
.makeAtomRef(Atom(2)));
66 TopBoolValue
V2(A
.makeAtomRef(Atom(3)));
67 V1
.setProperty("foo", Prop1
);
68 V2
.setProperty("bar", Prop2
);
69 EXPECT_FALSE(areEquivalentValues(V1
, V2
));
70 EXPECT_FALSE(areEquivalentValues(V2
, V1
));
73 TEST(ValueTest
, ValuesWithDifferentNumberPropsDifferent
) {
75 TopBoolValue
Prop(A
.makeAtomRef(Atom(0)));
76 TopBoolValue
V1(A
.makeAtomRef(Atom(2)));
77 TopBoolValue
V2(A
.makeAtomRef(Atom(3)));
78 // Only set a property on `V1`.
79 V1
.setProperty("foo", Prop
);
80 EXPECT_FALSE(areEquivalentValues(V1
, V2
));
81 EXPECT_FALSE(areEquivalentValues(V2
, V1
));
84 TEST(ValueTest
, DifferentKindsNotEquivalent
) {
86 auto L
= ScalarStorageLocation(QualType());
88 TopBoolValue
V2(A
.makeAtomRef(Atom(0)));
89 EXPECT_FALSE(areEquivalentValues(V1
, V2
));
90 EXPECT_FALSE(areEquivalentValues(V2
, V1
));
93 TEST(ValueTest
, NotAliasedPointersNotEquivalent
) {
94 auto L1
= ScalarStorageLocation(QualType());
96 auto L2
= ScalarStorageLocation(QualType());
98 EXPECT_FALSE(areEquivalentValues(V1
, V2
));
99 EXPECT_FALSE(areEquivalentValues(V2
, V1
));