diff options
Diffstat (limited to 'clang/test/SemaCXX/uninitialized.cpp')
-rw-r--r-- | clang/test/SemaCXX/uninitialized.cpp | 177 |
1 files changed, 175 insertions, 2 deletions
diff --git a/clang/test/SemaCXX/uninitialized.cpp b/clang/test/SemaCXX/uninitialized.cpp index 8a640c9..95dd488 100644 --- a/clang/test/SemaCXX/uninitialized.cpp +++ b/clang/test/SemaCXX/uninitialized.cpp @@ -1,5 +1,6 @@ -// RUN: %clang_cc1 -fsyntax-only -Wall -Wuninitialized -Wno-unused-value -Wno-unused-lambda-capture -Wno-uninitialized-const-reference -std=c++1z -verify %s -// RUN: %clang_cc1 -fsyntax-only -Wall -Wuninitialized -Wno-unused-value -Wno-unused-lambda-capture -Wno-uninitialized-const-reference -std=c++1z -verify %s -fexperimental-new-constant-interpreter +// RUN: %clang_cc1 -fsyntax-only -Wall -Wc++20-compat -Wuninitialized -Wno-unused-value -Wno-unused-lambda-capture -Wno-uninitialized-const-reference -std=c++1z -verify %s +// RUN: %clang_cc1 -fsyntax-only -Wall -Wc++20-compat -Wuninitialized -Wno-unused-value -Wno-unused-lambda-capture -Wno-uninitialized-const-reference -std=c++1z -verify %s -fexperimental-new-constant-interpreter +// RUN: %clang_cc1 -fsyntax-only -Wall -Wc++20-compat -Wuninitialized -Wno-unused-value -Wno-unused-lambda-capture -Wno-uninitialized-const-reference -std=c++20 -verify %s // definitions for std::move namespace std { @@ -1472,3 +1473,175 @@ template<typename T> struct Outer { }; }; Outer<int>::Inner outerinner; + +struct Polymorphic { virtual ~Polymorphic() { } }; + +template<class... Bases> +struct Inherit : Bases... { // #TYPE_INHERIT + int g1; // #FIELD_G1 +}; + +template<class... Bases> +struct InheritWithExplicit : Bases... { // #TYPE_INHERIT_WITH_EXPLICIT + int g2 [[clang::requires_explicit_initialization]]; // #FIELD_G2 +}; + +struct Special {}; + +template<> +struct Inherit<Special> { + int g3 [[clang::requires_explicit_initialization]]; // #FIELD_G3 +}; + +template<> +struct InheritWithExplicit<Special> { + int g4; // #FIELD_G4 +}; + +void aggregate() { + struct NonAgg { + NonAgg() { } + [[clang::requires_explicit_initialization]] int na; // expected-warning {{'requires_explicit_initialization' attribute is ignored in non-aggregate type 'NonAgg'}} + }; + NonAgg nonagg; // no-warning + (void)nonagg; + + struct S { + [[clang::requires_explicit_initialization]] int s1; // #FIELD_S1 + int s2; + int s3 = 12; + [[clang::requires_explicit_initialization]] int s4 = 100; // #FIELD_S4 + static void foo(S) { } + }; + + struct C { +#if __cplusplus < 202002L + // expected-warning@+1 {{explicit initialization of field 'c1' will not be enforced in C++20 and later because 'C' has a user-declared constructor, making the type no longer an aggregate}} + [[clang::requires_explicit_initialization]] +#endif + int c1; // #FIELD_C1 + C() = default; // Test pre-C++20 aggregates + }; + + struct D : S { // #TYPE_D + int d1; + int d2 [[clang::requires_explicit_initialization]]; // #FIELD_D2 + }; + + struct D2 : D { // #TYPE_D2 + }; + + struct E { // #TYPE_E + int e1; + D e2 [[clang::requires_explicit_initialization]]; // #FIELD_E2 + struct { + [[clang::requires_explicit_initialization]] D e3; + D2 e4 [[clang::requires_explicit_initialization]]; + }; + }; + + S::foo(S{1, 2, 3, 4}); + S::foo(S{.s1 = 100, .s4 = 100}); + S::foo(S{.s1 = 100}); // expected-warning {{field 's4' requires explicit initialization but is not explicitly initialized}} expected-note@#FIELD_S4 {{'s4' declared here}} + + S s{.s1 = 100, .s4 = 100}; + (void)s; + + S t{.s4 = 100}; // expected-warning {{field 's1' requires explicit initialization but is not explicitly initialized}} expected-note@#FIELD_S1 {{'s1' declared here}} + (void)t; + + S *ptr1 = new S; // expected-warning {{field in 'S' requires explicit initialization but is not explicitly initialized}} expected-note@#FIELD_S4 {{'s4' declared here}} expected-note@#FIELD_S1 {{'s1' declared here}} + delete ptr1; + + S *ptr2 = new S{.s1 = 100, .s4 = 100}; + delete ptr2; + +#if __cplusplus >= 202002L + // expected-warning@+3 {{field 's1' requires explicit initialization but is not explicitly initialized}} expected-note@#FIELD_S1 {{'s1' declared here}} + // expected-warning@+2 {{field 's4' requires explicit initialization but is not explicitly initialized}} expected-note@#FIELD_S4 {{'s4' declared here}} + // expected-warning@+1 {{field 'd2' requires explicit initialization but is not explicitly initialized}} expected-note@#FIELD_D2 {{'d2' declared here}} + D a1({}, 0); + (void)a1; + + // expected-warning@+3 {{field 's1' requires explicit initialization but is not explicitly initialized}} expected-note@#FIELD_S1 {{'s1' declared here}} + // expected-warning@+2 {{field 's4' requires explicit initialization but is not explicitly initialized}} expected-note@#FIELD_S4 {{'s4' declared here}} + // expected-warning@+1 {{field 'd2' requires explicit initialization but is not explicitly initialized}} expected-note@#FIELD_D2 {{'d2' declared here}} + D a2(S{}, 0); + (void)a2; + + // expected-warning@+2 {{field 's4' requires explicit initialization but is not explicitly initialized}} expected-note@#FIELD_S4 {{'s4' declared here}} + // expected-warning@+1 {{field 'd2' requires explicit initialization but is not explicitly initialized}} expected-note@#FIELD_D2 {{'d2' declared here}} + D a3(S{.s1 = 0}, 0); + (void)a3; + + // expected-warning@+3 {{field 's1' requires explicit initialization but is not explicitly initialized}} expected-note@#FIELD_S1 {{'s1' declared here}} + // expected-warning@+2 {{field 's4' requires explicit initialization but is not explicitly initialized}} expected-note@#FIELD_S4 {{'s4' declared here}} + // expected-warning@+1 {{field 'd2' requires explicit initialization but is not explicitly initialized}} expected-note@#FIELD_D2 {{'d2' declared here}} + D a4(S(), 0); + (void)a4; + + // expected-warning@+2 {{field 's4' requires explicit initialization but is not explicitly initialized}} expected-note@#FIELD_S4 {{'s4' declared here}} + // expected-warning@+1 {{field 'd2' requires explicit initialization but is not explicitly initialized}} expected-note@#FIELD_D2 {{'d2' declared here}} + D a5(S(0), 0); + (void)a5; + + // expected-warning@+2 {{field 's4' requires explicit initialization but is not explicitly initialized}} expected-note@#FIELD_S4 {{'s4' declared here}} + // expected-warning@+1 {{field 'd2' requires explicit initialization but is not explicitly initialized}} expected-note@#FIELD_D2 {{'d2' declared here}} + D a6 = {S(0), 0}; + (void)a6; +#endif + +#if 201103L <= __cplusplus && __cplusplus < 202002L + C a; // expected-warning {{field in 'C' requires explicit initialization but is not explicitly initialized}} expected-note@#FIELD_C1 {{'c1' declared here}} + (void)a; +#endif + + // expected-warning@+2 {{field 's4' requires explicit initialization but is not explicitly initialized}} expected-note@#FIELD_S4 {{'s4' declared here}} + // expected-warning@+1 {{field 's1' requires explicit initialization but is not explicitly initialized}} expected-note@#FIELD_S1 {{'s1' declared here}} + D b{.d2 = 1}; + (void)b; + + // expected-warning@+3 {{field 's4' requires explicit initialization but is not explicitly initialized}} expected-note@#FIELD_S4 {{'s4' declared here}} + // expected-warning@+2 {{field 'd2' requires explicit initialization but is not explicitly initialized}} expected-note@#FIELD_D2 {{'d2' declared here}} + // expected-warning@+1 {{field 's1' requires explicit initialization but is not explicitly initialized}} expected-note@#FIELD_S1 {{'s1' declared here}} + D c{.d1 = 5}; + + // expected-warning@+3 {{field 's4' requires explicit initialization but is not explicitly initialized}} expected-note@#FIELD_S4 {{'s4' declared here}} + // expected-warning@+2 {{field 'd2' requires explicit initialization but is not explicitly initialized}} expected-note@#FIELD_D2 {{'d2' declared here}} + // expected-warning@+1 {{field 's1' requires explicit initialization but is not explicitly initialized}} expected-note@#FIELD_S1 {{'s1' declared here}} + c = {{}, 0}; + (void)c; + + // expected-note@+3 {{in implicit default constructor for 'D' first required here}} + // expected-warning@#TYPE_D {{field in 'S' requires explicit initialization but is not explicitly initialized}} expected-note@#FIELD_S1 {{'s1' declared here}} expected-note@#FIELD_S4 {{'s4' declared here}} + // expected-warning@+1 {{field in 'D' requires explicit initialization but is not explicitly initialized}} expected-note@#FIELD_D2 {{'d2' declared here}} + D d; + (void)d; + + // expected-warning@+12 {{field in 'E' requires explicit initialization but is not explicitly initialized}} + // expected-note@#FIELD_E2 {{'e2' declared here}} + // expected-warning@#TYPE_E {{field in 'D' requires explicit initialization but is not explicitly initialized}} + // expected-note@+9 {{in implicit default constructor for 'E' first required here}} + // expected-note@#FIELD_D2 {{'d2' declared here}} + // expected-warning@#TYPE_E {{field in 'D' requires explicit initialization but is not explicitly initialized}} + // expected-note@#FIELD_D2 {{'d2' declared here}} + // expected-warning@#TYPE_E {{field in 'D2' requires explicit initialization but is not explicitly initialized}} + // expected-note@#TYPE_E {{in implicit default constructor for 'D2' first required here}} + // expected-warning@#TYPE_D2 {{field in 'D' requires explicit initialization but is not explicitly initialized}} + // expected-note@+2 {{in implicit default constructor for 'E' first required here}} + // expected-note@#FIELD_D2 {{'d2' declared here}} + E e; + (void)e; + + InheritWithExplicit<> agg; // expected-warning {{field in 'InheritWithExplicit<>' requires explicit initialization but is not explicitly initialized}} expected-note@#FIELD_G2 {{'g2' declared here}} + (void)agg; + + InheritWithExplicit<Polymorphic> polymorphic; // expected-warning@#FIELD_G2 {{'requires_explicit_initialization' attribute is ignored in non-aggregate type 'InheritWithExplicit<Polymorphic>'}} + (void)polymorphic; + + Inherit<Special> specialized_explicit; // expected-warning {{field in 'Inherit<Special>' requires explicit initialization but is not explicitly initialized}} expected-note@#FIELD_G3 {{'g3' declared here}} + (void)specialized_explicit; + + InheritWithExplicit<Special> specialized_implicit; // no-warning + (void)specialized_implicit; +} |