cxx-uninitialized-object.cpp 25.7 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184
// RUN: %clang_analyze_cc1 -std=c++14 -verify  %s \
// RUN:   -analyzer-checker=core \
// RUN:   -analyzer-checker=optin.cplusplus.UninitializedObject \
// RUN:   -analyzer-config optin.cplusplus.UninitializedObject:Pedantic=true -DPEDANTIC \
// RUN:   -analyzer-config \
// RUN:     optin.cplusplus.UninitializedObject:CheckPointeeInitialization=true

// RUN: %clang_analyze_cc1 -std=c++14 -verify  %s \
// RUN:   -analyzer-checker=core \
// RUN:   -analyzer-checker=optin.cplusplus.UninitializedObject \
// RUN:   -analyzer-config \
// RUN:     optin.cplusplus.UninitializedObject:CheckPointeeInitialization=true

//===----------------------------------------------------------------------===//
// Default constructor test.
//===----------------------------------------------------------------------===//

class CompilerGeneratedConstructorTest {
  int a, b, c, d, e, f, g, h, i, j;

public:
  CompilerGeneratedConstructorTest() = default;
};

void fCompilerGeneratedConstructorTest() {
  CompilerGeneratedConstructorTest();
}

#ifdef PEDANTIC
class DefaultConstructorTest {
  int a; // expected-note{{uninitialized field 'this->a'}}

public:
  DefaultConstructorTest();
};

DefaultConstructorTest::DefaultConstructorTest() = default;

void fDefaultConstructorTest() {
  DefaultConstructorTest(); // expected-warning{{1 uninitialized field}}
}
#else
class DefaultConstructorTest {
  int a;

public:
  DefaultConstructorTest();
};

DefaultConstructorTest::DefaultConstructorTest() = default;

void fDefaultConstructorTest() {
  DefaultConstructorTest();
}
#endif // PEDANTIC

//===----------------------------------------------------------------------===//
// Initializer list test.
//===----------------------------------------------------------------------===//

class InitListTest1 {
  int a;
  int b;

public:
  InitListTest1()
      : a(1),
        b(2) {
    // All good!
  }
};

void fInitListTest1() {
  InitListTest1();
}

class InitListTest2 {
  int a;
  int b; // expected-note{{uninitialized field 'this->b'}}

public:
  InitListTest2()
      : a(3) {} // expected-warning{{1 uninitialized field}}
};

void fInitListTest2() {
  InitListTest2();
}

class InitListTest3 {
  int a; // expected-note{{uninitialized field 'this->a'}}
  int b;

public:
  InitListTest3()
      : b(4) {} // expected-warning{{1 uninitialized field}}
};

void fInitListTest3() {
  InitListTest3();
}

//===----------------------------------------------------------------------===//
// Constructor body test.
//===----------------------------------------------------------------------===//

class CtorBodyTest1 {
  int a, b;

public:
  CtorBodyTest1() {
    a = 5;
    b = 6;
    // All good!
  }
};

void fCtorBodyTest1() {
  CtorBodyTest1();
}

class CtorBodyTest2 {
  int a;
  int b; // expected-note{{uninitialized field 'this->b'}}

public:
  CtorBodyTest2() {
    a = 7; // expected-warning{{1 uninitialized field}}
  }
};

void fCtorBodyTest2() {
  CtorBodyTest2();
}

class CtorBodyTest3 {
  int a; // expected-note{{uninitialized field 'this->a'}}
  int b;

public:
  CtorBodyTest3() {
    b = 8; // expected-warning{{1 uninitialized field}}
  }
};

void fCtorBodyTest3() {
  CtorBodyTest3();
}

#ifdef PEDANTIC
class CtorBodyTest4 {
  int a; // expected-note{{uninitialized field 'this->a'}}
  int b; // expected-note{{uninitialized field 'this->b'}}

public:
  CtorBodyTest4() {}
};

void fCtorBodyTest4() {
  CtorBodyTest4(); // expected-warning{{2 uninitialized fields}}
}
#else
class CtorBodyTest4 {
  int a;
  int b;

public:
  CtorBodyTest4() {}
};

void fCtorBodyTest4() {
  CtorBodyTest4();
}
#endif

//===----------------------------------------------------------------------===//
// Constructor delegation test.
//===----------------------------------------------------------------------===//

