summaryrefslogtreecommitdiffstats
path: root/test/Analysis/const-method-call.cpp
blob: 17df2a016b89267d49de7337912f1c0d6b975f7e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
// RUN: %clang_analyze_cc1 -analyzer-checker=core,debug.ExprInspection -verify %s

void clang_analyzer_eval(bool);

struct A {
  int x;
  void foo() const;
  void bar();
};

struct B {
  mutable int mut;
  void foo() const;
};

struct C {
  int *p;
  void foo() const;
};

struct MutBase {
  mutable int b_mut;
};

struct MutDerived : MutBase {
  void foo() const;
};

struct PBase {
  int *p;
};

struct PDerived : PBase {
  void foo() const;
};

struct Inner {
  int x;
  int *p;
  void bar() const;
};

struct Outer {
  int x;
  Inner in;
  void foo() const;
};

void checkThatConstMethodWithoutDefinitionDoesNotInvalidateObject() {
  A t;
  t.x = 3;
  t.foo();
  clang_analyzer_eval(t.x == 3); // expected-warning{{TRUE}}
  // Test non-const does invalidate
  t.bar();
  clang_analyzer_eval(t.x); // expected-warning{{UNKNOWN}}
}

void checkThatConstMethodDoesInvalidateMutableFields() {
  B t;
  t.mut = 4;
  t.foo();
  clang_analyzer_eval(t.mut); // expected-warning{{UNKNOWN}}
}

void checkThatConstMethodDoesInvalidatePointedAtMemory() {
  int x = 1;
  C t;
  t.p = &x;
  t.foo();
  clang_analyzer_eval(x); // expected-warning{{UNKNOWN}}
  clang_analyzer_eval(t.p == &x); // expected-warning{{TRUE}}
}

void checkThatConstMethodDoesInvalidateInheritedMutableFields() {
  MutDerived t;
  t.b_mut = 4;
  t.foo();
  clang_analyzer_eval(t.b_mut); // expected-warning{{UNKNOWN}}
}

void checkThatConstMethodDoesInvalidateInheritedPointedAtMemory() {
  int x = 1;
  PDerived t;
  t.p = &x;
  t.foo();
  clang_analyzer_eval(x); // expected-warning{{UNKNOWN}}
  clang_analyzer_eval(t.p == &x); // expected-warning{{TRUE}}
}

void checkThatConstMethodDoesInvalidateContainedPointedAtMemory() {
  int x = 1;
  Outer t;
  t.x = 2;
  t.in.p = &x;
  t.foo();
  clang_analyzer_eval(x); // expected-warning{{UNKNOWN}}
  clang_analyzer_eval(t.x == 2); // expected-warning{{TRUE}}
  clang_analyzer_eval(t.in.p == &x); // expected-warning{{TRUE}}
}

void checkThatContainedConstMethodDoesNotInvalidateObjects() {
  Outer t;
  t.x = 1;
  t.in.x = 2;
  t.in.bar();
  clang_analyzer_eval(t.x == 1); // expected-warning{{TRUE}}
  clang_analyzer_eval(t.in.x == 2); // expected-warning{{TRUE}}
}

// --- Versions of the above tests where the const method is inherited --- //

struct B1 {
  void foo() const;
};

struct D1 : public B1 {
  int x;
};

struct D2 : public B1 {
  mutable int mut;
};

struct D3 : public B1 {
  int *p;
};

struct DInner : public B1 {
  int x;
  int *p;
};

struct DOuter : public B1 {
  int x;
  DInner in;
};

void checkThatInheritedConstMethodDoesNotInvalidateObject() {
  D1 t;
  t.x = 1;
  t.foo();
  clang_analyzer_eval(t.x == 1); // expected-warning{{TRUE}}
}

void checkThatInheritedConstMethodDoesInvalidateMutableFields() {
  D2 t;
  t.mut = 1;
  t.foo();
  clang_analyzer_eval(t.mut); // expected-warning{{UNKNOWN}}
}

void checkThatInheritedConstMethodDoesInvalidatePointedAtMemory() {
  int x = 1;
  D3 t;
  t.p = &x;
  t.foo();
  clang_analyzer_eval(x); // expected-warning{{UNKNOWN}}
  clang_analyzer_eval(t.p == &x); // expected-warning{{TRUE}}
}

void checkThatInheritedConstMethodDoesInvalidateContainedPointedAtMemory() {
  int x = 1;
  DOuter t;
  t.x = 2;
  t.in.x = 3;
  t.in.p = &x;
  t.foo();
  clang_analyzer_eval(x); // expected-warning{{UNKNOWN}}
  clang_analyzer_eval(t.x == 2); // expected-warning{{TRUE}}
  clang_analyzer_eval(t.in.x == 3); // expected-warning{{TRUE}}
  clang_analyzer_eval(t.in.p == &x); // expected-warning{{TRUE}}
}

void checkThatInheritedContainedConstMethodDoesNotInvalidateObjects() {
  DOuter t;
  t.x = 1;
  t.in.x = 2;
  t.in.foo();
  clang_analyzer_eval(t.x == 1); // expected-warning{{TRUE}}
  clang_analyzer_eval(t.in.x == 2); // expected-warning{{TRUE}}
}

// --- PR21606 --- //

struct s1 {
    void g(const int *i) const;
};

struct s2 {
    void f(int *i) {
        m_i = i;
        m_s.g(m_i);
        if (m_i)
            *i = 42; // no-warning
    }

    int *m_i;
    s1 m_s;
};

void PR21606()
{
    s2().f(0);
}

// --- PR25392 --- //

struct HasConstMemberFunction {
public:
  void constMemberFunction() const;
};

HasConstMemberFunction hasNoReturn() { } // expected-warning {{control reaches end of non-void function}}

void testUnknownWithConstMemberFunction() {
  hasNoReturn().constMemberFunction();
}

void testNonRegionLocWithConstMemberFunction() {
  (*((HasConstMemberFunction *)(&&label))).constMemberFunction();

  label: return;
}

// FIXME
// When there is a circular reference to an object and a const method is called
// the object is not invalidated because TK_PreserveContents has already been
// set.
struct Outer2;

struct InnerWithRef {
  Outer2 *ref;
};

struct Outer2 {
  int x;
  InnerWithRef in;
  void foo() const;
};

void checkThatConstMethodCallDoesInvalidateObjectForCircularReferences() {
  Outer2 t;
  t.x = 1;
  t.in.ref = &t;
  t.foo();
  // FIXME: Should be UNKNOWN.
  clang_analyzer_eval(t.x); // expected-warning{{TRUE}}
}