PageRenderTime 108ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 1ms

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

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