[C] _Generic should not warn in non-active branches [PR68193,PR97100]

Message ID f041b82554fd897f2a609a4d274716ad62d33c66.camel@tugraz.at
State Accepted
Headers
Series [C] _Generic should not warn in non-active branches [PR68193,PR97100] |

Checks

Context Check Description
snail/gcc-patch-check success Github commit url

Commit Message

Martin Uecker Aug. 4, 2023, 6:04 a.m. UTC
  Here is a patch to reduce false positives in _Generic.

Bootstrapped and regression tested on x86_64-linux.

Martin

    c: _Generic should not warn in non-active branches [PR68193,PR97100]
    
    To avoid false diagnostics, use c_inhibit_evaluation_warnings when
    a generic association is known to match during parsing.  We may still
    generate false positives if the default branch comes earler than
    a specific association that matches.
    
    PR c/68193
    PR c/97100
    
    gcc/c/:
            * c-parser.cc (c_parser_generic_selection): Inhibit evaluation
            warnings branches that are known not be taken during parsing.
    
    gcc/testsuite/ChangeLog:
            * gcc.dg/pr68193.c: New test.
  

Comments

Joseph Myers Aug. 4, 2023, 4:42 p.m. UTC | #1
On Fri, 4 Aug 2023, Martin Uecker via Gcc-patches wrote:

> Here is a patch to reduce false positives in _Generic.
> 
> Bootstrapped and regression tested on x86_64-linux.
> 
> Martin
> 
>     c: _Generic should not warn in non-active branches [PR68193,PR97100]
>     
>     To avoid false diagnostics, use c_inhibit_evaluation_warnings when
>     a generic association is known to match during parsing.  We may still
>     generate false positives if the default branch comes earler than
>     a specific association that matches.
>     
>     PR c/68193
>     PR c/97100
>     
>     gcc/c/:
>             * c-parser.cc (c_parser_generic_selection): Inhibit evaluation
>             warnings branches that are known not be taken during parsing.
>     
>     gcc/testsuite/ChangeLog:
>             * gcc.dg/pr68193.c: New test.

OK.
  

Patch

diff --git a/gcc/c/c-parser.cc b/gcc/c/c-parser.cc
index 24a6eb6e459..d1863b301e0 100644
--- a/gcc/c/c-parser.cc
+++ b/gcc/c/c-parser.cc
@@ -9350,7 +9350,17 @@  c_parser_generic_selection (c_parser *parser)
 	  return error_expr;
 	}
 
+      bool match = assoc.type == NULL_TREE
+		   || comptypes (assoc.type, selector_type);
+
+      if (!match)
+	c_inhibit_evaluation_warnings++;
+
       assoc.expression = c_parser_expr_no_commas (parser, NULL);
+
+      if (!match)
+	  c_inhibit_evaluation_warnings--;
+
       if (assoc.expression.value == error_mark_node)
 	{
 	  c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
@@ -9387,7 +9397,7 @@  c_parser_generic_selection (c_parser *parser)
 	      match_found = associations.length ();
 	    }
 	}
-      else if (comptypes (assoc.type, selector_type))
+      else if (match)
 	{
 	  if (match_found < 0 || matched_assoc.type == NULL_TREE)
 	    {
diff --git a/gcc/testsuite/gcc.dg/pr68193.c b/gcc/testsuite/gcc.dg/pr68193.c
new file mode 100644
index 00000000000..2267593e363
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr68193.c
@@ -0,0 +1,15 @@ 
+/*  pr69193 */
+/* { dg-do compile } */
+/* { dg-options "-Wall" } */
+
+int
+main (void)
+{
+  int i = 0;
+  int j = _Generic (i,
+		    int: 0,
+		    long int: (i = (long int) 9223372036854775808UL));
+  return i + j;
+}
+
+