class CtorDelegationTest1 {
  int a;
  int b;

public:
  CtorDelegationTest1(int)
      : a(9) {
    // leaves 'b' unintialized, but we'll never check this function
  }

  CtorDelegationTest1()
      : CtorDelegationTest1(int{}) { // Initializing 'a'
    b = 10;
    // All good!
  }
};

void fCtorDelegationTest1() {
  CtorDelegationTest1();
}

class CtorDelegationTest2 {
  int a; // expected-note{{uninitialized field 'this->a'}}
  int b;

public:
  CtorDelegationTest2(int)
      : b(11) {
    // leaves 'a' unintialized, but we'll never check this function
  }

  CtorDelegationTest2()
      : CtorDelegationTest2(int{}) { // expected-warning{{1 uninitialized field}}
  }
};

void fCtorDelegationTest2() {
  CtorDelegationTest2();
}

//===----------------------------------------------------------------------===//
// Tests for classes containing records.
//===----------------------------------------------------------------------===//

class ContainsRecordTest1 {
  struct RecordType {
    int x;
    int y;
  } rec;
  int c, d;

public:
  ContainsRecordTest1()
      : rec({12, 13}),
        c(14),
        d(15) {
    // All good!
  }
};

void fContainsRecordTest1() {
  ContainsRecordTest1();
}

class ContainsRecordTest2 {
  struct RecordType {
    int x;
    int y; // expected-note{{uninitialized field 'this->rec.y'}}
  } rec;
  int c, d;

public:
  ContainsRecordTest2()
      : c(16),
        d(17) {
    rec.x = 18; // expected-warning{{1 uninitialized field}}
  }
};

void fContainsRecordTest2() {
  ContainsRecordTest2();
}

class ContainsRecordTest3 {
  struct RecordType {
    int x; // expected-note{{uninitialized field 'this->rec.x'}}
    int y; // expected-note{{uninitialized field 'this->rec.y'}}
  } rec;
  int c, d;

public:
  ContainsRecordTest3()
      : c(19),
        d(20) { // expected-warning{{2 uninitialized fields}}
  }
};

void fContainsRecordTest3() {
  ContainsRecordTest3();
}

class ContainsRecordTest4 {
  struct RecordType {
    int x; // expected-note{{uninitialized field 'this->rec.x'}}
    int y; // expected-note{{uninitialized field 'this->rec.y'}}
  } rec;
  int c, d; // expected-note{{uninitialized field 'this->d'}}

public:
  ContainsRecordTest4()
      : c(19) { // expected-warning{{3 uninitialized fields}}
  }
};

void fContainsRecordTest4() {
  ContainsRecordTest4();
}

//===----------------------------------------------------------------------===//
// Tests for template classes.
//===----------------------------------------------------------------------===//

template <class T>
class IntTemplateClassTest1 {
  T t;
  int b;

public:
  IntTemplateClassTest1(T i) {
    b = 21;
    t = i;
    // All good!
  }
};

void fIntTemplateClassTest1() {
  IntTemplateClassTest1<int>(22);
}

template <class T>
class IntTemplateClassTest2 {
  T t; // expected-note{{uninitialized field 'this->t'}}
  int b;

public:
  IntTemplateClassTest2() {
    b = 23; // expected-warning{{1 uninitialized field}}
  }
};

void fIntTemplateClassTest2() {
  IntTemplateClassTest2<int>();
}

struct Record {
  int x; // expected-note{{uninitialized field 'this->t.x'}}
  int y; // expected-note{{uninitialized field 'this->t.y'}}
};

template <class T>
class RecordTemplateClassTest {
  T t;
  int b;

public:
  RecordTemplateClassTest() {
    b = 24; // expected-warning{{2 uninitialized fields}}
  }
};

void fRecordTemplateClassTest() {
  RecordTemplateClassTest<Record>();
}

//===----------------------------------------------------------------------===//
// Tests involving functions with unknown implementations.
//===----------------------------------------------------------------------===//

template <class T>
void mayInitialize(T &);

template <class T>
void wontInitialize(const T &);

class PassingToUnknownFunctionTest1 {
  int a, b;

public:
  PassingToUnknownFunctionTest1() {
    mayInitialize(a);
    mayInitialize(b);
    // All good!
  }

