conditional-expr.c revision b13c87f0c9705d91d5a3e134be9934c9ad531071
1d7d5f0223bd30dfd618762349c6209dd1d5ea3e6Daniel Dunbar// RUN: clang-cc -fsyntax-only -verify -pedantic %s 2b6d54e56a5c65c2728080578b84374c3c594daecSteve Naroffvoid foo() { 3b6d54e56a5c65c2728080578b84374c3c594daecSteve Naroff *(0 ? (double *)0 : (void *)0) = 0; 4890d93eec45f2ba720dce98e34a1a904697ae842Steve Naroff // FIXME: GCC doesn't consider the 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; 16aaffbf7c790a324ed114184db771aae2d2e9151cSteve Naroff ip = dp; // expected-warning {{incompatible pointer types assigning 'double *', expected 'int *'}} 17aaffbf7c790a324ed114184db771aae2d2e9151cSteve Naroff dp = ip; // expected-warning {{incompatible pointer types assigning 'int *', expected 'double *'}} 18b6d54e56a5c65c2728080578b84374c3c594daecSteve Naroff dp = 0 ? (double *)0 : (void *)0; 19b6d54e56a5c65c2728080578b84374c3c594daecSteve Naroff vp = 0 ? (double *)0 : (void *)0; 20aaffbf7c790a324ed114184db771aae2d2e9151cSteve Naroff ip = 0 ? (double *)0 : (void *)0; // expected-warning {{incompatible pointer types assigning 'double *', expected 'int *'}} 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; 39b13c87f0c9705d91d5a3e134be9934c9ad531071John McCall test0 = test0 ? (long) test0 : test0; // expected-warning {{operands of ? are integers of different signs}} 40b13c87f0c9705d91d5a3e134be9934c9ad531071John McCall test0 = test0 ? (int) test0 : test0; // expected-warning {{operands of ? are integers of different signs}} 41b13c87f0c9705d91d5a3e134be9934c9ad531071John McCall test0 = test0 ? (short) test0 : test0; // expected-warning {{operands of ? are integers of different signs}} 42b13c87f0c9705d91d5a3e134be9934c9ad531071John McCall test0 = test0 ? test0 : (long) test0; // expected-warning {{operands of ? are integers of different signs}} 43b13c87f0c9705d91d5a3e134be9934c9ad531071John McCall test0 = test0 ? test0 : (int) test0; // expected-warning {{operands of ? are integers of different signs}} 44b13c87f0c9705d91d5a3e134be9934c9ad531071John McCall test0 = test0 ? test0 : (short) test0; // expected-warning {{operands of ? are integers of different signs}} 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 52b13c87f0c9705d91d5a3e134be9934c9ad531071John McCall enum Enum { EVal }; 53b13c87f0c9705d91d5a3e134be9934c9ad531071John McCall test0 = test0 ? EVal : test0; 54b13c87f0c9705d91d5a3e134be9934c9ad531071John McCall test0 = test0 ? EVal : (int) test0; // okay: EVal is an int 55b13c87f0c9705d91d5a3e134be9934c9ad531071John McCall test0 = test0 ? (unsigned) EVal : (int) test0; // expected-warning {{operands of ? are integers of different signs}} 56b6d54e56a5c65c2728080578b84374c3c594daecSteve Naroff} 57b6d54e56a5c65c2728080578b84374c3c594daecSteve Naroff 58e701c0a953d05c3403a74fdb449a8f4a1e4e6594Steve Naroffint Postgresql() { 59e701c0a953d05c3403a74fdb449a8f4a1e4e6594Steve Naroff char x; 60e701c0a953d05c3403a74fdb449a8f4a1e4e6594Steve 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}} 61e701c0a953d05c3403a74fdb449a8f4a1e4e6594Steve Naroff} 629158804749356f88be8c5a3bade75b761846273cSteve Naroff 639158804749356f88be8c5a3bade75b761846273cSteve Naroff#define nil ((void*) 0) 649158804749356f88be8c5a3bade75b761846273cSteve Naroff 659158804749356f88be8c5a3bade75b761846273cSteve Naroffextern int f1(void); 669158804749356f88be8c5a3bade75b761846273cSteve Naroff 679158804749356f88be8c5a3bade75b761846273cSteve Naroffint f0(int a) { 689158804749356f88be8c5a3bade75b761846273cSteve Naroff // GCC considers this a warning. 699158804749356f88be8c5a3bade75b761846273cSteve Naroff 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 *', expected 'int'}} 709158804749356f88be8c5a3bade75b761846273cSteve Naroff} 71