public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] work harder to avoid -Wuninitialized for empty structs (PR 96295)
@ 2020-09-15 19:00 Martin Sebor
  2020-09-17  3:43 ` Jeff Law
  0 siblings, 1 reply; 2+ messages in thread
From: Martin Sebor @ 2020-09-15 19:00 UTC (permalink / raw)
  To: gcc-patches

[-- Attachment #1: Type: text/plain, Size: 476 bytes --]

The -Wuninitialized/-Wmaybe-uninitialized enhancement to warn when
a pointer or reference to an uninitialized object is passed to
a const-qualified function argument tries to avoid triggering for
objects of empty types.  However, the suppression is incomplete
and lets the warning trigger in some corner cases.  The attached
patch extends the suppression to those as well.

Tested on x86_64-linux.  I will plan to commit it later this week
if there are no objections.

Martin

[-- Attachment #2: gcc-96295.diff --]
[-- Type: text/x-patch, Size: 1769 bytes --]

Work harder to avoid -Wuninitialized for objects of empty structs (PR middle-end/96295).

Resolves:
PR middle-end/96295 - -Wmaybe-uninitialized warning for range operator with
reference to an empty struct

gcc/ChangeLog:

	PR middle-end/96295
	* tree-ssa-uninit.c (maybe_warn_operand): Work harder to avoid
	warning for objects of empty structs

gcc/testsuite/ChangeLog:

	PR middle-end/96295
	* g++.dg/warn/Wuninitialized-11.C: New test.

diff --git a/gcc/testsuite/g++.dg/warn/Wuninitialized-11.C b/gcc/testsuite/g++.dg/warn/Wuninitialized-11.C
new file mode 100644
index 00000000000..a6e8beb5740
--- /dev/null
+++ b/gcc/testsuite/g++.dg/warn/Wuninitialized-11.C
@@ -0,0 +1,26 @@
+/* PR middle-end/96295 - -Wmaybe-uninitialized warning for range operator
+   with reference to an empty struct
+   { dg-do compile }
+   { dg-options "-Wall" }
+   { dg-require-effective-target c++11 } */
+
+struct I
+{
+  bool operator!= (const I&) const;
+  void* operator* () const;
+  I& operator++ ();
+};
+
+struct A
+{
+  I begin () const { return I (); }
+  I end () const { return I (); }
+};
+
+void f (void)
+{
+  for (void *p : A ())   // { dg-bogus "\\\[-Wmaybe-uninitialized" }
+    {
+      (void)p;
+    }
+}
diff --git a/gcc/tree-ssa-uninit.c b/gcc/tree-ssa-uninit.c
index 7eddca397b3..c7f994b0587 100644
--- a/gcc/tree-ssa-uninit.c
+++ b/gcc/tree-ssa-uninit.c
@@ -401,6 +401,8 @@ maybe_warn_operand (ao_ref &ref, gimple *stmt, tree lhs, tree rhs,
      The first_field() test is important for C++ where the predicate
      alone isn't always sufficient.  */
   tree rhstype = TREE_TYPE (rhs);
+  if (POINTER_TYPE_P (rhstype))
+    rhstype = TREE_TYPE (rhstype);
   if (TYPE_EMPTY_P (rhstype)
       || (RECORD_OR_UNION_TYPE_P (rhstype)
 	  && (!first_field (rhstype)

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] work harder to avoid -Wuninitialized for empty structs (PR 96295)
  2020-09-15 19:00 [PATCH] work harder to avoid -Wuninitialized for empty structs (PR 96295) Martin Sebor
@ 2020-09-17  3:43 ` Jeff Law
  0 siblings, 0 replies; 2+ messages in thread
From: Jeff Law @ 2020-09-17  3:43 UTC (permalink / raw)
  To: Martin Sebor, gcc-patches

[-- Attachment #1: Type: text/plain, Size: 1433 bytes --]


On 9/15/20 1:00 PM, Martin Sebor via Gcc-patches wrote:
> The -Wuninitialized/-Wmaybe-uninitialized enhancement to warn when
> a pointer or reference to an uninitialized object is passed to
> a const-qualified function argument tries to avoid triggering for
> objects of empty types.  However, the suppression is incomplete
> and lets the warning trigger in some corner cases.  The attached
> patch extends the suppression to those as well.
>
> Tested on x86_64-linux.  I will plan to commit it later this week
> if there are no objections.
>
> Martin
>
> gcc-96295.diff
>
> Work harder to avoid -Wuninitialized for objects of empty structs (PR middle-end/96295).
>
> Resolves:
> PR middle-end/96295 - -Wmaybe-uninitialized warning for range operator with
> reference to an empty struct
>
> gcc/ChangeLog:
>
> 	PR middle-end/96295
> 	* tree-ssa-uninit.c (maybe_warn_operand): Work harder to avoid
> 	warning for objects of empty structs
>
> gcc/testsuite/ChangeLog:
>
> 	PR middle-end/96295
> 	* g++.dg/warn/Wuninitialized-11.C: New test.

FWIW, we had a build in Fedora rawhide fail just in the last couple
weeks because of the enhanced uninitialized warning.  I've already
forgotten the package, but it was a true positive, though sadly it was
just in the package's testsuite and thus we didn't actually find/fix a
user visible package bug.


OK for the trunk.  Thanks.


jeff



[-- Attachment #2: pEpkey.asc --]
[-- Type: application/pgp-keys, Size: 1763 bytes --]

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2020-09-17  3:43 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-09-15 19:00 [PATCH] work harder to avoid -Wuninitialized for empty structs (PR 96295) Martin Sebor
2020-09-17  3:43 ` Jeff Law

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