Skip to content

Reorganize -Winitializer-overrides and -Wreorder-init-list #136586

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,10 @@ Improvements to Clang's diagnostics
they're only triggered if the authors are already making the choice to use
``preferred_type`` attribute.

- ``-Winitializer-overrides`` and ``-Wreorder-init-list`` are now grouped under
the ``-Wc99-designator`` diagnostic group, as they also are about the
behavior of the C99 feature as it was introduced into C++20. Fixes #GH47037

Improvements to Clang's time-trace
----------------------------------

Expand Down
17 changes: 10 additions & 7 deletions clang/include/clang/Basic/DiagnosticGroups.td
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,13 @@ def AbstractFinalClass : DiagGroup<"abstract-final-class">;
def FinalDtorNonFinalClass : DiagGroup<"final-dtor-non-final-class">;
def GNUOffsetofExtensions : DiagGroup<"gnu-offsetof-extensions">;

def InitializerOverrides : DiagGroup<"initializer-overrides">;
// For compatibility with GCC; -Woverride-init = -Winitializer-overrides
def : DiagGroup<"override-init", [InitializerOverrides]>;
def ReorderCtor : DiagGroup<"reorder-ctor">;
def ReorderInitList : DiagGroup<"reorder-init-list">;
def Reorder : DiagGroup<"reorder", [ReorderCtor, ReorderInitList]>;

def CXX11CompatDeprecatedWritableStr :
DiagGroup<"c++11-compat-deprecated-writable-strings">;

Expand Down Expand Up @@ -250,7 +257,9 @@ def Deprecated : DiagGroup<"deprecated", [DeprecatedAnonEnumEnumConversion,
def CXX20Designator : DiagGroup<"c++20-designator">;
// Allow -Wno-c99-designator to be used to turn off all warnings on valid C99
// designators (including the warning controlled by -Wc++20-designator).
def C99Designator : DiagGroup<"c99-designator", [CXX20Designator]>;
def C99Designator : DiagGroup<"c99-designator", [CXX20Designator,
InitializerOverrides,
ReorderInitList]>;
def GNUDesignator : DiagGroup<"gnu-designator">;
def DtorName : DiagGroup<"dtor-name">;

Expand Down Expand Up @@ -595,9 +604,6 @@ def NullabilityCompleteness : DiagGroup<"nullability-completeness",
def NullArithmetic : DiagGroup<"null-arithmetic">;
def NullCharacter : DiagGroup<"null-character">;
def NullDereference : DiagGroup<"null-dereference">;
def InitializerOverrides : DiagGroup<"initializer-overrides">;
// For compatibility with GCC; -Woverride-init = -Winitializer-overrides
def : DiagGroup<"override-init", [InitializerOverrides]>;
def NonNull : DiagGroup<"nonnull">;
def NonPODVarargs : DiagGroup<"non-pod-varargs">;
def ClassVarargs : DiagGroup<"class-varargs", [NonPODVarargs]>;
Expand Down Expand Up @@ -919,9 +925,6 @@ def UsedButMarkedUnused : DiagGroup<"used-but-marked-unused">;
def UsedSearchPath : DiagGroup<"search-path-usage">;
def UserDefinedLiterals : DiagGroup<"user-defined-literals">;
def UserDefinedWarnings : DiagGroup<"user-defined-warnings">;
def ReorderCtor : DiagGroup<"reorder-ctor">;
def ReorderInitList : DiagGroup<"reorder-init-list">;
def Reorder : DiagGroup<"reorder", [ReorderCtor, ReorderInitList]>;
def UndeclaredSelector : DiagGroup<"undeclared-selector">;
def ImplicitAtomic : DiagGroup<"implicit-atomic-properties">;
def AtomicAlignment : DiagGroup<"atomic-alignment">;
Expand Down
21 changes: 21 additions & 0 deletions clang/test/SemaCXX/cxx20-c99-designator.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// RUN: %clang_cc1 -std=c++20 -fsyntax-only -verify=override,reorder -Werror=c99-designator %s
// RUN: %clang_cc1 -std=c++20 -fsyntax-only -verify=override -Wno-reorder-init-list -Werror=initializer-overrides %s
// RUN: %clang_cc1 -std=c++20 -fsyntax-only -verify=reorder -Wno-initializer-overrides -Werror=reorder-init-list %s
// RUN: %clang_cc1 -std=c++20 -fsyntax-only -verify=good -Wno-c99-designator %s
// good-no-diagnostics

// Ensure that -Wc99-designator controls both -Winitializer-overrides and
// -Wreorder-init-list.

struct X {
int a;
int b;
};

void test() {
X x{.a = 0, // override-note {{previous initialization is here}}
.a = 1}; // override-error {{initializer overrides prior initialization of this subobject}}
X y{.b = 0, // reorder-note {{previous initialization for field 'b' is here}}
.a = 1}; // reorder-error {{ISO C++ requires field designators to be specified in declaration order; field 'b' will be initialized after field 'a'}}
}

11 changes: 1 addition & 10 deletions clang/test/SemaCXX/decltype.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,23 +80,14 @@ namespace D5789 {
struct P1 { char x[6]; } g1 = { "foo" };
struct LP1 { struct P1 p1; };

// expected-warning@+3 {{initializer partially overrides}}
// expected-note@+2 {{previous initialization}}
// expected-note@+1 {{previous definition}}
template<class T> void foo(decltype(T(LP1{ .p1 = g1, .p1.x[1] = 'x' }))) {}
template<class T> void foo(decltype(T(LP1{ .p1 = g1, .p1.x[1] = 'x' }))) {} // expected-note {{previous definition is here}}

// expected-warning@+3 {{initializer partially overrides}}
// expected-note@+2 {{previous initialization}}
template<class T>
void foo(decltype(T(LP1{ .p1 = g1, .p1.x[1] = 'r' }))) {} // okay

// expected-warning@+3 {{initializer partially overrides}}
// expected-note@+2 {{previous initialization}}
template<class T>
void foo(decltype(T(LP1{ .p1 = { "foo" }, .p1.x[1] = 'x'}))) {} // okay

// expected-warning@+3 {{initializer partially overrides}}
// expected-note@+2 {{previous initialization}}
// expected-error@+1 {{redefinition of 'foo'}}
template<class T> void foo(decltype(T(LP1{ .p1 = g1, .p1.x[1] = 'x' }))) {}
}
Expand Down
Loading