1 //===- unittests/Analysis/FlowSensitive/ValueTest.cpp ---===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8
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"
14 #include <memory>
15
16 namespace {
17
18 using namespace clang;
19 using namespace dataflow;
20
TEST(ValueTest,EquivalenceReflexive)21 TEST(ValueTest, EquivalenceReflexive) {
22 IntegerValue V;
23 EXPECT_TRUE(areEquivalentValues(V, V));
24 }
25
TEST(ValueTest,DifferentIntegerValuesNotEquivalent)26 TEST(ValueTest, DifferentIntegerValuesNotEquivalent) {
27 IntegerValue V1;
28 IntegerValue V2;
29 EXPECT_FALSE(areEquivalentValues(V1, V2));
30 }
31
TEST(ValueTest,AliasedPointersEquivalent)32 TEST(ValueTest, AliasedPointersEquivalent) {
33 auto L = ScalarStorageLocation(QualType());
34 PointerValue V1(L);
35 PointerValue V2(L);
36 EXPECT_TRUE(areEquivalentValues(V1, V2));
37 EXPECT_TRUE(areEquivalentValues(V2, V1));
38 }
39
TEST(ValueTest,TopsEquivalent)40 TEST(ValueTest, TopsEquivalent) {
41 Arena A;
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));
46 }
47
48 // The framework does not (currently) consider equivalence for values with
49 // properties, leaving such to individual analyses.
TEST(ValueTest,ValuesWithSamePropsDifferent)50 TEST(ValueTest, ValuesWithSamePropsDifferent) {
51 Arena A;
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));
59 }
60
TEST(ValueTest,ValuesWithDifferentPropsDifferent)61 TEST(ValueTest, ValuesWithDifferentPropsDifferent) {
62 Arena A;
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));
71 }
72
TEST(ValueTest,ValuesWithDifferentNumberPropsDifferent)73 TEST(ValueTest, ValuesWithDifferentNumberPropsDifferent) {
74 Arena A;
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));
82 }
83
TEST(ValueTest,DifferentKindsNotEquivalent)84 TEST(ValueTest, DifferentKindsNotEquivalent) {
85 Arena A;
86 auto L = ScalarStorageLocation(QualType());
87 PointerValue V1(L);
88 TopBoolValue V2(A.makeAtomRef(Atom(0)));
89 EXPECT_FALSE(areEquivalentValues(V1, V2));
90 EXPECT_FALSE(areEquivalentValues(V2, V1));
91 }
92
TEST(ValueTest,NotAliasedPointersNotEquivalent)93 TEST(ValueTest, NotAliasedPointersNotEquivalent) {
94 auto L1 = ScalarStorageLocation(QualType());
95 PointerValue V1(L1);
96 auto L2 = ScalarStorageLocation(QualType());
97 PointerValue V2(L2);
98 EXPECT_FALSE(areEquivalentValues(V1, V2));
99 EXPECT_FALSE(areEquivalentValues(V2, V1));
100 }
101
102 } // namespace
103