aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--gcc/c/ChangeLog6
-rw-r--r--gcc/c/c-typeck.c6
-rw-r--r--gcc/testsuite/ChangeLog5
-rw-r--r--gcc/testsuite/gcc.dg/pr46921.c40
4 files changed, 55 insertions, 2 deletions
diff --git a/gcc/c/ChangeLog b/gcc/c/ChangeLog
index 599ffa2..8147835 100644
--- a/gcc/c/ChangeLog
+++ b/gcc/c/ChangeLog
@@ -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
diff --git a/gcc/c/c-typeck.c b/gcc/c/c-typeck.c
index 1eae4ea..2ac8500 100644
--- a/gcc/c/c-typeck.c
+++ b/gcc/c/c-typeck.c
@@ -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;
diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog
index 8f15d5f..c518ebe 100644
--- a/gcc/testsuite/ChangeLog
+++ b/gcc/testsuite/ChangeLog
@@ -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
index 0000000..17dfedd
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr46921.c
@@ -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;
+}