struct-packed-align.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // RUN: %clang_cc1 %s -fsyntax-only -verify
  2. // expected-no-diagnostics
  3. // Packed structs.
  4. struct s {
  5. char a;
  6. int b __attribute__((packed));
  7. char c;
  8. int d;
  9. };
  10. extern int a1[sizeof(struct s) == 12 ? 1 : -1];
  11. extern int a2[__alignof(struct s) == 4 ? 1 : -1];
  12. struct __attribute__((packed)) packed_s {
  13. char a;
  14. int b __attribute__((packed));
  15. char c;
  16. int d;
  17. };
  18. extern int b1[sizeof(struct packed_s) == 10 ? 1 : -1];
  19. extern int b2[__alignof(struct packed_s) == 1 ? 1 : -1];
  20. struct fas {
  21. char a;
  22. int b[];
  23. };
  24. extern int c1[sizeof(struct fas) == 4 ? 1 : -1];
  25. extern int c2[__alignof(struct fas) == 4 ? 1 : -1];
  26. struct __attribute__((packed)) packed_fas {
  27. char a;
  28. int b[];
  29. };
  30. extern int d1[sizeof(struct packed_fas) == 1 ? 1 : -1];
  31. extern int d2[__alignof(struct packed_fas) == 1 ? 1 : -1];
  32. struct packed_after_fas {
  33. char a;
  34. int b[];
  35. } __attribute__((packed));
  36. extern int d1_2[sizeof(struct packed_after_fas) == 1 ? 1 : -1];
  37. extern int d2_2[__alignof(struct packed_after_fas) == 1 ? 1 : -1];
  38. // Alignment
  39. struct __attribute__((aligned(8))) as1 {
  40. char c;
  41. };
  42. extern int e1[sizeof(struct as1) == 8 ? 1 : -1];
  43. extern int e2[__alignof(struct as1) == 8 ? 1 : -1];
  44. struct __attribute__((aligned)) as1_2 {
  45. char c;
  46. };
  47. #ifdef __s390x__
  48. extern int e1_2[sizeof(struct as1_2) == 8 ? 1 : -1];
  49. extern int e2_2[__alignof(struct as1_2) == 8 ? 1 : -1];
  50. #else
  51. extern int e1_2[sizeof(struct as1_2) == 16 ? 1 : -1];
  52. extern int e2_2[__alignof(struct as1_2) == 16 ? 1 : -1];
  53. #endif
  54. struct as2 {
  55. char c;
  56. int __attribute__((aligned(8))) a;
  57. };
  58. extern int f1[sizeof(struct as2) == 16 ? 1 : -1];
  59. extern int f2[__alignof(struct as2) == 8 ? 1 : -1];
  60. struct __attribute__((packed)) as3 {
  61. char c;
  62. int a;
  63. int __attribute__((aligned(8))) b;
  64. };
  65. extern int g1[sizeof(struct as3) == 16 ? 1 : -1];
  66. extern int g2[__alignof(struct as3) == 8 ? 1 : -1];
  67. // rdar://5921025
  68. struct packedtest {
  69. int ted_likes_cheese;
  70. void *args[] __attribute__((packed));
  71. };
  72. // Packed union
  73. union __attribute__((packed)) au4 {char c; int x;};
  74. extern int h1[sizeof(union au4) == 4 ? 1 : -1];
  75. extern int h2[__alignof(union au4) == 1 ? 1 : -1];
  76. // Aligned union
  77. union au5 {__attribute__((aligned(4))) char c;};
  78. extern int h1[sizeof(union au5) == 4 ? 1 : -1];
  79. extern int h2[__alignof(union au5) == 4 ? 1 : -1];
  80. // Alignment+packed
  81. struct as6 {char c; __attribute__((packed, aligned(2))) int x;};
  82. extern int i1[sizeof(struct as6) == 6 ? 1 : -1];
  83. extern int i2[__alignof(struct as6) == 2 ? 1 : -1];
  84. union au6 {char c; __attribute__((packed, aligned(2))) int x;};
  85. extern int k1[sizeof(union au6) == 4 ? 1 : -1];
  86. extern int k2[__alignof(union au6) == 2 ? 1 : -1];
  87. // Check postfix attributes
  88. union au7 {char c; int x;} __attribute__((packed));
  89. extern int l1[sizeof(union au7) == 4 ? 1 : -1];
  90. extern int l2[__alignof(union au7) == 1 ? 1 : -1];
  91. struct packed_fas2 {
  92. char a;
  93. int b[];
  94. } __attribute__((packed));
  95. extern int m1[sizeof(struct packed_fas2) == 1 ? 1 : -1];
  96. extern int m2[__alignof(struct packed_fas2) == 1 ? 1 : -1];
  97. // Attribute aligned can round down typedefs. PR9253
  98. typedef long long __attribute__((aligned(1))) nt;
  99. struct nS {
  100. char buf_nr;
  101. nt start_lba;
  102. };
  103. #if defined(_WIN32) && !defined(__declspec) // _MSC_VER is unavailable in cc1.
  104. // Alignment doesn't affect packing in MS mode.
  105. extern int n1[sizeof(struct nS) == 16 ? 1 : -1];
  106. extern int n2[__alignof(struct nS) == 8 ? 1 : -1];
  107. #else
  108. extern int n1[sizeof(struct nS) == 9 ? 1 : -1];
  109. extern int n2[__alignof(struct nS) == 1 ? 1 : -1];
  110. #endif