diff mbox series

c++: -Wmissing-field-initializers and empty class [PR110064]

Message ID 20230719192047.449259-1-polacek@redhat.com
State New
Headers show
Series c++: -Wmissing-field-initializers and empty class [PR110064] | expand

Commit Message

Marek Polacek July 19, 2023, 7:20 p.m. UTC
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


base-commit: 2971ff7b1d564ac04b537d907c70e6093af70832

Comments

Jason Merrill July 19, 2023, 7:36 p.m. UTC | #1
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
Marek Polacek July 19, 2023, 7:40 p.m. UTC | #2
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
diff mbox series

Patch

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> ();
+}