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