92_enum_bitfield.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /* This checks if enums needing 8 bit but only having positive
  2. values are correctly zero extended (instead of sign extended)
  3. when stored into/loaded from a 8 bit bit-field of enum type (which
  4. itself is implementation defined, so isn't necessarily supported by all
  5. other compilers). */
  6. enum tree_code {
  7. SOME_CODE = 148, /* has bit 7 set, and hence all further enum values as well */
  8. LAST_AND_UNUSED_TREE_CODE
  9. };
  10. typedef union tree_node *tree;
  11. struct tree_common
  12. {
  13. union tree_node *chain;
  14. union tree_node *type;
  15. enum tree_code code : 8;
  16. unsigned side_effects_flag : 1;
  17. };
  18. union tree_node
  19. {
  20. struct tree_common common;
  21. };
  22. enum c_tree_code {
  23. C_DUMMY_TREE_CODE = LAST_AND_UNUSED_TREE_CODE,
  24. STMT_EXPR,
  25. LAST_C_TREE_CODE
  26. };
  27. enum cplus_tree_code {
  28. CP_DUMMY_TREE_CODE = LAST_C_TREE_CODE,
  29. AMBIG_CONV,
  30. LAST_CPLUS_TREE_CODE
  31. };
  32. extern int printf(const char *, ...);
  33. int blah(){return 0;}
  34. int convert_like_real (tree convs)
  35. {
  36. switch (((enum tree_code) (convs)->common.code))
  37. {
  38. case AMBIG_CONV: /* This has bit 7 set, which must not be the sign
  39. bit in tree_common.code, i.e. the bitfield must
  40. be somehow marked unsigned. */
  41. return blah();
  42. default:
  43. break;
  44. };
  45. printf("unsigned enum bit-fields broken\n");
  46. }
  47. int main()
  48. {
  49. union tree_node convs;
  50. convs.common.code = AMBIG_CONV;
  51. convert_like_real (&convs);
  52. return 0;
  53. }