155 lines · cpp
1// RUN: %clang_analyze_cc1 %s -verify -analyzer-checker=core,debug.ExprInspection2 3// Refer to https://github.com/llvm/llvm-project/issues/70464 for more details.4//5// When the base class does not have a declared constructor, the base6// initializer in the constructor of the derived class should use the given7// initializer list to finish the initialization of the base class.8//9// Also testing base constructor and delegate constructor to make sure this10// change will not break these two cases when a CXXConstructExpr is available.11 12void clang_analyzer_dump(int);13 14namespace init_list {15 16struct Base {17 int foox;18};19 20class Derived : public Base {21public:22 Derived() : Base{42} {23 // The dereference to this->foox below should be initialized properly.24 clang_analyzer_dump(this->foox); // expected-warning{{42 S32b}}25 clang_analyzer_dump(foox); // expected-warning{{42 S32b}}26 }27};28 29void entry() { Derived test; }30 31} // namespace init_list32 33namespace base_ctor_call {34 35struct Base {36 int foox;37 Base(int x) : foox(x) {}38};39 40class Derived : public Base {41public:42 Derived() : Base{42} {43 clang_analyzer_dump(this->foox); // expected-warning{{42 S32b}}44 clang_analyzer_dump(foox); // expected-warning{{42 S32b}}45 }46};47 48void entry() { Derived test; }49 50} // namespace base_ctor_call51 52namespace delegate_ctor_call {53 54struct Base {55 int foox;56};57 58struct Derived : Base {59 Derived(int parx) { this->foox = parx; }60 Derived() : Derived{42} {61 clang_analyzer_dump(this->foox); // expected-warning{{42 S32b}}62 clang_analyzer_dump(foox); // expected-warning{{42 S32b}}63 }64};65 66void entry() { Derived test; }67 68} // namespace delegate_ctor_call69 70// Additional test case from issue #5949371namespace init_list_array {72 73struct Base {74 int foox[5];75};76 77class Derived1 : public Base {78public:79 Derived1() : Base{{1,4,5,3,2}} {80 // The dereference to this->foox below should be initialized properly.81 clang_analyzer_dump(this->foox[0]); // expected-warning{{1 S32b}}82 clang_analyzer_dump(this->foox[1]); // expected-warning{{4 S32b}}83 clang_analyzer_dump(this->foox[2]); // expected-warning{{5 S32b}}84 clang_analyzer_dump(this->foox[3]); // expected-warning{{3 S32b}}85 clang_analyzer_dump(this->foox[4]); // expected-warning{{2 S32b}}86 clang_analyzer_dump(foox[0]); // expected-warning{{1 S32b}}87 clang_analyzer_dump(foox[1]); // expected-warning{{4 S32b}}88 clang_analyzer_dump(foox[2]); // expected-warning{{5 S32b}}89 clang_analyzer_dump(foox[3]); // expected-warning{{3 S32b}}90 clang_analyzer_dump(foox[4]); // expected-warning{{2 S32b}}91 }92};93 94void entry1() { Derived1 test; }95 96class Derived2 : public Base {97public:98 Derived2() : Base{{0}} {99 // The dereference to this->foox below should be initialized properly.100 clang_analyzer_dump(this->foox[0]); // expected-warning{{0 S32b}}101 clang_analyzer_dump(this->foox[1]); // expected-warning{{0 S32b}}102 clang_analyzer_dump(this->foox[2]); // expected-warning{{0 S32b}}103 clang_analyzer_dump(this->foox[3]); // expected-warning{{0 S32b}}104 clang_analyzer_dump(this->foox[4]); // expected-warning{{0 S32b}}105 clang_analyzer_dump(foox[0]); // expected-warning{{0 S32b}}106 clang_analyzer_dump(foox[1]); // expected-warning{{0 S32b}}107 clang_analyzer_dump(foox[2]); // expected-warning{{0 S32b}}108 clang_analyzer_dump(foox[3]); // expected-warning{{0 S32b}}109 clang_analyzer_dump(foox[4]); // expected-warning{{0 S32b}}110 }111};112 113void entry2() { Derived2 test; }114 115} // namespace init_list_array116 117namespace init_list_struct {118 119struct Base {120 struct { int a; int b; } foox;121};122 123class Derived : public Base {124public:125 Derived() : Base{{42, 24}} {126 // The dereference to this->foox below should be initialized properly.127 clang_analyzer_dump(this->foox.a); // expected-warning{{42 S32b}}128 clang_analyzer_dump(this->foox.b); // expected-warning{{24 S32b}}129 clang_analyzer_dump(foox.a); // expected-warning{{42 S32b}}130 clang_analyzer_dump(foox.b); // expected-warning{{24 S32b}}131 }132};133 134} // namespace init_list_struct135 136// Additional test case from issue 54533137namespace init_list_enum {138 139struct Base {140 enum : unsigned char { ZERO = 0, FORTY_TWO = 42 } foox;141};142 143class Derived : public Base {144public:145 Derived() : Base{FORTY_TWO} {146 // The dereference to this->foox below should be initialized properly.147 clang_analyzer_dump(this->foox); // expected-warning{{42 S32b}}148 clang_analyzer_dump(foox); // expected-warning{{42 S32b}}149 }150};151 152void entry() { Derived test; }153 154} // namespace init_list_enum155