Skip to content

Commit 7ddeb70

Browse files
Improve tests
1 parent eac04e1 commit 7ddeb70

File tree

3 files changed

+58
-1
lines changed

3 files changed

+58
-1
lines changed

clang/include/clang/Basic/AttrDocs.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1640,6 +1640,8 @@ initializers exist):
16401640
}, {
16411641
.count = sizeof(buf) / sizeof(*buf),
16421642
// warning: field 'element_size' is not explicitly initialized
1643+
// (Note that a missing initializer for 'flags' is not diagnosed, because
1644+
// the field is not marked as requiring explicit initialization.)
16431645
});
16441646
}
16451647

clang/test/Sema/uninit-variables.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -557,8 +557,27 @@ struct with_explicit_field {
557557
int y [[clang::requires_explicit_initialization]]; // #FIELD_Y
558558
};
559559

560+
struct with_explicit_array {
561+
[[clang::requires_explicit_initialization]] int arr[2]; // #FIELD_ARR
562+
};
563+
564+
struct with_explicit_flex_array {
565+
int x;
566+
[[clang::requires_explicit_initialization]] int flex_arr[]; // #FIELD_FLEX_ARR
567+
};
568+
560569
void aggregate() {
561570
struct with_explicit_field a; // expected-warning {{field in 'with_explicit_field' requires explicit initialization but is not explicitly initialized}} expected-note@#FIELD_Y {{'y' declared here}}
562571
struct with_explicit_field b = {1}; // expected-warning {{field 'y' requires explicit initialization but is not explicitly initialized}} expected-note@#FIELD_Y {{'y' declared here}}
563572
(void)(&a != &b);
573+
574+
struct with_explicit_field c = {1, 2};
575+
struct with_explicit_field d = {.y = 3};
576+
(void)(&c != &d);
577+
578+
struct with_explicit_array e = {{1}}; // OK -- part of array is still initialized
579+
(void)e;
580+
581+
struct with_explicit_flex_array f = {2}; // expected-warning {{field 'flex_arr' requires explicit initialization but is not explicitly initialized}} expected-note@#FIELD_FLEX_ARR {{'flex_arr' declared here}}
582+
(void)f;
564583
}

clang/test/SemaCXX/uninitialized.cpp

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1473,6 +1473,30 @@ template<typename T> struct Outer {
14731473
};
14741474
Outer<int>::Inner outerinner;
14751475

1476+
struct Polymorphic { virtual ~Polymorphic() { } };
1477+
1478+
template<class... Bases>
1479+
struct Inherit : Bases... { // #TYPE_INHERIT
1480+
int g1; // #FIELD_G1
1481+
};
1482+
1483+
template<class... Bases>
1484+
struct InheritWithExplicit : Bases... { // #TYPE_INHERIT_WITH_EXPLICIT
1485+
int g2 [[clang::requires_explicit_initialization]]; // #FIELD_G2
1486+
};
1487+
1488+
struct Special {};
1489+
1490+
template<>
1491+
struct Inherit<Special> {
1492+
int g3 [[clang::requires_explicit_initialization]]; // #FIELD_G3
1493+
};
1494+
1495+
template<>
1496+
struct InheritWithExplicit<Special> {
1497+
int g4; // #FIELD_G4
1498+
};
1499+
14761500
void aggregate() {
14771501
struct NonAgg {
14781502
NonAgg() { }
@@ -1490,7 +1514,7 @@ void aggregate() {
14901514
};
14911515

14921516
struct C {
1493-
[[clang::requires_explicit_initialization]] int c1 = 2; // #FIELD_C1
1517+
[[clang::requires_explicit_initialization]] int c1; // #FIELD_C1
14941518
C() = default; // Test pre-C++20 aggregates
14951519
};
14961520

@@ -1573,4 +1597,16 @@ void aggregate() {
15731597
// expected-note@#FIELD_D2 {{'d2' declared here}}
15741598
E e;
15751599
(void)e;
1600+
1601+
InheritWithExplicit<> agg; // expected-warning {{field in 'InheritWithExplicit<>' requires explicit initialization but is not explicitly initialized}} expected-note@#FIELD_G2 {{'g2' declared here}}
1602+
(void)agg;
1603+
1604+
InheritWithExplicit<Polymorphic> polymorphic; // expected-warning@#FIELD_G2 {{'requires_explicit_initialization' attribute is ignored in non-aggregate type 'InheritWithExplicit<Polymorphic>'}}
1605+
(void)polymorphic;
1606+
1607+
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}}
1608+
(void)specialized_explicit;
1609+
1610+
InheritWithExplicit<Special> specialized_implicit; // no-warning
1611+
(void)specialized_implicit;
15761612
}

0 commit comments

Comments
 (0)