]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
re PR c/59871 (No unused value warning for comma expression)
authorMarek Polacek <polacek@redhat.com>
Thu, 23 Jan 2014 19:04:29 +0000 (19:04 +0000)
committerMarek Polacek <mpolacek@gcc.gnu.org>
Thu, 23 Jan 2014 19:04:29 +0000 (19:04 +0000)
PR c/59871
c/
* c-typeck.c (build_compound_expr): Warn even for right-hand operand
of a comma expression.
(emit_side_effect_warnings): Likewise.
libdecnumber/
* decNumberLocal.h (UBFROMUS, UBFROMUI): Remove last argument.
testsuite/
* gcc.dg/20020220-2.c: Adjust dg-warning message.
* gcc.dg/pr59871.c: New test.

From-SVN: r207002

gcc/c/ChangeLog
gcc/c/c-typeck.c
gcc/testsuite/ChangeLog
gcc/testsuite/gcc.dg/20020220-2.c
gcc/testsuite/gcc.dg/pr59871.c [new file with mode: 0644]
libdecnumber/ChangeLog
libdecnumber/decNumberLocal.h

index a158f11cb01b90535d90a90e402397a4ec2791b0..237ac1a959c3368b8b14a0c6a4d7a9ba6324a720 100644 (file)
@@ -1,3 +1,10 @@
+2014-01-23  Marek Polacek  <polacek@redhat.com>
+
+       PR c/59871
+       * c-typeck.c (build_compound_expr): Warn even for right-hand operand
+       of a comma expression.
+       (emit_side_effect_warnings): Likewise.
+
 2014-01-23  Balaji V. Iyer  <balaji.v.iyer@intel.com>
 
        PR c/59825
index 92304b0db9ec7cb7c67fac2650c845396e048daf..781d4df246ddc94745a1711d2b1b806d8b0a3c9e 100644 (file)
@@ -4778,6 +4778,23 @@ build_compound_expr (location_t loc, tree expr1, tree expr2)
                        "left-hand operand of comma expression has no effect");
        }
     }
+  else if (TREE_CODE (expr1) == COMPOUND_EXPR
+          && warn_unused_value)
+    {
+      tree r = expr1;
+      location_t cloc = loc;
+      while (TREE_CODE (r) == COMPOUND_EXPR)
+        {
+         if (EXPR_HAS_LOCATION (r))
+           cloc = EXPR_LOCATION (r);
+         r = TREE_OPERAND (r, 1);
+       }
+      if (!TREE_SIDE_EFFECTS (r)
+         && !VOID_TYPE_P (TREE_TYPE (r))
+         && !CONVERT_EXPR_P (r))
+       warning_at (cloc, OPT_Wunused_value,
+                   "right-hand operand of comma expression has no effect");
+    }
 
   /* With -Wunused, we should also warn if the left-hand operand does have
      side-effects, but computes a value which is not used.  For example, in
@@ -9643,6 +9660,23 @@ emit_side_effect_warnings (location_t loc, tree expr)
       if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
        warning_at (loc, OPT_Wunused_value, "statement with no effect");
     }
+  else if (TREE_CODE (expr) == COMPOUND_EXPR)
+    {
+      tree r = expr;
+      location_t cloc = loc;
+      while (TREE_CODE (r) == COMPOUND_EXPR)
+       {
+         if (EXPR_HAS_LOCATION (r))
+           cloc = EXPR_LOCATION (r);
+         r = TREE_OPERAND (r, 1);
+       }
+      if (!TREE_SIDE_EFFECTS (r)
+         && !VOID_TYPE_P (TREE_TYPE (r))
+         && !CONVERT_EXPR_P (r)
+         && !TREE_NO_WARNING (expr))
+       warning_at (cloc, OPT_Wunused_value,
+                   "right-hand operand of comma expression has no effect");
+    }
   else
     warn_if_unused_value (expr, loc);
 }
index 564d4258c7b1c088ada21d394449f1650ab7412d..3857078b1db5684395ce00e3e4aea873cbcf42b1 100644 (file)
@@ -1,3 +1,9 @@
+2014-01-23  Marek Polacek  <polacek@redhat.com>
+
+       PR c/59871
+       * gcc.dg/20020220-2.c: Adjust dg-warning message.
+       * gcc.dg/pr59871.c: New test.
+
 2014-01-23  Paolo Carlini  <paolo.carlini@oracle.com>
 
        PR c++/58980
index c6c57a921363b18b2b8f325f06fe6a833623da13..95606703cc50185f8e2711334fe2569149ca172b 100644 (file)
@@ -1,5 +1,5 @@
 /* PR c/4697
-   Test whether value computed not used warning is given for compound
+   Test whether operand has no effect warning is given for compound
    expression.  */
 /* { dg-do compile } */
 /* { dg-options "-O2 -Wunused" } */