  PassingToUnknownFunctionTest1(int) {
    mayInitialize(a);
    // All good!
  }

  PassingToUnknownFunctionTest1(int, int) {
    mayInitialize(*this);
    // All good!
  }
};

void fPassingToUnknownFunctionTest1() {
  PassingToUnknownFunctionTest1();
  PassingToUnknownFunctionTest1(int());
  PassingToUnknownFunctionTest1(int(), int());
}

class PassingToUnknownFunctionTest2 {
  int a; // expected-note{{uninitialized field 'this->a'}}
  int b;

public:
  PassingToUnknownFunctionTest2() {
    wontInitialize(a);
    b = 4; // expected-warning{{1 uninitialized field}}
  }
};

void fPassingToUnknownFunctionTest2() {
  PassingToUnknownFunctionTest2();
}

//===----------------------------------------------------------------------===//
// Tests for classes containing unions.
//===----------------------------------------------------------------------===//

// FIXME: As of writing this checker, there is no good support for union types
// in the Static Analyzer. Here is non-exhaustive list of cases.
// Note that the rules for unions are different in C and C++.
// http://lists.llvm.org/pipermail/cfe-dev/2017-March/052910.html

class ContainsSimpleUnionTest1 {
  union SimpleUnion {
    float uf;
    int ui;
    char uc;
  } u;

public:
  ContainsSimpleUnionTest1() {
    u.uf = 3.14;
    // All good!
  }
};

void fContainsSimpleUnionTest1() {
  ContainsSimpleUnionTest1();
}

class ContainsSimpleUnionTest2 {
  union SimpleUnion {
    float uf;
    int ui;
    char uc;
    // TODO: we'd expect the note: {{uninitialized field 'this->u'}}
  } u; // no-note

public:
  ContainsSimpleUnionTest2() {}
};

void fContainsSimpleUnionTest2() {
  // TODO: we'd expect the warning: {{1 uninitialized field}}
  ContainsSimpleUnionTest2(); // no-warning
}

class UnionPointerTest1 {
public:
  union SimpleUnion {
    float uf;
    int ui;
    char uc;
  };

private:
  SimpleUnion *uptr;

public:
  UnionPointerTest1(SimpleUnion *uptr, int) : uptr(uptr) {
    // All good!
  }
};

void fUnionPointerTest1() {
  UnionPointerTest1::SimpleUnion u;
  u.uf = 41;
  UnionPointerTest1(&u, int());
}

class UnionPointerTest2 {
public:
  union SimpleUnion {
    float uf;
    int ui;
    char uc;
  };

private:
  // TODO: we'd expect the note: {{uninitialized field 'this->uptr'}}
  SimpleUnion *uptr; // no-note

public:
  UnionPointerTest2(SimpleUnion *uptr, char) : uptr(uptr) {}
};

void fUnionPointerTest2() {
  UnionPointerTest2::SimpleUnion u;
  // TODO: we'd expect the warning: {{1 uninitialized field}}
  UnionPointerTest2(&u, int()); // no-warning
}

class ContainsUnionWithRecordTest1 {
  union UnionWithRecord {
    struct RecordType {
      int x;
      int y;
    } us;
    double ud;
    long ul;

    UnionWithRecord(){};
  } u;

public:
  ContainsUnionWithRecordTest1() {
    u.ud = 3.14;
    // All good!
  }
};

void fContainsUnionWithRecordTest1() {
  ContainsUnionWithRecordTest1();
}

class ContainsUnionWithRecordTest2 {
  union UnionWithRecord {
    struct RecordType {
      int x;
      int y;
    } us;
    double ud;
    long ul;

    UnionWithRecord(){};
  } u;

public:
  ContainsUnionWithRecordTest2() {
    u.us = UnionWithRecord::RecordType{42, 43};
    // All good!
  }
};

void fContainsUnionWithRecordTest2() {
  ContainsUnionWithRecordTest1();
}

class ContainsUnionWithRecordTest3 {
  union UnionWithRecord {
    struct RecordType {
      int x;
      int y;
    } us;
    double ud;
    long ul;

    UnionWithRecord(){};
    // TODO: we'd expect the note: {{uninitialized field 'this->u'}}
  } u; // no-note

public:
  ContainsUnionWithRecordTest3() {
    UnionWithRecord::RecordType rec;
    rec.x = 44;
    // TODO: we'd expect the warning: {{1 uninitialized field}}
    u.us = rec; // no-warning
  }
};

