Skip to content

Commit 00f9ef2

Browse files
[C] Modify -Wdefault-const-init (#137961)
Post-commit review feedback on #137166 raised a concern from the Linux kernel about wanting to silence the new diagnostic when the uninitialized object is a const member of a structure. These members can be initialized later if the containing object is non-const, such as through a call to memset, for example. This splits the diagnostic groups into: ``` -Wc++-compat -Wdefault-const-init -Wdefault-const-init-field -Wdefault-const-init-var -Wdefault-const-init-unsafe -Wdefault-const-init-field-unsafe -Wdefault-const-init-var-unsafe ``` --------- Co-authored-by: Mariya Podchishchaeva <[email protected]>
1 parent 4b3acfb commit 00f9ef2

File tree

7 files changed

+50
-38
lines changed

7 files changed

+50
-38
lines changed

clang/docs/ReleaseNotes.rst

+10-6
Original file line numberDiff line numberDiff line change
@@ -141,12 +141,16 @@ C Language Changes
141141
function type in Microsoft compatibility mode. #GH124869
142142
- Clang now allows ``restrict`` qualifier for array types with pointer elements (#GH92847).
143143
- Clang now diagnoses ``const``-qualified object definitions without an
144-
initializer. If the object is zero-initialized, it will be diagnosed under
145-
the new warning ``-Wdefault-const-init`` (which is grouped under
146-
``-Wc++-compat`` because this construct is not compatible with C++). If the
147-
object is left uninitialized, it will be diagnosed unsed the new warning
148-
``-Wdefault-const-init-unsafe`` (which is grouped under
149-
``-Wdefault-const-init``). #GH19297
144+
initializer. If the object is a variable or field which is zero-initialized,
145+
it will be diagnosed under the new warning ``-Wdefault-const-init-var`` or
146+
``-Wdefault-const-init-field``, respectively. Similarly, if the variable or
147+
field is not zero-initialized, it will be diagnosed under the new diagnostic
148+
``-Wdefault-const-init-var-unsafe`` or ``-Wdefault-const-init-field-unsafe``,
149+
respectively. The unsafe diagnostic variants are grouped under a new
150+
diagnostic ``-Wdefault-const-init-unsafe``, which itself is grouped under the
151+
new diagnostic ``-Wdefault-const-init``. Finally, ``-Wdefault-const-init`` is
152+
grouped under ``-Wc++-compat`` because these constructs are not compatible
153+
with C++. #GH19297
150154
- Added ``-Wimplicit-void-ptr-cast``, grouped under ``-Wc++-compat``, which
151155
diagnoses implicit conversion from ``void *`` to another pointer type as
152156
being incompatible with C++. (#GH17792)

clang/include/clang/Basic/DiagnosticGroups.td

+10-2
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,16 @@ def InitStringTooLongMissingNonString :
163163
def InitStringTooLongForCpp :
164164
DiagGroup<"c++-unterminated-string-initialization">;
165165
def HiddenCppDecl : DiagGroup<"c++-hidden-decl">;
166-
def DefaultConstInitUnsafe : DiagGroup<"default-const-init-unsafe">;
167-
def DefaultConstInit : DiagGroup<"default-const-init", [DefaultConstInitUnsafe]>;
166+
def DefaultConstInitFieldUnsafe : DiagGroup<"default-const-init-field-unsafe">;
167+
def DefaultConstInitVarUnsafe : DiagGroup<"default-const-init-var-unsafe">;
168+
def DefaultConstInitUnsafe : DiagGroup<"default-const-init-unsafe",
169+
[DefaultConstInitFieldUnsafe,
170+
DefaultConstInitVarUnsafe]>;
171+
def DefaultConstInitField : DiagGroup<"default-const-init-field">;
172+
def DefaultConstInitVar : DiagGroup<"default-const-init-var">;
173+
def DefaultConstInit : DiagGroup<"default-const-init",
174+
[DefaultConstInitField, DefaultConstInitVar,
175+
DefaultConstInitUnsafe]>;
168176
def ImplicitVoidPtrCast : DiagGroup<"implicit-void-ptr-cast">;
169177
def ImplicitIntToEnumCast : DiagGroup<"implicit-int-enum-cast",
170178
[ImplicitEnumEnumCast]>;

clang/include/clang/Basic/DiagnosticSemaKinds.td

+12-6
Original file line numberDiff line numberDiff line change
@@ -8218,14 +8218,20 @@ def err_address_space_qualified_delete : Error<
82188218

82198219
def note_default_init_const_member : Note<
82208220
"member %0 declared 'const' here">;
8221+
def warn_default_init_const_field : Warning<
8222+
"default initialization of an object of type %0 with const member is "
8223+
"incompatible with C++">, InGroup<DefaultConstInitField>, DefaultIgnore;
82218224
def warn_default_init_const : Warning<
8222-
"default initialization of an object of type %0%select{| with const member}1 "
8223-
"is incompatible with C++">,
8224-
InGroup<DefaultConstInit>, DefaultIgnore;
8225+
"default initialization of an object of type %0 is incompatible with C++">,
8226+
InGroup<DefaultConstInitVar>, DefaultIgnore;
8227+
def warn_default_init_const_field_unsafe : Warning<
8228+
"default initialization of an object of type %0 with const member leaves the "
8229+
"object uninitialized and is incompatible with C++">,
8230+
InGroup<DefaultConstInitFieldUnsafe>;
82258231
def warn_default_init_const_unsafe : Warning<
8226-
"default initialization of an object of type %0%select{| with const member}1 "
8227-
"leaves the object uninitialized and is incompatible with C++">,
8228-
InGroup<DefaultConstInitUnsafe>;
8232+
"default initialization of an object of type %0 leaves the object "
8233+
"uninitialized and is incompatible with C++">,
8234+
InGroup<DefaultConstInitVarUnsafe>;
82298235
def err_default_init_const : Error<
82308236
"default initialization of an object of const type %0"
82318237
"%select{| without a user-provided default constructor}1">;

clang/lib/Sema/Sema.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1457,7 +1457,7 @@ void Sema::ActOnEndOfTranslationUnit() {
14571457
if (VD->getStorageDuration() == SD_Static ||
14581458
VD->getStorageDuration() == SD_Thread)
14591459
DiagID = diag::warn_default_init_const;
1460-
Diag(VD->getLocation(), DiagID) << Type << /*not a field*/ 0;
1460+
Diag(VD->getLocation(), DiagID) << Type;
14611461
}
14621462

14631463
// Notify the consumer that we've completed a tentative definition.

clang/lib/Sema/SemaDecl.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -14348,7 +14348,7 @@ void Sema::ActOnUninitializedDecl(Decl *RealDecl) {
1434814348
if (Var->getStorageDuration() == SD_Static ||
1434914349
Var->getStorageDuration() == SD_Thread)
1435014350
DiagID = diag::warn_default_init_const;
14351-
Diag(Var->getLocation(), DiagID) << Type << /*not a field*/ 0;
14351+
Diag(Var->getLocation(), DiagID) << Type;
1435214352
}
1435314353

1435414354
// Check for jumps past the implicit initializer. C++0x

clang/lib/Sema/SemaInit.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -6607,12 +6607,12 @@ void InitializationSequence::InitializeFrom(Sema &S,
66076607
// initializer present.
66086608
if (!Initializer) {
66096609
if (const FieldDecl *FD = getConstField(Rec)) {
6610-
unsigned DiagID = diag::warn_default_init_const_unsafe;
6610+
unsigned DiagID = diag::warn_default_init_const_field_unsafe;
66116611
if (Var->getStorageDuration() == SD_Static ||
66126612
Var->getStorageDuration() == SD_Thread)
6613-
DiagID = diag::warn_default_init_const;
6613+
DiagID = diag::warn_default_init_const_field;
66146614

6615-
S.Diag(Var->getLocation(), DiagID) << Var->getType() << /*member*/ 1;
6615+
S.Diag(Var->getLocation(), DiagID) << Var->getType();
66166616
S.Diag(FD->getLocation(), diag::note_default_init_const_member) << FD;
66176617
}
66186618
}
+13-19
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,22 @@
1-
// RUN: %clang_cc1 -fsyntax-only -verify=c,unsafe -Wdefault-const-init %s
2-
// RUN: %clang_cc1 -fsyntax-only -verify=c,unsafe -Wc++-compat %s
3-
// RUN: %clang_cc1 -fsyntax-only -verify=unsafe %s
4-
// RUN: %clang_cc1 -fsyntax-only -verify=c -Wdefault-const-init -Wno-default-const-init-unsafe %s
5-
// RUN: %clang_cc1 -fsyntax-only -verify=good -Wno-default-const-init-unsafe %s
6-
// RUN: %clang_cc1 -fsyntax-only -verify=cxx -x c++ %s
7-
// good-no-diagnostics
1+
// RUN: %clang_cc1 -fsyntax-only -verify=unsafe-var,unsafe-field -Wdefault-const-init-unsafe %s
82

93
struct A { int i; };
10-
struct S{ const int i; }; // unsafe-note 2 {{member 'i' declared 'const' here}} \
4+
struct S{ const int i; }; // unsafe-field-note 2 {{member 'i' declared 'const' here}} \
115
cxx-note 3 {{default constructor of 'S' is implicitly deleted because field 'i' of const-qualified type 'const int' would not be initialized}}
126
struct T { struct S s; }; // cxx-note {{default constructor of 'T' is implicitly deleted because field 's' has a deleted default constructor}}
137
struct U { struct S s; const int j; };
14-
struct V { int i; const struct A a; }; // unsafe-note {{member 'a' declared 'const' here}} \
8+
struct V { int i; const struct A a; }; // unsafe-field-note {{member 'a' declared 'const' here}} \
159
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}} \
10+
struct W { struct A a; const int j; }; // unsafe-field-note {{member 'j' declared 'const' here}} \
1711
cxx-note {{default constructor of 'W' is implicitly deleted because field 'j' of const-qualified type 'const int' would not be initialized}}
1812

1913
void f() {
20-
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++}} \
14+
struct S s1; // unsafe-field-warning {{default initialization of an object of type 'struct S' with const member leaves the object uninitialized and is incompatible with C++}} \
2115
cxx-error {{call to implicitly-deleted default constructor of 'struct S'}}
2216
struct S s2 = { 0 };
2317
}
2418
void g() {
25-
struct T t1; // unsafe-warning {{default initialization of an object of type 'struct T' with const member leaves the object uninitialized and is incompatible with C++}} \
19+
struct T t1; // unsafe-field-warning {{default initialization of an object of type 'struct T' with const member leaves the object uninitialized and is incompatible with C++}} \
2620
cxx-error {{call to implicitly-deleted default constructor of 'struct T'}}
2721
struct T t2 = { { 0 } };
2822
}
@@ -31,13 +25,13 @@ void h() {
3125
struct U u2 = { { 0 }, 0 };
3226
}
3327
void x() {
34-
struct V v1; // unsafe-warning {{default initialization of an object of type 'struct V' with const member leaves the object uninitialized and is incompatible with C++}} \
28+
struct V v1; // unsafe-field-warning {{default initialization of an object of type 'struct V' with const member leaves the object uninitialized and is incompatible with C++}} \
3529
cxx-error {{call to implicitly-deleted default constructor of 'struct V'}}
3630
struct V v2 = { 0 };
3731
struct V v3 = { 0, { 0 } };
3832
}
3933
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++}} \
34+
struct W w1; // unsafe-field-warning {{default initialization of an object of type 'struct W' with const member leaves the object uninitialized and is incompatible with C++}} \
4135
cxx-error {{call to implicitly-deleted default constructor of 'struct W'}}
4236
struct W w2 = { 0 };
4337
struct W w3 = { { 0 }, 0 };
@@ -47,17 +41,17 @@ void y() {
4741
extern const int i;
4842
const int i = 12;
4943

50-
static const int j; // c-warning {{default initialization of an object of type 'const int' is incompatible with C++}} \
44+
static const int j; // zero-init-var-warning {{default initialization of an object of type 'const int' is incompatible with C++}} \
5145
cxx-error {{default initialization of an object of const type 'const int'}}
52-
const int k; // c-warning {{default initialization of an object of type 'const int' is incompatible with C++}} \
46+
const int k; // zero-init-var-warning {{default initialization of an object of type 'const int' is incompatible with C++}} \
5347
cxx-error {{default initialization of an object of const type 'const int'}}
54-
const struct S s; // c-warning {{default initialization of an object of type 'const struct S' is incompatible with C++}} \
48+
const struct S s; // zero-init-field-warning {{default initialization of an object of type 'const struct S' is incompatible with C++}} \
5549
cxx-error {{call to implicitly-deleted default constructor of 'const struct S'}}
5650

5751
void func() {
58-
const int a; // unsafe-warning {{default initialization of an object of type 'const int' leaves the object uninitialized and is incompatible with C++}} \
52+
const int a; // unsafe-var-warning {{default initialization of an object of type 'const int' leaves the object uninitialized and is incompatible with C++}} \
5953
cxx-error {{default initialization of an object of const type 'const int'}}
60-
static const int b; // c-warning {{default initialization of an object of type 'const int' is incompatible with C++}} \
54+
static const int b; // zero-init-var-warning {{default initialization of an object of type 'const int' is incompatible with C++}} \
6155
cxx-error {{default initialization of an object of const type 'const int'}}
6256
}
6357

0 commit comments

Comments
 (0)