@@ -7,6 +7,6 @@
 int b;
 int foo (int a)
 {
-  a = a + 1, 5 * b;    /* { dg-warning "value computed is not used" } */
+  a = a + 1, 5 * b; /* { dg-warning "right-hand operand of comma expression has no effect" } */
   return a;
 }
diff --git a/gcc/testsuite/gcc.dg/pr59871.c b/gcc/testsuite/gcc.dg/pr59871.c
new file mode 100644 (file)
index 0000000..c881aa1
--- /dev/null
@@ -0,0 +1,22 @@
+/* PR c/59871 */
+/* { dg-do compile } */
+/* { dg-options "-Wunused" } */
+
+extern int bar ();
+
+void
+foo (int *p, int i)
+{
+  p[0] = (bar (), 1, bar ()); /* { dg-warning "right-hand operand of comma expression has no effect" } */
+  p[1] = (1, bar ()); /* { dg-warning "left-hand operand of comma expression has no effect" } */
+  bar (), 1, bar (); /* { dg-warning "right-hand operand of comma expression has no effect" } */
+  bar (), 1; /* { dg-warning "right-hand operand of comma expression has no effect" } */
+  1, bar (); /* { dg-warning "left-hand operand of comma expression has no effect" } */
+  (bar (), 1); /* { dg-warning "right-hand operand of comma expression has no effect" } */
+  bar (), 5 * i; /* { dg-warning "right-hand operand of comma expression has no effect" } */
+  (bar (), 5 * i); /* { dg-warning "right-hand operand of comma expression has no effect" } */
+  (bar (), (bar (), (bar (), (bar (), (bar (), (bar (), (bar (), 7))))))); /* { dg-warning "right-hand operand of comma expression has no effect" } */
+  bar (), (bar (), (bar (), (bar (), (bar (), (bar (), (bar (), 7)))))); /* { dg-warning "right-hand operand of comma expression has no effect" } */
+  bar (), (bar (), (bar (), (bar (), (bar (), (bar (), (7, bar ())))))); /* { dg-warning "left-hand operand of comma expression has no effect" } */
+  (bar (), (bar (), (bar (), (bar (), (bar (), (bar (), (7, bar ()))))))); /* { dg-warning "left-hand operand of comma expression has no effect" } */
+}
index 36a60033c1bc476bce612389d80f1e5a4b40f1d0..14df41c99ea4334173c0f0b907f33a31bcdd7053 100644 (file)
@@ -1,3 +1,8 @@
+2014-01-23  Marek Polacek  <polacek@redhat.com>
+
+       PR c/59871
+       * decNumberLocal.h (UBFROMUS, UBFROMUI): Remove last argument.
+
 2014-01-02  Richard Sandiford  <rdsandiford@googlemail.com>
 
        Update copyright years
index 94e7f7f9b1f7458218aa2baa827d0654528dc3b4..4936231f2a2b93fa0b20e2e31882a11353b9c0e9 100644 (file)
@@ -153,10 +153,9 @@ see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
   #define UBTOUI(b)  (memcpy((void *)&uiwork, b, 4), uiwork)
 
   /* Store a uInt, etc., into bytes starting at a char* or uByte*.    */
-  /* Returns i, evaluated, for convenience; has to use uiwork because */
-  /* i may be an expression.                                         */
-  #define UBFROMUS(b, i)  (uswork=(i), memcpy(b, (void *)&uswork, 2), uswork)
-  #define UBFROMUI(b, i)  (uiwork=(i), memcpy(b, (void *)&uiwork, 4), uiwork)
+  /* Has to use uiwork because i may be an expression.               */
+  #define UBFROMUS(b, i)  (uswork=(i), memcpy(b, (void *)&uswork, 2))
+  #define UBFROMUI(b, i)  (uiwork=(i), memcpy(b, (void *)&uiwork, 4))
 
   /* X10 and X100 -- multiply integer i by 10 or 100                 */
   /* [shifts are usually faster than multiply; could be conditional]  */