c, c++: Allow ignoring -Winit-self through pragmas [PR105593]

Message ID Y8CmpuU+Js7Tlsqz@tucnak
State Unresolved
Headers
Series c, c++: Allow ignoring -Winit-self through pragmas [PR105593] |

Checks

Context Check Description
snail/gcc-patch-check warning Git am fail log

Commit Message

Jakub Jelinek Jan. 13, 2023, 12:32 a.m. UTC
  Hi!

As mentioned in the PR, various x86 intrinsics need to return
an uninitialized vector.  Currently they use self initialization
to avoid -Wuninitialized warnings, which works fine in C, but
doesn't work in C++ where -Winit-self is enabled in -Wall.
We don't have an attribute to mark a variable as knowingly
uninitialized (the uninitialized attribute exists but means
something else, only in the -ftrivial-auto-var-init context),
and trying to suppress either -Wuninitialized or -Winit-self
inside of the _mm_undefined_ps etc. intrinsic definitions
doesn't work, one needs to currently disable through pragmas
-Wuninitialized warning at the point where _mm_undefined_ps etc.
result is actually used, but that goes against the intent of
those intrinsics.

The -Winit-self warning option actually doesn't do any warning,
all we do is record a suppression for -Winit-self if !warn_init_self
on the decl definition and later look that up in uninit pass.

The following patch changes those !warn_init_self tests which
are true only based on the command line option setting, not based
on GCC diagnostic pragma overrides to
!warning_enabled_at (DECL_SOURCE_LOCATION (decl), OPT_Winit_self)
such that it takes them into account.

Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?

Will post incremental patch for the intrinsic headers.

2023-01-13  Jakub Jelinek  <jakub@redhat.com>

	PR c++/105593
gcc/c/
	* c-parser.cc (c_parser_initializer): Check warning_enabled_at
	at the DECL_SOURCE_LOCATION (decl) for OPT_Winit_self instead
	of warn_init_self.
gcc/cp/
	* decl.cc (cp_finish_decl): Check warning_enabled_at
	at the DECL_SOURCE_LOCATION (decl) for OPT_Winit_self instead
	of warn_init_self.
gcc/testsuite/
	* c-c++-common/Winit-self3.c: New test.
	* c-c++-common/Winit-self4.c: New test.
	* c-c++-common/Winit-self5.c: New test.


	Jakub
  

Comments

Jason Merrill Jan. 13, 2023, 2:49 a.m. UTC | #1
On 1/12/23 19:32, Jakub Jelinek wrote:
> Hi!
> 
> As mentioned in the PR, various x86 intrinsics need to return
> an uninitialized vector.  Currently they use self initialization
> to avoid -Wuninitialized warnings, which works fine in C, but
> doesn't work in C++ where -Winit-self is enabled in -Wall.
> We don't have an attribute to mark a variable as knowingly
> uninitialized (the uninitialized attribute exists but means
> something else, only in the -ftrivial-auto-var-init context),
> and trying to suppress either -Wuninitialized or -Winit-self
> inside of the _mm_undefined_ps etc. intrinsic definitions
> doesn't work, one needs to currently disable through pragmas
> -Wuninitialized warning at the point where _mm_undefined_ps etc.
> result is actually used, but that goes against the intent of
> those intrinsics.
> 
> The -Winit-self warning option actually doesn't do any warning,
> all we do is record a suppression for -Winit-self if !warn_init_self
> on the decl definition and later look that up in uninit pass.
> 
> The following patch changes those !warn_init_self tests which
> are true only based on the command line option setting, not based
> on GCC diagnostic pragma overrides to
> !warning_enabled_at (DECL_SOURCE_LOCATION (decl), OPT_Winit_self)
> such that it takes them into account.
> 
> Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?

OK on Monday if the C maintainers don't comment.