void fContainsUnionWithRecordTest3() {
  ContainsUnionWithRecordTest3();
}

class ContainsUnionWithSimpleUnionTest1 {
  union UnionWithSimpleUnion {
    union SimpleUnion {
      float uf;
      int ui;
      char uc;
    } usu;
    long ul;
    unsigned uu;
  } u;

public:
  ContainsUnionWithSimpleUnionTest1() {
    u.usu.ui = 5;
    // All good!
  }
};

void fContainsUnionWithSimpleUnionTest1() {
  ContainsUnionWithSimpleUnionTest1();
}

class ContainsUnionWithSimpleUnionTest2 {
  union UnionWithSimpleUnion {
    union SimpleUnion {
      float uf;
      int ui;
      char uc;
    } usu;
    long ul;
    unsigned uu;
    // TODO: we'd expect the note: {{uninitialized field 'this->u'}}
  } u; // no-note

public:
  ContainsUnionWithSimpleUnionTest2() {}
};

void fContainsUnionWithSimpleUnionTest2() {
  // TODO: we'd expect the warning: {{1 uninitialized field}}
  ContainsUnionWithSimpleUnionTest2(); // no-warning
}

//===----------------------------------------------------------------------===//
// Zero initialization tests.
//===----------------------------------------------------------------------===//

struct GlobalVariableTest {
  int i;

  GlobalVariableTest() {}
};

GlobalVariableTest gvt; // no-warning

//===----------------------------------------------------------------------===//
// Copy and move constructor tests.
//===----------------------------------------------------------------------===//

template <class T>
void funcToSquelchCompilerWarnings(const T &t);

#ifdef PEDANTIC
struct CopyConstructorTest {
  int i; // expected-note{{uninitialized field 'this->i'}}

  CopyConstructorTest() : i(1337) {}
  CopyConstructorTest(const CopyConstructorTest &other) {}
};

void fCopyConstructorTest() {
  CopyConstructorTest cct;
  CopyConstructorTest copy = cct; // expected-warning{{1 uninitialized field}}
  funcToSquelchCompilerWarnings(copy);
}
#else
struct CopyConstructorTest {
  int i;

  CopyConstructorTest() : i(1337) {}
  CopyConstructorTest(const CopyConstructorTest &other) {}
};

void fCopyConstructorTest() {
  CopyConstructorTest cct;
  CopyConstructorTest copy = cct;
  funcToSquelchCompilerWarnings(copy);
}
#endif // PEDANTIC

struct MoveConstructorTest {
  // TODO: we'd expect the note: {{uninitialized field 'this->i'}}
  int i; // no-note

  MoveConstructorTest() : i(1337) {}
  MoveConstructorTest(const CopyConstructorTest &other) = delete;
  MoveConstructorTest(const CopyConstructorTest &&other) {}
};

void fMoveConstructorTest() {
  MoveConstructorTest cct;
  // TODO: we'd expect the warning: {{1 uninitialized field}}
  MoveConstructorTest copy(static_cast<MoveConstructorTest &&>(cct)); // no-warning
  funcToSquelchCompilerWarnings(copy);
}

//===----------------------------------------------------------------------===//
// Array tests.
//===----------------------------------------------------------------------===//

struct IntArrayTest {
  int arr[256];

  IntArrayTest() {
    // All good!
  }
};

void fIntArrayTest() {
  IntArrayTest();
}

struct RecordTypeArrayTest {
  struct RecordType {
    int x, y;
  } arr[256];

  RecordTypeArrayTest() {
    // All good!
  }
};

void fRecordTypeArrayTest() {
  RecordTypeArrayTest();
}

template <class T>
class CharArrayPointerTest {
  T *t; // no-crash

public:
  CharArrayPointerTest(T *t, int) : t(t) {}
};

void fCharArrayPointerTest() {
  char str[16] = "012345678912345";
  CharArrayPointerTest<char[16]>(&str, int());
}

//===----------------------------------------------------------------------===//
// Memset tests.
//===----------------------------------------------------------------------===//

struct MemsetTest1 {
  int a, b, c;

