This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [C++ PATCH] PR c++/90590 Suppress warning for enumeration value not handled in switch warning


Hi,
This should fix the concerns you had! Now the function only gets the
location from the type, and then only look for the name if the type's
location is in the header.

On Tue, Jul 9, 2019 at 5:21 PM Jason Merrill <jason@redhat.com> wrote:
>
> On 7/9/19 11:18 AM, Matthew Beliveau wrote:
> > This patch suppresses the warning:  "enumeration value not handled in
> > switch", for enumerators that are defined in system headers and use
> > reserved names.
>
> > +      if (decl == NULL_TREE)
> > +     decl = lookup_name (TREE_PURPOSE (chain));
>
> This seems likely to find an unrelated declaration.  If we have a name
> without a decl, I think it would be better to just look at that name
> rather than try to find the corresponding decl.  For location, we can
> use the location of the type.
>
> Jason
Bootstrapped/regtested on x86_64-linux, ok for trunk?

2019-07-12  Matthew Beliveau  <mbelivea@redhat.com>

	PR c++/90590
	* c-warn.c (c_do_switch_warnings): Suppress warning for enumerators
	with reserved names that are in a system header.

	* c-c++-common/pr90590-1.c: New test.
	* c-c++-common/pr90590-1.h: New test.
	* c-c++-common/pr90590-2.c: New test.
	* c-c++-common/pr90590-2.h: New test.

diff --git gcc/c-family/c-warn.c gcc/c-family/c-warn.c
index b5d09e761d7..e120ad96f4b 100644
--- gcc/c-family/c-warn.c
+++ gcc/c-family/c-warn.c
@@ -34,6 +34,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "gcc-rich-location.h"
 #include "gimplify.h"
 #include "c-family/c-indentation.h"
+#include "c-family/c-spellcheck.h"
 #include "calls.h"
 #include "stor-layout.h"
 
@@ -1628,6 +1629,16 @@ c_do_switch_warnings (splay_tree cases, location_t switch_location,
       if (cond && tree_int_cst_compare (cond, value))
 	continue;
 
+      /* If the enumerator is defined in a system header and uses a reserved
+	 name, then we continue to avoid throwing a warning.  */
+      location_t loc = DECL_SOURCE_LOCATION
+	    (TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type)));
+      if (in_system_header_at (loc)
+	  && name_reserved_for_implementation_p
+	      (IDENTIFIER_POINTER (TREE_PURPOSE (chain))))
+	continue;
+
+
       /* If there is a default_node, the only relevant option is
 	 Wswitch-enum.  Otherwise, if both are enabled then we prefer
 	 to warn using -Wswitch because -Wswitch is enabled by -Wall
diff --git gcc/testsuite/c-c++-common/pr90590-1.c gcc/testsuite/c-c++-common/pr90590-1.c
new file mode 100644
index 00000000000..4e11debb7fa
--- /dev/null
+++ gcc/testsuite/c-c++-common/pr90590-1.c
@@ -0,0 +1,15 @@
+// PR c++/90590
+// { dg-options -Wswitch }
+#include "pr90590-1.h"
+
+void
+g ()
+{
+  enum E e = _A;
+  switch (e) // { dg-bogus "enumeration value '_C' not handled in switch" }
+    {
+    case _A:
+    case _B:
+      break;
+    }
+}
diff --git gcc/testsuite/c-c++-common/pr90590-1.h gcc/testsuite/c-c++-common/pr90590-1.h
new file mode 100644
index 00000000000..22f1a7d5d52
--- /dev/null
+++ gcc/testsuite/c-c++-common/pr90590-1.h
@@ -0,0 +1,2 @@
+#pragma GCC system_header
+enum E { _A, _B, _C };
diff --git gcc/testsuite/c-c++-common/pr90590-2.c gcc/testsuite/c-c++-common/pr90590-2.c
new file mode 100644
index 00000000000..23da97f9d74
--- /dev/null
+++ gcc/testsuite/c-c++-common/pr90590-2.c
@@ -0,0 +1,11 @@
+// PR c++/90590
+// { dg-options -Wswitch }
+
+#include "pr90590-2.h"
+
+void
+fn ()
+{
+  switch (c.b) // { dg-bogus "enumeration value" }
+    ;
+}
diff --git gcc/testsuite/c-c++-common/pr90590-2.h gcc/testsuite/c-c++-common/pr90590-2.h
new file mode 100644
index 00000000000..e4f8635576f
--- /dev/null
+++ gcc/testsuite/c-c++-common/pr90590-2.h
@@ -0,0 +1,4 @@
+#pragma GCC system_header
+struct {
+  enum { _A } b;
+} c;

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]