init.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. // RUN: %clang_cc1 %s -verify -fsyntax-only -ffreestanding
  2. #include <stddef.h>
  3. #include <stdint.h>
  4. typedef void (* fp)(void);
  5. void foo(void);
  6. // PR clang/3377
  7. fp a[(short int)1] = { foo };
  8. int myArray[5] = {1, 2, 3, 4, 5};
  9. int *myPointer2 = myArray;
  10. int *myPointer = &(myArray[2]);
  11. extern int x;
  12. void *g = &x;
  13. int *h = &x;
  14. struct union_crash
  15. {
  16. union
  17. {
  18. };
  19. };
  20. int test() {
  21. int a[10];
  22. int b[10] = a; // expected-error {{array initializer must be an initializer list}}
  23. int +; // expected-error {{expected identifier or '('}}
  24. struct union_crash u = { .d = 1 }; // expected-error {{field designator 'd' does not refer to any field in type 'struct union_crash'}}
  25. }
  26. // PR2050
  27. struct cdiff_cmd {
  28. const char *name;
  29. unsigned short argc;
  30. int (*handler)();
  31. };
  32. int cdiff_cmd_open();
  33. struct cdiff_cmd commands[] = {
  34. {"OPEN", 1, &cdiff_cmd_open }
  35. };
  36. // PR2348
  37. static struct { int z; } s[2];
  38. int *t = &(*s).z;
  39. // PR2349
  40. short *a2(void)
  41. {
  42. short int b;
  43. static short *bp = &b; // expected-error {{initializer element is not a compile-time constant}}
  44. return bp;
  45. }
  46. int pbool(void) {
  47. typedef const _Bool cbool;
  48. _Bool pbool1 = (void *) 0;
  49. cbool pbool2 = &pbool;
  50. return pbool2;
  51. }
  52. // rdar://5870981
  53. union { float f; unsigned u; } u = { 1.0f };
  54. // rdar://6156694
  55. int f3(int x) { return x; }
  56. typedef void (*vfunc)(void);
  57. void *bar = (vfunc) f3;
  58. // PR2747
  59. struct sym_reg {
  60. char nc_gpreg;
  61. };
  62. int sym_fw1a_scr[] = {
  63. ((int)(&((struct sym_reg *)0)->nc_gpreg)) & 0,
  64. 8 * ((int)(&((struct sym_reg *)0)->nc_gpreg))
  65. };
  66. // PR3001
  67. struct s1 s2 = { // expected-error {{variable has incomplete type 'struct s1'}} \
  68. // expected-note {{forward declaration of 'struct s1'}}
  69. .a = sizeof(struct s3), // expected-error {{invalid application of 'sizeof'}} \
  70. // expected-note{{forward declaration of 'struct s3'}}
  71. .b = bogus // expected-error {{use of undeclared identifier 'bogus'}}
  72. }
  73. // PR3382
  74. char t[] = ("Hello");
  75. // <rdar://problem/6094855>
  76. typedef struct { } empty;
  77. typedef struct {
  78. empty e;
  79. int i2;
  80. } st;
  81. st st1 = { .i2 = 1 };
  82. // <rdar://problem/6096826>
  83. struct {
  84. int a;
  85. int z[2];
  86. } y = { .z = {} };
  87. int bbb[10];
  88. struct foo2 {
  89. uintptr_t a;
  90. };
  91. struct foo2 bar2[] = {
  92. { (intptr_t)bbb }
  93. };
  94. struct foo2 bar3 = { 1, 2 }; // expected-warning{{excess elements in struct initializer}}
  95. int* ptest1 = __builtin_choose_expr(1, (int*)0, (int*)0);
  96. typedef int32_t ivector4 __attribute((vector_size(16)));
  97. ivector4 vtest1 = 1 ? (ivector4){1} : (ivector4){1};
  98. ivector4 vtest2 = __builtin_choose_expr(1, (ivector4){1}, (ivector4){1});
  99. uintptr_t ptrasintadd1 = (uintptr_t)&a - 4;
  100. uintptr_t ptrasintadd2 = (uintptr_t)&a + 4;
  101. uintptr_t ptrasintadd3 = 4 + (uintptr_t)&a;
  102. // PR4285
  103. const wchar_t widestr[] = L"asdf";
  104. // PR5447
  105. const double pr5447 = (0.05 < -1.0) ? -1.0 : 0.0499878;
  106. // PR4386
  107. // None of these are constant initializers, but we implement GCC's old
  108. // behaviour of accepting bar and zed but not foo. GCC's behaviour was
  109. // changed in 2007 (rev 122551), so we should be able to change too one
  110. // day.
  111. int PR4386_bar();
  112. int PR4386_foo() __attribute((weak));
  113. int PR4386_zed();
  114. int PR4386_a = ((void *) PR4386_bar) != 0;
  115. int PR4386_b = ((void *) PR4386_foo) != 0; // expected-error{{initializer element is not a compile-time constant}}
  116. int PR4386_c = ((void *) PR4386_zed) != 0;
  117. int PR4386_zed() __attribute((weak));
  118. // <rdar://problem/10185490> (derived from SPEC vortex benchmark)
  119. typedef char strty[10];
  120. struct vortexstruct { strty s; };
  121. struct vortexstruct vortexvar = { "asdf" };
  122. typedef struct { uintptr_t x : 2; } StructWithBitfield;
  123. StructWithBitfield bitfieldvar = { (uintptr_t)&bitfieldvar }; // expected-error {{initializer element is not a compile-time constant}}