  MemsetTest1() {
    __builtin_memset(this, 0, sizeof(decltype(*this)));
  }
};

void fMemsetTest1() {
  MemsetTest1();
}

struct MemsetTest2 {
  int a;

  MemsetTest2() {
    __builtin_memset(&a, 0, sizeof(int));
  }
};

void fMemsetTest2() {
  MemsetTest2();
}

//===----------------------------------------------------------------------===//
// Lambda tests.
//===----------------------------------------------------------------------===//

template <class Callable>
struct LambdaThisTest {
  Callable functor;

  LambdaThisTest(const Callable &functor, int) : functor(functor) {
    // All good!
  }
};

struct HasCapturableThis {
  void fLambdaThisTest() {
    auto isEven = [this](int a) { return a % 2 == 0; }; // no-crash
    LambdaThisTest<decltype(isEven)>(isEven, int());
  }
};

template <class Callable>
struct LambdaTest1 {
  Callable functor;

  LambdaTest1(const Callable &functor, int) : functor(functor) {
    // All good!
  }
};

void fLambdaTest1() {
  auto isEven = [](int a) { return a % 2 == 0; };
  LambdaTest1<decltype(isEven)>(isEven, int());
}

#ifdef PEDANTIC
template <class Callable>
struct LambdaTest2 {
  Callable functor;

  LambdaTest2(const Callable &functor, int) : functor(functor) {} // expected-warning{{1 uninitialized field}}
};

void fLambdaTest2() {
  int b;
  auto equals = [&b](int a) { return a == b; }; // expected-note{{uninitialized pointee 'this->functor./*captured variable*/b'}}
  LambdaTest2<decltype(equals)>(equals, int());
}
#else
template <class Callable>
struct LambdaTest2 {
  Callable functor;

  LambdaTest2(const Callable &functor, int) : functor(functor) {}
};

void fLambdaTest2() {
  int b;
  auto equals = [&b](int a) { return a == b; };
  LambdaTest2<decltype(equals)>(equals, int());
}
#endif //PEDANTIC

#ifdef PEDANTIC
namespace LT3Detail {

struct RecordType {
  int x; // expected-note{{uninitialized field 'this->functor./*captured variable*/rec1.x'}}
  int y; // expected-note{{uninitialized field 'this->functor./*captured variable*/rec1.y'}}
};

} // namespace LT3Detail
template <class Callable>
struct LambdaTest3 {
  Callable functor;

  LambdaTest3(const Callable &functor, int) : functor(functor) {} // expected-warning{{2 uninitialized fields}}
};

void fLambdaTest3() {
  LT3Detail::RecordType rec1;
  auto equals = [&rec1](LT3Detail::RecordType rec2) {
    return rec1.x == rec2.x;
  };
  LambdaTest3<decltype(equals)>(equals, int());
}
#else
namespace LT3Detail {

struct RecordType {
  int x;
  int y;
};

} // namespace LT3Detail
template <class Callable>
struct LambdaTest3 {
  Callable functor;

  LambdaTest3(const Callable &functor, int) : functor(functor) {}
};

void fLambdaTest3() {
  LT3Detail::RecordType rec1;
  auto equals = [&rec1](LT3Detail::RecordType rec2) {
    return rec1.x == rec2.x;
  };
  LambdaTest3<decltype(equals)>(equals, int());
}
#endif //PEDANTIC

template <class Callable>
struct MultipleLambdaCapturesTest1 {
  Callable functor;
  int dontGetFilteredByNonPedanticMode = 0;

  MultipleLambdaCapturesTest1(const Callable &functor, int) : functor(functor) {} // expected-warning{{2 uninitialized field}}
};

void fMultipleLambdaCapturesTest1() {
  int b1, b2 = 3, b3;
  auto equals = [&b1, &b2, &b3](int a) { return a == b1 == b2 == b3; }; // expected-note{{uninitialized pointee 'this->functor./*captured variable*/b1'}}
  // expected-note@-1{{uninitialized pointee 'this->functor./*captured variable*/b3'}}
  MultipleLambdaCapturesTest1<decltype(equals)>(equals, int());
}

template <class Callable>
struct MultipleLambdaCapturesTest2 {
  Callable functor;
  int dontGetFilteredByNonPedanticMode = 0;

  MultipleLambdaCapturesTest2(const Callable &functor, int) : functor(functor) {} // expected-warning{{1 uninitialized field}}
};

