]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c: _Generic should not warn in non-active branches [PR68193,PR97100,PR110703]
authorMartin Uecker <uecker@tugraz.at>
Fri, 4 Aug 2023 05:48:21 +0000 (07:48 +0200)
committerMartin Uecker <uecker@tugraz.at>
Sat, 5 Aug 2023 08:39:44 +0000 (10:39 +0200)
To avoid false diagnostics, use c_inhibit_evaluation_warnings when
a generic association is known to not 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
PR c/110703

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.

gcc/c/c-parser.cc
gcc/testsuite/gcc.dg/pr68193.c [new file with mode: 0644]

index 7a9fdda6c88b03e2c0ac7af389aa2b5f186d8890..57a01dc2fa384fe741dd6b8e6c507aa87d58c3ee 100644 (file)
@@ -9392,7 +9392,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);
@@ -9429,7 +9439,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 (file)
index 0000000..2267593
--- /dev/null
@@ -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;
+}
+
+