1 // RUN: %clang_analyze_cc1 -analyzer-checker=core,optin.cplusplus.UninitializedObject \
2 // RUN: -analyzer-config optin.cplusplus.UninitializedObject:Pedantic=true -DPEDANTIC \
3 // RUN: -analyzer-config optin.cplusplus.UninitializedObject:CheckPointeeInitialization=true \
4 // RUN: -std=c++11 -verify %s
6 //===----------------------------------------------------------------------===//
7 // Non-polymorphic inheritance tests
8 //===----------------------------------------------------------------------===//
10 class NonPolymorphicLeft1
{
17 NonPolymorphicLeft1() = default;
18 NonPolymorphicLeft1(int) : x(1) {}
21 class NonPolymorphicInheritanceTest1
: public NonPolymorphicLeft1
{
25 NonPolymorphicInheritanceTest1()
26 : NonPolymorphicLeft1(int{}) {
33 void fNonPolymorphicInheritanceTest1() {
34 NonPolymorphicInheritanceTest1();
37 class NonPolymorphicBaseClass2
{
38 int x
; // expected-note{{uninitialized field 'this->NonPolymorphicBaseClass2::x'}}
43 NonPolymorphicBaseClass2() = default;
44 NonPolymorphicBaseClass2(int) : x(4) {}
47 class NonPolymorphicInheritanceTest2
: public NonPolymorphicBaseClass2
{
51 NonPolymorphicInheritanceTest2() {
53 z
= 6; // expected-warning{{1 uninitialized field}}
57 void fNonPolymorphicInheritanceTest2() {
58 NonPolymorphicInheritanceTest2();
61 class NonPolymorphicBaseClass3
{
65 int y
; // expected-note{{uninitialized field 'this->NonPolymorphicBaseClass3::y'}}
67 NonPolymorphicBaseClass3() = default;
68 NonPolymorphicBaseClass3(int) : x(7) {}
71 class NonPolymorphicInheritanceTest3
: public NonPolymorphicBaseClass3
{
75 NonPolymorphicInheritanceTest3()
76 : NonPolymorphicBaseClass3(int{}) {
77 z
= 8; // expected-warning{{1 uninitialized field}}
81 void fNonPolymorphicInheritanceTest3() {
82 NonPolymorphicInheritanceTest3();
85 class NonPolymorphicBaseClass4
{
92 NonPolymorphicBaseClass4() = default;
93 NonPolymorphicBaseClass4(int) : x(9) {}
96 class NonPolymorphicInheritanceTest4
: public NonPolymorphicBaseClass4
{
97 int z
; // expected-note{{uninitialized field 'this->z'}}
100 NonPolymorphicInheritanceTest4()
101 : NonPolymorphicBaseClass4(int{}) {
102 y
= 10; // expected-warning{{1 uninitialized field}}
106 void fNonPolymorphicInheritanceTest4() {
107 NonPolymorphicInheritanceTest4();
110 //===----------------------------------------------------------------------===//
111 // Polymorphic inheritance tests
112 //===----------------------------------------------------------------------===//
114 class PolymorphicLeft1
{
121 virtual ~PolymorphicLeft1() = default;
122 PolymorphicLeft1() = default;
123 PolymorphicLeft1(int) : x(11) {}
126 class PolymorphicInheritanceTest1
: public PolymorphicLeft1
{
130 PolymorphicInheritanceTest1()
131 : PolymorphicLeft1(int{}) {
138 void fPolymorphicInheritanceTest1() {
139 PolymorphicInheritanceTest1();
142 class PolymorphicRight1
{
143 int x
; // expected-note{{uninitialized field 'this->PolymorphicRight1::x'}}
148 virtual ~PolymorphicRight1() = default;
149 PolymorphicRight1() = default;
150 PolymorphicRight1(int) : x(14) {}
153 class PolymorphicInheritanceTest2
: public PolymorphicRight1
{
157 PolymorphicInheritanceTest2() {
159 z
= 16; // expected-warning{{1 uninitialized field}}
163 void fPolymorphicInheritanceTest2() {
164 PolymorphicInheritanceTest2();
167 class PolymorphicBaseClass3
{
171 int y
; // expected-note{{uninitialized field 'this->PolymorphicBaseClass3::y'}}
173 virtual ~PolymorphicBaseClass3() = default;
174 PolymorphicBaseClass3() = default;
175 PolymorphicBaseClass3(int) : x(17) {}
178 class PolymorphicInheritanceTest3
: public PolymorphicBaseClass3
{
182 PolymorphicInheritanceTest3()
183 : PolymorphicBaseClass3(int{}) {
184 z
= 18; // expected-warning{{1 uninitialized field}}
188 void fPolymorphicInheritanceTest3() {
189 PolymorphicInheritanceTest3();
192 class PolymorphicBaseClass4
{
199 virtual ~PolymorphicBaseClass4() = default;
200 PolymorphicBaseClass4() = default;
201 PolymorphicBaseClass4(int) : x(19) {}
204 class PolymorphicInheritanceTest4
: public PolymorphicBaseClass4
{
205 int z
; // expected-note{{uninitialized field 'this->z'}}
208 PolymorphicInheritanceTest4()
209 : PolymorphicBaseClass4(int{}) {
210 y
= 20; // expected-warning{{1 uninitialized field}}
214 void fPolymorphicInheritanceTest4() {
215 PolymorphicInheritanceTest4();
218 //===----------------------------------------------------------------------===//
219 // Virtual inheritance tests
220 //===----------------------------------------------------------------------===//
222 class VirtualPolymorphicLeft1
{
229 virtual ~VirtualPolymorphicLeft1() = default;
230 VirtualPolymorphicLeft1() = default;
231 VirtualPolymorphicLeft1(int) : x(21) {}
234 class VirtualInheritanceTest1
: virtual public VirtualPolymorphicLeft1
{
238 VirtualInheritanceTest1()
239 : VirtualPolymorphicLeft1(int()) {
246 void fVirtualInheritanceTest1() {
247 VirtualInheritanceTest1();
250 class VirtualPolymorphicRight1
{
251 int x
; // expected-note{{uninitialized field 'this->VirtualPolymorphicRight1::x'}}
256 virtual ~VirtualPolymorphicRight1() = default;
257 VirtualPolymorphicRight1() = default;
258 VirtualPolymorphicRight1(int) : x(24) {}
261 class VirtualInheritanceTest2
: virtual public VirtualPolymorphicRight1
{
265 VirtualInheritanceTest2() {
267 z
= 26; // expected-warning{{1 uninitialized field}}
271 void fVirtualInheritanceTest2() {
272 VirtualInheritanceTest2();
275 class VirtualPolymorphicBaseClass3
{
279 int y
; // expected-note{{uninitialized field 'this->VirtualPolymorphicBaseClass3::y'}}
281 virtual ~VirtualPolymorphicBaseClass3() = default;
282 VirtualPolymorphicBaseClass3() = default;
283 VirtualPolymorphicBaseClass3(int) : x(27) {}
286 class VirtualInheritanceTest3
: virtual public VirtualPolymorphicBaseClass3
{
290 VirtualInheritanceTest3()
291 : VirtualPolymorphicBaseClass3(int{}) {
292 z
= 28; // expected-warning{{1 uninitialized field}}
296 void fVirtualInheritanceTest3() {
297 VirtualInheritanceTest3();
300 //===----------------------------------------------------------------------===//
301 // Multiple inheritance tests
302 //===----------------------------------------------------------------------===//
309 MultipleInheritanceTest
315 Left1(int) : x(29) {}
320 Right1(int) : y(30) {}
323 class MultipleInheritanceTest1
: public Left1
, public Right1
{
327 MultipleInheritanceTest1()
334 MultipleInheritanceTest1(int)
341 MultipleInheritanceTest1(int, int)
349 void fMultipleInheritanceTest1() {
350 MultipleInheritanceTest1();
351 MultipleInheritanceTest1(int());
352 MultipleInheritanceTest1(int(), int());
358 Left2(int) : x(36) {}
361 int y
; // expected-note{{uninitialized field 'this->Right2::y'}}
363 Right2(int) : y(37) {}
366 class MultipleInheritanceTest2
: public Left2
, public Right2
{
370 MultipleInheritanceTest2()
372 z
= 38; // expected-warning{{1 uninitialized field}}
376 void fMultipleInheritanceTest2() {
377 MultipleInheritanceTest2();
381 int x
; // expected-note{{uninitialized field 'this->Left3::x'}}
383 Left3(int) : x(39) {}
388 Right3(int) : y(40) {}
391 class MultipleInheritanceTest3
: public Left3
, public Right3
{
395 MultipleInheritanceTest3()
397 z
= 41; // expected-warning{{1 uninitialized field}}
401 void fMultipleInheritanceTest3() {
402 MultipleInheritanceTest3();
408 Left4(int) : x(42) {}
413 Right4(int) : y(43) {}
416 class MultipleInheritanceTest4
: public Left4
, public Right4
{
417 int z
; // expected-note{{uninitialized field 'this->z'}}
420 MultipleInheritanceTest4()
422 Right4(char{}) { // expected-warning{{1 uninitialized field}}
426 void fMultipleInheritanceTest4() {
427 MultipleInheritanceTest4();
433 Left5(int) : x(44) {}
436 int y
; // expected-note{{uninitialized field 'this->Right5::y'}}
438 Right5(int) : y(45) {}
441 class MultipleInheritanceTest5
: public Left5
, public Right5
{
442 int z
; // expected-note{{uninitialized field 'this->z'}}
445 MultipleInheritanceTest5() // expected-warning{{2 uninitialized fields}}
450 void fMultipleInheritanceTest5() {
451 MultipleInheritanceTest5();
454 //===----------------------------------------------------------------------===//
455 // Non-virtual diamond inheritance tests
456 //===----------------------------------------------------------------------===//
459 NonVirtualBase NonVirtualBase
467 NonVirtualDiamondInheritanceTest
470 struct NonVirtualBase1
{
472 NonVirtualBase1() = default;
473 NonVirtualBase1(int) : x(46) {}
475 struct First1
: public NonVirtualBase1
{
477 First1(int) : NonVirtualBase1(int{}) {}
479 struct Second1
: public NonVirtualBase1
{
481 Second1(int) : NonVirtualBase1(int{}) {}
484 class NonVirtualDiamondInheritanceTest1
: public First1
, public Second1
{
488 NonVirtualDiamondInheritanceTest1()
495 NonVirtualDiamondInheritanceTest1(int)
502 NonVirtualDiamondInheritanceTest1(int, int)
510 void fNonVirtualDiamondInheritanceTest1() {
511 NonVirtualDiamondInheritanceTest1();
512 NonVirtualDiamondInheritanceTest1(int());
513 NonVirtualDiamondInheritanceTest1(int(), int());
516 struct NonVirtualBase2
{
517 int x
; // expected-note{{uninitialized field 'this->NonVirtualBase2::x'}}
518 NonVirtualBase2() = default;
519 NonVirtualBase2(int) : x(52) {}
521 struct First2
: public NonVirtualBase2
{
523 First2(int) : NonVirtualBase2(int{}) {}
525 struct Second2
: public NonVirtualBase2
{
527 Second2(int) : NonVirtualBase2(int{}) {}
530 class NonVirtualDiamondInheritanceTest2
: public First2
, public Second2
{
534 NonVirtualDiamondInheritanceTest2()
536 z
= 53; // expected-warning{{1 uninitialized field}}
540 void fNonVirtualDiamondInheritanceTest2() {
541 NonVirtualDiamondInheritanceTest2();
544 struct NonVirtualBase3
{
545 int x
; // expected-note{{uninitialized field 'this->NonVirtualBase3::x'}}
546 NonVirtualBase3() = default;
547 NonVirtualBase3(int) : x(54) {}
549 struct First3
: public NonVirtualBase3
{
551 First3(int) : NonVirtualBase3(int{}) {}
553 struct Second3
: public NonVirtualBase3
{
555 Second3(int) : NonVirtualBase3(int{}) {}
558 class NonVirtualDiamondInheritanceTest3
: public First3
, public Second3
{
562 NonVirtualDiamondInheritanceTest3()
564 z
= 55; // expected-warning{{1 uninitialized field}}
568 void fNonVirtualDiamondInheritanceTest3() {
569 NonVirtualDiamondInheritanceTest3();
572 struct NonVirtualBase4
{
573 int x
; // expected-note{{uninitialized field 'this->NonVirtualBase4::x'}}
574 // expected-note@-1{{uninitialized field 'this->NonVirtualBase4::x'}}
575 NonVirtualBase4() = default;
576 NonVirtualBase4(int) : x(56) {}
578 struct First4
: public NonVirtualBase4
{
580 First4(int) : NonVirtualBase4(int{}) {}
582 struct Second4
: public NonVirtualBase4
{
584 Second4(int) : NonVirtualBase4(int{}) {}
587 class NonVirtualDiamondInheritanceTest4
: public First4
, public Second4
{
591 NonVirtualDiamondInheritanceTest4() {
592 z
= 57; // expected-warning{{2 uninitialized fields}}
596 void fNonVirtualDiamondInheritanceTest4() {
597 NonVirtualDiamondInheritanceTest4();
600 struct NonVirtualBase5
{
602 NonVirtualBase5() = default;
603 NonVirtualBase5(int) : x(58) {}
605 struct First5
: public NonVirtualBase5
{
607 First5(int) : NonVirtualBase5(int{}) {}
609 struct Second5
: public NonVirtualBase5
{
611 Second5(int) : NonVirtualBase5(int{}) {}
614 class NonVirtualDiamondInheritanceTest5
: public First5
, public Second5
{
615 int z
; // expected-note{{uninitialized field 'this->z'}}
618 NonVirtualDiamondInheritanceTest5()
620 Second5(int{}) { // expected-warning{{1 uninitialized field}}
624 void fNonVirtualDiamondInheritanceTest5() {
625 NonVirtualDiamondInheritanceTest5();
628 struct NonVirtualBase6
{
629 int x
; // expected-note{{uninitialized field 'this->NonVirtualBase6::x'}}
630 NonVirtualBase6() = default;
631 NonVirtualBase6(int) : x(59) {}
633 struct First6
: public NonVirtualBase6
{
635 First6(int) : NonVirtualBase6(int{}) {}
637 struct Second6
: public NonVirtualBase6
{
639 Second6(int) : NonVirtualBase6(int{}) {}
642 class NonVirtualDiamondInheritanceTest6
: public First6
, public Second6
{
643 int z
; // expected-note{{uninitialized field 'this->z'}}
646 NonVirtualDiamondInheritanceTest6() // expected-warning{{2 uninitialized fields}}
648 // 'z' and 'Second::x' unintialized
652 void fNonVirtualDiamondInheritanceTest6() {
653 NonVirtualDiamondInheritanceTest6();
656 //===----------------------------------------------------------------------===//
657 // Virtual diamond inheritance tests
658 //===----------------------------------------------------------------------===//
665 VirtualFirst VirtualSecond
669 VirtualDiamondInheritanceTest
672 struct VirtualBase1
{
674 VirtualBase1() = default;
675 VirtualBase1(int) : x(60) {}
677 struct VirtualFirst1
: virtual public VirtualBase1
{
678 VirtualFirst1() = default;
679 VirtualFirst1(int) : VirtualBase1(int{}) {}
680 VirtualFirst1(int, int) { x
= 61; }
682 struct VirtualSecond1
: virtual public VirtualBase1
{
683 VirtualSecond1() = default;
684 VirtualSecond1(int) : VirtualBase1(int{}) {}
685 VirtualSecond1(int, int) { x
= 62; }
688 class VirtualDiamondInheritanceTest1
: public VirtualFirst1
, public VirtualSecond1
{
691 VirtualDiamondInheritanceTest1() {
696 VirtualDiamondInheritanceTest1(int)
697 : VirtualFirst1(int{}, int{}),
698 VirtualSecond1(int{}, int{}) {
702 VirtualDiamondInheritanceTest1(int, int)
703 : VirtualFirst1(int{}, int{}) {
708 void fVirtualDiamondInheritanceTest1() {
709 VirtualDiamondInheritanceTest1();
710 VirtualDiamondInheritanceTest1(int());
711 VirtualDiamondInheritanceTest1(int(), int());
714 struct VirtualBase2
{
715 int x
; // expected-note{{uninitialized field 'this->VirtualBase2::x'}}
716 VirtualBase2() = default;
717 VirtualBase2(int) : x(63) {}
719 struct VirtualFirst2
: virtual public VirtualBase2
{
720 VirtualFirst2() = default;
721 VirtualFirst2(int) : VirtualBase2(int{}) {}
722 VirtualFirst2(int, int) { x
= 64; }
724 struct VirtualSecond2
: virtual public VirtualBase2
{
725 VirtualSecond2() = default;
726 VirtualSecond2(int) : VirtualBase2(int{}) {}
727 VirtualSecond2(int, int) { x
= 65; }
730 class VirtualDiamondInheritanceTest2
: public VirtualFirst2
, public VirtualSecond2
{
733 VirtualDiamondInheritanceTest2() // expected-warning{{1 uninitialized field}}
734 : VirtualFirst2(int{}) {
735 // From the N4659 C++ Standard Working Draft:
738 // [...] A 'mem-initializer' where the 'mem-initializer-id' denotes a
739 // virtual base class is ignored during execution of a constructor of any
740 // class that is not the most derived class.
742 // This means that Left1::x will not be initialized, because in both
743 // VirtualFirst::VirtualFirst(int) and VirtualSecond::VirtualSecond(int)
744 // the constructor delegation to Left1::Left1(int) will be
749 void fVirtualDiamondInheritanceTest2() {
750 VirtualDiamondInheritanceTest2();
753 struct VirtualBase3
{
754 int x
; // expected-note{{uninitialized field 'this->VirtualBase3::x'}}
755 VirtualBase3() = default;
756 VirtualBase3(int) : x(66) {}
758 struct VirtualFirst3
: virtual public VirtualBase3
{
759 VirtualFirst3() = default;
760 VirtualFirst3(int) : VirtualBase3(int{}) {}
761 VirtualFirst3(int, int) { x
= 67; }
763 struct VirtualSecond3
: virtual public VirtualBase3
{
764 VirtualSecond3() = default;
765 VirtualSecond3(int) : VirtualBase3(int{}) {}
766 VirtualSecond3(int, int) { x
= 68; }
769 class VirtualDiamondInheritanceTest3
: public VirtualFirst3
, public VirtualSecond3
{
772 VirtualDiamondInheritanceTest3() // expected-warning{{1 uninitialized field}}
773 : VirtualFirst3(int{}) {}
776 void fVirtualDiamondInheritanceTest3() {
777 VirtualDiamondInheritanceTest3();
780 //===----------------------------------------------------------------------===//
781 // Dynamic type test.
782 //===----------------------------------------------------------------------===//
785 struct DynTDerived1
: DynTBase1
{
786 int y
; // expected-note{{uninitialized field 'static_cast<DynTDerived1 *>(this->bptr)->y'}}
789 struct DynamicTypeTest1
{
793 DynamicTypeTest1(DynTBase1
*bptr
) : bptr(bptr
) {} // expected-warning{{1 uninitialized field}}
796 void fDynamicTypeTest1() {
798 DynamicTypeTest1
t(&d
);
802 int x
; // expected-note{{uninitialized field 'static_cast<DynTDerived2 *>(this->bptr)->DynTBase2::x'}}
804 struct DynTDerived2
: DynTBase2
{
805 int y
; // expected-note{{uninitialized field 'static_cast<DynTDerived2 *>(this->bptr)->y'}}
808 struct DynamicTypeTest2
{
812 DynamicTypeTest2(DynTBase2
*bptr
) : bptr(bptr
) {} // expected-warning{{2 uninitialized fields}}
815 void fDynamicTypeTest2() {
817 DynamicTypeTest2
t(&d
);
820 struct SymbolicSuperRegionBase
{
821 SymbolicSuperRegionBase() {}
824 struct SymbolicSuperRegionDerived
: SymbolicSuperRegionBase
{
825 SymbolicSuperRegionBase
*bptr
; // no-crash
826 SymbolicSuperRegionDerived(SymbolicSuperRegionBase
*bptr
) : bptr(bptr
) {}
829 SymbolicSuperRegionDerived
*getSymbolicRegion();
831 void fSymbolicSuperRegionTest() {
832 SymbolicSuperRegionDerived
test(getSymbolicRegion());