const-eval.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // RUN: %clang_cc1 -fsyntax-only -verify -triple i686-linux %s -Wno-tautological-pointer-compare
  2. #define EVAL_EXPR(testno, expr) int test##testno = sizeof(struct{char qq[expr];});
  3. int x;
  4. EVAL_EXPR(1, (_Bool)&x)
  5. EVAL_EXPR(2, (int)(1.0+(double)4))
  6. EVAL_EXPR(3, (int)(1.0+(float)4.0))
  7. EVAL_EXPR(4, (_Bool)(1 ? (void*)&x : 0))
  8. EVAL_EXPR(5, (_Bool)(int[]){0})
  9. struct y {int x,y;};
  10. EVAL_EXPR(6, (int)(1+(struct y*)0))
  11. EVAL_EXPR(7, (int)&((struct y*)0)->y)
  12. EVAL_EXPR(8, (_Bool)"asdf")
  13. EVAL_EXPR(9, !!&x)
  14. EVAL_EXPR(10, ((void)1, 12))
  15. void g0(void);
  16. EVAL_EXPR(11, (g0(), 12)) // expected-error {{must have a constant size}}
  17. EVAL_EXPR(12, 1.0&&2.0)
  18. EVAL_EXPR(13, x || 3.0) // expected-error {{must have a constant size}}
  19. unsigned int l_19 = 1;
  20. EVAL_EXPR(14, (1 ^ l_19) && 1); // expected-error {{fields must have a constant size}}
  21. void f()
  22. {
  23. int a;
  24. EVAL_EXPR(15, (_Bool)&a);
  25. }
  26. // FIXME: Turn into EVAL_EXPR test once we have more folding.
  27. _Complex float g16 = (1.0f + 1.0fi);
  28. // ?: in constant expressions.
  29. int g17[(3?:1) - 2];
  30. EVAL_EXPR(18, ((int)((void*)10 + 10)) == 20 ? 1 : -1);
  31. struct s {
  32. int a[(int)-1.0f]; // expected-error {{'a' declared as an array with a negative size}}
  33. };
  34. EVAL_EXPR(19, ((int)&*(char*)10 == 10 ? 1 : -1));
  35. EVAL_EXPR(20, __builtin_constant_p(*((int*) 10)));
  36. EVAL_EXPR(21, (__imag__ 2i) == 2 ? 1 : -1);
  37. EVAL_EXPR(22, (__real__ (2i+3)) == 3 ? 1 : -1);
  38. int g23[(int)(1.0 / 1.0)] = { 1 };
  39. int g24[(int)(1.0 / 1.0)] = { 1 , 2 }; // expected-warning {{excess elements in array initializer}}
  40. int g25[(int)(1.0 + 1.0)], g26 = sizeof(g25);
  41. EVAL_EXPR(26, (_Complex double)0 ? -1 : 1)
  42. EVAL_EXPR(27, (_Complex int)0 ? -1 : 1)
  43. EVAL_EXPR(28, (_Complex double)1 ? 1 : -1)
  44. EVAL_EXPR(29, (_Complex int)1 ? 1 : -1)
  45. // PR4027 + rdar://6808859
  46. struct a { int x, y; };
  47. static struct a V2 = (struct a)(struct a){ 1, 2};
  48. static const struct a V1 = (struct a){ 1, 2};
  49. EVAL_EXPR(30, (int)(_Complex float)((1<<30)-1) == (1<<30) ? 1 : -1)
  50. EVAL_EXPR(31, (int*)0 == (int*)0 ? 1 : -1)
  51. EVAL_EXPR(32, (int*)0 != (int*)0 ? -1 : 1)
  52. EVAL_EXPR(33, (void*)0 - (void*)0 == 0 ? 1 : -1)
  53. void foo(void) {}
  54. EVAL_EXPR(34, (foo == (void *)0) ? -1 : 1)
  55. // No PR. Mismatched bitwidths lead to a crash on second evaluation.
  56. const _Bool constbool = 0;
  57. EVAL_EXPR(35, constbool)
  58. EVAL_EXPR(36, constbool)
  59. EVAL_EXPR(37, (1,2.0) == 2.0 ? 1 : -1)
  60. EVAL_EXPR(38, __builtin_expect(1,1) == 1 ? 1 : -1)
  61. // PR7884
  62. EVAL_EXPR(39, __real__(1.f) == 1 ? 1 : -1)
  63. EVAL_EXPR(40, __imag__(1.f) == 0 ? 1 : -1)
  64. // From gcc testsuite
  65. EVAL_EXPR(41, (int)(1+(_Complex unsigned)2))
  66. // rdar://8875946
  67. void rdar8875946() {
  68. double _Complex P;
  69. float _Complex P2 = 3.3f + P;
  70. }
  71. double d = (d = 0.0); // expected-error {{not a compile-time constant}}
  72. double d2 = ++d; // expected-error {{not a compile-time constant}}
  73. int n = 2;
  74. int intLvalue[*(int*)((long)&n ?: 1)] = { 1, 2 }; // expected-error {{variable length array}}
  75. union u { int a; char b[4]; };
  76. char c = ((union u)(123456)).b[0]; // expected-error {{not a compile-time constant}}
  77. extern const int weak_int __attribute__((weak));
  78. const int weak_int = 42;
  79. int weak_int_test = weak_int; // expected-error {{not a compile-time constant}}
  80. int literalVsNull1 = "foo" == 0;
  81. int literalVsNull2 = 0 == "foo";
  82. // PR11385.
  83. int castViaInt[*(int*)(unsigned long)"test"]; // expected-error {{variable length array}}
  84. // PR11391.
  85. struct PR11391 { _Complex float f; } pr11391;
  86. EVAL_EXPR(42, __builtin_constant_p(pr11391.f = 1))
  87. // PR12043
  88. float varfloat;
  89. const float constfloat = 0;
  90. EVAL_EXPR(43, varfloat && constfloat) // expected-error {{must have a constant size}}
  91. // <rdar://problem/11205586>
  92. // (Make sure we continue to reject this.)
  93. EVAL_EXPR(44, "x"[0]); // expected-error {{variable length array}}
  94. // <rdar://problem/10962435>
  95. EVAL_EXPR(45, ((char*)-1) + 1 == 0 ? 1 : -1)
  96. EVAL_EXPR(46, ((char*)-1) + 1 < (char*) -1 ? 1 : -1)
  97. EVAL_EXPR(47, &x < &x + 1 ? 1 : -1)
  98. EVAL_EXPR(48, &x != &x - 1 ? 1 : -1)
  99. EVAL_EXPR(49, &x < &x - 100 ? 1 : -1) // expected-error {{must have a constant size}}
  100. extern struct Test50S Test50;
  101. EVAL_EXPR(50, &Test50 < (struct Test50S*)((unsigned)&Test50 + 10)) // expected-error {{must have a constant size}}
  102. // <rdar://problem/11874571>
  103. EVAL_EXPR(51, 0 != (float)1e99)
  104. // PR21945
  105. void PR21945() { int i = (({}), 0l); }