PageRenderTime 51ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/gcc/testsuite/gcc.dg/bf-no-ms-layout.c

https://gitlab.com/4144/gcc
C | 231 lines | 184 code | 38 blank | 9 comment | 18 complexity | 7f07619c88610f37cf243e4e9548534f MD5 | raw file
  1. /* bf-no-ms-layout.c */
  2. /* Test for gcc bitfield layout, with -mno-ms-bitfields */
  3. /* Adapted from Donn Terry <donnte@microsoft.com> testcase
  4. posted to GCC-patches
  5. http://gcc.gnu.org/ml/gcc-patches/2000-08/msg00577.html */
  6. /* { dg-do run { target *-*-mingw* *-*-cygwin* i?86-*-darwin } } */
  7. /* { dg-options "-mno-ms-bitfields" } */
  8. #include <stddef.h>
  9. #include <string.h>
  10. extern void abort();
  11. #pragma pack(8)
  12. struct one {
  13. int d;
  14. unsigned char a;
  15. unsigned short b:7;
  16. char c;
  17. };
  18. struct two {
  19. int d;
  20. unsigned char a;
  21. unsigned int b:7;
  22. char c;
  23. };
  24. struct three {
  25. short d;
  26. unsigned short a:3;
  27. unsigned short b:9;
  28. unsigned char c:7;
  29. };
  30. /* Bitfields of size 0 have some truly odd behaviors. */
  31. struct four {
  32. unsigned short a:3;
  33. unsigned short b:9;
  34. unsigned int :0; /* forces struct alignment to int */
  35. unsigned char c:7;
  36. };
  37. struct five {
  38. char a;
  39. int :0; /* ignored; prior field is not a bitfield. */
  40. char b;
  41. char c;
  42. };
  43. struct six {
  44. char a :8;
  45. int :0; /* not ignored; prior field IS a bitfield, causes
  46. struct alignment as well. */
  47. char b;
  48. char c;
  49. } ;
  50. struct seven {
  51. char a:8;
  52. char :0;
  53. int :0; /* Ignored; prior field is zero size bitfield. */
  54. char b;
  55. char c;
  56. };
  57. struct eight { /* ms size 4 */
  58. short b:3;
  59. char c;
  60. };
  61. #ifdef _MSC_VER
  62. #define LONGLONG __int64
  63. #else
  64. #define LONGLONG long long
  65. #endif
  66. union nine { /* ms size 8 */
  67. LONGLONG a:3;
  68. char c;
  69. };
  70. struct ten { /* ms size 16 */
  71. LONGLONG a:3;
  72. LONGLONG b:3;
  73. char c;
  74. };
  75. #define val(s,f) (s.f)
  76. #define check_struct(_X) \
  77. { \
  78. if (sizeof (struct _X) != exp_sizeof_##_X ) \
  79. abort(); \
  80. memcpy(&test_##_X, filler, sizeof(test_##_X));\
  81. if (val(test_##_X,c) != exp_##_X##_c) \
  82. abort(); \
  83. }
  84. #define check_union(_X) \
  85. { \
  86. if (sizeof (union _X) != exp_sizeof_##_X ) \
  87. abort(); \
  88. memcpy(&test_##_X, filler, sizeof(test_##_X));\
  89. if (val(test_##_X,c) != exp_##_X##_c) \
  90. abort(); \
  91. }
  92. #define check_struct_size(_X) \
  93. { \
  94. if (sizeof (struct _X) != exp_sizeof_##_X ) \
  95. abort(); \
  96. }
  97. #define check_struct_off(_X) \
  98. { \
  99. memcpy(&test_##_X, filler, sizeof(test_##_X));\
  100. if (val(test_##_X,c) != exp_##_X##_c) \
  101. abort(); \
  102. }
  103. #define check_union_size(_X) \
  104. { \
  105. if (sizeof (union _X) != exp_sizeof_##_X ) \
  106. abort(); \
  107. }
  108. #define check_union_off(_X) \
  109. { \
  110. memcpy(&test_##_X, filler, sizeof(test_##_X));\
  111. if (val(test_##_X,c) != exp_##_X##_c) \
  112. abort(); \
  113. }
  114. int main(){
  115. unsigned char filler[16];
  116. struct one test_one;
  117. struct two test_two;
  118. struct three test_three;
  119. struct four test_four;
  120. struct five test_five;
  121. struct six test_six;
  122. struct seven test_seven;
  123. struct eight test_eight;
  124. union nine test_nine;
  125. struct ten test_ten;
  126. #if defined (_TEST_MS_LAYOUT) || defined (_MSC_VER)
  127. size_t exp_sizeof_one = 12;
  128. size_t exp_sizeof_two = 16;
  129. size_t exp_sizeof_three =6;
  130. size_t exp_sizeof_four = 8;
  131. size_t exp_sizeof_five = 3;
  132. size_t exp_sizeof_six = 8;
  133. size_t exp_sizeof_seven = 3;
  134. size_t exp_sizeof_eight = 4;
  135. size_t exp_sizeof_nine = 8;
  136. size_t exp_sizeof_ten = 16;
  137. unsigned char exp_one_c = 8;
  138. unsigned char exp_two_c = 12;
  139. unsigned char exp_three_c = 4;
  140. unsigned char exp_four_c = 4;
  141. char exp_five_c = 2;
  142. char exp_six_c = 5;
  143. char exp_seven_c = 2;
  144. char exp_eight_c = 2;
  145. char exp_nine_c = 0;
  146. char exp_ten_c = 8;
  147. #else /* testing -mno-ms-bitfields */
  148. size_t exp_sizeof_one = 8;
  149. size_t exp_sizeof_two = 8;
  150. size_t exp_sizeof_three = 6;
  151. size_t exp_sizeof_four = 6;
  152. size_t exp_sizeof_five = 6;
  153. size_t exp_sizeof_six = 6;
  154. size_t exp_sizeof_seven = 6;
  155. size_t exp_sizeof_eight = 2;
  156. size_t exp_sizeof_nine = 8;
  157. size_t exp_sizeof_ten = 8;
  158. unsigned short exp_one_c = 6;
  159. unsigned int exp_two_c = 6;
  160. unsigned char exp_three_c = 64;
  161. unsigned char exp_four_c = 4;
  162. char exp_five_c = 5;
  163. char exp_six_c = 5;
  164. char exp_seven_c = 5;
  165. char exp_eight_c = 1;
  166. char exp_nine_c = 0;
  167. char exp_ten_c = 1;
  168. #endif
  169. unsigned char i;
  170. for ( i = 0; i < 16; i++ )
  171. filler[i] = i;
  172. check_struct_off (one);
  173. check_struct_off (two);
  174. check_struct_off (three);
  175. check_struct_off (four);
  176. check_struct_off (five);
  177. check_struct_off (six);
  178. check_struct_off (seven);
  179. check_struct_off (eight);
  180. check_union_off (nine);
  181. check_struct_off (ten);
  182. check_struct_size (one);
  183. check_struct_size (two);
  184. check_struct_size (three);
  185. check_struct_size (four);
  186. check_struct_size (five);
  187. check_struct_size (six);
  188. check_struct_size (seven);
  189. check_struct_size (eight);
  190. check_union_size (nine);
  191. check_struct_size (ten);
  192. return 0;
  193. };