C: Avoid incorrect warning for volatile in compound expressions [PR98260]
authorMartin Uecker <muecker@gwdg.de>
Wed, 16 Dec 2020 22:43:42 +0000 (23:43 +0100)
committerMartin Uecker <muecker@gwdg.de>
Wed, 16 Dec 2020 22:43:42 +0000 (23:43 +0100)
2020-12-16  Martin Uecker  <muecker@gwdg.de>

gcc/c/
PR c/98260
* c-parser.c (c_parser_expression): Look into
nop expression when marking expressions as read.

gcc/testsuite/
PR c/98260
* gcc.dg/unused-9.c: New test.

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

index 69ecdb5e82260cb2fa14732432da3439a18c13fe..b9fdc90274eff79a0e6aaca0436ad0471aa8d3e6 100644 (file)
@@ -10615,8 +10615,14 @@ c_parser_expression (c_parser *parser)
       c_parser_consume_token (parser);
       expr_loc = c_parser_peek_token (parser)->location;
       lhsval = expr.value;
-      while (TREE_CODE (lhsval) == COMPOUND_EXPR)
-       lhsval = TREE_OPERAND (lhsval, 1);
+      while (TREE_CODE (lhsval) == COMPOUND_EXPR
+            || TREE_CODE (lhsval) == NOP_EXPR)
+       {
+         if (TREE_CODE (lhsval) == COMPOUND_EXPR)
+           lhsval = TREE_OPERAND (lhsval, 1);
+         else
+           lhsval = TREE_OPERAND (lhsval, 0);
+       }
       if (DECL_P (lhsval) || handled_component_p (lhsval))
        mark_exp_read (lhsval);
       next = c_parser_expr_no_commas (parser, NULL);
diff --git a/gcc/testsuite/gcc.dg/unused-9.c b/gcc/testsuite/gcc.dg/unused-9.c
new file mode 100644 (file)
index 0000000..bdf36e1
--- /dev/null
@@ -0,0 +1,13 @@
+/* PR c/98260 */
+/* { dg-do compile } */
+/* { dg-options "-Wunused" } */
+
+
+void g(void)
+{
+  int i = 0;
+  volatile int x;
+  (x, i++);    /* { dg-bogus "set but not used" } */
+}
+
+