15254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu// RUN: %clang_cc1 -fsyntax-only -verify -pedantic -Wsign-conversion %s
2b6d54e56a5c65c2728080578b84374c3c594daecSteve Naroffvoid foo() {
3b6d54e56a5c65c2728080578b84374c3c594daecSteve Naroff  *(0 ? (double *)0 : (void *)0) = 0;
4bed28ac1d1463adca3ecf24fca5c30646fa9dbb2Sylvestre Ledru  // FIXME: GCC doesn't consider the following two statements to be errors.
5aa58f00ebba5f14955001736b7aea20bb5bd91e6Steve Naroff  *(0 ? (double *)0 : (void *)(int *)0) = 0; // expected-error {{incomplete type 'void' is not assignable}}
6aa58f00ebba5f14955001736b7aea20bb5bd91e6Steve Naroff  *(0 ? (double *)0 : (void *)(double *)0) = 0; // expected-error {{incomplete type 'void' is not assignable}}
7890d93eec45f2ba720dce98e34a1a904697ae842Steve Naroff  *(0 ? (double *)0 : (int *)(void *)0) = 0; // expected-error {{incomplete type 'void' is not assignable}} expected-warning {{pointer type mismatch ('double *' and 'int *')}}
8aaffbf7c790a324ed114184db771aae2d2e9151cSteve Naroff  *(0 ? (double *)0 : (double *)(void *)0) = 0;
9aaffbf7c790a324ed114184db771aae2d2e9151cSteve Naroff  *((void *) 0) = 0; // expected-error {{incomplete type 'void' is not assignable}}
10b6d54e56a5c65c2728080578b84374c3c594daecSteve Naroff  double *dp;
11b6d54e56a5c65c2728080578b84374c3c594daecSteve Naroff  int *ip;
12b6d54e56a5c65c2728080578b84374c3c594daecSteve Naroff  void *vp;
13b6d54e56a5c65c2728080578b84374c3c594daecSteve Naroff
14b6d54e56a5c65c2728080578b84374c3c594daecSteve Naroff  dp = vp;
15b6d54e56a5c65c2728080578b84374c3c594daecSteve Naroff  vp = dp;
16d4eea8362605807327735727a9098abe1eb23b19Douglas Gregor  ip = dp; // expected-warning {{incompatible pointer types assigning to 'int *' from 'double *'}}
17d4eea8362605807327735727a9098abe1eb23b19Douglas Gregor  dp = ip; // expected-warning {{incompatible pointer types assigning to 'double *' from 'int *'}}
18b6d54e56a5c65c2728080578b84374c3c594daecSteve Naroff  dp = 0 ? (double *)0 : (void *)0;
19b6d54e56a5c65c2728080578b84374c3c594daecSteve Naroff  vp = 0 ? (double *)0 : (void *)0;
20d4eea8362605807327735727a9098abe1eb23b19Douglas Gregor  ip = 0 ? (double *)0 : (void *)0; // expected-warning {{incompatible pointer types assigning to 'int *' from 'double *'}}
21f76f5ed0505bc4b9c3c7d6003335b34cdf9afe47Eli Friedman
22f76f5ed0505bc4b9c3c7d6003335b34cdf9afe47Eli Friedman  const int *cip;
23f76f5ed0505bc4b9c3c7d6003335b34cdf9afe47Eli Friedman  vp = (0 ? vp : cip); // expected-warning {{discards qualifiers}}
24f76f5ed0505bc4b9c3c7d6003335b34cdf9afe47Eli Friedman  vp = (0 ? cip : vp); // expected-warning {{discards qualifiers}}
254c721d381fb279899337d120edd4a24d405e56b2Eli Friedman
264c721d381fb279899337d120edd4a24d405e56b2Eli Friedman  int i = 2;
274c721d381fb279899337d120edd4a24d405e56b2Eli Friedman  int (*pf)[2];
284c721d381fb279899337d120edd4a24d405e56b2Eli Friedman  int (*pv)[i];
294c721d381fb279899337d120edd4a24d405e56b2Eli Friedman  pf = (i ? pf : pv);
30bab96968886f4b77083f4e26a28986ddb1e42d67Eli Friedman
31bab96968886f4b77083f4e26a28986ddb1e42d67Eli Friedman  enum {xxx,yyy,zzz} e, *ee;
32bab96968886f4b77083f4e26a28986ddb1e42d67Eli Friedman  short x;
33bab96968886f4b77083f4e26a28986ddb1e42d67Eli Friedman  ee = ee ? &x : ee ? &i : &e; // expected-warning {{pointer type mismatch}}
344b3f9b367c16e494c181d6f03d53497ae1275fbeEli Friedman
354b3f9b367c16e494c181d6f03d53497ae1275fbeEli Friedman  typedef void *asdf;
364b3f9b367c16e494c181d6f03d53497ae1275fbeEli Friedman  *(0 ? (asdf) 0 : &x) = 10;
37b13c87f0c9705d91d5a3e134be9934c9ad531071John McCall
38b13c87f0c9705d91d5a3e134be9934c9ad531071John McCall  unsigned long test0 = 5;
395254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu  test0 = test0 ? (long) test0 : test0; // expected-warning {{operand of ? changes signedness: 'long' to 'unsigned long'}}
405254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu  test0 = test0 ? (int) test0 : test0; // expected-warning {{operand of ? changes signedness: 'int' to 'unsigned long'}}
415254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu  test0 = test0 ? (short) test0 : test0; // expected-warning {{operand of ? changes signedness: 'short' to 'unsigned long'}}
425254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu  test0 = test0 ? test0 : (long) test0; // expected-warning {{operand of ? changes signedness: 'long' to 'unsigned long'}}
435254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu  test0 = test0 ? test0 : (int) test0; // expected-warning {{operand of ? changes signedness: 'int' to 'unsigned long'}}
445254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu  test0 = test0 ? test0 : (short) test0; // expected-warning {{operand of ? changes signedness: 'short' to 'unsigned long'}}
45b13c87f0c9705d91d5a3e134be9934c9ad531071John McCall  test0 = test0 ? test0 : (long) 10;
46b13c87f0c9705d91d5a3e134be9934c9ad531071John McCall  test0 = test0 ? test0 : (int) 10;
47b13c87f0c9705d91d5a3e134be9934c9ad531071John McCall  test0 = test0 ? test0 : (short) 10;
48b13c87f0c9705d91d5a3e134be9934c9ad531071John McCall  test0 = test0 ? (long) 10 : test0;
49b13c87f0c9705d91d5a3e134be9934c9ad531071John McCall  test0 = test0 ? (int) 10 : test0;
50b13c87f0c9705d91d5a3e134be9934c9ad531071John McCall  test0 = test0 ? (short) 10 : test0;
51b13c87f0c9705d91d5a3e134be9934c9ad531071John McCall
525254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu  int test1;
53b13c87f0c9705d91d5a3e134be9934c9ad531071John McCall  enum Enum { EVal };
54b13c87f0c9705d91d5a3e134be9934c9ad531071John McCall  test0 = test0 ? EVal : test0;
555254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu  test1 = test0 ? EVal : (int) test0;
565254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu  test0 = test0 ?
57323ed74658bc8375278eabf074b4777458376540John McCall                  (unsigned) EVal
585254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu                : (int) test0;  // expected-warning {{operand of ? changes signedness: 'int' to 'unsigned long'}}
595254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu
605254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu  test0 = test0 ? EVal : test1; // expected-warning {{operand of ? changes signedness: 'int' to 'unsigned long'}}
615254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu  test0 = test0 ? test1 : EVal; // expected-warning {{operand of ? changes signedness: 'int' to 'unsigned long'}}
625254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu
63ae916a14cf727b4ce3ac316f4fd780d1c83c5bafEli Friedman  const int *const_int;
64ae916a14cf727b4ce3ac316f4fd780d1c83c5bafEli Friedman  int *nonconst_int;
65ae916a14cf727b4ce3ac316f4fd780d1c83c5bafEli Friedman  *(test0 ? const_int : nonconst_int) = 42; // expected-error {{read-only variable is not assignable}}
66ae916a14cf727b4ce3ac316f4fd780d1c83c5bafEli Friedman  *(test0 ? nonconst_int : const_int) = 42; // expected-error {{read-only variable is not assignable}}
67ae916a14cf727b4ce3ac316f4fd780d1c83c5bafEli Friedman
68ae916a14cf727b4ce3ac316f4fd780d1c83c5bafEli Friedman  // The composite type here should be "int (*)[12]", fine for the sizeof
69ae916a14cf727b4ce3ac316f4fd780d1c83c5bafEli Friedman  int (*incomplete)[];
70ae916a14cf727b4ce3ac316f4fd780d1c83c5bafEli Friedman  int (*complete)[12];
71ae916a14cf727b4ce3ac316f4fd780d1c83c5bafEli Friedman  sizeof(*(test0 ? incomplete : complete)); // expected-warning {{expression result unused}}
72ae916a14cf727b4ce3ac316f4fd780d1c83c5bafEli Friedman  sizeof(*(test0 ? complete : incomplete)); // expected-warning {{expression result unused}}
73ae916a14cf727b4ce3ac316f4fd780d1c83c5bafEli Friedman
74ae916a14cf727b4ce3ac316f4fd780d1c83c5bafEli Friedman  int __attribute__((address_space(2))) *adr2;
75ae916a14cf727b4ce3ac316f4fd780d1c83c5bafEli Friedman  int __attribute__((address_space(3))) *adr3;
76ae916a14cf727b4ce3ac316f4fd780d1c83c5bafEli Friedman  test0 ? adr2 : adr3; // expected-warning {{pointer type mismatch}} expected-warning {{expression result unused}}
77ae916a14cf727b4ce3ac316f4fd780d1c83c5bafEli Friedman
78ae916a14cf727b4ce3ac316f4fd780d1c83c5bafEli Friedman  // Make sure address-space mask ends up in the result type
79ae916a14cf727b4ce3ac316f4fd780d1c83c5bafEli Friedman  (test0 ? (test0 ? adr2 : adr2) : nonconst_int); // expected-warning {{pointer type mismatch}} expected-warning {{expression result unused}}
80b6d54e56a5c65c2728080578b84374c3c594daecSteve Naroff}
81b6d54e56a5c65c2728080578b84374c3c594daecSteve Naroff
82e701c0a953d05c3403a74fdb449a8f4a1e4e6594Steve Naroffint Postgresql() {
83e701c0a953d05c3403a74fdb449a8f4a1e4e6594Steve Naroff  char x;
84e701c0a953d05c3403a74fdb449a8f4a1e4e6594Steve Naroff  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}}
85e701c0a953d05c3403a74fdb449a8f4a1e4e6594Steve Naroff}
869158804749356f88be8c5a3bade75b761846273cSteve Naroff
879158804749356f88be8c5a3bade75b761846273cSteve Naroff#define nil ((void*) 0)
889158804749356f88be8c5a3bade75b761846273cSteve Naroff
899158804749356f88be8c5a3bade75b761846273cSteve Naroffextern int f1(void);
909158804749356f88be8c5a3bade75b761846273cSteve Naroff
919158804749356f88be8c5a3bade75b761846273cSteve Naroffint f0(int a) {
929158804749356f88be8c5a3bade75b761846273cSteve Naroff  // GCC considers this a warning.
93d4eea8362605807327735727a9098abe1eb23b19Douglas Gregor  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'}}
949158804749356f88be8c5a3bade75b761846273cSteve Naroff}
95323ed74658bc8375278eabf074b4777458376540John McCall
96323ed74658bc8375278eabf074b4777458376540John McCallint f2(int x) {
97323ed74658bc8375278eabf074b4777458376540John McCall  // We can suppress this because the immediate context wants an int.
98323ed74658bc8375278eabf074b4777458376540John McCall  return (x != 0) ? 0U : x;
99323ed74658bc8375278eabf074b4777458376540John McCall}
10082214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth
10182214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth#define NULL (void*)0
10282214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth
10382214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruthvoid PR9236() {
10482214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth  struct A {int i;} A1;
10582214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth  (void)(1 ? A1 : NULL); // expected-error{{non-pointer operand type 'struct A' incompatible with NULL}}
10682214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth  (void)(1 ? NULL : A1); // expected-error{{non-pointer operand type 'struct A' incompatible with NULL}}
10782214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth  (void)(1 ? 0 : A1); // expected-error{{incompatible operand types}}
10882214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth  (void)(1 ? (void*)0 : A1); // expected-error{{incompatible operand types}}
10982214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth  (void)(1 ? A1: (void*)0); // expected-error{{incompatible operand types}}
11082214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth  (void)(1 ? A1 : (NULL)); // expected-error{{non-pointer operand type 'struct A' incompatible with NULL}}
11182214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth}
11282214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth
113