This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[C PATCH] Fix handling side-effects of parameters (PR c/77767)


Hi!

We only record side-effects from the last parameter, the following patch
fixes that by accumulating them from all the parameters.  I've checked all
the callers of grokdeclarator and grokdeclarator is always called with expr
either pointing to NULL_TREE, or being NULL, or where we want the
side-effects to be accumulated.  Creating a STATEMENT_LIST instead of
COMPOUND_EXPRs doesn't work because we want to mark_exp_read the resulting
expression, which doesn't work for a statement list.  COMPOUND_EXPR is what
is used in the two other places where side-effect expressions are
accumulated - one in grokdeclarator, another one in declspecs_add_type.

Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?

2016-12-20  Jakub Jelinek  <jakub@redhat.com>

	PR c/77767
	* c-decl.c (grokdeclarator): If *expr is non-NULL, append expression
	to *expr instead of overwriting it.

	* gcc.c-torture/execute/pr77767.c: New test.

--- gcc/c/c-decl.c.jj	2016-11-21 19:47:01.000000000 +0100
+++ gcc/c/c-decl.c	2016-12-20 12:01:14.518748131 +0100
@@ -5580,11 +5580,21 @@ grokdeclarator (const struct c_declarato
   if (TREE_CODE (type) == ERROR_MARK)
     return error_mark_node;
   if (expr == NULL)
-    expr = &expr_dummy;
+    {
+      expr = &expr_dummy;
+      expr_dummy = NULL_TREE;
+    }
   if (expr_const_operands == NULL)
     expr_const_operands = &expr_const_operands_dummy;
 
-  *expr = declspecs->expr;
+  if (declspecs->expr)
+    {
+      if (*expr)
+	*expr = build2 (COMPOUND_EXPR, TREE_TYPE (declspecs->expr), *expr,
+			declspecs->expr);
+      else
+	*expr = declspecs->expr;
+    }
   *expr_const_operands = declspecs->expr_const_operands;
 
   if (decl_context == FUNCDEF)
--- gcc/testsuite/gcc.c-torture/execute/pr77767.c.jj	2016-12-20 12:08:22.569163979 +0100
+++ gcc/testsuite/gcc.c-torture/execute/pr77767.c	2016-12-20 11:45:03.000000000 +0100
@@ -0,0 +1,16 @@
+/* PR c/77767 */
+
+void
+foo (int a, int b[a++], int c, int d[c++])
+{
+  if (a != 2 || c != 2)
+    __builtin_abort ();
+}
+
+int
+main ()
+{
+  int e[10];
+  foo (1, e, 1, e);
+  return 0;
+}

	Jakub


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]