xref: /minix3/external/bsd/llvm/dist/clang/test/Sema/conditional-expr.c (revision f4a2713ac843a11c696ec80c0a5e3e5d80b4d338)
1*f4a2713aSLionel Sambuc // RUN: %clang_cc1 -fsyntax-only -verify -pedantic -Wsign-conversion %s
foo()2*f4a2713aSLionel Sambuc void foo() {
3*f4a2713aSLionel Sambuc   *(0 ? (double *)0 : (void *)0) = 0;
4*f4a2713aSLionel Sambuc   // FIXME: GCC doesn't consider the following two statements to be errors.
5*f4a2713aSLionel Sambuc   *(0 ? (double *)0 : (void *)(int *)0) = 0; // expected-error {{incomplete type 'void' is not assignable}}
6*f4a2713aSLionel Sambuc   *(0 ? (double *)0 : (void *)(double *)0) = 0; // expected-error {{incomplete type 'void' is not assignable}}
7*f4a2713aSLionel Sambuc   *(0 ? (double *)0 : (int *)(void *)0) = 0; // expected-error {{incomplete type 'void' is not assignable}} expected-warning {{pointer type mismatch ('double *' and 'int *')}}
8*f4a2713aSLionel Sambuc   *(0 ? (double *)0 : (double *)(void *)0) = 0;
9*f4a2713aSLionel Sambuc   *((void *) 0) = 0; // expected-error {{incomplete type 'void' is not assignable}}
10*f4a2713aSLionel Sambuc   double *dp;
11*f4a2713aSLionel Sambuc   int *ip;
12*f4a2713aSLionel Sambuc   void *vp;
13*f4a2713aSLionel Sambuc 
14*f4a2713aSLionel Sambuc   dp = vp;
15*f4a2713aSLionel Sambuc   vp = dp;
16*f4a2713aSLionel Sambuc   ip = dp; // expected-warning {{incompatible pointer types assigning to 'int *' from 'double *'}}
17*f4a2713aSLionel Sambuc   dp = ip; // expected-warning {{incompatible pointer types assigning to 'double *' from 'int *'}}
18*f4a2713aSLionel Sambuc   dp = 0 ? (double *)0 : (void *)0;
19*f4a2713aSLionel Sambuc   vp = 0 ? (double *)0 : (void *)0;
20*f4a2713aSLionel Sambuc   ip = 0 ? (double *)0 : (void *)0; // expected-warning {{incompatible pointer types assigning to 'int *' from 'double *'}}
21*f4a2713aSLionel Sambuc 
22*f4a2713aSLionel Sambuc   const int *cip;
23*f4a2713aSLionel Sambuc   vp = (0 ? vp : cip); // expected-warning {{discards qualifiers}}
24*f4a2713aSLionel Sambuc   vp = (0 ? cip : vp); // expected-warning {{discards qualifiers}}
25*f4a2713aSLionel Sambuc 
26*f4a2713aSLionel Sambuc   int i = 2;
27*f4a2713aSLionel Sambuc   int (*pf)[2];
28*f4a2713aSLionel Sambuc   int (*pv)[i];
29*f4a2713aSLionel Sambuc   pf = (i ? pf : pv);
30*f4a2713aSLionel Sambuc 
31*f4a2713aSLionel Sambuc   enum {xxx,yyy,zzz} e, *ee;
32*f4a2713aSLionel Sambuc   short x;
33*f4a2713aSLionel Sambuc   ee = ee ? &x : ee ? &i : &e; // expected-warning {{pointer type mismatch}}
34*f4a2713aSLionel Sambuc 
35*f4a2713aSLionel Sambuc   typedef void *asdf;
36*f4a2713aSLionel Sambuc   *(0 ? (asdf) 0 : &x) = 10;
37*f4a2713aSLionel Sambuc 
38*f4a2713aSLionel Sambuc   unsigned long test0 = 5;
39*f4a2713aSLionel Sambuc   test0 = test0 ? (long) test0 : test0; // expected-warning {{operand of ? changes signedness: 'long' to 'unsigned long'}}
40*f4a2713aSLionel Sambuc   test0 = test0 ? (int) test0 : test0; // expected-warning {{operand of ? changes signedness: 'int' to 'unsigned long'}}
41*f4a2713aSLionel Sambuc   test0 = test0 ? (short) test0 : test0; // expected-warning {{operand of ? changes signedness: 'short' to 'unsigned long'}}
42*f4a2713aSLionel Sambuc   test0 = test0 ? test0 : (long) test0; // expected-warning {{operand of ? changes signedness: 'long' to 'unsigned long'}}
43*f4a2713aSLionel Sambuc   test0 = test0 ? test0 : (int) test0; // expected-warning {{operand of ? changes signedness: 'int' to 'unsigned long'}}
44*f4a2713aSLionel Sambuc   test0 = test0 ? test0 : (short) test0; // expected-warning {{operand of ? changes signedness: 'short' to 'unsigned long'}}
45*f4a2713aSLionel Sambuc   test0 = test0 ? test0 : (long) 10;
46*f4a2713aSLionel Sambuc   test0 = test0 ? test0 : (int) 10;
47*f4a2713aSLionel Sambuc   test0 = test0 ? test0 : (short) 10;
48*f4a2713aSLionel Sambuc   test0 = test0 ? (long) 10 : test0;
49*f4a2713aSLionel Sambuc   test0 = test0 ? (int) 10 : test0;
50*f4a2713aSLionel Sambuc   test0 = test0 ? (short) 10 : test0;
51*f4a2713aSLionel Sambuc 
52*f4a2713aSLionel Sambuc   int test1;
53*f4a2713aSLionel Sambuc   enum Enum { EVal };
54*f4a2713aSLionel Sambuc   test0 = test0 ? EVal : test0;
55*f4a2713aSLionel Sambuc   test1 = test0 ? EVal : (int) test0;
56*f4a2713aSLionel Sambuc   test0 = test0 ?
57*f4a2713aSLionel Sambuc                   (unsigned) EVal
58*f4a2713aSLionel Sambuc                 : (int) test0;  // expected-warning {{operand of ? changes signedness: 'int' to 'unsigned long'}}
59*f4a2713aSLionel Sambuc 
60*f4a2713aSLionel Sambuc   test0 = test0 ? EVal : test1; // expected-warning {{operand of ? changes signedness: 'int' to 'unsigned long'}}
61*f4a2713aSLionel Sambuc   test0 = test0 ? test1 : EVal; // expected-warning {{operand of ? changes signedness: 'int' to 'unsigned long'}}
62*f4a2713aSLionel Sambuc 
63*f4a2713aSLionel Sambuc   const int *const_int;
64*f4a2713aSLionel Sambuc   int *nonconst_int;
65*f4a2713aSLionel Sambuc   *(test0 ? const_int : nonconst_int) = 42; // expected-error {{read-only variable is not assignable}}
66*f4a2713aSLionel Sambuc   *(test0 ? nonconst_int : const_int) = 42; // expected-error {{read-only variable is not assignable}}
67*f4a2713aSLionel Sambuc 
68*f4a2713aSLionel Sambuc   // The composite type here should be "int (*)[12]", fine for the sizeof
69*f4a2713aSLionel Sambuc   int (*incomplete)[];
70*f4a2713aSLionel Sambuc   int (*complete)[12];
71*f4a2713aSLionel Sambuc   sizeof(*(test0 ? incomplete : complete)); // expected-warning {{expression result unused}}
72*f4a2713aSLionel Sambuc   sizeof(*(test0 ? complete : incomplete)); // expected-warning {{expression result unused}}
73*f4a2713aSLionel Sambuc 
74*f4a2713aSLionel Sambuc   int __attribute__((address_space(2))) *adr2;
75*f4a2713aSLionel Sambuc   int __attribute__((address_space(3))) *adr3;
76*f4a2713aSLionel Sambuc   test0 ? adr2 : adr3; // expected-warning {{pointer type mismatch}} expected-warning {{expression result unused}}
77*f4a2713aSLionel Sambuc 
78*f4a2713aSLionel Sambuc   // Make sure address-space mask ends up in the result type
79*f4a2713aSLionel Sambuc   (test0 ? (test0 ? adr2 : adr2) : nonconst_int); // expected-warning {{pointer type mismatch}} expected-warning {{expression result unused}}
80*f4a2713aSLionel Sambuc }
81*f4a2713aSLionel Sambuc 
Postgresql()82*f4a2713aSLionel Sambuc int Postgresql() {
83*f4a2713aSLionel Sambuc   char x;
84*f4a2713aSLionel Sambuc   return ((((&x) != ((void *) 0)) ? (*(&x) = ((char) 1)) : (void) ((void *) 0)), (unsigned long) ((void *) 0)); // expected-warning {{C99 forbids conditional expressions with only one void side}}
85*f4a2713aSLionel Sambuc }
86*f4a2713aSLionel Sambuc 
87*f4a2713aSLionel Sambuc #define nil ((void*) 0)
88*f4a2713aSLionel Sambuc 
89*f4a2713aSLionel Sambuc extern int f1(void);
90*f4a2713aSLionel Sambuc 
f0(int a)91*f4a2713aSLionel Sambuc int f0(int a) {
92*f4a2713aSLionel Sambuc   // GCC considers this a warning.
93*f4a2713aSLionel Sambuc   return a ? f1() : nil; // expected-warning {{pointer/integer type mismatch in conditional expression ('int' and 'void *')}} expected-warning {{incompatible pointer to integer conversion returning 'void *' from a function with result type 'int'}}
94*f4a2713aSLionel Sambuc }
95*f4a2713aSLionel Sambuc 
f2(int x)96*f4a2713aSLionel Sambuc int f2(int x) {
97*f4a2713aSLionel Sambuc   // We can suppress this because the immediate context wants an int.
98*f4a2713aSLionel Sambuc   return (x != 0) ? 0U : x;
99*f4a2713aSLionel Sambuc }
100*f4a2713aSLionel Sambuc 
101*f4a2713aSLionel Sambuc #define NULL (void*)0
102*f4a2713aSLionel Sambuc 
PR9236()103*f4a2713aSLionel Sambuc void PR9236() {
104*f4a2713aSLionel Sambuc   struct A {int i;} A1;
105*f4a2713aSLionel Sambuc   (void)(1 ? A1 : NULL); // expected-error{{non-pointer operand type 'struct A' incompatible with NULL}}
106*f4a2713aSLionel Sambuc   (void)(1 ? NULL : A1); // expected-error{{non-pointer operand type 'struct A' incompatible with NULL}}
107*f4a2713aSLionel Sambuc   (void)(1 ? 0 : A1); // expected-error{{incompatible operand types}}
108*f4a2713aSLionel Sambuc   (void)(1 ? (void*)0 : A1); // expected-error{{incompatible operand types}}
109*f4a2713aSLionel Sambuc   (void)(1 ? A1: (void*)0); // expected-error{{incompatible operand types}}
110*f4a2713aSLionel Sambuc   (void)(1 ? A1 : (NULL)); // expected-error{{non-pointer operand type 'struct A' incompatible with NULL}}
111*f4a2713aSLionel Sambuc }
112*f4a2713aSLionel Sambuc 
113