[docs] Add LICENSE.txt to the root of the mono-repo
[llvm-project.git] / clang / test / Sema / conditional-expr.c
blobff4fd73f4c5c8ba7e985ec4f1ec5c2172fc1164b
1 // RUN: %clang_cc1 -fsyntax-only -Wno-pointer-to-int-cast -verify -pedantic -Wsign-conversion %s
2 void foo(void) {
3 *(0 ? (double *)0 : (void *)0) = 0;
4 // FIXME: GCC doesn't consider the following two statements to be errors.
5 *(0 ? (double *)0 : (void *)(int *)0) = 0; // expected-error {{incomplete type 'void' is not assignable}}
6 *(0 ? (double *)0 : (void *)(double *)0) = 0; // expected-error {{incomplete type 'void' is not assignable}}
7 *(0 ? (double *)0 : (int *)(void *)0) = 0; // expected-error {{incomplete type 'void' is not assignable}} expected-warning {{pointer type mismatch ('double *' and 'int *')}}
8 *(0 ? (double *)0 : (double *)(void *)0) = 0;
9 *((void *) 0) = 0; // expected-error {{incomplete type 'void' is not assignable}}
10 double *dp;
11 int *ip;
12 void *vp;
14 dp = vp;
15 vp = dp;
16 ip = dp; // expected-warning {{incompatible pointer types assigning to 'int *' from 'double *'}}
17 dp = ip; // expected-warning {{incompatible pointer types assigning to 'double *' from 'int *'}}
18 dp = 0 ? (double *)0 : (void *)0;
19 vp = 0 ? (double *)0 : (void *)0;
20 ip = 0 ? (double *)0 : (void *)0; // expected-warning {{incompatible pointer types assigning to 'int *' from 'double *'}}
22 const int *cip;
23 vp = (0 ? vp : cip); // expected-warning {{discards qualifiers}}
24 vp = (0 ? cip : vp); // expected-warning {{discards qualifiers}}
26 int i = 2;
27 int (*pf)[2];
28 int (*pv)[i];
29 pf = (i ? pf : pv);
31 enum {xxx,yyy,zzz} e, *ee;
32 short x;
33 ee = ee ? &x : ee ? &i : &e; // expected-warning {{pointer type mismatch}}
35 typedef void *asdf;
36 *(0 ? (asdf) 0 : &x) = 10;
38 unsigned long test0 = 5;
39 test0 = test0 ? (long) test0 : test0; // expected-warning {{operand of ? changes signedness: 'long' to 'unsigned long'}}
40 test0 = test0 ? (int) test0 : test0; // expected-warning {{operand of ? changes signedness: 'int' to 'unsigned long'}}
41 test0 = test0 ? (short) test0 : test0; // expected-warning {{operand of ? changes signedness: 'short' to 'unsigned long'}}
42 test0 = test0 ? test0 : (long) test0; // expected-warning {{operand of ? changes signedness: 'long' to 'unsigned long'}}
43 test0 = test0 ? test0 : (int) test0; // expected-warning {{operand of ? changes signedness: 'int' to 'unsigned long'}}
44 test0 = test0 ? test0 : (short) test0; // expected-warning {{operand of ? changes signedness: 'short' to 'unsigned long'}}
45 test0 = test0 ? test0 : (long) 10;
46 test0 = test0 ? test0 : (int) 10;
47 test0 = test0 ? test0 : (short) 10;
48 test0 = test0 ? (long) 10 : test0;
49 test0 = test0 ? (int) 10 : test0;
50 test0 = test0 ? (short) 10 : test0;
52 int test1;
53 enum Enum { EVal };
54 test0 = test0 ? EVal : test0;
55 test1 = test0 ? EVal : (int) test0;
56 test0 = test0 ?
57 (unsigned) EVal
58 : (int) test0; // expected-warning {{operand of ? changes signedness: 'int' to 'unsigned long'}}
60 test0 = test0 ? EVal : test1; // expected-warning {{operand of ? changes signedness: 'int' to 'unsigned long'}}
61 test0 = test0 ? test1 : EVal; // expected-warning {{operand of ? changes signedness: 'int' to 'unsigned long'}}
63 const int *const_int;
64 int *nonconst_int;
65 *(test0 ? const_int : nonconst_int) = 42; // expected-error {{read-only variable is not assignable}}
66 *(test0 ? nonconst_int : const_int) = 42; // expected-error {{read-only variable is not assignable}}
68 // The composite type here should be "int (*)[12]", fine for the sizeof
69 int (*incomplete)[];
70 int (*complete)[12];
71 sizeof(*(test0 ? incomplete : complete)); // expected-warning {{expression result unused}}
72 sizeof(*(test0 ? complete : incomplete)); // expected-warning {{expression result unused}}
74 int __attribute__((address_space(2))) *adr2;
75 int __attribute__((address_space(3))) *adr3;
76 test0 ? adr2 : adr3; // expected-error{{conditional operator with the second and third operands of type ('__attribute__((address_space(2))) int *' and '__attribute__((address_space(3))) int *') which are pointers to non-overlapping address spaces}}
78 // Make sure address-space mask ends up in the result type
79 (test0 ? (test0 ? adr2 : adr2) : nonconst_int); // expected-error{{conditional operator with the second and third operands of type ('__attribute__((address_space(2))) int *' and 'int *') which are pointers to non-overlapping address spaces}}
82 int Postgresql(void) {
83 char x;
84 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}}
87 #define nil ((void*) 0)
89 extern int f1(void);
91 int f0(int a) {
92 // GCC considers this a warning.
93 return a ? f1() : nil; // expected-warning {{pointer/integer type mismatch in conditional expression ('int' and 'void *')}} expected-error {{incompatible pointer to integer conversion returning 'void *' from a function with result type 'int'}}
96 int f2(int x) {
97 // We can suppress this because the immediate context wants an int.
98 return (x != 0) ? 0U : x;
101 #define NULL (void*)0
103 void PR9236(void) {
104 struct A {int i;} A1;
105 (void)(1 ? A1 : NULL); // expected-error{{non-pointer operand type 'struct A' incompatible with NULL}}
106 (void)(1 ? NULL : A1); // expected-error{{non-pointer operand type 'struct A' incompatible with NULL}}
107 (void)(1 ? 0 : A1); // expected-error{{incompatible operand types}}
108 (void)(1 ? (void*)0 : A1); // expected-error{{incompatible operand types}}
109 (void)(1 ? A1: (void*)0); // expected-error{{incompatible operand types}}
110 (void)(1 ? A1 : (NULL)); // expected-error{{non-pointer operand type 'struct A' incompatible with NULL}}