OSDN Git Service

* gcc.c-torture/execute/20040208-2.c: Move ...
[pf3gnuchains/gcc-fork.git] / gcc / testsuite / gcc.c-torture / execute / stdio-opt-1.c
1 /* Copyright (C) 2000, 2001  Free Software Foundation.
2
3    Ensure all expected transformations of builtin fputs occur and that
4    we honor side effects in the stream argument.
5
6    Written by Kaveh R. Ghazi, 10/30/2000.  */
7
8 #include <stdio.h>
9 extern void abort(void);
10 /* Declare this without args because that's what gcc does internally.
11    We want to make sure it works without a helpful prototype from us.
12    If stdio.h provides one, that is okay.  */
13 extern int fputs();
14
15 int i;
16
17 int main()
18 {
19   FILE *s_array[] = {stdout, NULL}, **s_ptr = s_array;
20   const char *const s1 = "hello world";
21   
22   fputs ("", *s_ptr);
23   fputs ("\n", *s_ptr);
24   fputs ("bye", *s_ptr);
25   fputs (s1, *s_ptr);
26   fputs (s1+5, *s_ptr);
27   fputs (s1+10, *s_ptr);
28   fputs (s1+11, *s_ptr);
29   
30   /* Check side-effects when transforming fputs -> NOP.  */
31   fputs ("", *s_ptr++);
32   if (s_ptr != s_array+1 || *s_ptr != 0)
33     abort();
34
35   /* Check side-effects when transforming fputs -> fputc.  */
36   s_ptr = s_array;
37   fputs ("\n", *s_ptr++);
38   if (s_ptr != s_array+1 || *s_ptr != 0)
39     abort();
40
41   /* Check side-effects when transforming fputs -> fwrite.  */
42   s_ptr = s_array;
43   fputs ("hello\n", *s_ptr++);
44   if (s_ptr != s_array+1 || *s_ptr != 0)
45     abort();
46
47   /* Test at least one instance of the __builtin_ style.  We do this
48      to ensure that it works and that the prototype is correct.  */
49   s_ptr = s_array;
50   __builtin_fputs ("", *s_ptr);
51   /* These builtin stubs are called by __builtin_fputs, ensure their
52      prototypes are set correctly too.  */
53   __builtin_fputc ('\n', *s_ptr);
54   __builtin_fwrite ("hello\n", 1, 6, *s_ptr);
55
56   /* Check side-effects in conditional expression.  */
57   s_ptr = s_array;
58   fputs (i++ ? "f" : "x", *s_ptr++);
59   if (s_ptr != s_array+1 || *s_ptr != 0 || i != 1)
60     abort();
61   fputs (--i ? "\n" : "\n", *--s_ptr);
62   if (s_ptr != s_array || i != 0)
63     abort();
64
65   return 0;
66 }
67
68 #if defined (__OPTIMIZE__) && ! defined (__OPTIMIZE_SIZE__)
69 /* When optimizing, all the above cases should be transformed into
70    something else.  So any remaining calls to the original function
71    should abort.  */
72 __attribute__ ((noinline))
73 static int
74 fputs(const char *string, FILE *stream)
75 {
76   abort();
77 }
78 #endif