public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r13-5185] c, c++: Allow ignoring -Winit-self through pragmas [PR105593]
@ 2023-01-16  8:41 Jakub Jelinek
  0 siblings, 0 replies; only message in thread
From: Jakub Jelinek @ 2023-01-16  8:41 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:98b41fd4045b7856e7b85dd58d67c600bd909379

commit r13-5185-g98b41fd4045b7856e7b85dd58d67c600bd909379
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Mon Jan 16 09:40:14 2023 +0100

    c, c++: Allow ignoring -Winit-self through pragmas [PR105593]
    
    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.
    
    2023-01-16  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.

Diff:
---
 gcc/c/c-parser.cc                        |  2 +-
 gcc/cp/decl.cc                           |  2 +-
 gcc/testsuite/c-c++-common/Winit-self3.c | 36 ++++++++++++++++++++++++++++++++
 gcc/testsuite/c-c++-common/Winit-self4.c | 36 ++++++++++++++++++++++++++++++++
 gcc/testsuite/c-c++-common/Winit-self5.c | 36 ++++++++++++++++++++++++++++++++
 5 files changed, 110 insertions(+), 2 deletions(-)

diff --git a/gcc/c/c-parser.cc b/gcc/c/c-parser.cc
index 3e2109bfd4f..a6985069932 100644
--- a/gcc/c/c-parser.cc
+++ b/gcc/c/c-parser.cc
@@ -5701,7 +5701,7 @@ c_parser_initializer (c_parser *parser, tree decl)
 	  && !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
diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc
index 66e7d9ff50e..6b1c4a2749c 100644
--- a/gcc/cp/decl.cc
+++ b/gcc/cp/decl.cc
@@ -8407,7 +8407,7 @@ cp_finish_decl (tree decl, tree init, bool init_const_expr_p,
       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);
     }
 
diff --git a/gcc/testsuite/c-c++-common/Winit-self3.c b/gcc/testsuite/c-c++-common/Winit-self3.c
new file mode 100644
index 00000000000..b83135f193b
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/Winit-self3.c
@@ -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);
+}
diff --git a/gcc/testsuite/c-c++-common/Winit-self4.c b/gcc/testsuite/c-c++-common/Winit-self4.c
new file mode 100644
index 00000000000..b38b7cc60b5
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/Winit-self4.c
@@ -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);
+}
diff --git a/gcc/testsuite/c-c++-common/Winit-self5.c b/gcc/testsuite/c-c++-common/Winit-self5.c
new file mode 100644
index 00000000000..db2d9a13219
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/Winit-self5.c
@@ -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);
+}

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2023-01-16  8:41 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-01-16  8:41 [gcc r13-5185] c, c++: Allow ignoring -Winit-self through pragmas [PR105593] Jakub Jelinek

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).