conditional-expr.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // RUN: %clang_cc1 -fsyntax-only -verify -pedantic -Wsign-conversion %s
  2. void foo() {
  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;
  13. dp = vp;
  14. vp = dp;
  15. ip = dp; // expected-warning {{incompatible pointer types assigning to 'int *' from 'double *'}}
  16. dp = ip; // expected-warning {{incompatible pointer types assigning to 'double *' from 'int *'}}
  17. dp = 0 ? (double *)0 : (void *)0;
  18. vp = 0 ? (double *)0 : (void *)0;
  19. ip = 0 ? (double *)0 : (void *)0; // expected-warning {{incompatible pointer types assigning to 'int *' from 'double *'}}
  20. const int *cip;
  21. vp = (0 ? vp : cip); // expected-warning {{discards qualifiers}}
  22. vp = (0 ? cip : vp); // expected-warning {{discards qualifiers}}
  23. int i = 2;
  24. int (*pf)[2];
  25. int (*pv)[i];
  26. pf = (i ? pf : pv);
  27. enum {xxx,yyy,zzz} e, *ee;
  28. short x;
  29. ee = ee ? &x : ee ? &i : &e; // expected-warning {{pointer type mismatch}}
  30. typedef void *asdf;
  31. *(0 ? (asdf) 0 : &x) = 10;
  32. unsigned long test0 = 5;
  33. test0 = test0 ? (long) test0 : test0; // expected-warning {{operand of ? changes signedness: 'long' to 'unsigned long'}}
  34. test0 = test0 ? (int) test0 : test0; // expected-warning {{operand of ? changes signedness: 'int' to 'unsigned long'}}
  35. test0 = test0 ? (short) test0 : test0; // expected-warning {{operand of ? changes signedness: 'short' to 'unsigned long'}}
  36. test0 = test0 ? test0 : (long) test0; // expected-warning {{operand of ? changes signedness: 'long' to 'unsigned long'}}
  37. test0 = test0 ? test0 : (int) test0; // expected-warning {{operand of ? changes signedness: 'int' to 'unsigned long'}}
  38. test0 = test0 ? test0 : (short) test0; // expected-warning {{operand of ? changes signedness: 'short' to 'unsigned long'}}
  39. test0 = test0 ? test0 : (long) 10;
  40. test0 = test0 ? test0 : (int) 10;
  41. test0 = test0 ? test0 : (short) 10;
  42. test0 = test0 ? (long) 10 : test0;
  43. test0 = test0 ? (int) 10 : test0;
  44. test0 = test0 ? (short) 10 : test0;
  45. int test1;
  46. enum Enum { EVal };
  47. test0 = test0 ? EVal : test0;
  48. test1 = test0 ? EVal : (int) test0;
  49. test0 = test0 ?
  50. (unsigned) EVal
  51. : (int) test0; // expected-warning {{operand of ? changes signedness: 'int' to 'unsigned long'}}
  52. test0 = test0 ? EVal : test1; // expected-warning {{operand of ? changes signedness: 'int' to 'unsigned long'}}
  53. test0 = test0 ? test1 : EVal; // expected-warning {{operand of ? changes signedness: 'int' to 'unsigned long'}}
  54. const int *const_int;
  55. int *nonconst_int;
  56. *(test0 ? const_int : nonconst_int) = 42; // expected-error {{read-only variable is not assignable}}
  57. *(test0 ? nonconst_int : const_int) = 42; // expected-error {{read-only variable is not assignable}}
  58. // The composite type here should be "int (*)[12]", fine for the sizeof
  59. int (*incomplete)[];
  60. int (*complete)[12];
  61. sizeof(*(test0 ? incomplete : complete)); // expected-warning {{expression result unused}}
  62. sizeof(*(test0 ? complete : incomplete)); // expected-warning {{expression result unused}}
  63. int __attribute__((address_space(2))) *adr2;
  64. int __attribute__((address_space(3))) *adr3;
  65. test0 ? adr2 : adr3; // expected-warning {{pointer type mismatch}} expected-warning {{expression result unused}}
  66. // Make sure address-space mask ends up in the result type
  67. (test0 ? (test0 ? adr2 : adr2) : nonconst_int); // expected-warning {{pointer type mismatch}} expected-warning {{expression result unused}}
  68. }
  69. int Postgresql() {
  70. char x;
  71. 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}}
  72. }
  73. #define nil ((void*) 0)
  74. extern int f1(void);
  75. int f0(int a) {
  76. // GCC considers this a warning.
  77. 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'}}
  78. }
  79. int f2(int x) {
  80. // We can suppress this because the immediate context wants an int.
  81. return (x != 0) ? 0U : x;
  82. }
  83. #define NULL (void*)0
  84. void PR9236() {
  85. struct A {int i;} A1;
  86. (void)(1 ? A1 : NULL); // expected-error{{non-pointer operand type 'struct A' incompatible with NULL}}
  87. (void)(1 ? NULL : A1); // expected-error{{non-pointer operand type 'struct A' incompatible with NULL}}
  88. (void)(1 ? 0 : A1); // expected-error{{incompatible operand types}}
  89. (void)(1 ? (void*)0 : A1); // expected-error{{incompatible operand types}}
  90. (void)(1 ? A1: (void*)0); // expected-error{{incompatible operand types}}
  91. (void)(1 ? A1 : (NULL)); // expected-error{{non-pointer operand type 'struct A' incompatible with NULL}}
  92. }