[clang] Handle __declspec() attributes in using
[llvm-project.git] / clang / test / Analysis / cxx-uninitialized-object-inheritance.cpp
blob8456751173ffcf94317bab5010181b1ab5c9bb90
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 {
11 int x;
13 protected:
14 int y;
16 public:
17 NonPolymorphicLeft1() = default;
18 NonPolymorphicLeft1(int) : x(1) {}
21 class NonPolymorphicInheritanceTest1 : public NonPolymorphicLeft1 {
22 int z;
24 public:
25 NonPolymorphicInheritanceTest1()
26 : NonPolymorphicLeft1(int{}) {
27 y = 2;
28 z = 3;
29 // All good!
33 void fNonPolymorphicInheritanceTest1() {
34 NonPolymorphicInheritanceTest1();
37 class NonPolymorphicBaseClass2 {
38 int x; // expected-note{{uninitialized field 'this->NonPolymorphicBaseClass2::x'}}
39 protected:
40 int y;
42 public:
43 NonPolymorphicBaseClass2() = default;
44 NonPolymorphicBaseClass2(int) : x(4) {}
47 class NonPolymorphicInheritanceTest2 : public NonPolymorphicBaseClass2 {
48 int z;
50 public:
51 NonPolymorphicInheritanceTest2() {
52 y = 5;
53 z = 6; // expected-warning{{1 uninitialized field}}
57 void fNonPolymorphicInheritanceTest2() {
58 NonPolymorphicInheritanceTest2();
61 class NonPolymorphicBaseClass3 {
62 int x;
64 protected:
65 int y; // expected-note{{uninitialized field 'this->NonPolymorphicBaseClass3::y'}}
66 public:
67 NonPolymorphicBaseClass3() = default;
68 NonPolymorphicBaseClass3(int) : x(7) {}
71 class NonPolymorphicInheritanceTest3 : public NonPolymorphicBaseClass3 {
72 int z;
74 public:
75 NonPolymorphicInheritanceTest3()
76 : NonPolymorphicBaseClass3(int{}) {
77 z = 8; // expected-warning{{1 uninitialized field}}
81 void fNonPolymorphicInheritanceTest3() {
82 NonPolymorphicInheritanceTest3();
85 class NonPolymorphicBaseClass4 {
86 int x;
88 protected:
89 int y;
91 public:
92 NonPolymorphicBaseClass4() = default;
93 NonPolymorphicBaseClass4(int) : x(9) {}
96 class NonPolymorphicInheritanceTest4 : public NonPolymorphicBaseClass4 {
97 int z; // expected-note{{uninitialized field 'this->z'}}
99 public:
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 {
115 int x;
117 protected:
118 int y;
120 public:
121 virtual ~PolymorphicLeft1() = default;
122 PolymorphicLeft1() = default;
123 PolymorphicLeft1(int) : x(11) {}
126 class PolymorphicInheritanceTest1 : public PolymorphicLeft1 {
127 int z;
129 public:
130 PolymorphicInheritanceTest1()
131 : PolymorphicLeft1(int{}) {
132 y = 12;
133 z = 13;
134 // All good!
138 void fPolymorphicInheritanceTest1() {
139 PolymorphicInheritanceTest1();
142 class PolymorphicRight1 {
143 int x; // expected-note{{uninitialized field 'this->PolymorphicRight1::x'}}
144 protected:
145 int y;
147 public:
148 virtual ~PolymorphicRight1() = default;
149 PolymorphicRight1() = default;
150 PolymorphicRight1(int) : x(14) {}
153 class PolymorphicInheritanceTest2 : public PolymorphicRight1 {
154 int z;
156 public:
157 PolymorphicInheritanceTest2() {
158 y = 15;
159 z = 16; // expected-warning{{1 uninitialized field}}
163 void fPolymorphicInheritanceTest2() {
164 PolymorphicInheritanceTest2();
167 class PolymorphicBaseClass3 {
168 int x;
170 protected:
171 int y; // expected-note{{uninitialized field 'this->PolymorphicBaseClass3::y'}}
172 public:
173 virtual ~PolymorphicBaseClass3() = default;
174 PolymorphicBaseClass3() = default;
175 PolymorphicBaseClass3(int) : x(17) {}
178 class PolymorphicInheritanceTest3 : public PolymorphicBaseClass3 {
179 int z;
181 public:
182 PolymorphicInheritanceTest3()
183 : PolymorphicBaseClass3(int{}) {
184 z = 18; // expected-warning{{1 uninitialized field}}
188 void fPolymorphicInheritanceTest3() {
189 PolymorphicInheritanceTest3();
192 class PolymorphicBaseClass4 {
193 int x;
195 protected:
196 int y;
198 public:
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'}}
207 public:
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 {
223 int x;
225 protected:
226 int y;
228 public:
229 virtual ~VirtualPolymorphicLeft1() = default;
230 VirtualPolymorphicLeft1() = default;
231 VirtualPolymorphicLeft1(int) : x(21) {}
234 class VirtualInheritanceTest1 : virtual public VirtualPolymorphicLeft1 {
235 int z;
237 public:
238 VirtualInheritanceTest1()
239 : VirtualPolymorphicLeft1(int()) {
240 y = 22;
241 z = 23;
242 // All good!
246 void fVirtualInheritanceTest1() {
247 VirtualInheritanceTest1();
250 class VirtualPolymorphicRight1 {
251 int x; // expected-note{{uninitialized field 'this->VirtualPolymorphicRight1::x'}}
252 protected:
253 int y;
255 public:
256 virtual ~VirtualPolymorphicRight1() = default;
257 VirtualPolymorphicRight1() = default;
258 VirtualPolymorphicRight1(int) : x(24) {}
261 class VirtualInheritanceTest2 : virtual public VirtualPolymorphicRight1 {
262 int z;
264 public:
265 VirtualInheritanceTest2() {
266 y = 25;
267 z = 26; // expected-warning{{1 uninitialized field}}
271 void fVirtualInheritanceTest2() {
272 VirtualInheritanceTest2();
275 class VirtualPolymorphicBaseClass3 {
276 int x;
278 protected:
279 int y; // expected-note{{uninitialized field 'this->VirtualPolymorphicBaseClass3::y'}}
280 public:
281 virtual ~VirtualPolymorphicBaseClass3() = default;
282 VirtualPolymorphicBaseClass3() = default;
283 VirtualPolymorphicBaseClass3(int) : x(27) {}
286 class VirtualInheritanceTest3 : virtual public VirtualPolymorphicBaseClass3 {
287 int z;
289 public:
290 VirtualInheritanceTest3()
291 : VirtualPolymorphicBaseClass3(int{}) {
292 z = 28; // expected-warning{{1 uninitialized field}}
296 void fVirtualInheritanceTest3() {
297 VirtualInheritanceTest3();
300 //===----------------------------------------------------------------------===//
301 // Multiple inheritance tests
302 //===----------------------------------------------------------------------===//
305 Left Right
309 MultipleInheritanceTest
312 struct Left1 {
313 int x;
314 Left1() = default;
315 Left1(int) : x(29) {}
317 struct Right1 {
318 int y;
319 Right1() = default;
320 Right1(int) : y(30) {}
323 class MultipleInheritanceTest1 : public Left1, public Right1 {
324 int z;
326 public:
327 MultipleInheritanceTest1()
328 : Left1(int{}),
329 Right1(char{}) {
330 z = 31;
331 // All good!
334 MultipleInheritanceTest1(int)
335 : Left1(int{}) {
336 y = 32;
337 z = 33;
338 // All good!
341 MultipleInheritanceTest1(int, int)
342 : Right1(char{}) {
343 x = 34;
344 z = 35;
345 // All good!
349 void fMultipleInheritanceTest1() {
350 MultipleInheritanceTest1();
351 MultipleInheritanceTest1(int());
352 MultipleInheritanceTest1(int(), int());
355 struct Left2 {
356 int x;
357 Left2() = default;
358 Left2(int) : x(36) {}
360 struct Right2 {
361 int y; // expected-note{{uninitialized field 'this->Right2::y'}}
362 Right2() = default;
363 Right2(int) : y(37) {}
366 class MultipleInheritanceTest2 : public Left2, public Right2 {
367 int z;
369 public:
370 MultipleInheritanceTest2()
371 : Left2(int{}) {
372 z = 38; // expected-warning{{1 uninitialized field}}
376 void fMultipleInheritanceTest2() {
377 MultipleInheritanceTest2();
380 struct Left3 {
381 int x; // expected-note{{uninitialized field 'this->Left3::x'}}
382 Left3() = default;
383 Left3(int) : x(39) {}
385 struct Right3 {
386 int y;
387 Right3() = default;
388 Right3(int) : y(40) {}
391 class MultipleInheritanceTest3 : public Left3, public Right3 {
392 int z;
394 public:
395 MultipleInheritanceTest3()
396 : Right3(char{}) {
397 z = 41; // expected-warning{{1 uninitialized field}}
401 void fMultipleInheritanceTest3() {
402 MultipleInheritanceTest3();
405 struct Left4 {
406 int x;
407 Left4() = default;
408 Left4(int) : x(42) {}
410 struct Right4 {
411 int y;
412 Right4() = default;
413 Right4(int) : y(43) {}
416 class MultipleInheritanceTest4 : public Left4, public Right4 {
417 int z; // expected-note{{uninitialized field 'this->z'}}
419 public:
420 MultipleInheritanceTest4()
421 : Left4(int{}),
422 Right4(char{}) { // expected-warning{{1 uninitialized field}}
426 void fMultipleInheritanceTest4() {
427 MultipleInheritanceTest4();
430 struct Left5 {
431 int x;
432 Left5() = default;
433 Left5(int) : x(44) {}
435 struct Right5 {
436 int y; // expected-note{{uninitialized field 'this->Right5::y'}}
437 Right5() = default;
438 Right5(int) : y(45) {}
441 class MultipleInheritanceTest5 : public Left5, public Right5 {
442 int z; // expected-note{{uninitialized field 'this->z'}}
444 public:
445 MultipleInheritanceTest5() // expected-warning{{2 uninitialized fields}}
446 : Left5(int{}) {
450 void fMultipleInheritanceTest5() {
451 MultipleInheritanceTest5();
454 //===----------------------------------------------------------------------===//
455 // Non-virtual diamond inheritance tests
456 //===----------------------------------------------------------------------===//
459 NonVirtualBase NonVirtualBase
463 First Second
467 NonVirtualDiamondInheritanceTest
470 struct NonVirtualBase1 {
471 int x;
472 NonVirtualBase1() = default;
473 NonVirtualBase1(int) : x(46) {}
475 struct First1 : public NonVirtualBase1 {
476 First1() = default;
477 First1(int) : NonVirtualBase1(int{}) {}
479 struct Second1 : public NonVirtualBase1 {
480 Second1() = default;
481 Second1(int) : NonVirtualBase1(int{}) {}
484 class NonVirtualDiamondInheritanceTest1 : public First1, public Second1 {
485 int z;
487 public:
488 NonVirtualDiamondInheritanceTest1()
489 : First1(int{}),
490 Second1(int{}) {
491 z = 47;
492 // All good!
495 NonVirtualDiamondInheritanceTest1(int)
496 : First1(int{}) {
497 Second1::x = 48;
498 z = 49;
499 // All good!
502 NonVirtualDiamondInheritanceTest1(int, int)
503 : Second1(int{}) {
504 First1::x = 50;
505 z = 51;
506 // All good!
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 {
522 First2() = default;
523 First2(int) : NonVirtualBase2(int{}) {}
525 struct Second2 : public NonVirtualBase2 {
526 Second2() = default;
527 Second2(int) : NonVirtualBase2(int{}) {}
530 class NonVirtualDiamondInheritanceTest2 : public First2, public Second2 {
531 int z;
533 public:
534 NonVirtualDiamondInheritanceTest2()
535 : First2(int{}) {
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 {
550 First3() = default;
551 First3(int) : NonVirtualBase3(int{}) {}
553 struct Second3 : public NonVirtualBase3 {
554 Second3() = default;
555 Second3(int) : NonVirtualBase3(int{}) {}
558 class NonVirtualDiamondInheritanceTest3 : public First3, public Second3 {
559 int z;
561 public:
562 NonVirtualDiamondInheritanceTest3()
563 : Second3(int{}) {
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 {
579 First4() = default;
580 First4(int) : NonVirtualBase4(int{}) {}
582 struct Second4 : public NonVirtualBase4 {
583 Second4() = default;
584 Second4(int) : NonVirtualBase4(int{}) {}
587 class NonVirtualDiamondInheritanceTest4 : public First4, public Second4 {
588 int z;
590 public:
591 NonVirtualDiamondInheritanceTest4() {
592 z = 57; // expected-warning{{2 uninitialized fields}}
596 void fNonVirtualDiamondInheritanceTest4() {
597 NonVirtualDiamondInheritanceTest4();
600 struct NonVirtualBase5 {
601 int x;
602 NonVirtualBase5() = default;
603 NonVirtualBase5(int) : x(58) {}
605 struct First5 : public NonVirtualBase5 {
606 First5() = default;
607 First5(int) : NonVirtualBase5(int{}) {}
609 struct Second5 : public NonVirtualBase5 {
610 Second5() = default;
611 Second5(int) : NonVirtualBase5(int{}) {}
614 class NonVirtualDiamondInheritanceTest5 : public First5, public Second5 {
615 int z; // expected-note{{uninitialized field 'this->z'}}
617 public:
618 NonVirtualDiamondInheritanceTest5()
619 : First5(int{}),
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 {
634 First6() = default;
635 First6(int) : NonVirtualBase6(int{}) {}
637 struct Second6 : public NonVirtualBase6 {
638 Second6() = default;
639 Second6(int) : NonVirtualBase6(int{}) {}
642 class NonVirtualDiamondInheritanceTest6 : public First6, public Second6 {
643 int z; // expected-note{{uninitialized field 'this->z'}}
645 public:
646 NonVirtualDiamondInheritanceTest6() // expected-warning{{2 uninitialized fields}}
647 : First6(int{}) {
648 // 'z' and 'Second::x' unintialized
652 void fNonVirtualDiamondInheritanceTest6() {
653 NonVirtualDiamondInheritanceTest6();
656 //===----------------------------------------------------------------------===//
657 // Virtual diamond inheritance tests
658 //===----------------------------------------------------------------------===//
661 VirtualBase
665 VirtualFirst VirtualSecond
669 VirtualDiamondInheritanceTest
672 struct VirtualBase1 {
673 int x;
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 {
690 public:
691 VirtualDiamondInheritanceTest1() {
692 x = 0;
693 // All good!
696 VirtualDiamondInheritanceTest1(int)
697 : VirtualFirst1(int{}, int{}),
698 VirtualSecond1(int{}, int{}) {
699 // All good!
702 VirtualDiamondInheritanceTest1(int, int)
703 : VirtualFirst1(int{}, int{}) {
704 // All good!
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 {
732 public:
733 VirtualDiamondInheritanceTest2() // expected-warning{{1 uninitialized field}}
734 : VirtualFirst2(int{}) {
735 // From the N4659 C++ Standard Working Draft:
737 // (15.6.2.7)
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
745 // ignored.
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 {
771 public:
772 VirtualDiamondInheritanceTest3() // expected-warning{{1 uninitialized field}}
773 : VirtualFirst3(int{}) {}
776 void fVirtualDiamondInheritanceTest3() {
777 VirtualDiamondInheritanceTest3();
780 //===----------------------------------------------------------------------===//
781 // Dynamic type test.
782 //===----------------------------------------------------------------------===//
784 struct DynTBase1 {};
785 struct DynTDerived1 : DynTBase1 {
786 int y; // expected-note{{uninitialized field 'static_cast<DynTDerived1 *>(this->bptr)->y'}}
789 struct DynamicTypeTest1 {
790 DynTBase1 *bptr;
791 int i = 0;
793 DynamicTypeTest1(DynTBase1 *bptr) : bptr(bptr) {} // expected-warning{{1 uninitialized field}}
796 void fDynamicTypeTest1() {
797 DynTDerived1 d;
798 DynamicTypeTest1 t(&d);
801 struct DynTBase2 {
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 {
809 DynTBase2 *bptr;
810 int i = 0;
812 DynamicTypeTest2(DynTBase2 *bptr) : bptr(bptr) {} // expected-warning{{2 uninitialized fields}}
815 void fDynamicTypeTest2() {
816 DynTDerived2 d;
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());