enum.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // RUN: %clang_cc1 %s -fsyntax-only -verify -pedantic
  2. enum e {A,
  3. B = 42LL << 32, // expected-warning {{ISO C restricts enumerator values to range of 'int'}}
  4. C = -4, D = 12456 };
  5. enum f { a = -2147483648, b = 2147483647 }; // ok.
  6. enum g { // too negative
  7. c = -2147483649, // expected-warning {{ISO C restricts enumerator values to range of 'int'}}
  8. d = 2147483647 };
  9. enum h { e = -2147483648, // too pos
  10. f = 2147483648, // expected-warning {{ISO C restricts enumerator values to range of 'int'}}
  11. i = 0xFFFF0000 // expected-warning {{too large}}
  12. };
  13. // minll maxull
  14. enum x // expected-warning {{enumeration values exceed range of largest integer}}
  15. { y = -9223372036854775807LL-1, // expected-warning {{ISO C restricts enumerator values to range of 'int'}}
  16. z = 9223372036854775808ULL }; // expected-warning {{ISO C restricts enumerator values to range of 'int'}}
  17. int test() {
  18. return sizeof(enum e) ;
  19. }
  20. enum gccForwardEnumExtension ve; // expected-warning{{ISO C forbids forward references to 'enum' types}} \
  21. // expected-error{{tentative definition has type 'enum gccForwardEnumExtension' that is never completed}} \
  22. // expected-note{{forward declaration of 'enum gccForwardEnumExtension'}}
  23. int test2(int i)
  24. {
  25. ve + i; // expected-error{{invalid operands to binary expression}}
  26. }
  27. // PR2020
  28. union u0; // expected-note {{previous use is here}}
  29. enum u0 { U0A }; // expected-error {{use of 'u0' with tag type that does not match previous declaration}}
  30. // rdar://6095136
  31. extern enum some_undefined_enum ve2; // expected-warning {{ISO C forbids forward references to 'enum' types}}
  32. void test4() {
  33. for (; ve2;) // expected-error {{statement requires expression of scalar type}}
  34. ;
  35. (_Bool)ve2; // expected-error {{arithmetic or pointer type is required}}
  36. for (; ;ve2) // expected-warning {{expression result unused}}
  37. ;
  38. (void)ve2;
  39. ve2; // expected-warning {{expression result unused}}
  40. }
  41. // PR2416
  42. enum someenum {}; // expected-error {{use of empty enum}}
  43. // <rdar://problem/6093889>
  44. enum e0 { // expected-note {{previous definition is here}}
  45. E0 = sizeof(enum e0 { E1 }), // expected-error {{nested redefinition}}
  46. };
  47. // PR3173
  48. enum { PR3173A, PR3173B = PR3173A+50 };
  49. // PR2753
  50. void foo() {
  51. enum xpto; // expected-warning{{ISO C forbids forward references to 'enum' types}}
  52. enum xpto; // expected-warning{{ISO C forbids forward references to 'enum' types}}
  53. }
  54. // <rdar://problem/6503878>
  55. typedef enum { X = 0 }; // expected-warning{{typedef requires a name}}
  56. enum NotYetComplete { // expected-note{{definition of 'enum NotYetComplete' is not complete until the closing '}'}}
  57. NYC1 = sizeof(enum NotYetComplete) // expected-error{{invalid application of 'sizeof' to an incomplete type 'enum NotYetComplete'}}
  58. };
  59. /// PR3688
  60. struct s1 {
  61. enum e1 (*bar)(void); // expected-warning{{ISO C forbids forward references to 'enum' types}}
  62. };
  63. enum e1 { YES, NO };
  64. static enum e1 badfunc(struct s1 *q) {
  65. return q->bar();
  66. }
  67. // Make sure we don't a.k.a. anonymous enums.
  68. typedef enum {
  69. an_enumerator = 20
  70. } an_enum;
  71. char * s = (an_enum) an_enumerator; // expected-warning {{incompatible integer to pointer conversion initializing 'char *' with an expression of type 'an_enum'}}
  72. // PR4515
  73. enum PR4515 {PR4515a=1u,PR4515b=(PR4515a-2)/2};
  74. int CheckPR4515[PR4515b==0?1:-1];
  75. // PR7911
  76. extern enum PR7911T PR7911V; // expected-warning{{ISO C forbids forward references to 'enum' types}}
  77. void PR7911F() {
  78. switch (PR7911V); // expected-error {{statement requires expression of integer type}}
  79. }
  80. char test5[__has_feature(enumerator_attributes) ? 1 : -1];
  81. // PR8694
  82. // rdar://8707031
  83. void PR8694(int* e) // expected-note {{passing argument to parameter 'e' here}}
  84. {
  85. }
  86. void crash(enum E* e) // expected-warning {{declaration of 'enum E' will not be visible outside of this function}} \
  87. // expected-warning {{ISO C forbids forward references to 'enum' types}}
  88. {
  89. PR8694(e); // expected-warning {{incompatible pointer types passing 'enum E *' to parameter of type 'int *'}}
  90. }
  91. typedef enum { NegativeShort = (short)-1 } NegativeShortEnum;
  92. int NegativeShortTest[NegativeShort == -1 ? 1 : -1];