]> gcc.gnu.org Git - gcc.git/commitdiff
PR c/46921 Lost side effect when struct initializer expression uses comma operator
authorDavid Pagan <dave.pagan@oracle.com>
Tue, 13 Mar 2018 18:10:09 +0000 (18:10 +0000)
committerJoseph Myers <jsm28@gcc.gnu.org>
Tue, 13 Mar 2018 18:10:09 +0000 (18:10 +0000)
This patch fixes improper handling of comma operator expression in a
struct field initializer as described in:

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=46921

Currently, function output_init_element () does not evaluate the left
hand expression in a comma operator that's used for a struct
initializer field if the right hand side is zero-sized. However, the
left hand expression must be evaluated if it's found to have side
effects (for example, a function call).

Patch was successfully bootstrapped and tested on x86_64-linux.

gcc/c:
2018-03-13  David Pagan  <dave.pagan@oracle.com>

PR c/46921
* c-typeck.c (output_init_element): Ensure field initializer
expression is always evaluated if there are side effects.

gcc/testsuite:
2018-03-13  David Pagan  <dave.pagan@oracle.com>

PR c/46921
* gcc.dg/pr46921.c: New test.

From-SVN: r258497

gcc/c/ChangeLog
gcc/c/c-typeck.c
gcc/testsuite/ChangeLog
gcc/testsuite/gcc.dg/pr46921.c [new file with mode: 0644]

index 599ffa2cbe32724d05ebe6240ae912b7e8c895c8..8147835935ceef5843f555570ff56cca39f195b2 100644 (file)
@@ -1,3 +1,9 @@
+2018-03-13  David Pagan  <dave.pagan@oracle.com>
+
+       PR c/46921
+       * c-typeck.c (output_init_element): Ensure field initializer
+       expression is always evaluated if there are side effects.
+
 2018-03-06  Jakub Jelinek  <jakub@redhat.com>
 
        PR c/84721
index 1eae4ea849c84bacaa97cda080e357c33acac5ae..2ac85003ebb2059eff9ff0d207758227ad4e751f 100644 (file)
@@ -9208,12 +9208,14 @@ output_init_element (location_t loc, tree value, tree origtype,
                      "enum conversion in initialization is invalid in C++");
     }
 
-  /* If this field is empty (and not at the end of structure),
-     don't do anything other than checking the initializer.  */
+  /* If this field is empty and does not have side effects (and is not at
+     the end of structure), don't do anything other than checking the
+     initializer.  */
   if (field
       && (TREE_TYPE (field) == error_mark_node
          || (COMPLETE_TYPE_P (TREE_TYPE (field))
              && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
+             && !TREE_SIDE_EFFECTS (new_value)
              && (TREE_CODE (constructor_type) == ARRAY_TYPE
                  || DECL_CHAIN (field)))))
     return;
index 8f15d5fc6aad9725375ed978ff2cc545b489671d..c518ebeff22b4e2d761fd0152dc2af64cf554f6b 100644 (file)
@@ -1,3 +1,8 @@
+2018-03-13  David Pagan  <dave.pagan@oracle.com>
+
+       PR c/46921
+       * gcc.dg/pr46921.c: New test.
+
 2018-03-13  Martin Sebor  <msebor@redhat.com>
 
        PR tree-optimization/84725
diff --git a/gcc/testsuite/gcc.dg/pr46921.c b/gcc/testsuite/gcc.dg/pr46921.c
new file mode 100644 (file)
index 0000000..17dfedd
--- /dev/null
@@ -0,0 +1,40 @@
+/* PR c/46921  lost side effect when struct initializer expr uses comma
+      operator  */
+
+/* { dg-do run } */
+/* { dg-options "" } */
+
+extern int printf(const char *, ...);
+extern void abort (void);
+
+typedef struct __uws_0 { } uw_unit;
+uw_unit uw_unit_v = {};
+
+struct __uws_1 
+{
+  struct __uws_0 __uwf_1;
+  struct __uws_1* __uwf_2;
+};
+
+static int left_hand_eval = 0;
+
+static void
+foo (const char *s)
+{
+  ++left_hand_eval;
+  printf("%s", s);
+}
+
+int
+main ()
+{
+  struct __uws_1 tmp = {(foo("Inner\n"), uw_unit_v)};
+  
+  printf("Outer\n");
+  /* left hand expression in comma operator initializer must always be
+     evaluated if there are side effects.  */
+  if (!left_hand_eval)
+    abort ();
+  
+  return 0;
+}
This page took 0.097654 seconds and 5 git commands to generate.