[Reland][Runtimes] Merge 'compile_commands.json' files from runtimes build (#116303)
[llvm-project.git] / clang / test / Sema / conditional-expr.c
blobb54b689ec4f055bbd774faa6633b7896bfa188dd
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 expected-warning {{ISO C does not allow indirection on operand of type 'void *'}} */
7 *(0 ? (double *)0 : (void *)(double *)0) = 0; /* expected-error {{incomplete type 'void' is not assignable}}
8 expected-warning {{ISO C does not allow indirection on operand of type 'void *'}} */
9 *(0 ? (double *)0 : (int *)(void *)0) = 0; /* expected-error {{incomplete type 'void' is not assignable}}
10 expected-warning {{pointer type mismatch ('double *' and 'int *')}}
11 expected-warning {{ISO C does not allow indirection on operand of type 'void *'}} */
12 *(0 ? (double *)0 : (double *)(void *)0) = 0;
13 *((void *) 0) = 0; /* expected-error {{incomplete type 'void' is not assignable}}
14 expected-warning {{ISO C does not allow indirection on operand of type 'void *'}} */
15 double *dp;
16 int *ip;
17 void *vp;
19 dp = vp;
20 vp = dp;
21 ip = dp; // expected-warning {{incompatible pointer types assigning to 'int *' from 'double *'}}
22 dp = ip; // expected-warning {{incompatible pointer types assigning to 'double *' from 'int *'}}
23 dp = 0 ? (double *)0 : (void *)0;
24 vp = 0 ? (double *)0 : (void *)0;
25 ip = 0 ? (double *)0 : (void *)0; // expected-warning {{incompatible pointer types assigning to 'int *' from 'double *'}}
27 const int *cip;
28 vp = (0 ? vp : cip); // expected-warning {{discards qualifiers}}
29 vp = (0 ? cip : vp); // expected-warning {{discards qualifiers}}
31 int i = 2;
32 int (*pf)[2];
33 int (*pv)[i];
34 pf = (i ? pf : pv);
36 enum {xxx,yyy,zzz} e, *ee;
37 short x;
38 ee = ee ? &x : ee ? &i : &e; // expected-warning {{pointer type mismatch}}
40 typedef void *asdf;
41 *(0 ? (asdf) 0 : &x) = 10;
43 unsigned long test0 = 5;
44 test0 = test0 ? (long) test0 : test0; // expected-warning {{operand of ? changes signedness: 'long' to 'unsigned long'}}
45 test0 = test0 ? (int) test0 : test0; // expected-warning {{operand of ? changes signedness: 'int' to 'unsigned long'}}
46 test0 = test0 ? (short) test0 : test0; // expected-warning {{operand of ? changes signedness: 'short' to 'unsigned long'}}
47 test0 = test0 ? test0 : (long) test0; // expected-warning {{operand of ? changes signedness: 'long' to 'unsigned long'}}
48 test0 = test0 ? test0 : (int) test0; // expected-warning {{operand of ? changes signedness: 'int' to 'unsigned long'}}
49 test0 = test0 ? test0 : (short) test0; // expected-warning {{operand of ? changes signedness: 'short' to 'unsigned long'}}
50 test0 = test0 ? test0 : (long) 10;
51 test0 = test0 ? test0 : (int) 10;
52 test0 = test0 ? test0 : (short) 10;
53 test0 = test0 ? (long) 10 : test0;
54 test0 = test0 ? (int) 10 : test0;
55 test0 = test0 ? (short) 10 : test0;
57 int test1;
58 enum Enum { EVal };
59 test0 = test0 ? EVal : test0;
60 test1 = test0 ? EVal : (int) test0;
61 test0 = test0 ?
62 (unsigned) EVal
63 : (int) test0; // expected-warning {{operand of ? changes signedness: 'int' to 'unsigned long'}}
65 test0 = test0 ? EVal : test1; // expected-warning {{operand of ? changes signedness: 'int' to 'unsigned long'}}
66 test0 = test0 ? test1 : EVal; // expected-warning {{operand of ? changes signedness: 'int' to 'unsigned long'}}
68 const int *const_int;
69 int *nonconst_int;
70 *(test0 ? const_int : nonconst_int) = 42; // expected-error {{read-only variable is not assignable}}
71 *(test0 ? nonconst_int : const_int) = 42; // expected-error {{read-only variable is not assignable}}
73 // The composite type here should be "int (*)[12]", fine for the sizeof
74 int (*incomplete)[];
75 int (*complete)[12];
76 sizeof(*(test0 ? incomplete : complete)); // expected-warning {{expression result unused}}
77 sizeof(*(test0 ? complete : incomplete)); // expected-warning {{expression result unused}}
79 int __attribute__((address_space(2))) *adr2;
80 int __attribute__((address_space(3))) *adr3;
81 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}}
83 // Make sure address-space mask ends up in the result type
84 (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}}
87 int Postgresql(void) {
88 char x;
89 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}}
90 expected-warning {{comparison of address of 'x' not equal to a null pointer is always true}} */
93 #define nil ((void*) 0)
95 extern int f1(void);
97 int f0(int a) {
98 // GCC considers this a warning.
99 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'}}
102 int f2(int x) {
103 // We can suppress this because the immediate context wants an int.
104 return (x != 0) ? 0U : x;
107 #define NULL (void*)0
109 void PR9236(void) {
110 struct A {int i;} A1;
111 (void)(1 ? A1 : NULL); // expected-error{{non-pointer operand type 'struct A' incompatible with NULL}}
112 (void)(1 ? NULL : A1); // expected-error{{non-pointer operand type 'struct A' incompatible with NULL}}
113 (void)(1 ? 0 : A1); // expected-error{{incompatible operand types}}
114 (void)(1 ? (void*)0 : A1); // expected-error{{incompatible operand types}}
115 (void)(1 ? A1: (void*)0); // expected-error{{incompatible operand types}}
116 (void)(1 ? A1 : (NULL)); // expected-error{{non-pointer operand type 'struct A' incompatible with NULL}}