void fMultipleLambdaCapturesTest2() {
  int b1, b2 = 3, b3;
  auto equals = [b1, &b2, &b3](int a) { return a == b1 == b2 == b3; }; // expected-note{{uninitialized pointee 'this->functor./*captured variable*/b3'}}
  MultipleLambdaCapturesTest2<decltype(equals)>(equals, int());
}

struct LambdaWrapper {
  void *func; // no-crash
  int dontGetFilteredByNonPedanticMode = 0;

  LambdaWrapper(void *ptr) : func(ptr) {} // expected-warning{{1 uninitialized field}}
};

struct ThisCapturingLambdaFactory {
  int a; // expected-note{{uninitialized field 'static_cast<decltype(a.ret()) *>(this->func)->/*'this' capture*/->a'}}

  auto ret() {
    return [this] { (void)this; };
  }
};

void fLambdaFieldWithInvalidThisCapture() {
  void *ptr;
  {
    ThisCapturingLambdaFactory a;
    decltype(a.ret()) lambda = a.ret();
    ptr = &lambda;
  }
  LambdaWrapper t(ptr);
}

//===----------------------------------------------------------------------===//
// System header tests.
//===----------------------------------------------------------------------===//

#include "Inputs/system-header-simulator-for-cxx-uninitialized-object.h"

struct SystemHeaderTest1 {
  RecordInSystemHeader rec; // defined in the system header simulator

  SystemHeaderTest1() {
    // All good!
  }
};

void fSystemHeaderTest1() {
  SystemHeaderTest1();
}

#ifdef PEDANTIC
struct SystemHeaderTest2 {
  struct RecordType {
    int x; // expected-note{{uninitialized field 'this->container.t.x}}
    int y; // expected-note{{uninitialized field 'this->container.t.y}}
  };
  ContainerInSystemHeader<RecordType> container;

  SystemHeaderTest2(RecordType &rec, int) : container(rec) {} // expected-warning{{2 uninitialized fields}}
};

void fSystemHeaderTest2() {
  SystemHeaderTest2::RecordType rec;
  SystemHeaderTest2(rec, int());
}
#else
struct SystemHeaderTest2 {
  struct RecordType {
    int x;
    int y;
  };
  ContainerInSystemHeader<RecordType> container;

  SystemHeaderTest2(RecordType &rec, int) : container(rec) {}
};

void fSystemHeaderTest2() {
  SystemHeaderTest2::RecordType rec;
  SystemHeaderTest2(rec, int());
}
#endif //PEDANTIC

//===----------------------------------------------------------------------===//
// Incomplete type tests.
//===----------------------------------------------------------------------===//

struct IncompleteTypeTest1 {
  struct RecordType;
  // no-crash
  RecordType *recptr; // expected-note{{uninitialized pointer 'this->recptr}}
  int dontGetFilteredByNonPedanticMode = 0;

  IncompleteTypeTest1() {} // expected-warning{{1 uninitialized field}}
};

void fIncompleteTypeTest1() {
  IncompleteTypeTest1();
}

struct IncompleteTypeTest2 {
  struct RecordType;
  RecordType *recptr; // no-crash
  int dontGetFilteredByNonPedanticMode = 0;

  RecordType *recordTypeFactory();

  IncompleteTypeTest2() : recptr(recordTypeFactory()) {}
};

void fIncompleteTypeTest2() {
  IncompleteTypeTest2();
}

struct IncompleteTypeTest3 {
  struct RecordType;
  RecordType &recref; // no-crash
  int dontGetFilteredByNonPedanticMode = 0;

  RecordType &recordTypeFactory();

  IncompleteTypeTest3() : recref(recordTypeFactory()) {}
};

void fIncompleteTypeTest3() {
  IncompleteTypeTest3();
}

//===----------------------------------------------------------------------===//
// Builtin type or enumeration type related tests.
//===----------------------------------------------------------------------===//

struct IntegralTypeTest {
  int a; // expected-note{{uninitialized field 'this->a'}}
  int dontGetFilteredByNonPedanticMode = 0;

  IntegralTypeTest() {} // expected-warning{{1 uninitialized field}}
};

void fIntegralTypeTest() {
  IntegralTypeTest();
}

