attr-flag-enum.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // RUN: %clang_cc1 -verify -fsyntax-only -std=c11 -Wassign-enum %s
  2. enum __attribute__((flag_enum)) flag {
  3. ea = 0x1,
  4. eb = 0x2,
  5. ec = 0x8,
  6. };
  7. enum __attribute__((flag_enum)) flag2 {
  8. ga = 0x1,
  9. gb = 0x4,
  10. gc = 0x5, // no-warning
  11. gd = 0x7, // expected-warning {{enumeration value 'gd' is out of range}}
  12. ge = ~0x2, // expected-warning {{enumeration value 'ge' is out of range}}
  13. gf = ~0x4, // no-warning
  14. gg = ~0x1, // no-warning
  15. gh = ~0x5, // no-warning
  16. gi = ~0x11, // expected-warning {{enumeration value 'gi' is out of range}}
  17. };
  18. enum __attribute__((flag_enum)) flag3 {
  19. fa = 0x1,
  20. fb = ~0x1u, // no-warning
  21. };
  22. // What happens here is that ~0x2 is negative, and so the enum must be signed.
  23. // But ~0x1u is unsigned and has the high bit set, so the enum must be 64-bit.
  24. // The result is that ~0x1u does not have high bits set, and so it is considered
  25. // to be an invalid value. See Sema::IsValueInFlagEnum in SemaDecl.cpp for more
  26. // discussion.
  27. enum __attribute__((flag_enum)) flag4 {
  28. ha = 0x1,
  29. hb = 0x2,
  30. hc = ~0x1u, // expected-warning {{enumeration value 'hc' is out of range}}
  31. hd = ~0x2, // no-warning
  32. };
  33. void f(void) {
  34. enum flag e = 0; // no-warning
  35. e = 0x1; // no-warning
  36. e = 0x3; // no-warning
  37. e = 0xa; // no-warning
  38. e = 0x4; // expected-warning {{integer constant not in range of enumerated type}}
  39. e = 0xf; // expected-warning {{integer constant not in range of enumerated type}}
  40. e = ~0; // no-warning
  41. e = ~0x1; // no-warning
  42. e = ~0x2; // no-warning
  43. e = ~0x3; // no-warning
  44. e = ~0x4; // expected-warning {{integer constant not in range of enumerated type}}
  45. switch (e) {
  46. case 0: break; // no-warning
  47. case 0x1: break; // no-warning
  48. case 0x3: break; // no-warning
  49. case 0xa: break; // no-warning
  50. case 0x4: break; // expected-warning {{case value not in enumerated type}}
  51. case 0xf: break; // expected-warning {{case value not in enumerated type}}
  52. case ~0: break; // expected-warning {{case value not in enumerated type}}
  53. case ~0x1: break; // expected-warning {{case value not in enumerated type}}
  54. case ~0x2: break; // expected-warning {{case value not in enumerated type}}
  55. case ~0x3: break; // expected-warning {{case value not in enumerated type}}
  56. case ~0x4: break; // expected-warning {{case value not in enumerated type}}
  57. default: break;
  58. }
  59. enum flag2 f = ~0x1; // no-warning
  60. f = ~0x1u; // no-warning
  61. enum flag4 h = ~0x1; // no-warning
  62. h = ~0x1u; // expected-warning {{integer constant not in range of enumerated type}}
  63. }