> Will post incremental patch for the intrinsic headers.
> 
> 2023-01-13  Jakub Jelinek  <jakub@redhat.com>
> 
> 	PR c++/105593
> gcc/c/
> 	* c-parser.cc (c_parser_initializer): Check warning_enabled_at
> 	at the DECL_SOURCE_LOCATION (decl) for OPT_Winit_self instead
> 	of warn_init_self.
> gcc/cp/
> 	* decl.cc (cp_finish_decl): Check warning_enabled_at
> 	at the DECL_SOURCE_LOCATION (decl) for OPT_Winit_self instead
> 	of warn_init_self.
> gcc/testsuite/
> 	* c-c++-common/Winit-self3.c: New test.
> 	* c-c++-common/Winit-self4.c: New test.
> 	* c-c++-common/Winit-self5.c: New test.
> 
> --- gcc/c/c-parser.cc.jj	2023-01-11 22:18:25.560492345 +0100
> +++ gcc/c/c-parser.cc	2023-01-12 15:30:10.460233783 +0100
> @@ -5701,7 +5701,7 @@ c_parser_initializer (c_parser *parser,
>   	  && !DECL_EXTERNAL (decl)
>   	  && !TREE_STATIC (decl)
>   	  && ret.value == decl
> -	  && !warn_init_self)
> +	  && !warning_enabled_at (DECL_SOURCE_LOCATION (decl), OPT_Winit_self))
>   	suppress_warning (decl, OPT_Winit_self);
>         if (TREE_CODE (ret.value) != STRING_CST
>   	  && (TREE_CODE (ret.value) != COMPOUND_LITERAL_EXPR
> --- gcc/cp/decl.cc.jj	2023-01-04 18:42:24.597997547 +0100
> +++ gcc/cp/decl.cc	2023-01-12 15:26:01.257817526 +0100
> @@ -8407,7 +8407,7 @@ cp_finish_decl (tree decl, tree init, bo
>         if (!DECL_EXTERNAL (decl)
>   	  && !TREE_STATIC (decl)
>   	  && decl == tree_strip_any_location_wrapper (init)
> -	  && !warn_init_self)
> +	  && !warning_enabled_at (DECL_SOURCE_LOCATION (decl), OPT_Winit_self))
>   	suppress_warning (decl, OPT_Winit_self);
>       }
>   
> --- gcc/testsuite/c-c++-common/Winit-self3.c.jj	2023-01-12 15:49:56.759172518 +0100
> +++ gcc/testsuite/c-c++-common/Winit-self3.c	2023-01-12 15:50:51.512384963 +0100
> @@ -0,0 +1,36 @@
> +/* PR c++/105593 */
> +/* { dg-do compile } */
> +/* { dg-options "-W -Wall" } */
> +
> +void bar (int);
> +
> +static inline int
> +baz (void)
> +{
> +#pragma GCC diagnostic push
> +#pragma GCC diagnostic ignored "-Winit-self"
> +  int u = u;		/* { dg-bogus "'u' is used uninitialized" } */
> +#pragma GCC diagnostic pop
> +  return u;
> +}
> +
> +void
> +foo (void)
> +{
> +  int u = baz ();
> +  bar (u);
> +}
> +
> +static inline int
> +qux (void)
> +{
> +  int u = u;		/* { dg-warning "'u' is used uninitialized" "" { target c++ } } */
> +  return u;		/* { dg-message "'u' was declared here" "" { target c++ } .-1 } */
> +}
> +
> +void
> +corge (void)
> +{
> +  int u = qux ();
> +  bar (u);
> +}
> --- gcc/testsuite/c-c++-common/Winit-self4.c.jj	2023-01-12 15:50:15.233906776 +0100
> +++ gcc/testsuite/c-c++-common/Winit-self4.c	2023-01-12 15:50:42.445515372 +0100
> @@ -0,0 +1,36 @@
> +/* PR c++/105593 */
> +/* { dg-do compile } */
> +/* { dg-options "-W -Wall -Winit-self" } */
> +
> +void bar (int);
> +
> +static inline int
> +baz (void)
> +{
> +#pragma GCC diagnostic push
> +#pragma GCC diagnostic ignored "-Winit-self"
> +  int u = u;		/* { dg-bogus "'u' is used uninitialized" } */
> +#pragma GCC diagnostic pop
> +  return u;
> +}
> +
> +void
> +foo (void)
> +{
> +  int u = baz ();
> +  bar (u);
> +}
> +
> +static inline int
> +qux (void)
> +{
> +  int u = u;		/* { dg-warning "'u' is used uninitialized" } */
> +  return u;		/* { dg-message "'u' was declared here" "" { target *-*-* } .-1 } */
> +}
> +
> +void
> +corge (void)
> +{
> +  int u = qux ();
> +  bar (u);
> +}
> --- gcc/testsuite/c-c++-common/Winit-self5.c.jj	2023-01-12 15:51:01.045247847 +0100
> +++ gcc/testsuite/c-c++-common/Winit-self5.c	2023-01-12 15:51:12.929076923 +0100
> @@ -0,0 +1,36 @@
> +/* PR c++/105593 */
> +/* { dg-do compile } */
> +/* { dg-options "-W -Wall -Wno-init-self" } */
> +
> +void bar (int);
> +
> +static inline int
> +baz (void)
> +{
> +#pragma GCC diagnostic push
> +#pragma GCC diagnostic ignored "-Winit-self"
> +  int u = u;		/* { dg-bogus "'u' is used uninitialized" } */
> +#pragma GCC diagnostic pop
> +  return u;
> +}
> +
> +void
> +foo (void)
> +{
> +  int u = baz ();
> +  bar (u);
> +}
> +
> +static inline int
> +qux (void)
> +{
> +  int u = u;		/* { dg-bogus "'u' is used uninitialized" } */
> +  return u;
> +}
> +
> +void
> +corge (void)
> +{
> +  int u = qux ();
> +  bar (u);
> +}
> 
> 	Jakub
>
  
Marek Polacek Jan. 13, 2023, 1:48 p.m. UTC | #2
On Thu, Jan 12, 2023 at 09:49:56PM -0500, Jason Merrill wrote:
> On 1/12/23 19:32, Jakub Jelinek wrote:
> > Hi!
> > 
> > As mentioned in the PR, various x86 intrinsics need to return
> > an uninitialized vector.  Currently they use self initialization
> > to avoid -Wuninitialized warnings, which works fine in C, but
> > doesn't work in C++ where -Winit-self is enabled in -Wall.
> > We don't have an attribute to mark a variable as knowingly
> > uninitialized (the uninitialized attribute exists but means
> > something else, only in the -ftrivial-auto-var-init context),
> > and trying to suppress either -Wuninitialized or -Winit-self
> > inside of the _mm_undefined_ps etc. intrinsic definitions
> > doesn't work, one needs to currently disable through pragmas
> > -Wuninitialized warning at the point where _mm_undefined_ps etc.
> > result is actually used, but that goes against the intent of
> > those intrinsics.
> > 
> > The -Winit-self warning option actually doesn't do any warning,
> > all we do is record a suppression for -Winit-self if !warn_init_self
> > on the decl definition and later look that up in uninit pass.
> > 
> > The following patch changes those !warn_init_self tests which
> > are true only based on the command line option setting, not based
> > on GCC diagnostic pragma overrides to
> > !warning_enabled_at (DECL_SOURCE_LOCATION (decl), OPT_Winit_self)
> > such that it takes them into account.
> > 
> > Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?
> 
> OK on Monday if the C maintainers don't comment.

The C changes LGTM, thanks.
 
> > Will post incremental patch for the intrinsic headers.
> > 
> > 2023-01-13  Jakub Jelinek  <jakub@redhat.com>
> > 
> > 	PR c++/105593
> > gcc/c/
> > 	* c-parser.cc (c_parser_initializer): Check warning_enabled_at
> > 	at the DECL_SOURCE_LOCATION (decl) for OPT_Winit_self instead
> > 	of warn_init_self.
> > gcc/cp/
> > 	* decl.cc (cp_finish_decl): Check warning_enabled_at
> > 	at the DECL_SOURCE_LOCATION (decl) for OPT_Winit_self instead
> > 	of warn_init_self.
> > gcc/testsuite/
> > 	* c-c++-common/Winit-self3.c: New test.
> > 	* c-c++-common/Winit-self4.c: New test.
> > 	* c-c++-common/Winit-self5.c: New test.
> > 
> > --- gcc/c/c-parser.cc.jj	2023-01-11 22:18:25.560492345 +0100
> > +++ gcc/c/c-parser.cc	2023-01-12 15:30:10.460233783 +0100
> > @@ -5701,7 +5701,7 @@ c_parser_initializer (c_parser *parser,
> >   	  && !DECL_EXTERNAL (decl)
> >   	  && !TREE_STATIC (decl)
> >   	  && ret.value == decl
> > -	  && !warn_init_self)
> > +	  && !warning_enabled_at (DECL_SOURCE_LOCATION (decl), OPT_Winit_self))
> >   	suppress_warning (decl, OPT_Winit_self);
> >         if (TREE_CODE (ret.value) != STRING_CST
> >   	  && (TREE_CODE (ret.value) != COMPOUND_LITERAL_EXPR
> > --- gcc/cp/decl.cc.jj	2023-01-04 18:42:24.597997547 +0100
> > +++ gcc/cp/decl.cc	2023-01-12 15:26:01.257817526 +0100
> > @@ -8407,7 +8407,7 @@ cp_finish_decl (tree decl, tree init, bo
> >         if (!DECL_EXTERNAL (decl)
> >   	  && !TREE_STATIC (decl)
> >   	  && decl == tree_strip_any_location_wrapper (init)
> > -	  && !warn_init_self)
> > +	  && !warning_enabled_at (DECL_SOURCE_LOCATION (decl), OPT_Winit_self))
> >   	suppress_warning (decl, OPT_Winit_self);
> >       }
> > --- gcc/testsuite/c-c++-common/Winit-self3.c.jj	2023-01-12 15:49:56.759172518 +0100
> > +++ gcc/testsuite/c-c++-common/Winit-self3.c	2023-01-12 15:50:51.512384963 +0100
> > @@ -0,0 +1,36 @@
> > +/* PR c++/105593 */
> > +/* { dg-do compile } */
> > +/* { dg-options "-W -Wall" } */
> > +
> > +void bar (int);
> > +
> > +static inline int
> > +baz (void)
> > +{
> > +#pragma GCC diagnostic push
> > +#pragma GCC diagnostic ignored "-Winit-self"
> > +  int u = u;		/* { dg-bogus "'u' is used uninitialized" } */
> > +#pragma GCC diagnostic pop
> > +  return u;
> > +}
> > +
> > +void
> > +foo (void)
> > +{
> > +  int u = baz ();
> > +  bar (u);
> > +}
> > +
> > +static inline int
> > +qux (void)
> > +{
> > +  int u = u;		/* { dg-warning "'u' is used uninitialized" "" { target c++ } } */
> > +  return u;		/* { dg-message "'u' was declared here" "" { target c++ } .-1 } */
> > +}
> > +
> > +void
> > +corge (void)
> > +{
> > +  int u = qux ();
> > +  bar (u);
> > +}
> > --- gcc/testsuite/c-c++-common/Winit-self4.c.jj	2023-01-12 15:50:15.233906776 +0100
> > +++ gcc/testsuite/c-c++-common/Winit-self4.c	2023-01-12 15:50:42.445515372 +0100
> > @@ -0,0 +1,36 @@
> > +/* PR c++/105593 */
> > +/* { dg-do compile } */
> > +/* { dg-options "-W -Wall -Winit-self" } */
> > +
> > +void bar (int);
> > +
> > +static inline int
> > +baz (void)
> > +{
> > +#pragma GCC diagnostic push
> > +#pragma GCC diagnostic ignored "-Winit-self"
> > +  int u = u;		/* { dg-bogus "'u' is used uninitialized" } */
> > +#pragma GCC diagnostic pop
> > +  return u;
> > +}
> > +
> > +void
> > +foo (void)
> > +{
> > +  int u = baz ();
> > +  bar (u);
> > +}
> > +
> > +static inline int
> > +qux (void)
> > +{
> > +  int u = u;		/* { dg-warning "'u' is used uninitialized" } */
> > +  return u;		/* { dg-message "'u' was declared here" "" { target *-*-* } .-1 } */
> > +}
> > +
> > +void
> > +corge (void)
> > +{
> > +  int u = qux ();
> > +  bar (u);
> > +}
> > --- gcc/testsuite/c-c++-common/Winit-self5.c.jj	2023-01-12 15:51:01.045247847 +0100
> > +++ gcc/testsuite/c-c++-common/Winit-self5.c	2023-01-12 15:51:12.929076923 +0100
> > @@ -0,0 +1,36 @@
> > +/* PR c++/105593 */
> > +/* { dg-do compile } */
> > +/* { dg-options "-W -Wall -Wno-init-self" } */
> > +
> > +void bar (int);
> > +
> > +static inline int
> > +baz (void)
> > +{
> > +#pragma GCC diagnostic push
> > +#pragma GCC diagnostic ignored "-Winit-self"
> > +  int u = u;		/* { dg-bogus "'u' is used uninitialized" } */
> > +#pragma GCC diagnostic pop
> > +  return u;
> > +}
> > +
> > +void
> > +foo (void)
> > +{
> > +  int u = baz ();
> > +  bar (u);
> > +}
> > +
> > +static inline int
> > +qux (void)
> > +{
> > +  int u = u;		/* { dg-bogus "'u' is used uninitialized" } */
> > +  return u;
> > +}
> > +
> > +void
> > +corge (void)
> > +{
> > +  int u = qux ();
> > +  bar (u);
> > +}
> > 
> > 	Jakub
> > 
> 

Marek
  

Patch

--- gcc/c/c-parser.cc.jj	2023-01-11 22:18:25.560492345 +0100
+++ gcc/c/c-parser.cc	2023-01-12 15:30:10.460233783 +0100
@@ -5701,7 +5701,7 @@  c_parser_initializer (c_parser *parser,
 	  && !DECL_EXTERNAL (decl)
 	  && !TREE_STATIC (decl)
 	  && ret.value == decl
-	  && !warn_init_self)
+	  && !warning_enabled_at (DECL_SOURCE_LOCATION (decl), OPT_Winit_self))
 	suppress_warning (decl, OPT_Winit_self);
       if (TREE_CODE (ret.value) != STRING_CST
 	  && (TREE_CODE (ret.value) != COMPOUND_LITERAL_EXPR
--- gcc/cp/decl.cc.jj	2023-01-04 18:42:24.597997547 +0100
+++ gcc/cp/decl.cc	2023-01-12 15:26:01.257817526 +0100
@@ -8407,7 +8407,7 @@  cp_finish_decl (tree decl, tree init, bo
       if (!DECL_EXTERNAL (decl)
 	  && !TREE_STATIC (decl)
 	  && decl == tree_strip_any_location_wrapper (init)
-	  && !warn_init_self)
+	  && !warning_enabled_at (DECL_SOURCE_LOCATION (decl), OPT_Winit_self))
 	suppress_warning (decl, OPT_Winit_self);
     }
 
--- gcc/testsuite/c-c++-common/Winit-self3.c.jj	2023-01-12 15:49:56.759172518 +0100
+++ gcc/testsuite/c-c++-common/Winit-self3.c	2023-01-12 15:50:51.512384963 +0100
@@ -0,0 +1,36 @@ 
+/* PR c++/105593 */
+/* { dg-do compile } */
+/* { dg-options "-W -Wall" } */
+
+void bar (int);
+
+static inline int
+baz (void)
+{
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Winit-self"
+  int u = u;		/* { dg-bogus "'u' is used uninitialized" } */
+#pragma GCC diagnostic pop
+  return u;
+}
+
+void
+foo (void)
+{
+  int u = baz ();
+  bar (u);
+}
+
+static inline int
+qux (void)
+{
+  int u = u;		/* { dg-warning "'u' is used uninitialized" "" { target c++ } } */
+  return u;		/* { dg-message "'u' was declared here" "" { target c++ } .-1 } */
+}
+
+void
+corge (void)
+{
+  int u = qux ();
+  bar (u);
+}
--- gcc/testsuite/c-c++-common/Winit-self4.c.jj	2023-01-12 15:50:15.233906776 +0100
+++ gcc/testsuite/c-c++-common/Winit-self4.c	2023-01-12 15:50:42.445515372 +0100
@@ -0,0 +1,36 @@ 
+/* PR c++/105593 */
+/* { dg-do compile } */
+/* { dg-options "-W -Wall -Winit-self" } */
+
+void bar (int);
+
+static inline int
+baz (void)
+{
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Winit-self"
+  int u = u;		/* { dg-bogus "'u' is used uninitialized" } */
+#pragma GCC diagnostic pop
+  return u;
+}
+
+void
+foo (void)
+{
+  int u = baz ();
+  bar (u);
+}
+
+static inline int
+qux (void)
+{
+  int u = u;		/* { dg-warning "'u' is used uninitialized" } */
+  return u;		/* { dg-message "'u' was declared here" "" { target *-*-* } .-1 } */
+}
+
+void
+corge (void)
+{
+  int u = qux ();
+  bar (u);
+}
--- gcc/testsuite/c-c++-common/Winit-self5.c.jj	2023-01-12 15:51:01.045247847 +0100
+++ gcc/testsuite/c-c++-common/Winit-self5.c	2023-01-12 15:51:12.929076923 +0100
@@ -0,0 +1,36 @@ 
+/* PR c++/105593 */
+/* { dg-do compile } */
+/* { dg-options "-W -Wall -Wno-init-self" } */
+
+void bar (int);
+
+static inline int
+baz (void)
+{
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Winit-self"
+  int u = u;		/* { dg-bogus "'u' is used uninitialized" } */
+#pragma GCC diagnostic pop
+  return u;
+}
+
+void
+foo (void)
+{
+  int u = baz ();
+  bar (u);
+}
+
+static inline int
+qux (void)
+{
+  int u = u;		/* { dg-bogus "'u' is used uninitialized" } */
+  return u;
+}
+
+void
+corge (void)
+{
+  int u = qux ();
+  bar (u);
+}