struct FloatingTypeTest {
  float a; // expected-note{{uninitialized field 'this->a'}}
  int dontGetFilteredByNonPedanticMode = 0;

  FloatingTypeTest() {} // expected-warning{{1 uninitialized field}}
};

void fFloatingTypeTest() {
  FloatingTypeTest();
}

struct NullptrTypeTypeTest {
  decltype(nullptr) a; // expected-note{{uninitialized field 'this->a'}}
  int dontGetFilteredByNonPedanticMode = 0;

  NullptrTypeTypeTest() {} // expected-warning{{1 uninitialized field}}
};

void fNullptrTypeTypeTest() {
  NullptrTypeTypeTest();
}

struct EnumTest {
  enum Enum {
    A,
    B
  } enum1; // expected-note{{uninitialized field 'this->enum1'}}
  enum class Enum2 {
    A,
    B
  } enum2; // expected-note{{uninitialized field 'this->enum2'}}
  int dontGetFilteredByNonPedanticMode = 0;

  EnumTest() {} // expected-warning{{2 uninitialized fields}}
};

void fEnumTest() {
  EnumTest();
}

//===----------------------------------------------------------------------===//
// Tests for constructor calls within another cunstructor, without the two
// records being in any relation.
//===----------------------------------------------------------------------===//

void halt() __attribute__((__noreturn__));
void assert(int b) {
  if (!b)
    halt();
}

// While a singleton would make more sense as a static variable, that would zero
// initialize all of its fields, hence the not too practical implementation.
struct Singleton {
  int i; // expected-note{{uninitialized field 'this->i'}}
  int dontGetFilteredByNonPedanticMode = 0;

  Singleton() {
    assert(!isInstantiated);
    isInstantiated = true; // expected-warning{{1 uninitialized field}}
  }

  ~Singleton() {
    isInstantiated = false;
  }

  static bool isInstantiated;
};

bool Singleton::isInstantiated = false;

struct SingletonTest {
  int dontGetFilteredByNonPedanticMode = 0;

  SingletonTest() {
    Singleton();
  }
};

void fSingletonTest() {
  SingletonTest();
}

//===----------------------------------------------------------------------===//
// C++11 member initializer tests.
//===----------------------------------------------------------------------===//

struct CXX11MemberInitTest1 {
  int a = 3;
  int b;
  CXX11MemberInitTest1() : b(2) {
    // All good!
  }
};

void fCXX11MemberInitTest1() {
  CXX11MemberInitTest1();
}

struct CXX11MemberInitTest2 {
  struct RecordType {
    // TODO: we'd expect the note: {{uninitialized field 'this->rec.a'}}
    int a; // no-note
    // TODO: we'd expect the note: {{uninitialized field 'this->rec.b'}}
    int b; // no-note

    RecordType(int) {}
  };

  RecordType rec = RecordType(int());
  int dontGetFilteredByNonPedanticMode = 0;

  CXX11MemberInitTest2() {}
};

void fCXX11MemberInitTest2() {
  // TODO: we'd expect the warning: {{2 uninitializeds field}}
  CXX11MemberInitTest2(); // no-warning
}

//===----------------------------------------------------------------------===//
// "Esoteric" primitive type tests.
//===----------------------------------------------------------------------===//

struct MyAtomicInt {
  _Atomic(int) x; // expected-note{{uninitialized field 'this->x'}}
  int dontGetFilteredByNonPedanticMode = 0;

  MyAtomicInt() {} // expected-warning{{1 uninitialized field}}
};

void _AtomicTest() {
  MyAtomicInt b;
}

struct VectorSizeLong {
  VectorSizeLong() {}
  __attribute__((__vector_size__(16))) long x;
};

void __vector_size__LongTest() {
  // TODO: Warn for v.x.
  VectorSizeLong v;
  v.x[0] = 0;
}

struct ComplexUninitTest {
  ComplexUninitTest() {}
  __complex__ float x;
  __complex__ int y;
};

struct ComplexInitTest {
  ComplexInitTest() {
    x = {1.0f, 1.0f};
    y = {1, 1};
  }
  __complex__ float x;
  __complex__ int y;
};

void fComplexTest() {
  ComplexInitTest x;

  // TODO: we should emit a warning for x2.x and x2.y.
  ComplexUninitTest x2;
}