Re: [PATCH] c: Fix -Wunused-but-set-* warning with _Generic [PR96571]

2020-08-17 Thread Joseph Myers
On Fri, 14 Aug 2020, Jakub Jelinek via Gcc-patches wrote:

> Hi!
> 
> The following testcase shows various problems with -Wunused-but-set*
> warnings and _Generic construct.  I think it is best to treat the selector
> and the ignored expressions as (potentially) read, because when they are
> parsed, the vars in there are already marked as TREE_USED.
> 
> Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?

OK.

-- 
Joseph S. Myers
jos...@codesourcery.com


[PATCH] c: Fix -Wunused-but-set-* warning with _Generic [PR96571]

2020-08-14 Thread Jakub Jelinek via Gcc-patches
Hi!

The following testcase shows various problems with -Wunused-but-set*
warnings and _Generic construct.  I think it is best to treat the selector
and the ignored expressions as (potentially) read, because when they are
parsed, the vars in there are already marked as TREE_USED.

Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?

2020-08-14  Jakub Jelinek  

PR c/96571
* c-parser.c (c_parser_generic_selection): Change match_found from bool
to int, holding index of the match.  Call mark_exp_read on the selector
expression and on expressions other than the selected one.

* gcc.dg/Wunused-var-4.c: New test.

--- gcc/c/c-parser.c.jj 2020-07-28 15:39:09.672760847 +0200
+++ gcc/c/c-parser.c2020-08-13 15:41:09.476861020 +0200
@@ -8686,7 +8686,7 @@ c_parser_generic_selection (c_parser *pa
   struct c_expr selector, error_expr;
   tree selector_type;
   struct c_generic_association matched_assoc;
-  bool match_found = false;
+  int match_found = -1;
   location_t generic_loc, selector_loc;
 
   error_expr.original_code = ERROR_MARK;
@@ -8721,6 +8721,7 @@ c_parser_generic_selection (c_parser *pa
   c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
   return selector;
 }
+  mark_exp_read (selector.value);
   selector_type = TREE_TYPE (selector.value);
   /* In ISO C terms, rvalues (including the controlling expression of
  _Generic) do not have qualified types.  */
@@ -8820,18 +8821,18 @@ c_parser_generic_selection (c_parser *pa
 
   if (assoc.type == NULL_TREE)
{
- if (!match_found)
+ if (match_found < 0)
{
  matched_assoc = assoc;
- match_found = true;
+ match_found = associations.length ();
}
}
   else if (comptypes (assoc.type, selector_type))
{
- if (!match_found || matched_assoc.type == NULL_TREE)
+ if (match_found < 0 || matched_assoc.type == NULL_TREE)
{
  matched_assoc = assoc;
- match_found = true;
+ match_found = associations.length ();
}
  else
{
@@ -8849,13 +8850,19 @@ c_parser_generic_selection (c_parser *pa
   c_parser_consume_token (parser);
 }
 
+  unsigned int ix;
+  struct c_generic_association *iter;
+  FOR_EACH_VEC_ELT (associations, ix, iter)
+if (ix != match_found)
+  mark_exp_read (iter->expression.value);
+
   if (!parens.require_close (parser))
 {
   c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
   return error_expr;
 }
 
-  if (!match_found)
+  if (match_found < 0)
 {
   error_at (selector_loc, "%<_Generic%> selector of type %qT is not "
"compatible with any association",
--- gcc/testsuite/gcc.dg/Wunused-var-4.c.jj 2020-08-13 18:36:35.608060608 
+0200
+++ gcc/testsuite/gcc.dg/Wunused-var-4.c2020-08-13 18:36:48.866880017 
+0200
@@ -0,0 +1,33 @@
+/* PR c/96571 */
+/* { dg-do compile } */
+/* { dg-options "-std=c99 -O2 -Wunused-but-set-variable" } */
+
+enum E { V };
+
+int
+foo (void)
+{
+  enum E v;/* { dg-bogus "set but not used" } */
+  return _Generic (v, enum E : 0);
+}
+
+int
+bar (void)
+{
+  int a = 0;   /* { dg-bogus "set but not used" } */
+  return _Generic (0, int : a);
+}
+
+int
+baz (void)
+{
+  int a;   /* { dg-bogus "set but not used" } */
+  return _Generic (0, long long : a, int : 0);
+}
+
+int
+qux (void)
+{
+  int a;   /* { dg-bogus "set but not used" } */
+  return _Generic (0, long long : a, default: 0);
+}

Jakub