247 lines · c
1// RUN: %clang_cc1 -triple %itanium_abi_triple %s -fsyntax-only -verify=expected,pre-c23 -pedantic2// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu %s -fsyntax-only -std=c23 -verify=expected -pedantic3enum e {A,4 B = 42LL << 32, // pre-c23-warning {{enumerator value which exceeds the range of 'int' is a C23 extension}}5 C = -4, D = 12456 };6 7enum f { a = -2147483648, b = 2147483647 }; // ok.8 9enum g { // too negative10 c = -2147483649, // pre-c23-warning {{enumerator value which exceeds the range of 'int' is a C23 extension}}11 d = 2147483647 };12enum h { e = -2147483648, // too pos13 f = 2147483648, // pre-c23-warning {{enumerator value which exceeds the range of 'int' is a C23 extension}}14 i = 0xFFFF0000 // pre-c23-warning {{too large}}15};16 17// minll maxull18enum x // expected-warning {{enumeration values exceed range of largest integer}}19{ y = -9223372036854775807LL-1, // pre-c23-warning {{enumerator value which exceeds the range of 'int' is a C23 extension}}20z = 9223372036854775808ULL }; // pre-c23-warning {{enumerator value which exceeds the range of 'int' is a C23 extension}}21 22int test(void) {23 return sizeof(enum e) ;24}25 26enum gccForwardEnumExtension ve; // expected-warning{{ISO C forbids forward references to 'enum' types}} \27// expected-error{{tentative definition has type 'enum gccForwardEnumExtension' that is never completed}} \28// expected-note{{forward declaration of 'enum gccForwardEnumExtension'}}29 30int test2(int i)31{32 ve + i; // expected-error{{invalid operands to binary expression}}33}34 35// PR202036union u0; // expected-note {{previous use is here}}37enum u0 { U0A }; // expected-error {{use of 'u0' with tag type that does not match previous declaration}}38 39extern enum some_undefined_enum ve2; // expected-warning {{ISO C forbids forward references to 'enum' types}}40 41void test4(void) {42 for (; ve2;) // expected-error {{statement requires expression of scalar type}}43 ;44 (_Bool)ve2; // expected-error {{arithmetic or pointer type is required}}45 46 for (; ;ve2) // expected-warning {{expression result unused}}47 ;48 (void)ve2;49 ve2; // expected-warning {{expression result unused}}50}51 52// PR241653enum someenum {}; // expected-error {{use of empty enum}}54 55enum e0 { // expected-note {{previous definition is here}}56 E0 = sizeof(enum e0 { E1 }), // expected-error {{nested redefinition}}57};58 59// PR317360enum { PR3173A, PR3173B = PR3173A+50 };61 62// PR275363void foo(void) {64 enum xpto; // expected-warning{{ISO C forbids forward references to 'enum' types}}65 enum xpto; // expected-warning{{ISO C forbids forward references to 'enum' types}}66}67 68typedef enum { X = 0 }; // expected-warning{{typedef requires a name}}69 70 71enum NotYetComplete { // expected-note{{definition of 'enum NotYetComplete' is not complete until the closing '}'}}72 NYC1 = sizeof(enum NotYetComplete) // expected-error{{invalid application of 'sizeof' to an incomplete type 'enum NotYetComplete'}}73};74 75/// PR368876struct s1 {77 enum e1 (*bar)(void); // expected-warning{{ISO C forbids forward references to 'enum' types}}78};79 80enum e1 { YES, NO };81 82static enum e1 badfunc(struct s1 *q) {83 return q->bar();84}85 86 87// Make sure we don't a.k.a. anonymous enums.88typedef enum {89 an_enumerator = 2090} an_enum;91char * s = (an_enum) an_enumerator; // expected-error {{incompatible integer to pointer conversion initializing 'char *' with an expression of type 'an_enum'}}92 93// PR451594enum PR4515 {PR4515a=1u,PR4515b=(PR4515a-2)/2};95int CheckPR4515[PR4515b==0?1:-1];96 97// PR791198extern enum PR7911T PR7911V; // expected-warning{{ISO C forbids forward references to 'enum' types}}99void PR7911F(void) {100 switch (PR7911V) // expected-error {{statement requires expression of integer type}}101 ;102}103 104char test5[__has_feature(enumerator_attributes) ? 1 : -1];105 106// PR8694107void PR8694(int* e) // expected-note {{passing argument to parameter 'e' here}}108{109}110 111void crash(enum E *e) // expected-warning {{declaration of 'enum E' will not be visible outside of this function}} \112 // expected-warning {{ISO C forbids forward references to 'enum' types}}113{114 PR8694(e); // expected-error {{incompatible pointer types passing 'enum E *' to parameter of type 'int *'}}115}116 117typedef enum { NegativeShort = (short)-1 } NegativeShortEnum;118int NegativeShortTest[NegativeShort == -1 ? 1 : -1];119 120// PR24610121enum Color { Red, Green, Blue }; // expected-note{{previous use is here}}122typedef struct Color NewColor; // expected-error {{use of 'Color' with tag type that does not match previous declaration}}123 124// Enumerations with a fixed underlying type. 125// https://github.com/llvm/llvm-project/issues/116880126#if __STDC_VERSION__ >= 202311L127 static_assert(__has_feature(c_fixed_enum));128 static_assert(__has_extension(c_fixed_enum)); // Matches behavior for c_alignas, etc129#else130 _Static_assert(__has_extension(c_fixed_enum), "");131 _Static_assert(!__has_feature(c_fixed_enum), "");132#if __STDC_VERSION__ < 201112L133 // expected-warning@-3 {{'_Static_assert' is a C11 extension}}134 // expected-warning@-3 {{'_Static_assert' is a C11 extension}}135#endif136#endif137typedef enum : unsigned char { Pink, Black, Cyan } Color; // pre-c23-warning {{enumeration types with a fixed underlying type are a C23 extension}}138 139// PR28903140// In C it is valid to define tags inside enums.141struct PR28903 {142 enum {143 PR28903_A = (enum {144 PR28903_B,145 PR28903_C = PR28903_B146 })0147 };148 int makeStructNonEmpty;149};150 151static int EnumRedecl; // expected-note 2 {{previous definition is here}}152struct S {153 enum {154 EnumRedecl = 4 // expected-error {{redefinition of 'EnumRedecl'}}155 } e;156};157 158union U {159 enum {160 EnumRedecl = 5 // expected-error {{redefinition of 'EnumRedecl'}}161 } e;162};163 164enum PR15071 {165 PR15071_One // expected-note {{previous definition is here}}166};167 168struct EnumRedeclStruct {169 enum {170 PR15071_One // expected-error {{redefinition of enumerator 'PR15071_One'}}171 } e;172};173 174enum struct GH42372_1 { // expected-error {{expected identifier or '{'}}175 One176};177 178// Because class is not a keyword in C, this looks like a forward declaration.179// expected-error@+4 {{expected ';' after top level declarator}}180// expected-error@+3 {{tentative definition has type 'enum class' that is never completed}}181// expected-warning@+2 {{ISO C forbids forward references to 'enum' types}}182// expected-note@+1 {{forward declaration of 'enum class'}}183enum class GH42372_2 {184 One185};186 187enum IncOverflow {188 V2 = __INT_MAX__,189 V3 // pre-c23-warning {{incremented enumerator value which exceeds the range of 'int' is a C23 extension}}190};191 192#if __STDC_VERSION__ >= 202311L193// FIXME: GCC picks __uint128_t as the underlying type for the enumeration194// value and Clang picks unsigned long long.195enum GH59352 { // expected-warning {{enumeration values exceed range of largest integer}}196 BigVal = 66666666666666666666wb197};198_Static_assert(BigVal == 66666666666666666666wb); /* expected-error {{static assertion failed due to requirement 'BigVal == 66666666666666666666wb'}}199 expected-note {{expression evaluates to '11326434445538011818 == 66666666666666666666'}}200 */201_Static_assert(202 _Generic(BigVal, // expected-error {{static assertion failed}}203 _BitInt(67) : 0,204 __INTMAX_TYPE__ : 0,205 __UINTMAX_TYPE__ : 0,206 long long : 0,207 unsigned long long : 0,208 __int128_t : 0,209 __uint128_t : 1210 )211);212 213#include <limits.h>214 215void fooinc23() {216 enum E1 {217 V1 = INT_MAX218 } e1;219 220 enum E2 {221 V2 = INT_MAX,222 V3223 } e2;224 225 enum E3 {226 V4 = INT_MAX,227 V5 = LONG_MIN228 } e3;229 230 enum E4 {231 V6 = 1u,232 V7 = 2wb233 } e4;234 235 _Static_assert(_Generic(V1, int : 1));236 _Static_assert(_Generic(V2, int : 0, unsigned int : 1));237 _Static_assert(_Generic(V3, int : 0, unsigned int : 1));238 _Static_assert(_Generic(V4, int : 0, signed long : 1));239 _Static_assert(_Generic(V5, int : 0, signed long : 1));240 _Static_assert(_Generic(V6, int : 1));241 _Static_assert(_Generic(V7, int : 1));242 _Static_assert(_Generic((enum E4){}, unsigned int : 1));243 244}245 246#endif // __STDC_VERSION__ >= 202311L247