builtins.c (expand_builtin_fputs): When deleting NOP calls to builtin fputs...
authorKaveh R. Ghazi <ghazi@caip.rutgers.edu>
Tue, 31 Oct 2000 18:27:42 +0000 (18:27 +0000)
committerKaveh Ghazi <ghazi@gcc.gnu.org>
Tue, 31 Oct 2000 18:27:42 +0000 (18:27 +0000)
* builtins.c (expand_builtin_fputs): When deleting NOP calls to
builtin fputs, ensure we still evaluate the stream in case it
has side-effects.

testsuite:
* gcc.c-torture/execute/stdio-opt-1.c: New test.

From-SVN: r37162

gcc/ChangeLog
gcc/builtins.c
gcc/testsuite/ChangeLog
gcc/testsuite/gcc.c-torture/execute/stdio-opt-1.c [new file with mode: 0644]

index 6d3e05273e7dcca00e3d03b7fe1ae206e063bad4..fcc65c8b8f28aa818d5eb46a952614b228aeede4 100644 (file)
@@ -1,3 +1,9 @@
+2000-10-31  Kaveh R. Ghazi  <ghazi@caip.rutgers.edu>
+
+       * builtins.c (expand_builtin_fputs): When deleting NOP calls to
+       builtin fputs, ensure we still evaluate the stream in case it
+       has side-effects.
+
 2000-10-31  Jakub Jelinek  <jakub@redhat.com>
 
        * expr.c (do_store_flag): Pass operand_mode instead of GET_MODE (op0)
index e2acada22c7e0efa0ee8210bcd557a13bab72556..3304b68a46a81d26a677e4cfd492948c408c4f15 100644 (file)
@@ -2351,7 +2351,13 @@ expand_builtin_fputs (arglist, ignore)
   switch (compare_tree_int (len, 1))
     {
     case -1: /* length is 0, delete the call entirely .  */
-      return const0_rtx;
+      {
+       /* Evaluate and ignore the argument in case it has
+           side-effects.  */
+       expand_expr (TREE_VALUE (TREE_CHAIN (arglist)), const0_rtx,
+                    VOIDmode, EXPAND_NORMAL);
+       return const0_rtx;
+      }
     case 0: /* length is 1, call fputc.  */
       {
        tree stripped_string = TREE_VALUE (arglist);
index 33002ac02374e1bc399a6ee5fb39799d498dc4dd..6097ca2cc2eedfb3ae17b1bf006291b2a98cbc9e 100644 (file)
@@ -1,3 +1,7 @@
+2000-10-31  Kaveh R. Ghazi  <ghazi@caip.rutgers.edu>
+
+       * gcc.c-torture/execute/stdio-opt-1.c: New test.
+
 2000-10-31  Jakub Jelinek  <jakub@redhat.com>
 
        * g++.old-deja/g++.other/inline16.C: New test.
diff --git a/gcc/testsuite/gcc.c-torture/execute/stdio-opt-1.c b/gcc/testsuite/gcc.c-torture/execute/stdio-opt-1.c
new file mode 100644 (file)
index 0000000..aeb7302
--- /dev/null
@@ -0,0 +1,28 @@
+/* Copyright (C) 2000  Free Software Foundation.
+
+   When eliminating NOP calls to builtin fputs, ensure that we still
+   evaluate the stream argument in case it has side effects.
+   Written by Kaveh R. Ghazi, 10/30/2000.  */
+
+#include <stdio.h>
+
+int main()
+{
+  FILE *s_array[3] = {stdout, NULL, stdout}, **s_ptr = s_array;
+  
+  /* Increment the stream pointer once.  */
+  fputs ("", *s_ptr++);
+
+  /* Increment the stream pointer a second time.  */
+  s_ptr++;
+
+  /* If we failed to increment the stream pointer twice, then the
+     stream passed in here will be NULL and we should crash.  */
+  fputs ("hello world\n", *s_ptr);
+  
+  /* Just in case, If *s_ptr is NULL abort anyway.  */
+  if (*s_ptr == 0)
+    abort();
+
+  return 0;
+}