Merge from mainline.
[official-gcc.git] / gcc / testsuite / gcc.dg / c99-condexpr-1.c
blob69d4250801a94b01bba91010c020d1c44e368ef1
1 /* Test for types of conditional expressions. */
2 /* Origin: Joseph Myers <jsm28@cam.ac.uk> */
3 /* { dg-do compile } */
4 /* { dg-options "-std=iso9899:1999 -pedantic-errors" } */
6 /* Notes:
8 (a) The rules are the same in both C standard versions, but C99 also
9 gives us the "restrict" qualifier to play with.
11 (b) Within the C standard, the value of a conditional expression can't
12 have qualified type - but nor can this be detected. Because of GCC's
13 extended lvalues, the value may in GCC have qualified type if the
14 arguments do. So don't use the following macro with arguments of
15 qualified type.
19 /* Assertion that the type of a conditional expression between E1 and E2
20 is T. Checks the expression both ways round. */
21 #define ASSERT_COND_TYPE(E1, E2, T) \
22 do { \
23 typedef T type; \
24 typedef type **typepp; \
25 typedef __typeof(0 ? (E1) : (E2)) ctype; \
26 typedef __typeof(0 ? (E2) : (E1)) ctype2; \
27 typedef ctype **ctypepp; \
28 typedef ctype2 **ctype2pp; \
29 typepp x = 0; \
30 ctypepp y = 0; \
31 ctype2pp z = 0; \
32 x = y; \
33 x = z; \
34 } while (0)
36 void
37 foo (void)
39 const void *c_vp;
40 void *vp;
41 const int *c_ip;
42 volatile int *v_ip;
43 int *ip;
44 const char *c_cp;
45 int *restrict *r_ipp;
46 typedef void (*fpt)(void);
47 fpt fp;
48 signed char sc;
49 struct s { int p; } st;
50 union u { int p; } un;
51 /* Arithmetic type. */
52 ASSERT_COND_TYPE (sc, sc, int);
53 /* Structure and union. */
54 ASSERT_COND_TYPE (st, st, struct s);
55 ASSERT_COND_TYPE (un, un, union u);
56 /* Void. */
57 ASSERT_COND_TYPE ((void)0, (void)1, void);
58 /* Pointers: examples from 6.5.15 paragraph 8. */
59 ASSERT_COND_TYPE (c_vp, c_ip, const void *);
60 ASSERT_COND_TYPE (v_ip, 0, volatile int *);
61 ASSERT_COND_TYPE (c_ip, v_ip, const volatile int *);
62 ASSERT_COND_TYPE (vp, c_cp, const void *);
63 ASSERT_COND_TYPE (ip, c_ip, const int *);
64 ASSERT_COND_TYPE (vp, ip, void *);
65 /* Null pointer constants. */
66 ASSERT_COND_TYPE (v_ip, (void *)0, volatile int *);
67 ASSERT_COND_TYPE (r_ipp, (void *)0, int *restrict *);
68 ASSERT_COND_TYPE (fp, 0, fpt);
69 ASSERT_COND_TYPE (fp, (void *)0, fpt);