FSF GCC merge 02/23/03
[official-gcc.git] / gcc / testsuite / gcc.c-torture / execute / string-opt-4.c
blob9182eb278387ee0c4de6baeb2aad7b1852c54597
1 /* Copyright (C) 2000 Free Software Foundation.
3 Ensure all expected transformations of builtin strchr and index
4 occur and perform correctly.
6 Written by Jakub Jelinek, 11/7/2000. */
8 extern void abort (void);
9 extern char *strchr (const char *, int);
10 extern char *index (const char *, int);
12 int main()
14 const char *const foo = "hello world";
16 if (strchr (foo, 'x'))
17 abort ();
18 if (strchr (foo, 'o') != foo + 4)
19 abort ();
20 if (strchr (foo + 5, 'o') != foo + 7)
21 abort ();
22 if (strchr (foo, '\0') != foo + 11)
23 abort ();
24 /* Test only one instance of index since the code path is the same
25 as that of strchr. */
26 if (index ("hello", 'z') != 0)
27 abort ();
29 /* Test at least one instance of the __builtin_ style. We do this
30 to ensure that it works and that the prototype is correct. */
31 if (__builtin_strchr (foo, 'o') != foo + 4)
32 abort ();
33 if (__builtin_index (foo, 'o') != foo + 4)
34 abort ();
36 return 0;
39 static char *
40 index (const char *s, int c)
42 /* For systems which don't have index, we ensure no link failures
43 occur by always providing a backup definition. During
44 optimization this function aborts to catch errors. */
45 #ifdef __OPTIMIZE__
46 abort ();
47 #else
48 return strchr(s, c);
49 #endif
52 #ifdef __OPTIMIZE__
53 /* When optimizing, all the above cases should be transformed into
54 something else. So any remaining calls to the original function
55 should abort. */
56 __attribute__ ((noinline))
57 static char *
58 strchr (const char *s, int c)
60 abort ();
62 #endif