1 // RUN: %clang_analyze_cc1 -x c++ -std=c++14 -analyzer-checker=core -analyzer-output=text -verify %s
3 int initializer1(int &p, int x) {
4 if (x) { // expected-note{{Taking false branch}}
8 return 1; // expected-note {{Returning without writing to 'p'}}
12 int param_not_initialized_by_func() {
13 int outP; // expected-note {{'outP' declared without an initial value}}
14 int out = initializer1(outP, 0); // expected-note{{Calling 'initializer1'}}
15 // expected-note@-1{{Returning from 'initializer1'}}
16 return outP; // expected-note{{Undefined or garbage value returned to caller}}
17 // expected-warning@-1{{Undefined or garbage value returned to caller}}
21 int initialize(int *p, int param) {
22 if (param) { //expected-note{{Taking false branch}}
26 return 0; // expected-note{{Returning without writing to '*p'}}
31 int p; //expected-note{{'p' declared without an initial value}}
32 s->initialize(&p, 0); //expected-note{{Calling 'S::initialize'}}
33 //expected-note@-1{{Returning from 'S::initialize'}}
34 return p; // expected-warning{{Undefined or garbage value returned to caller}}
35 // expected-note@-1{{Undefined or garbage value returned to caller}}
38 int initializer2(const int &p) {
42 int no_msg_const_ref() {
43 int p; //expected-note{{'p' declared without an initial value}}
45 return p; // expected-warning{{Undefined or garbage value returned to caller}}
46 // expected-note@-1{{Undefined or garbage value returned to caller}}
50 void init_in_nested_func(int **x) {
51 *x = 0; // expected-note{{Null pointer value stored to 'y'}}
55 int call_init_nested() {
58 init_in_nested_func(&y); // expected-note{{Calling 'init_in_nested_func'}}
59 // expected-note@-1{{Returning from 'init_in_nested_func'}}
60 return *y; //expected-warning{{Dereference of null pointer (loaded from variable 'y')}}
61 //expected-note@-1{{Dereference of null pointer (loaded from variable 'y')}}
69 void partial_init_by_reference(A &a) {
71 } // expected-note {{Returning without writing to 'a.y'}}
73 int use_partial_init_by_reference() {
75 partial_init_by_reference(a); // expected-note{{Calling 'partial_init_by_reference'}}
76 // expected-note@-1{{Returning from 'partial_init_by_reference'}}
77 return a.y; // expected-warning{{Undefined or garbage value returned to caller}}
78 // expected-note@-1{{Undefined or garbage value returned to caller}}
84 void partially_init_inherited_struct(B *b) {
86 } // expected-note{{Returning without writing to 'b->y'}}
88 int use_partially_init_inherited_struct() {
90 partially_init_inherited_struct(&b); // expected-note{{Calling 'partially_init_inherited_struct'}}
91 // expected-note@-1{{Returning from 'partially_init_inherited_struct'}}
92 return b.y; // expected-warning{{Undefined or garbage value returned to caller}}
93 // expected-note@-1{{Undefined or garbage value returned to caller}}
99 C(int pX, int pY) : x(pX) {} // expected-note{{Returning without writing to 'this->y'}}
101 C(int pX, int pY, bool Flag) {
103 if (Flag) // expected-note{{Assuming 'Flag' is not equal to 0}}
104 // expected-note@-1{{Taking true branch}}
105 return; // expected-note{{Returning without writing to 'this->y'}}
110 int use_constructor() {
111 C c(0, 0); // expected-note{{Calling constructor for 'C'}}
112 // expected-note@-1{{Returning from constructor for 'C'}}
113 return c.y; // expected-note{{Undefined or garbage value returned to caller}}
114 // expected-warning@-1{{Undefined or garbage value returned to caller}}
119 int use_other_constructor() {
120 C c(0, 0, coin()); // expected-note{{Calling constructor for 'C'}}
121 // expected-note@-1{{Returning from constructor for 'C'}}
122 return c.y; // expected-note{{Undefined or garbage value returned to caller}}
123 // expected-warning@-1{{Undefined or garbage value returned to caller}}
127 void initialize(int *);
130 void D::initialize(int *p) {
132 } // expected-note{{Returning without writing to '*p'}}
134 int use_d_initializer(D* d) {
135 int p; // expected-note {{'p' declared without an initial value}}
136 d->initialize(&p); // expected-note{{Calling 'D::initialize'}}
137 // expected-note@-1{{Returning from 'D::initialize'}}
138 return p; // expected-note{{Undefined or garbage value returned to caller}}
139 // expected-warning@-1{{Undefined or garbage value returned to caller}}
146 int pointerreference(S2* &s) {
147 if (coin()) // expected-note{{Assuming the condition is true}}
148 // expected-note@-1{{Taking true branch}}
149 return 1; // expected-note{{Returning without writing to 's->x'}}
155 int usepointerreference() {
158 pointerreference(p); //expected-note{{Calling 'pointerreference'}}
159 //expected-note@-1{{Returning from 'pointerreference'}}
160 return s.x; // expected-warning{{Undefined or garbage value returned to caller}}
161 // expected-note@-1{{Undefined or garbage value returned to caller}}
164 void *has_no_argument_and_returns_null(void) {
168 void rdar40335545() {
169 int local; // expected-note{{}}
170 void (*takes_int_ptr_argument)(int *) = (void (*)(int*))has_no_argument_and_returns_null;
172 takes_int_ptr_argument(&local); // no-crash
174 int useLocal = local; //expected-warning{{}}
175 //expected-note@-1{{}}
183 HasRef(int &a) : a(a) {}
187 void maybeInitialize(const HasRef &&pA) {
188 if (coin()) // expected-note{{Assuming the condition is false}}
189 // expected-note@-1{{Taking false branch}}
191 } // expected-note{{Returning without writing to 'pA.a'}}
193 int useMaybeInitializerWritingIntoField() {
194 int z; // expected-note{{'z' declared without an initial value}}
195 maybeInitialize(HasRef(z)); // expected-note{{Calling constructor for 'HasRef'}}
196 // expected-note@-1{{Returning from constructor for 'HasRef'}}
197 // expected-note@-2{{Calling 'maybeInitialize'}}
198 // expected-note@-3{{Returning from 'maybeInitialize'}}
199 return z; // expected-warning{{Undefined or garbage value returned to caller}}
200 // expected-note@-1{{Undefined or garbage value returned to caller}}
205 struct HasRefToItself {
206 HasRefToItself &Ref; // no infinite loop
208 HasRefToItself(int &z) : Ref(*this), z(z) {}
211 void maybeInitialize(const HasRefToItself &&pA) {
212 if (coin()) // expected-note{{Assuming the condition is false}}
213 // expected-note@-1{{Taking false branch}}
215 } // expected-note{{Returning without writing to 'pA.Ref.z'}}
217 int useMaybeInitializerWritingIntoFieldWithRefToItself() {
218 int z; // expected-note{{'z' declared without an initial value}}
219 maybeInitialize(HasRefToItself(z)); // expected-note{{Calling constructor for 'HasRefToItself'}}
220 // expected-note@-1{{Returning from constructor for 'HasRefToItself'}}
221 // expected-note@-2{{Calling 'maybeInitialize'}}
222 // expected-note@-3{{Returning from 'maybeInitialize'}}
223 return z; // expected-warning{{Undefined or garbage value returned to caller}}
224 // expected-note@-1{{Undefined or garbage value returned to caller}}
229 void maybeInitialize(const HasRef *pA) {
230 if (coin()) // expected-note{{Assuming the condition is false}}
231 // expected-note@-1{{Taking false branch}}
233 } // expected-note{{Returning without writing to 'pA->a'}}
235 int useMaybeInitializerStructByPointer() {
236 int z; // expected-note{{'z' declared without an initial value}}
237 HasRef wrapper(z); // expected-note{{Calling constructor for 'HasRef'}}
238 // expected-note@-1{{Returning from constructor for 'HasRef'}}
239 maybeInitialize(&wrapper); // expected-note{{Calling 'maybeInitialize'}}
240 // expected-note@-1{{Returning from 'maybeInitialize'}}
241 return z; // expected-warning{{Undefined or garbage value returned to caller}}
242 // expected-note@-1{{Undefined or garbage value returned to caller}}
247 struct HasParentWithRef : public HasRef {
248 HasParentWithRef(int &a) : HasRef(a) {} // expected-note{{Calling constructor for 'HasRef'}}
249 // expected-note@-1{{Returning from constructor for 'HasRef'}}
252 void maybeInitializeWithParent(const HasParentWithRef &pA) {
253 if (coin()) // expected-note{{Assuming the condition is false}}
254 // expected-note@-1{{Taking false branch}}
256 } // expected-note{{Returning without writing to 'pA.a'}}
258 int useMaybeInitializerWritingIntoParentField() {
259 int z; // expected-note{{'z' declared without an initial value}}
260 maybeInitializeWithParent(HasParentWithRef(z)); // expected-note{{Calling constructor for 'HasParentWithRef'}}
261 // expected-note@-1{{Returning from constructor for 'HasParentWithRef'}}
262 // expected-note@-2{{Calling 'maybeInitializeWithParent'}}
263 // expected-note@-3{{Returning from 'maybeInitializeWithParent'}}
264 return z; // expected-warning{{Undefined or garbage value returned to caller}}
265 // expected-note@-1{{Undefined or garbage value returned to caller}}
270 struct HasIndirectRef {
272 HasIndirectRef(HasRef &Ref) : Ref(Ref) {}
275 void maybeInitializeIndirectly(const HasIndirectRef &pA) {
276 if (coin()) // expected-note{{Assuming the condition is false}}
277 // expected-note@-1{{Taking false branch}}
279 } // expected-note{{Returning without writing to 'pA.Ref.a'}}
281 int useMaybeInitializeIndirectly() {
282 int z; // expected-note{{'z' declared without an initial value}}
283 HasRef r(z); // expected-note{{Calling constructor for 'HasRef'}}
284 // expected-note@-1{{Returning from constructor for 'HasRef'}}
285 maybeInitializeIndirectly(HasIndirectRef(r)); // expected-note{{Calling 'maybeInitializeIndirectly'}}
286 // expected-note@-1{{Returning from 'maybeInitializeIndirectly'}}
287 return z; // expected-warning{{Undefined or garbage value returned to caller}}
288 // expected-note@-1{{Undefined or garbage value returned to caller}}
293 struct HasIndirectRefByValue {
295 HasIndirectRefByValue(HasRef Ref) : Ref(Ref) {}
298 void maybeInitializeIndirectly(const HasIndirectRefByValue &pA) {
299 if (coin()) // expected-note{{Assuming the condition is false}}
300 // expected-note@-1{{Taking false branch}}
302 } // expected-note{{Returning without writing to 'pA.Ref.a'}}
304 int useMaybeInitializeIndirectlyIndirectRefByValue() {
305 int z; // expected-note{{'z' declared without an initial value}}
306 HasRef r(z); // expected-note{{Calling constructor for 'HasRef'}}
307 // expected-note@-1{{Returning from constructor for 'HasRef'}}
308 maybeInitializeIndirectly(HasIndirectRefByValue(r)); // expected-note{{Calling 'maybeInitializeIndirectly'}}
309 // expected-note@-1{{Returning from 'maybeInitializeIndirectly'}}
310 return z; // expected-warning{{Undefined or garbage value returned to caller}}
311 // expected-note@-1{{Undefined or garbage value returned to caller}}
316 struct HasIndirectPointerRef {
318 HasIndirectPointerRef(HasRef *Ref) : Ref(Ref) {}
321 void maybeInitializeIndirectly(const HasIndirectPointerRef &pA) {
322 if (coin()) // expected-note{{Assuming the condition is false}}
323 // expected-note@-1{{Taking false branch}}
325 } // expected-note{{Returning without writing to 'pA.Ref->a'}}
327 int useMaybeInitializeIndirectlyWithPointer() {
328 int z; // expected-note{{'z' declared without an initial value}}
329 HasRef r(z); // expected-note{{Calling constructor for 'HasRef'}}
330 // expected-note@-1{{Returning from constructor for 'HasRef'}}
331 maybeInitializeIndirectly(HasIndirectPointerRef(&r)); // expected-note{{Calling 'maybeInitializeIndirectly'}}
332 // expected-note@-1{{Returning from 'maybeInitializeIndirectly'}}
333 return z; // expected-warning{{Undefined or garbage value returned to caller}}
334 // expected-note@-1{{Undefined or garbage value returned to caller}}
347 void maybeInitializeHasField(HasFieldA *b) {
348 if (coin()) // expected-note{{Assuming the condition is false}}
349 // expected-note@-1{{Taking false branch}}
350 ((HasFieldB*)b)->x = 120;
353 int forceElementRegionApperence() {
355 maybeInitializeHasField(&a); // expected-note{{Calling 'maybeInitializeHasField'}}
356 // expected-note@-1{{Returning from 'maybeInitializeHasField'}}
357 return ((HasFieldB*)&a)->x; // expected-warning{{Undefined or garbage value returned to caller}}
358 // expected-note@-1{{Undefined or garbage value returned to caller}}
363 struct HasForgottenField {
365 HasForgottenField() {} // expected-note{{Returning without writing to 'this->x'}}
368 // Test that tracking across exclamation mark works.
369 bool tracksThroughExclamationMark() {
370 HasForgottenField a; // expected-note{{Calling default constructor for 'HasForgottenField'}}
371 // expected-note@-1{{Returning from default constructor for 'HasForgottenField'}}
372 return !a.x; // expected-warning{{Undefined or garbage value returned to caller}}
373 // expected-note@-1{{Undefined or garbage value returned to caller}}