Skip to content

Commit b23359e

Browse files
committed
Add assertion, update logic, add test
All based on review feedback
1 parent c642095 commit b23359e

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

clang/lib/Sema/SemaInit.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6497,12 +6497,15 @@ static bool canPerformArrayCopy(const InitializedEntity &Entity) {
64976497
}
64986498

64996499
static const FieldDecl *getConstField(const RecordDecl *RD) {
6500+
assert(!isa<CXXRecordDecl>(RD) && "Only expect to call this in C mode");
65006501
for (const FieldDecl *FD : RD->fields()) {
65016502
QualType QT = FD->getType();
65026503
if (QT.isConstQualified())
65036504
return FD;
6504-
if (const auto *RD = QT->getAsRecordDecl())
6505-
return getConstField(RD);
6505+
if (const auto *RD = QT->getAsRecordDecl()) {
6506+
if (const FieldDecl *FD = getConstField(RD))
6507+
return FD;
6508+
}
65066509
}
65076510
return nullptr;
65086511
}

clang/test/Sema/warn-default-const-init.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ struct T { struct S s; }; // cxx-note {{default constructor of 'T'
1313
struct U { struct S s; const int j; };
1414
struct V { int i; const struct A a; }; // unsafe-note {{member 'a' declared 'const' here}} \
1515
cxx-note {{default constructor of 'V' is implicitly deleted because field 'a' of const-qualified type 'const struct A' would not be initialized}}
16+
struct W { struct A a; const int j; }; // unsafe-note {{member 'j' declared 'const' here}} \
17+
cxx-note {{default constructor of 'W' is implicitly deleted because field 'j' of const-qualified type 'const int' would not be initialized}}
1618

1719
void f() {
1820
struct S s1; // unsafe-warning {{default initialization of an object of type 'struct S' with const member leaves the object uninitialized and is incompatible with C++}} \
@@ -34,6 +36,12 @@ void x() {
3436
struct V v2 = { 0 };
3537
struct V v3 = { 0, { 0 } };
3638
}
39+
void y() {
40+
struct W w1; // unsafe-warning {{default initialization of an object of type 'struct W' with const member leaves the object uninitialized and is incompatible with C++}} \
41+
cxx-error {{call to implicitly-deleted default constructor of 'struct W'}}
42+
struct W w2 = { 0 };
43+
struct W w3 = { { 0 }, 0 };
44+
}
3745

3846
// Test a tentative definition which does eventually get an initializer.
3947
extern const int i;

0 commit comments

Comments
 (0)