public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc(refs/vendors/redhat/heads/gcc-8-branch)] c: Fix -Wunused-but-set-* warning with _Generic [PR96571]
@ 2020-09-17 17:24 Jakub Jelinek
  0 siblings, 0 replies; only message in thread
From: Jakub Jelinek @ 2020-09-17 17:24 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:b87e46bffda7134586ff93e5f7709a5c9a1a0eae

commit b87e46bffda7134586ff93e5f7709a5c9a1a0eae
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Tue Aug 18 07:51:58 2020 +0200

    c: Fix -Wunused-but-set-* warning with _Generic [PR96571]
    
    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.
    
    2020-08-18  Jakub Jelinek  <jakub@redhat.com>
    
            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.
    
    (cherry picked from commit 6d42cbe5ad7a7b46437f2576c9920e44dc14b386)

Diff:
---
 gcc/c/c-parser.c                     | 19 +++++++++++++------
 gcc/testsuite/gcc.dg/Wunused-var-4.c | 33 +++++++++++++++++++++++++++++++++
 2 files changed, 46 insertions(+), 6 deletions(-)

diff --git a/gcc/c/c-parser.c b/gcc/c/c-parser.c
index 75ef0a437a7..bea4dd2a779 100644
--- a/gcc/c/c-parser.c
+++ b/gcc/c/c-parser.c
@@ -7585,7 +7585,7 @@ c_parser_generic_selection (c_parser *parser)
   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;
@@ -7620,6 +7620,7 @@ c_parser_generic_selection (c_parser *parser)
       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.  */
@@ -7719,18 +7720,18 @@ c_parser_generic_selection (c_parser *parser)
 
       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
 	    {
@@ -7748,13 +7749,19 @@ c_parser_generic_selection (c_parser *parser)
       c_parser_consume_token (parser);
     }
 
+  unsigned int ix;
+  struct c_generic_association *iter;
+  FOR_EACH_VEC_ELT (associations, ix, iter)
+    if (ix != (unsigned) 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",
diff --git a/gcc/testsuite/gcc.dg/Wunused-var-4.c b/gcc/testsuite/gcc.dg/Wunused-var-4.c
new file mode 100644
index 00000000000..08ddcf4407e
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/Wunused-var-4.c
@@ -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);
+}


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2020-09-17 17:24 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-09-17 17:24 [gcc(refs/vendors/redhat/heads/gcc-8-branch)] c: Fix -Wunused-but-set-* warning with _Generic [PR96571] Jakub Jelinek

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