blob: 39d4a7f80183139be1243ca5e500d4e82cd7aa91 [file] [log] [blame]
Kristof Umann30f08652018-06-18 11:50:17 +00001// RUN: %clang_analyze_cc1 -analyzer-checker=core,alpha.cplusplus.UninitializedObject -analyzer-config alpha.cplusplus.UninitializedObject:Pedantic=true -std=c++11 -DPEDANTIC -verify %s
2
3// RUN: %clang_analyze_cc1 -analyzer-checker=core,alpha.cplusplus.UninitializedObject -std=c++11 -verify %s
4
5//===----------------------------------------------------------------------===//
6// Default constructor test.
7//===----------------------------------------------------------------------===//
8
9class CompilerGeneratedConstructorTest {
10 int a, b, c, d, e, f, g, h, i, j;
11
12public:
13 CompilerGeneratedConstructorTest() = default;
14};
15
16void fCompilerGeneratedConstructorTest() {
17 CompilerGeneratedConstructorTest();
18}
19
20#ifdef PEDANTIC
21class DefaultConstructorTest {
22 int a; // expected-note{{uninitialized field 'this->a'}}
23
24public:
25 DefaultConstructorTest();
26};
27
28DefaultConstructorTest::DefaultConstructorTest() = default;
29
30void fDefaultConstructorTest() {
31 DefaultConstructorTest(); // expected-warning{{1 uninitialized field}}
32}
33#else
34class DefaultConstructorTest {
35 int a;
36
37public:
38 DefaultConstructorTest();
39};
40
41DefaultConstructorTest::DefaultConstructorTest() = default;
42
43void fDefaultConstructorTest() {
44 DefaultConstructorTest();
45}
46#endif // PEDANTIC
47
48//===----------------------------------------------------------------------===//
49// Initializer list test.
50//===----------------------------------------------------------------------===//
51
52class InitListTest1 {
53 int a;
54 int b;
55
56public:
57 InitListTest1()
58 : a(1),
59 b(2) {
60 // All good!
61 }
62};
63
64void fInitListTest1() {
65 InitListTest1();
66}
67
68class InitListTest2 {
69 int a;
70 int b; // expected-note{{uninitialized field 'this->b'}}
71
72public:
73 InitListTest2()
74 : a(3) {} // expected-warning{{1 uninitialized field}}
75};
76
77void fInitListTest2() {
78 InitListTest2();
79}
80
81class InitListTest3 {
82 int a; // expected-note{{uninitialized field 'this->a'}}
83 int b;
84
85public:
86 InitListTest3()
87 : b(4) {} // expected-warning{{1 uninitialized field}}
88};
89
90void fInitListTest3() {
91 InitListTest3();
92}
93
94//===----------------------------------------------------------------------===//
95// Constructor body test.
96//===----------------------------------------------------------------------===//
97
98class CtorBodyTest1 {
99 int a, b;
100
101public:
102 CtorBodyTest1() {
103 a = 5;
104 b = 6;
105 // All good!
106 }
107};
108
109void fCtorBodyTest1() {
110 CtorBodyTest1();
111}
112
113class CtorBodyTest2 {
114 int a;
115 int b; // expected-note{{uninitialized field 'this->b'}}
116
117public:
118 CtorBodyTest2() {
119 a = 7; // expected-warning{{1 uninitialized field}}
120 }
121};
122
123void fCtorBodyTest2() {
124 CtorBodyTest2();
125}
126
127class CtorBodyTest3 {
128 int a; // expected-note{{uninitialized field 'this->a'}}
129 int b;
130
131public:
132 CtorBodyTest3() {
133 b = 8; // expected-warning{{1 uninitialized field}}
134 }
135};
136
137void fCtorBodyTest3() {
138 CtorBodyTest3();
139}
140
141#ifdef PEDANTIC
142class CtorBodyTest4 {
143 int a; // expected-note{{uninitialized field 'this->a'}}
144 int b; // expected-note{{uninitialized field 'this->b'}}
145
146public:
147 CtorBodyTest4() {}
148};
149
150void fCtorBodyTest4() {
151 CtorBodyTest4(); // expected-warning{{2 uninitialized fields}}
152}
153#else
154class CtorBodyTest4 {
155 int a;
156 int b;
157
158public:
159 CtorBodyTest4() {}
160};
161
162void fCtorBodyTest4() {
163 CtorBodyTest4();
164}
165#endif
166
167//===----------------------------------------------------------------------===//
168// Constructor delegation test.
169//===----------------------------------------------------------------------===//
170
171class CtorDelegationTest1 {
172 int a;
173 int b;
174
175public:
176 CtorDelegationTest1(int)
177 : a(9) {
178 // leaves 'b' unintialized, but we'll never check this function
179 }
180
181 CtorDelegationTest1()
182 : CtorDelegationTest1(int{}) { // Initializing 'a'
183 b = 10;
184 // All good!
185 }
186};
187
188void fCtorDelegationTest1() {
189 CtorDelegationTest1();
190}
191
192class CtorDelegationTest2 {
193 int a; // expected-note{{uninitialized field 'this->a'}}
194 int b;
195
196public:
197 CtorDelegationTest2(int)
198 : b(11) {
199 // leaves 'a' unintialized, but we'll never check this function
200 }
201
202 CtorDelegationTest2()
203 : CtorDelegationTest2(int{}) { // expected-warning{{1 uninitialized field}}
204 }
205};
206
207void fCtorDelegationTest2() {
208 CtorDelegationTest2();
209}
210
211//===----------------------------------------------------------------------===//
212// Tests for classes containing records.
213//===----------------------------------------------------------------------===//
214
215class ContainsRecordTest1 {
216 struct RecordType {
217 int x;
218 int y;
219 } rec;
220 int c, d;
221
222public:
223 ContainsRecordTest1()
224 : rec({12, 13}),
225 c(14),
226 d(15) {
227 // All good!
228 }
229};
230
231void fContainsRecordTest1() {
232 ContainsRecordTest1();
233}
234
235class ContainsRecordTest2 {
236 struct RecordType {
237 int x;
238 int y; // expected-note{{uninitialized field 'this->rec.y'}}
239 } rec;
240 int c, d;
241
242public:
243 ContainsRecordTest2()
244 : c(16),
245 d(17) {
246 rec.x = 18; // expected-warning{{1 uninitialized field}}
247 }
248};
249
250void fContainsRecordTest2() {
251 ContainsRecordTest2();
252}
253
254class ContainsRecordTest3 {
255 struct RecordType {
256 int x; // expected-note{{uninitialized field 'this->rec.x'}}
257 int y; // expected-note{{uninitialized field 'this->rec.y'}}
258 } rec;
259 int c, d;
260
261public:
262 ContainsRecordTest3()
263 : c(19),
264 d(20) { // expected-warning{{2 uninitialized fields}}
265 }
266};
267
268void fContainsRecordTest3() {
269 ContainsRecordTest3();
270}
271
272class ContainsRecordTest4 {
273 struct RecordType {
274 int x; // expected-note{{uninitialized field 'this->rec.x'}}
275 int y; // expected-note{{uninitialized field 'this->rec.y'}}
276 } rec;
277 int c, d; // expected-note{{uninitialized field 'this->d'}}
278
279public:
280 ContainsRecordTest4()
281 : c(19) { // expected-warning{{3 uninitialized fields}}
282 }
283};
284
285void fContainsRecordTest4() {
286 ContainsRecordTest4();
287}
288
289//===----------------------------------------------------------------------===//
290// Tests for template classes.
291//===----------------------------------------------------------------------===//
292
293template <class T>
294class IntTemplateClassTest1 {
295 T t;
296 int b;
297
298public:
299 IntTemplateClassTest1(T i) {
300 b = 21;
301 t = i;
302 // All good!
303 }
304};
305
306void fIntTemplateClassTest1() {
307 IntTemplateClassTest1<int>(22);
308}
309
310template <class T>
311class IntTemplateClassTest2 {
312 T t; // expected-note{{uninitialized field 'this->t'}}
313 int b;
314
315public:
316 IntTemplateClassTest2() {
317 b = 23; // expected-warning{{1 uninitialized field}}
318 }
319};
320
321void fIntTemplateClassTest2() {
322 IntTemplateClassTest2<int>();
323}
324
325struct Record {
326 int x; // expected-note{{uninitialized field 'this->t.x'}}
327 int y; // expected-note{{uninitialized field 'this->t.y'}}
328};
329
330template <class T>
331class RecordTemplateClassTest {
332 T t;
333 int b;
334
335public:
336 RecordTemplateClassTest() {
337 b = 24; // expected-warning{{2 uninitialized fields}}
338 }
339};
340
341void fRecordTemplateClassTest() {
342 RecordTemplateClassTest<Record>();
343}
344
345//===----------------------------------------------------------------------===//
346// Tests involving functions with unknown implementations.
347//===----------------------------------------------------------------------===//
348
349template <class T>
350void mayInitialize(T &);
351
352template <class T>
353void wontInitialize(const T &);
354
355class PassingToUnknownFunctionTest1 {
356 int a, b;
357
358public:
359 PassingToUnknownFunctionTest1() {
360 mayInitialize(a);
361 mayInitialize(b);
362 // All good!
363 }
364
365 PassingToUnknownFunctionTest1(int) {
366 mayInitialize(a);
367 // All good!
368 }
369
370 PassingToUnknownFunctionTest1(int, int) {
371 mayInitialize(*this);
372 // All good!
373 }
374};
375
376void fPassingToUnknownFunctionTest1() {
377 PassingToUnknownFunctionTest1();
378 PassingToUnknownFunctionTest1(int());
379 PassingToUnknownFunctionTest1(int(), int());
380}
381
382class PassingToUnknownFunctionTest2 {
383 int a; // expected-note{{uninitialized field 'this->a'}}
384 int b;
385
386public:
387 PassingToUnknownFunctionTest2() {
388 wontInitialize(a);
389 b = 4; // expected-warning{{1 uninitialized field}}
390 }
391};
392
393void fPassingToUnknownFunctionTest2() {
394 PassingToUnknownFunctionTest2();
395}
396
397//===----------------------------------------------------------------------===//
398// Tests for classes containing unions.
399//===----------------------------------------------------------------------===//
400
401// FIXME: As of writing this checker, there is no good support for union types
402// in the Static Analyzer. Here is non-exhaustive list of cases.
403// Note that the rules for unions are different in C and C++.
404// http://lists.llvm.org/pipermail/cfe-dev/2017-March/052910.html
405
406class ContainsSimpleUnionTest1 {
407 union SimpleUnion {
408 float uf;
409 int ui;
410 char uc;
411 } u;
412
413public:
414 ContainsSimpleUnionTest1() {
415 u.uf = 3.14;
416 // All good!
417 }
418};
419
420void fContainsSimpleUnionTest1() {
421 ContainsSimpleUnionTest1();
422}
423
424class ContainsSimpleUnionTest2 {
425 union SimpleUnion {
426 float uf;
427 int ui;
428 char uc;
429 // TODO: we'd expect the note: {{uninitialized field 'this->u'}}
430 } u; // no-note
431
432public:
433 ContainsSimpleUnionTest2() {}
434};
435
436void fContainsSimpleUnionTest2() {
437 // TODO: we'd expect the warning: {{1 uninitialized field}}
438 ContainsSimpleUnionTest2(); // no-warning
439}
440
441class UnionPointerTest1 {
442public:
443 union SimpleUnion {
444 float uf;
445 int ui;
446 char uc;
447 };
448
449private:
450 SimpleUnion *uptr;
451
452public:
453 UnionPointerTest1(SimpleUnion *uptr, int) : uptr(uptr) {
454 // All good!
455 }
456};
457
458void fUnionPointerTest1() {
459 UnionPointerTest1::SimpleUnion u;
460 u.uf = 41;
461 UnionPointerTest1(&u, int());
462}
463
464class UnionPointerTest2 {
465public:
466 union SimpleUnion {
467 float uf;
468 int ui;
469 char uc;
470 };
471
472private:
473 // TODO: we'd expect the note: {{uninitialized field 'this->uptr'}}
474 SimpleUnion *uptr; // no-note
475
476public:
477 UnionPointerTest2(SimpleUnion *uptr, char) : uptr(uptr) {}
478};
479
480void fUnionPointerTest2() {
481 UnionPointerTest2::SimpleUnion u;
482 // TODO: we'd expect the warning: {{1 uninitialized field}}
483 UnionPointerTest2(&u, int()); // no-warning
484}
485
486class ContainsUnionWithRecordTest1 {
487 union UnionWithRecord {
488 struct RecordType {
489 int x;
490 int y;
491 } us;
492 double ud;
493 long ul;
494
495 UnionWithRecord(){};
496 } u;
497
498public:
499 ContainsUnionWithRecordTest1() {
500 u.ud = 3.14;
501 // All good!
502 }
503};
504
505void fContainsUnionWithRecordTest1() {
506 ContainsUnionWithRecordTest1();
507}
508
509class ContainsUnionWithRecordTest2 {
510 union UnionWithRecord {
511 struct RecordType {
512 int x;
513 int y;
514 } us;
515 double ud;
516 long ul;
517
518 UnionWithRecord(){};
519 } u;
520
521public:
522 ContainsUnionWithRecordTest2() {
523 u.us = UnionWithRecord::RecordType{42, 43};
524 // All good!
525 }
526};
527
528void fContainsUnionWithRecordTest2() {
529 ContainsUnionWithRecordTest1();
530}
531
532class ContainsUnionWithRecordTest3 {
533 union UnionWithRecord {
534 struct RecordType {
535 int x;
536 int y;
537 } us;
538 double ud;
539 long ul;
540
541 UnionWithRecord(){};
542 // TODO: we'd expect the note: {{uninitialized field 'this->u'}}
543 } u; // no-note
544
545public:
546 ContainsUnionWithRecordTest3() {
547 UnionWithRecord::RecordType rec;
548 rec.x = 44;
549 // TODO: we'd expect the warning: {{1 uninitialized field}}
550 u.us = rec; // no-warning
551 }
552};
553
554void fContainsUnionWithRecordTest3() {
555 ContainsUnionWithRecordTest3();
556}
557
558class ContainsUnionWithSimpleUnionTest1 {
559 union UnionWithSimpleUnion {
560 union SimpleUnion {
561 float uf;
562 int ui;
563 char uc;
564 } usu;
565 long ul;
566 unsigned uu;
567 } u;
568
569public:
570 ContainsUnionWithSimpleUnionTest1() {
571 u.usu.ui = 5;
572 // All good!
573 }
574};
575
576void fContainsUnionWithSimpleUnionTest1() {
577 ContainsUnionWithSimpleUnionTest1();
578}
579
580class ContainsUnionWithSimpleUnionTest2 {
581 union UnionWithSimpleUnion {
582 union SimpleUnion {
583 float uf;
584 int ui;
585 char uc;
586 } usu;
587 long ul;
588 unsigned uu;
589 // TODO: we'd expect the note: {{uninitialized field 'this->u'}}
590 } u; // no-note
591
592public:
593 ContainsUnionWithSimpleUnionTest2() {}
594};
595
596void fContainsUnionWithSimpleUnionTest2() {
597 // TODO: we'd expect the warning: {{1 uninitialized field}}
598 ContainsUnionWithSimpleUnionTest2(); // no-warning
599}
600
601//===----------------------------------------------------------------------===//
602// Zero initialization tests.
603//===----------------------------------------------------------------------===//
604
605struct GlobalVariableTest {
606 int i;
607
608 GlobalVariableTest() {}
609};
610
611GlobalVariableTest gvt; // no-warning
612
613//===----------------------------------------------------------------------===//
614// Copy and move constructor tests.
615//===----------------------------------------------------------------------===//
616
617template <class T>
618void funcToSquelchCompilerWarnings(const T &t);
619
620#ifdef PEDANTIC
621struct CopyConstructorTest {
622 int i; // expected-note{{uninitialized field 'this->i'}}
623
624 CopyConstructorTest() : i(1337) {}
625 CopyConstructorTest(const CopyConstructorTest &other) {}
626};
627
628void fCopyConstructorTest() {
629 CopyConstructorTest cct;
630 CopyConstructorTest copy = cct; // expected-warning{{1 uninitialized field}}
631 funcToSquelchCompilerWarnings(copy);
632}
633#else
634struct CopyConstructorTest {
635 int i;
636
637 CopyConstructorTest() : i(1337) {}
638 CopyConstructorTest(const CopyConstructorTest &other) {}
639};
640
641void fCopyConstructorTest() {
642 CopyConstructorTest cct;
643 CopyConstructorTest copy = cct;
644 funcToSquelchCompilerWarnings(copy);
645}
646#endif // PEDANTIC
647
648struct MoveConstructorTest {
649 // TODO: we'd expect the note: {{uninitialized field 'this->i'}}
650 int i; // no-note
651
652 MoveConstructorTest() : i(1337) {}
653 MoveConstructorTest(const CopyConstructorTest &other) = delete;
654 MoveConstructorTest(const CopyConstructorTest &&other) {}
655};
656
657void fMoveConstructorTest() {
658 MoveConstructorTest cct;
659 // TODO: we'd expect the warning: {{1 uninitialized field}}
660 MoveConstructorTest copy(static_cast<MoveConstructorTest &&>(cct)); // no-warning
661 funcToSquelchCompilerWarnings(copy);
662}
663
664//===----------------------------------------------------------------------===//
665// Array tests.
666//===----------------------------------------------------------------------===//
667
668struct IntArrayTest {
669 int arr[256];
670
671 IntArrayTest() {
672 // All good!
673 }
674};
675
676void fIntArrayTest() {
677 IntArrayTest();
678}
679
680struct RecordTypeArrayTest {
681 struct RecordType {
682 int x, y;
683 } arr[256];
684
685 RecordTypeArrayTest() {
686 // All good!
687 }
688};
689
690void fRecordTypeArrayTest() {
691 RecordTypeArrayTest();
692}
693
694template <class T>
695class CharArrayPointerTest {
696 T *t; // no-crash
697
698public:
699 CharArrayPointerTest(T *t, int) : t(t) {}
700};
701
702void fCharArrayPointerTest() {
703 char str[16] = "012345678912345";
704 CharArrayPointerTest<char[16]>(&str, int());
705}
706
707//===----------------------------------------------------------------------===//
708// Memset tests.
709//===----------------------------------------------------------------------===//
710
711struct MemsetTest1 {
712 int a, b, c;
713
714 MemsetTest1() {
715 __builtin_memset(this, 0, sizeof(decltype(*this)));
716 }
717};
718
719void fMemsetTest1() {
720 MemsetTest1();
721}
722
723struct MemsetTest2 {
724 int a;
725
726 MemsetTest2() {
727 __builtin_memset(&a, 0, sizeof(int));
728 }
729};
730
731void fMemsetTest2() {
732 MemsetTest2();
733}
734
735//===----------------------------------------------------------------------===//
736// Lambda tests.
737//===----------------------------------------------------------------------===//
738
739template <class Callable>
Kristof Umann8c119092018-07-13 12:54:47 +0000740struct LambdaThisTest {
741 Callable functor;
742
743 LambdaThisTest(const Callable &functor, int) : functor(functor) {
744 // All good!
745 }
746};
747
748struct HasCapturableThis {
749 void fLambdaThisTest() {
750 auto isEven = [this](int a) { return a % 2 == 0; }; // no-crash
751 LambdaThisTest<decltype(isEven)>(isEven, int());
752 }
753};
754
755template <class Callable>
Kristof Umann30f08652018-06-18 11:50:17 +0000756struct LambdaTest1 {
757 Callable functor;
758
759 LambdaTest1(const Callable &functor, int) : functor(functor) {
760 // All good!
761 }
762};
763
764void fLambdaTest1() {
765 auto isEven = [](int a) { return a % 2 == 0; };
766 LambdaTest1<decltype(isEven)>(isEven, int());
767}
768
769#ifdef PEDANTIC
770template <class Callable>
771struct LambdaTest2 {
772 Callable functor;
773
774 LambdaTest2(const Callable &functor, int) : functor(functor) {} // expected-warning{{1 uninitialized field}}
775};
776
777void fLambdaTest2() {
778 int b;
Kristof Umann8c119092018-07-13 12:54:47 +0000779 auto equals = [&b](int a) { return a == b; }; // expected-note{{uninitialized field 'this->functor.b'}}
Kristof Umann30f08652018-06-18 11:50:17 +0000780 LambdaTest2<decltype(equals)>(equals, int());
781}
782#else
783template <class Callable>
784struct LambdaTest2 {
785 Callable functor;
786
787 LambdaTest2(const Callable &functor, int) : functor(functor) {}
788};
789
790void fLambdaTest2() {
791 int b;
792 auto equals = [&b](int a) { return a == b; };
793 LambdaTest2<decltype(equals)>(equals, int());
794}
795#endif //PEDANTIC
796
797#ifdef PEDANTIC
798namespace LT3Detail {
799
800struct RecordType {
Kristof Umann8c119092018-07-13 12:54:47 +0000801 int x; // expected-note{{uninitialized field 'this->functor.rec1.x'}}
802 int y; // expected-note{{uninitialized field 'this->functor.rec1.y'}}
Kristof Umann30f08652018-06-18 11:50:17 +0000803};
804
805} // namespace LT3Detail
806template <class Callable>
807struct LambdaTest3 {
808 Callable functor;
809
810 LambdaTest3(const Callable &functor, int) : functor(functor) {} // expected-warning{{2 uninitialized fields}}
811};
812
813void fLambdaTest3() {
814 LT3Detail::RecordType rec1;
815 auto equals = [&rec1](LT3Detail::RecordType rec2) {
816 return rec1.x == rec2.x;
817 };
818 LambdaTest3<decltype(equals)>(equals, int());
819}
820#else
821namespace LT3Detail {
822
823struct RecordType {
824 int x;
825 int y;
826};
827
828} // namespace LT3Detail
829template <class Callable>
830struct LambdaTest3 {
831 Callable functor;
832
833 LambdaTest3(const Callable &functor, int) : functor(functor) {}
834};
835
836void fLambdaTest3() {
837 LT3Detail::RecordType rec1;
838 auto equals = [&rec1](LT3Detail::RecordType rec2) {
839 return rec1.x == rec2.x;
840 };
841 LambdaTest3<decltype(equals)>(equals, int());
842}
843#endif //PEDANTIC
844
Kristof Umann8c119092018-07-13 12:54:47 +0000845template <class Callable>
846struct MultipleLambdaCapturesTest1 {
847 Callable functor;
848 int dontGetFilteredByNonPedanticMode = 0;
849
850 MultipleLambdaCapturesTest1(const Callable &functor, int) : functor(functor) {} // expected-warning{{2 uninitialized field}}
851};
852
853void fMultipleLambdaCapturesTest1() {
854 int b1, b2 = 3, b3;
855 auto equals = [&b1, &b2, &b3](int a) { return a == b1 == b2 == b3; }; // expected-note{{uninitialized field 'this->functor.b1'}}
856 // expected-note@-1{{uninitialized field 'this->functor.b3'}}
857 MultipleLambdaCapturesTest1<decltype(equals)>(equals, int());
858}
859
860template <class Callable>
861struct MultipleLambdaCapturesTest2 {
862 Callable functor;
863 int dontGetFilteredByNonPedanticMode = 0;
864
865 MultipleLambdaCapturesTest2(const Callable &functor, int) : functor(functor) {} // expected-warning{{1 uninitialized field}}
866};
867
868void fMultipleLambdaCapturesTest2() {
869 int b1, b2 = 3, b3;
870 auto equals = [b1, &b2, &b3](int a) { return a == b1 == b2 == b3; }; // expected-note{{uninitialized field 'this->functor.b3'}}
871 MultipleLambdaCapturesTest2<decltype(equals)>(equals, int());
872}
873
Kristof Umann30f08652018-06-18 11:50:17 +0000874//===----------------------------------------------------------------------===//
875// System header tests.
876//===----------------------------------------------------------------------===//
877
878#include "Inputs/system-header-simulator-for-cxx-uninitialized-object.h"
879
880struct SystemHeaderTest1 {
881 RecordInSystemHeader rec; // defined in the system header simulator
882
883 SystemHeaderTest1() {
884 // All good!
885 }
886};
887
888void fSystemHeaderTest1() {
889 SystemHeaderTest1();
890}
891
892#ifdef PEDANTIC
893struct SystemHeaderTest2 {
894 struct RecordType {
895 int x; // expected-note{{uninitialized field 'this->container.t.x}}
896 int y; // expected-note{{uninitialized field 'this->container.t.y}}
897 };
898 ContainerInSystemHeader<RecordType> container;
899
900 SystemHeaderTest2(RecordType &rec, int) : container(rec) {} // expected-warning{{2 uninitialized fields}}
901};
902
903void fSystemHeaderTest2() {
904 SystemHeaderTest2::RecordType rec;
905 SystemHeaderTest2(rec, int());
906}
907#else
908struct SystemHeaderTest2 {
909 struct RecordType {
910 int x;
911 int y;
912 };
913 ContainerInSystemHeader<RecordType> container;
914
915 SystemHeaderTest2(RecordType &rec, int) : container(rec) {}
916};
917
918void fSystemHeaderTest2() {
919 SystemHeaderTest2::RecordType rec;
920 SystemHeaderTest2(rec, int());
921}
922#endif //PEDANTIC
923
924//===----------------------------------------------------------------------===//
925// Incomplete type tests.
926//===----------------------------------------------------------------------===//
927
928struct IncompleteTypeTest1 {
929 struct RecordType;
930 // no-crash
931 RecordType *recptr; // expected-note{{uninitialized pointer 'this->recptr}}
932 int dontGetFilteredByNonPedanticMode = 0;
933
934 IncompleteTypeTest1() {} // expected-warning{{1 uninitialized field}}
935};
936
937void fIncompleteTypeTest1() {
938 IncompleteTypeTest1();
939}
940
941struct IncompleteTypeTest2 {
942 struct RecordType;
943 RecordType *recptr; // no-crash
944 int dontGetFilteredByNonPedanticMode = 0;
945
946 RecordType *recordTypeFactory();
947
948 IncompleteTypeTest2() : recptr(recordTypeFactory()) {}
949};
950
951void fIncompleteTypeTest2() {
952 IncompleteTypeTest2();
953}
954
955struct IncompleteTypeTest3 {
956 struct RecordType;
957 RecordType &recref; // no-crash
958 int dontGetFilteredByNonPedanticMode = 0;
959
960 RecordType &recordTypeFactory();
961
962 IncompleteTypeTest3() : recref(recordTypeFactory()) {}
963};
964
965void fIncompleteTypeTest3() {
966 IncompleteTypeTest3();
967}
968
969//===----------------------------------------------------------------------===//
970// Builtin type or enumeration type related tests.
971//===----------------------------------------------------------------------===//
972
973struct IntegralTypeTest {
974 int a; // expected-note{{uninitialized field 'this->a'}}
975 int dontGetFilteredByNonPedanticMode = 0;
976
977 IntegralTypeTest() {} // expected-warning{{1 uninitialized field}}
978};
979
980void fIntegralTypeTest() {
981 IntegralTypeTest();
982}
983
984struct FloatingTypeTest {
985 float a; // expected-note{{uninitialized field 'this->a'}}
986 int dontGetFilteredByNonPedanticMode = 0;
987
988 FloatingTypeTest() {} // expected-warning{{1 uninitialized field}}
989};
990
991void fFloatingTypeTest() {
992 FloatingTypeTest();
993}
994
995struct NullptrTypeTypeTest {
996 decltype(nullptr) a; // expected-note{{uninitialized field 'this->a'}}
997 int dontGetFilteredByNonPedanticMode = 0;
998
999 NullptrTypeTypeTest() {} // expected-warning{{1 uninitialized field}}
1000};
1001
1002void fNullptrTypeTypeTest() {
1003 NullptrTypeTypeTest();
1004}
1005
1006struct EnumTest {
1007 enum Enum {
1008 A,
1009 B
1010 } enum1; // expected-note{{uninitialized field 'this->enum1'}}
1011 enum class Enum2 {
1012 A,
1013 B
1014 } enum2; // expected-note{{uninitialized field 'this->enum2'}}
1015 int dontGetFilteredByNonPedanticMode = 0;
1016
1017 EnumTest() {} // expected-warning{{2 uninitialized fields}}
1018};
1019
1020void fEnumTest() {
1021 EnumTest();
1022}
1023
1024//===----------------------------------------------------------------------===//
1025// Tests for constructor calls within another cunstructor, without the two
1026// records being in any relation.
1027//===----------------------------------------------------------------------===//
1028
1029void halt() __attribute__((__noreturn__));
1030void assert(int b) {
1031 if (!b)
1032 halt();
1033}
1034
1035// While a singleton would make more sense as a static variable, that would zero
1036// initialize all of its fields, hence the not too practical implementation.
1037struct Singleton {
1038 // TODO: we'd expect the note: {{uninitialized field 'this->i'}}
1039 int i; // no-note
1040
1041 Singleton() {
1042 assert(!isInstantiated);
1043 // TODO: we'd expect the warning: {{1 uninitialized field}}
1044 isInstantiated = true; // no-warning
1045 }
1046
1047 ~Singleton() {
1048 isInstantiated = false;
1049 }
1050
1051 static bool isInstantiated;
1052};
1053
1054bool Singleton::isInstantiated = false;
1055
1056struct SingletonTest {
1057 int dontGetFilteredByNonPedanticMode = 0;
1058
1059 SingletonTest() {
1060 Singleton();
1061 }
1062};
1063
1064void fSingletonTest() {
1065 SingletonTest();
1066}
1067
1068//===----------------------------------------------------------------------===//
1069// C++11 member initializer tests.
1070//===----------------------------------------------------------------------===//
1071
1072struct CXX11MemberInitTest1 {
1073 int a = 3;
1074 int b;
1075 CXX11MemberInitTest1() : b(2) {
1076 // All good!
1077 }
1078};
1079
1080void fCXX11MemberInitTest1() {
1081 CXX11MemberInitTest1();
1082}
1083
1084struct CXX11MemberInitTest2 {
1085 struct RecordType {
1086 // TODO: we'd expect the note: {{uninitialized field 'this->rec.a'}}
1087 int a; // no-note
1088 // TODO: we'd expect the note: {{uninitialized field 'this->rec.b'}}
1089 int b; // no-note
1090
1091 RecordType(int) {}
1092 };
1093
1094 RecordType rec = RecordType(int());
1095 int dontGetFilteredByNonPedanticMode = 0;
1096
1097 CXX11MemberInitTest2() {}
1098};
1099
1100void fCXX11MemberInitTest2() {
1101 // TODO: we'd expect the warning: {{2 uninitializeds field}}
1102 CXX11MemberInitTest2(); // no-warning
1103}