1 // RUN: %clang_analyze_cc1 -analyzer-checker=core,alpha.cplusplus.UninitializedObject -analyzer-config alpha.cplusplus.UninitializedObject:Pedantic=true -std=c++11 -verify %s 2 3 //===----------------------------------------------------------------------===// 4 // Non-polymorphic inheritance tests 5 //===----------------------------------------------------------------------===// 6 7 class NonPolymorphicLeft1 { 8 int x; 9 10 protected: 11 int y; 12 13 public: 14 NonPolymorphicLeft1() = default; 15 NonPolymorphicLeft1(int) : x(1) {} 16 }; 17 18 class NonPolymorphicInheritanceTest1 : public NonPolymorphicLeft1 { 19 int z; 20 21 public: 22 NonPolymorphicInheritanceTest1() 23 : NonPolymorphicLeft1(int{}) { 24 y = 2; 25 z = 3; 26 // All good! 27 } 28 }; 29 30 void fNonPolymorphicInheritanceTest1() { 31 NonPolymorphicInheritanceTest1(); 32 } 33 34 class NonPolymorphicBaseClass2 { 35 int x; // expected-note{{uninitialized field 'this->x'}} 36 protected: 37 int y; 38 39 public: 40 NonPolymorphicBaseClass2() = default; 41 NonPolymorphicBaseClass2(int) : x(4) {} 42 }; 43 44 class NonPolymorphicInheritanceTest2 : public NonPolymorphicBaseClass2 { 45 int z; 46 47 public: 48 NonPolymorphicInheritanceTest2() { 49 y = 5; 50 z = 6; // expected-warning{{1 uninitialized field}} 51 } 52 }; 53 54 void fNonPolymorphicInheritanceTest2() { 55 NonPolymorphicInheritanceTest2(); 56 } 57 58 class NonPolymorphicBaseClass3 { 59 int x; 60 61 protected: 62 int y; // expected-note{{uninitialized field 'this->y'}} 63 public: 64 NonPolymorphicBaseClass3() = default; 65 NonPolymorphicBaseClass3(int) : x(7) {} 66 }; 67 68 class NonPolymorphicInheritanceTest3 : public NonPolymorphicBaseClass3 { 69 int z; 70 71 public: 72 NonPolymorphicInheritanceTest3() 73 : NonPolymorphicBaseClass3(int{}) { 74 z = 8; // expected-warning{{1 uninitialized field}} 75 } 76 }; 77 78 void fNonPolymorphicInheritanceTest3() { 79 NonPolymorphicInheritanceTest3(); 80 } 81 82 class NonPolymorphicBaseClass4 { 83 int x; 84 85 protected: 86 int y; 87 88 public: 89 NonPolymorphicBaseClass4() = default; 90 NonPolymorphicBaseClass4(int) : x(9) {} 91 }; 92 93 class NonPolymorphicInheritanceTest4 : public NonPolymorphicBaseClass4 { 94 int z; // expected-note{{uninitialized field 'this->z'}} 95 96 public: 97 NonPolymorphicInheritanceTest4() 98 : NonPolymorphicBaseClass4(int{}) { 99 y = 10; // expected-warning{{1 uninitialized field}} 100 } 101 }; 102 103 void fNonPolymorphicInheritanceTest4() { 104 NonPolymorphicInheritanceTest4(); 105 } 106 107 //===----------------------------------------------------------------------===// 108 // Polymorphic inheritance tests 109 //===----------------------------------------------------------------------===// 110 111 class PolymorphicLeft1 { 112 int x; 113 114 protected: 115 int y; 116 117 public: 118 virtual ~PolymorphicLeft1() = default; 119 PolymorphicLeft1() = default; 120 PolymorphicLeft1(int) : x(11) {} 121 }; 122 123 class PolymorphicInheritanceTest1 : public PolymorphicLeft1 { 124 int z; 125 126 public: 127 PolymorphicInheritanceTest1() 128 : PolymorphicLeft1(int{}) { 129 y = 12; 130 z = 13; 131 // All good! 132 } 133 }; 134 135 void fPolymorphicInheritanceTest1() { 136 PolymorphicInheritanceTest1(); 137 } 138 139 class PolymorphicRight1 { 140 int x; // expected-note{{uninitialized field 'this->x'}} 141 protected: 142 int y; 143 144 public: 145 virtual ~PolymorphicRight1() = default; 146 PolymorphicRight1() = default; 147 PolymorphicRight1(int) : x(14) {} 148 }; 149 150 class PolymorphicInheritanceTest2 : public PolymorphicRight1 { 151 int z; 152 153 public: 154 PolymorphicInheritanceTest2() { 155 y = 15; 156 z = 16; // expected-warning{{1 uninitialized field}} 157 } 158 }; 159 160 void fPolymorphicInheritanceTest2() { 161 PolymorphicInheritanceTest2(); 162 } 163 164 class PolymorphicBaseClass3 { 165 int x; 166 167 protected: 168 int y; // expected-note{{uninitialized field 'this->y'}} 169 public: 170 virtual ~PolymorphicBaseClass3() = default; 171 PolymorphicBaseClass3() = default; 172 PolymorphicBaseClass3(int) : x(17) {} 173 }; 174 175 class PolymorphicInheritanceTest3 : public PolymorphicBaseClass3 { 176 int z; 177 178 public: 179 PolymorphicInheritanceTest3() 180 : PolymorphicBaseClass3(int{}) { 181 z = 18; // expected-warning{{1 uninitialized field}} 182 } 183 }; 184 185 void fPolymorphicInheritanceTest3() { 186 PolymorphicInheritanceTest3(); 187 } 188 189 class PolymorphicBaseClass4 { 190 int x; 191 192 protected: 193 int y; 194 195 public: 196 virtual ~PolymorphicBaseClass4() = default; 197 PolymorphicBaseClass4() = default; 198 PolymorphicBaseClass4(int) : x(19) {} 199 }; 200 201 class PolymorphicInheritanceTest4 : public PolymorphicBaseClass4 { 202 int z; // expected-note{{uninitialized field 'this->z'}} 203 204 public: 205 PolymorphicInheritanceTest4() 206 : PolymorphicBaseClass4(int{}) { 207 y = 20; // expected-warning{{1 uninitialized field}} 208 } 209 }; 210 211 void fPolymorphicInheritanceTest4() { 212 PolymorphicInheritanceTest4(); 213 } 214 215 //===----------------------------------------------------------------------===// 216 // Virtual inheritance tests 217 //===----------------------------------------------------------------------===// 218 219 class VirtualPolymorphicLeft1 { 220 int x; 221 222 protected: 223 int y; 224 225 public: 226 virtual ~VirtualPolymorphicLeft1() = default; 227 VirtualPolymorphicLeft1() = default; 228 VirtualPolymorphicLeft1(int) : x(21) {} 229 }; 230 231 class VirtualInheritanceTest1 : virtual public VirtualPolymorphicLeft1 { 232 int z; 233 234 public: 235 VirtualInheritanceTest1() 236 : VirtualPolymorphicLeft1(int()) { 237 y = 22; 238 z = 23; 239 // All good! 240 } 241 }; 242 243 void fVirtualInheritanceTest1() { 244 VirtualInheritanceTest1(); 245 } 246 247 class VirtualPolymorphicRight1 { 248 int x; // expected-note{{uninitialized field 'this->x'}} 249 protected: 250 int y; 251 252 public: 253 virtual ~VirtualPolymorphicRight1() = default; 254 VirtualPolymorphicRight1() = default; 255 VirtualPolymorphicRight1(int) : x(24) {} 256 }; 257 258 class VirtualInheritanceTest2 : virtual public VirtualPolymorphicRight1 { 259 int z; 260 261 public: 262 VirtualInheritanceTest2() { 263 y = 25; 264 z = 26; // expected-warning{{1 uninitialized field}} 265 } 266 }; 267 268 void fVirtualInheritanceTest2() { 269 VirtualInheritanceTest2(); 270 } 271 272 class VirtualPolymorphicBaseClass3 { 273 int x; 274 275 protected: 276 int y; // expected-note{{uninitialized field 'this->y'}} 277 public: 278 virtual ~VirtualPolymorphicBaseClass3() = default; 279 VirtualPolymorphicBaseClass3() = default; 280 VirtualPolymorphicBaseClass3(int) : x(27) {} 281 }; 282 283 class VirtualInheritanceTest3 : virtual public VirtualPolymorphicBaseClass3 { 284 int z; 285 286 public: 287 VirtualInheritanceTest3() 288 : VirtualPolymorphicBaseClass3(int{}) { 289 z = 28; // expected-warning{{1 uninitialized field}} 290 } 291 }; 292 293 void fVirtualInheritanceTest3() { 294 VirtualInheritanceTest3(); 295 } 296 297 //===----------------------------------------------------------------------===// 298 // Multiple inheritance tests 299 //===----------------------------------------------------------------------===// 300 301 /* 302 Left Right 303 \ / 304 \ / 305 \ / 306 MultipleInheritanceTest 307 */ 308 309 struct Left1 { 310 int x; 311 Left1() = default; 312 Left1(int) : x(29) {} 313 }; 314 struct Right1 { 315 int y; 316 Right1() = default; 317 Right1(int) : y(30) {} 318 }; 319 320 class MultipleInheritanceTest1 : public Left1, public Right1 { 321 int z; 322 323 public: 324 MultipleInheritanceTest1() 325 : Left1(int{}), 326 Right1(char{}) { 327 z = 31; 328 // All good! 329 } 330 331 MultipleInheritanceTest1(int) 332 : Left1(int{}) { 333 y = 32; 334 z = 33; 335 // All good! 336 } 337 338 MultipleInheritanceTest1(int, int) 339 : Right1(char{}) { 340 x = 34; 341 z = 35; 342 // All good! 343 } 344 }; 345 346 void fMultipleInheritanceTest1() { 347 MultipleInheritanceTest1(); 348 MultipleInheritanceTest1(int()); 349 MultipleInheritanceTest1(int(), int()); 350 } 351 352 struct Left2 { 353 int x; 354 Left2() = default; 355 Left2(int) : x(36) {} 356 }; 357 struct Right2 { 358 int y; // expected-note{{uninitialized field 'this->y'}} 359 Right2() = default; 360 Right2(int) : y(37) {} 361 }; 362 363 class MultipleInheritanceTest2 : public Left2, public Right2 { 364 int z; 365 366 public: 367 MultipleInheritanceTest2() 368 : Left2(int{}) { 369 z = 38; // expected-warning{{1 uninitialized field}} 370 } 371 }; 372 373 void fMultipleInheritanceTest2() { 374 MultipleInheritanceTest2(); 375 } 376 377 struct Left3 { 378 int x; // expected-note{{uninitialized field 'this->x'}} 379 Left3() = default; 380 Left3(int) : x(39) {} 381 }; 382 struct Right3 { 383 int y; 384 Right3() = default; 385 Right3(int) : y(40) {} 386 }; 387 388 class MultipleInheritanceTest3 : public Left3, public Right3 { 389 int z; 390 391 public: 392 MultipleInheritanceTest3() 393 : Right3(char{}) { 394 z = 41; // expected-warning{{1 uninitialized field}} 395 } 396 }; 397 398 void fMultipleInheritanceTest3() { 399 MultipleInheritanceTest3(); 400 } 401 402 struct Left4 { 403 int x; 404 Left4() = default; 405 Left4(int) : x(42) {} 406 }; 407 struct Right4 { 408 int y; 409 Right4() = default; 410 Right4(int) : y(43) {} 411 }; 412 413 class MultipleInheritanceTest4 : public Left4, public Right4 { 414 int z; // expected-note{{uninitialized field 'this->z'}} 415 416 public: 417 MultipleInheritanceTest4() 418 : Left4(int{}), 419 Right4(char{}) { // expected-warning{{1 uninitialized field}} 420 } 421 }; 422 423 void fMultipleInheritanceTest4() { 424 MultipleInheritanceTest4(); 425 } 426 427 struct Left5 { 428 int x; 429 Left5() = default; 430 Left5(int) : x(44) {} 431 }; 432 struct Right5 { 433 int y; // expected-note{{uninitialized field 'this->y'}} 434 Right5() = default; 435 Right5(int) : y(45) {} 436 }; 437 438 class MultipleInheritanceTest5 : public Left5, public Right5 { 439 int z; // expected-note{{uninitialized field 'this->z'}} 440 441 public: 442 MultipleInheritanceTest5() // expected-warning{{2 uninitialized fields}} 443 : Left5(int{}) { 444 } 445 }; 446 447 void fMultipleInheritanceTest5() { 448 MultipleInheritanceTest5(); 449 } 450 451 //===----------------------------------------------------------------------===// 452 // Non-virtual diamond inheritance tests 453 //===----------------------------------------------------------------------===// 454 455 /* 456 NonVirtualBase NonVirtualBase 457 | | 458 | | 459 | | 460 First Second 461 \ / 462 \ / 463 \ / 464 NonVirtualDiamondInheritanceTest 465 */ 466 467 struct NonVirtualBase1 { 468 int x; 469 NonVirtualBase1() = default; 470 NonVirtualBase1(int) : x(46) {} 471 }; 472 struct First1 : public NonVirtualBase1 { 473 First1() = default; 474 First1(int) : NonVirtualBase1(int{}) {} 475 }; 476 struct Second1 : public NonVirtualBase1 { 477 Second1() = default; 478 Second1(int) : NonVirtualBase1(int{}) {} 479 }; 480 481 class NonVirtualDiamondInheritanceTest1 : public First1, public Second1 { 482 int z; 483 484 public: 485 NonVirtualDiamondInheritanceTest1() 486 : First1(int{}), 487 Second1(int{}) { 488 z = 47; 489 // All good! 490 } 491 492 NonVirtualDiamondInheritanceTest1(int) 493 : First1(int{}) { 494 Second1::x = 48; 495 z = 49; 496 // All good! 497 } 498 499 NonVirtualDiamondInheritanceTest1(int, int) 500 : Second1(int{}) { 501 First1::x = 50; 502 z = 51; 503 // All good! 504 } 505 }; 506 507 void fNonVirtualDiamondInheritanceTest1() { 508 NonVirtualDiamondInheritanceTest1(); 509 NonVirtualDiamondInheritanceTest1(int()); 510 NonVirtualDiamondInheritanceTest1(int(), int()); 511 } 512 513 struct NonVirtualBase2 { 514 int x; // expected-note{{uninitialized field 'this->x'}} 515 NonVirtualBase2() = default; 516 NonVirtualBase2(int) : x(52) {} 517 }; 518 struct First2 : public NonVirtualBase2 { 519 First2() = default; 520 First2(int) : NonVirtualBase2(int{}) {} 521 }; 522 struct Second2 : public NonVirtualBase2 { 523 Second2() = default; 524 Second2(int) : NonVirtualBase2(int{}) {} 525 }; 526 527 class NonVirtualDiamondInheritanceTest2 : public First2, public Second2 { 528 int z; 529 530 public: 531 NonVirtualDiamondInheritanceTest2() 532 : First2(int{}) { 533 z = 53; // expected-warning{{1 uninitialized field}} 534 } 535 }; 536 537 void fNonVirtualDiamondInheritanceTest2() { 538 NonVirtualDiamondInheritanceTest2(); 539 } 540 541 struct NonVirtualBase3 { 542 int x; // expected-note{{uninitialized field 'this->x'}} 543 NonVirtualBase3() = default; 544 NonVirtualBase3(int) : x(54) {} 545 }; 546 struct First3 : public NonVirtualBase3 { 547 First3() = default; 548 First3(int) : NonVirtualBase3(int{}) {} 549 }; 550 struct Second3 : public NonVirtualBase3 { 551 Second3() = default; 552 Second3(int) : NonVirtualBase3(int{}) {} 553 }; 554 555 class NonVirtualDiamondInheritanceTest3 : public First3, public Second3 { 556 int z; 557 558 public: 559 NonVirtualDiamondInheritanceTest3() 560 : Second3(int{}) { 561 z = 55; // expected-warning{{1 uninitialized field}} 562 } 563 }; 564 565 void fNonVirtualDiamondInheritanceTest3() { 566 NonVirtualDiamondInheritanceTest3(); 567 } 568 569 struct NonVirtualBase4 { 570 int x; // expected-note{{uninitialized field 'this->x'}} 571 // expected-note@-1{{uninitialized field 'this->x'}} 572 NonVirtualBase4() = default; 573 NonVirtualBase4(int) : x(56) {} 574 }; 575 struct First4 : public NonVirtualBase4 { 576 First4() = default; 577 First4(int) : NonVirtualBase4(int{}) {} 578 }; 579 struct Second4 : public NonVirtualBase4 { 580 Second4() = default; 581 Second4(int) : NonVirtualBase4(int{}) {} 582 }; 583 584 class NonVirtualDiamondInheritanceTest4 : public First4, public Second4 { 585 int z; 586 587 public: 588 NonVirtualDiamondInheritanceTest4() { 589 z = 57; // expected-warning{{2 uninitialized fields}} 590 } 591 }; 592 593 void fNonVirtualDiamondInheritanceTest4() { 594 NonVirtualDiamondInheritanceTest4(); 595 } 596 597 struct NonVirtualBase5 { 598 int x; 599 NonVirtualBase5() = default; 600 NonVirtualBase5(int) : x(58) {} 601 }; 602 struct First5 : public NonVirtualBase5 { 603 First5() = default; 604 First5(int) : NonVirtualBase5(int{}) {} 605 }; 606 struct Second5 : public NonVirtualBase5 { 607 Second5() = default; 608 Second5(int) : NonVirtualBase5(int{}) {} 609 }; 610 611 class NonVirtualDiamondInheritanceTest5 : public First5, public Second5 { 612 int z; // expected-note{{uninitialized field 'this->z'}} 613 614 public: 615 NonVirtualDiamondInheritanceTest5() 616 : First5(int{}), 617 Second5(int{}) { // expected-warning{{1 uninitialized field}} 618 } 619 }; 620 621 void fNonVirtualDiamondInheritanceTest5() { 622 NonVirtualDiamondInheritanceTest5(); 623 } 624 625 struct NonVirtualBase6 { 626 int x; // expected-note{{uninitialized field 'this->x'}} 627 NonVirtualBase6() = default; 628 NonVirtualBase6(int) : x(59) {} 629 }; 630 struct First6 : public NonVirtualBase6 { 631 First6() = default; 632 First6(int) : NonVirtualBase6(int{}) {} 633 }; 634 struct Second6 : public NonVirtualBase6 { 635 Second6() = default; 636 Second6(int) : NonVirtualBase6(int{}) {} 637 }; 638 639 class NonVirtualDiamondInheritanceTest6 : public First6, public Second6 { 640 int z; // expected-note{{uninitialized field 'this->z'}} 641 642 public: 643 NonVirtualDiamondInheritanceTest6() // expected-warning{{2 uninitialized fields}} 644 : First6(int{}) { 645 // 'z' and 'Second::x' unintialized 646 } 647 }; 648 649 void fNonVirtualDiamondInheritanceTest6() { 650 NonVirtualDiamondInheritanceTest6(); 651 } 652 653 //===----------------------------------------------------------------------===// 654 // Virtual diamond inheritance tests 655 //===----------------------------------------------------------------------===// 656 657 /* 658 VirtualBase 659 / \ 660 / \ 661 / \ 662 VirtualFirst VirtualSecond 663 \ / 664 \ / 665 \ / 666 VirtualDiamondInheritanceTest 667 */ 668 669 struct VirtualBase1 { 670 int x; 671 VirtualBase1() = default; 672 VirtualBase1(int) : x(60) {} 673 }; 674 struct VirtualFirst1 : virtual public VirtualBase1 { 675 VirtualFirst1() = default; 676 VirtualFirst1(int) : VirtualBase1(int{}) {} 677 VirtualFirst1(int, int) { x = 61; } 678 }; 679 struct VirtualSecond1 : virtual public VirtualBase1 { 680 VirtualSecond1() = default; 681 VirtualSecond1(int) : VirtualBase1(int{}) {} 682 VirtualSecond1(int, int) { x = 62; } 683 }; 684 685 class VirtualDiamondInheritanceTest1 : public VirtualFirst1, public VirtualSecond1 { 686 687 public: 688 VirtualDiamondInheritanceTest1() { 689 x = 0; 690 // All good! 691 } 692 693 VirtualDiamondInheritanceTest1(int) 694 : VirtualFirst1(int{}, int{}), 695 VirtualSecond1(int{}, int{}) { 696 // All good! 697 } 698 699 VirtualDiamondInheritanceTest1(int, int) 700 : VirtualFirst1(int{}, int{}) { 701 // All good! 702 } 703 }; 704 705 void fVirtualDiamondInheritanceTest1() { 706 VirtualDiamondInheritanceTest1(); 707 VirtualDiamondInheritanceTest1(int()); 708 VirtualDiamondInheritanceTest1(int(), int()); 709 } 710 711 struct VirtualBase2 { 712 int x; // expected-note{{uninitialized field 'this->x'}} 713 VirtualBase2() = default; 714 VirtualBase2(int) : x(63) {} 715 }; 716 struct VirtualFirst2 : virtual public VirtualBase2 { 717 VirtualFirst2() = default; 718 VirtualFirst2(int) : VirtualBase2(int{}) {} 719 VirtualFirst2(int, int) { x = 64; } 720 }; 721 struct VirtualSecond2 : virtual public VirtualBase2 { 722 VirtualSecond2() = default; 723 VirtualSecond2(int) : VirtualBase2(int{}) {} 724 VirtualSecond2(int, int) { x = 65; } 725 }; 726 727 class VirtualDiamondInheritanceTest2 : public VirtualFirst2, public VirtualSecond2 { 728 729 public: 730 VirtualDiamondInheritanceTest2() // expected-warning{{1 uninitialized field}} 731 : VirtualFirst2(int{}) { 732 // From the N4659 C++ Standard Working Draft: 733 // 734 // (15.6.2.7) 735 // [...] A 'mem-initializer' where the 'mem-initializer-id' denotes a 736 // virtual base class is ignored during execution of a constructor of any 737 // class that is not the most derived class. 738 // 739 // This means that Left1::x will not be initialized, because in both 740 // VirtualFirst::VirtualFirst(int) and VirtualSecond::VirtualSecond(int) 741 // the constructor delegation to Left1::Left1(int) will be 742 // ignored. 743 } 744 }; 745 746 void fVirtualDiamondInheritanceTest2() { 747 VirtualDiamondInheritanceTest2(); 748 } 749 750 struct VirtualBase3 { 751 int x; // expected-note{{uninitialized field 'this->x'}} 752 VirtualBase3() = default; 753 VirtualBase3(int) : x(66) {} 754 }; 755 struct VirtualFirst3 : virtual public VirtualBase3 { 756 VirtualFirst3() = default; 757 VirtualFirst3(int) : VirtualBase3(int{}) {} 758 VirtualFirst3(int, int) { x = 67; } 759 }; 760 struct VirtualSecond3 : virtual public VirtualBase3 { 761 VirtualSecond3() = default; 762 VirtualSecond3(int) : VirtualBase3(int{}) {} 763 VirtualSecond3(int, int) { x = 68; } 764 }; 765 766 class VirtualDiamondInheritanceTest3 : public VirtualFirst3, public VirtualSecond3 { 767 768 public: 769 VirtualDiamondInheritanceTest3() // expected-warning{{1 uninitialized field}} 770 : VirtualFirst3(int{}) {} 771 }; 772 773 void fVirtualDiamondInheritanceTest3() { 774 VirtualDiamondInheritanceTest3(); 775 } 776