* [PATCH] c++: -Wmissing-field-initializers and empty class [PR110064]
@ 2023-07-19 19:20 Marek Polacek
2023-07-19 19:36 ` Jason Merrill
0 siblings, 1 reply; 3+ messages in thread
From: Marek Polacek @ 2023-07-19 19:20 UTC (permalink / raw)
To: GCC Patches, Jason Merrill; +Cc: Patrick Palka
Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?
-- >8 --
Let's suppress -Wmissing-field-initializers for empty classes.
Here I don't think I need the usual COMPLETE_TYPE_P/dependent_type_p
checks.
PR c++/110064
gcc/cp/ChangeLog:
* typeck2.cc (process_init_constructor_record): Don't emit
-Wmissing-field-initializers for empty classes.
gcc/testsuite/ChangeLog:
* g++.dg/warn/Wmissing-field-initializers-3.C: New test.
---
gcc/cp/typeck2.cc | 3 +-
.../warn/Wmissing-field-initializers-3.C | 48 +++++++++++++++++++
2 files changed, 50 insertions(+), 1 deletion(-)
create mode 100644 gcc/testsuite/g++.dg/warn/Wmissing-field-initializers-3.C
diff --git a/gcc/cp/typeck2.cc b/gcc/cp/typeck2.cc
index 1c204c8612b..582a73bb053 100644
--- a/gcc/cp/typeck2.cc
+++ b/gcc/cp/typeck2.cc
@@ -1874,7 +1874,8 @@ process_init_constructor_record (tree type, tree init, int nested, int flags,
to zero. */
if ((complain & tf_warning)
&& !cp_unevaluated_operand
- && !EMPTY_CONSTRUCTOR_P (init))
+ && !EMPTY_CONSTRUCTOR_P (init)
+ && !is_really_empty_class (fldtype, /*ignore_vptr*/false))
warning (OPT_Wmissing_field_initializers,
"missing initializer for member %qD", field);
diff --git a/gcc/testsuite/g++.dg/warn/Wmissing-field-initializers-3.C b/gcc/testsuite/g++.dg/warn/Wmissing-field-initializers-3.C
new file mode 100644
index 00000000000..a8d75b92bd1
--- /dev/null
+++ b/gcc/testsuite/g++.dg/warn/Wmissing-field-initializers-3.C
@@ -0,0 +1,48 @@
+// PR c++/110064
+// { dg-do compile { target c++17 } }
+// { dg-options "-Wmissing-field-initializers" }
+
+struct B { };
+struct D : B {
+ int x;
+ int y;
+};
+
+struct E {
+ int x;
+ int y;
+ B z;
+};
+
+template<typename> struct X { };
+
+template<typename T>
+struct F {
+ int i;
+ int j;
+ X<T> x;
+};
+
+int
+main ()
+{
+ D d = {.x=1, .y=2}; // { dg-bogus "missing" }
+ (void)d;
+ E e = {.x=1, .y=2}; // { dg-bogus "missing" }
+ (void)e;
+ F<int> f = {.i=1, .j=2 }; // { dg-bogus "missing" }
+ (void)f;
+}
+
+template<typename T>
+void fn ()
+{
+ F<T> f = {.i=1, .j=2 }; // { dg-bogus "missing" }
+ (void)f;
+}
+
+void
+g ()
+{
+ fn<int> ();
+}
base-commit: 2971ff7b1d564ac04b537d907c70e6093af70832
--
2.41.0
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] c++: -Wmissing-field-initializers and empty class [PR110064]
2023-07-19 19:20 [PATCH] c++: -Wmissing-field-initializers and empty class [PR110064] Marek Polacek
@ 2023-07-19 19:36 ` Jason Merrill
2023-07-19 19:40 ` Marek Polacek
0 siblings, 1 reply; 3+ messages in thread
From: Jason Merrill @ 2023-07-19 19:36 UTC (permalink / raw)
To: Marek Polacek, GCC Patches; +Cc: Patrick Palka
On 7/19/23 15:20, Marek Polacek wrote:
> Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?
OK. We might also improve the diagnostic for base classes, perhaps by
teaching dump_simple_decl about DECL_FIELD_IS_BASE?
> -- >8 --
>
> Let's suppress -Wmissing-field-initializers for empty classes.
>
> Here I don't think I need the usual COMPLETE_TYPE_P/dependent_type_p
> checks.
>
> PR c++/110064
>
> gcc/cp/ChangeLog:
>
> * typeck2.cc (process_init_constructor_record): Don't emit
> -Wmissing-field-initializers for empty classes.
>
> gcc/testsuite/ChangeLog:
>
> * g++.dg/warn/Wmissing-field-initializers-3.C: New test.
> ---
> gcc/cp/typeck2.cc | 3 +-
> .../warn/Wmissing-field-initializers-3.C | 48 +++++++++++++++++++
> 2 files changed, 50 insertions(+), 1 deletion(-)
> create mode 100644 gcc/testsuite/g++.dg/warn/Wmissing-field-initializers-3.C
>
> diff --git a/gcc/cp/typeck2.cc b/gcc/cp/typeck2.cc
> index 1c204c8612b..582a73bb053 100644
> --- a/gcc/cp/typeck2.cc
> +++ b/gcc/cp/typeck2.cc
> @@ -1874,7 +1874,8 @@ process_init_constructor_record (tree type, tree init, int nested, int flags,
> to zero. */
> if ((complain & tf_warning)
> && !cp_unevaluated_operand
> - && !EMPTY_CONSTRUCTOR_P (init))
> + && !EMPTY_CONSTRUCTOR_P (init)
> + && !is_really_empty_class (fldtype, /*ignore_vptr*/false))
> warning (OPT_Wmissing_field_initializers,
> "missing initializer for member %qD", field);
>
> diff --git a/gcc/testsuite/g++.dg/warn/Wmissing-field-initializers-3.C b/gcc/testsuite/g++.dg/warn/Wmissing-field-initializers-3.C
> new file mode 100644
> index 00000000000..a8d75b92bd1
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/warn/Wmissing-field-initializers-3.C
> @@ -0,0 +1,48 @@
> +// PR c++/110064
> +// { dg-do compile { target c++17 } }
> +// { dg-options "-Wmissing-field-initializers" }
> +
> +struct B { };
> +struct D : B {
> + int x;
> + int y;
> +};
> +
> +struct E {
> + int x;
> + int y;
> + B z;
> +};
> +
> +template<typename> struct X { };
> +
> +template<typename T>
> +struct F {
> + int i;
> + int j;
> + X<T> x;
> +};
> +
> +int
> +main ()
> +{
> + D d = {.x=1, .y=2}; // { dg-bogus "missing" }
> + (void)d;
> + E e = {.x=1, .y=2}; // { dg-bogus "missing" }
> + (void)e;
> + F<int> f = {.i=1, .j=2 }; // { dg-bogus "missing" }
> + (void)f;
> +}
> +
> +template<typename T>
> +void fn ()
> +{
> + F<T> f = {.i=1, .j=2 }; // { dg-bogus "missing" }
> + (void)f;
> +}
> +
> +void
> +g ()
> +{
> + fn<int> ();
> +}
>
> base-commit: 2971ff7b1d564ac04b537d907c70e6093af70832
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] c++: -Wmissing-field-initializers and empty class [PR110064]
2023-07-19 19:36 ` Jason Merrill
@ 2023-07-19 19:40 ` Marek Polacek
0 siblings, 0 replies; 3+ messages in thread
From: Marek Polacek @ 2023-07-19 19:40 UTC (permalink / raw)
To: Jason Merrill; +Cc: GCC Patches, Patrick Palka
On Wed, Jul 19, 2023 at 03:36:49PM -0400, Jason Merrill wrote:
> On 7/19/23 15:20, Marek Polacek wrote:
> > Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?
>
> OK. We might also improve the diagnostic for base classes, perhaps by
> teaching dump_simple_decl about DECL_FIELD_IS_BASE?
As in, instead of "D::<anonymous>" emit "D::B"? Good idea. I suppose
I could do that; Barry's testcase without this patch looks like a good
test case. Thanks,
Marek
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2023-07-19 19:41 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-07-19 19:20 [PATCH] c++: -Wmissing-field-initializers and empty class [PR110064] Marek Polacek
2023-07-19 19:36 ` Jason Merrill
2023-07-19 19:40 ` Marek Polacek
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).