| 1 |
// RUN: %clang_cc1 -verify -triple x86_64-unknown-linux-gnu -fsyntax-only -std=c23 %s -pedantic -Wall |
| 2 |
|
| 3 |
#include <limits.h> |
| 4 |
|
| 5 |
enum us : unsigned short { |
| 6 |
us_max = USHRT_MAX, |
| 7 |
us_violation, // expected-error {{enumerator value 65536 is not representable in the underlying type 'unsigned short'}} |
| 8 |
us_violation_2 = us_max + 1, // expected-error {{enumerator value is not representable in the underlying type 'unsigned short'}} |
| 9 |
us_wrap_around_to_zero = (unsigned short)(USHRT_MAX + 1) /* Okay: conversion |
| 10 |
done in constant expression before conversion to |
| 11 |
underlying type: unsigned semantics okay. */ |
| 12 |
}; |
| 13 |
|
| 14 |
enum ui : unsigned int { |
| 15 |
ui_max = UINT_MAX, |
| 16 |
ui_violation, // expected-error {{enumerator value 4294967296 is not representable in the underlying type 'unsigned int'}} |
| 17 |
ui_no_violation = ui_max + 1, |
| 18 |
ui_wrap_around_to_zero = (unsigned int)(UINT_MAX + 1) |
| 19 |
}; |
| 20 |
|
| 21 |
enum E1 : short; |
| 22 |
enum E2 : short; // expected-note {{previous}} |
| 23 |
enum E3; // expected-warning {{ISO C forbids forward references to 'enum' types}} |
| 24 |
enum E4 : unsigned long long; |
| 25 |
|
| 26 |
enum E1 : short { m11, m12 }; |
| 27 |
enum E1 x = m11; |
| 28 |
|
| 29 |
enum E2 : long { // expected-error {{enumeration redeclared with different underlying type 'long' (was 'short')}} |
| 30 |
m21, |
| 31 |
m22 |
| 32 |
}; |
| 33 |
|
| 34 |
enum E3 { // expected-note {{definition of 'enum E3' is not complete until the closing '}'}} |
| 35 |
// expected-note@-1 {{previous}} |
| 36 |
m31, |
| 37 |
m32, |
| 38 |
m33 = sizeof(enum E3) // expected-error {{invalid application of 'sizeof' to an incomplete type 'enum E3'}} |
| 39 |
}; |
| 40 |
enum E3 : int; // expected-error {{enumeration previously declared with nonfixed underlying type}} |
| 41 |
|
| 42 |
enum E4 : unsigned long long { |
| 43 |
m40 = sizeof(enum E4), |
| 44 |
m41 = ULLONG_MAX, |
| 45 |
m42 // expected-error {{enumerator value 18446744073709551616 is not representable in the underlying type 'unsigned long long'}} |
| 46 |
}; |
| 47 |
|
| 48 |
enum E5 y; // expected-error {{tentative definition has type 'enum E5' that is never completed}} |
| 49 |
// expected-warning@-1 {{ISO C forbids forward references to 'enum' types}} |
| 50 |
// expected-note@-2 {{forward declaration of 'enum E5'}} |
| 51 |
enum E6 : long int z; // expected-error {{non-defining declaration of enumeration with a fixed underlying type is only permitted as a standalone declaration; missing list of enumerators?}} |
| 52 |
enum E7 : long int = 0; // expected-error {{non-defining declaration of enumeration with a fixed underlying type is only permitted as a standalone declaration; missing list of enumerators?}} |
| 53 |
// expected-error@-1 {{expected identifier or '('}} |
| 54 |
|
| 55 |
enum underlying : unsigned char { b0 }; |
| 56 |
|
| 57 |
constexpr int a = _Generic(b0, int: 2, unsigned char: 1, default: 0); |
| 58 |
constexpr int b = _Generic((enum underlying)b0, int: 2, unsigned char: 1, default: 0); |
| 59 |
static_assert(a == 1); |
| 60 |
static_assert(b == 1); |
| 61 |
|
| 62 |
void f1(enum a : long b); // expected-error {{non-defining declaration of enumeration with a fixed underlying type is only permitted as a standalone declaration; missing list of enumerators?}} |
| 63 |
// expected-warning@-1 {{declaration of 'enum a' will not be visible outside of this function}} |
| 64 |
void f2(enum c : long{x} d); |
| 65 |
enum e : int f3(); // expected-error {{non-defining declaration of enumeration with a fixed underlying type is only permitted as a standalone declaration; missing list of enumerators?}} |
| 66 |
|
| 67 |
typedef enum t u; // expected-warning {{ISO C forbids forward references to 'enum' types}} |
| 68 |
typedef enum v : short W; // expected-error {{non-defining declaration of enumeration with a fixed underlying type is only permitted as a standalone declaration; missing list of enumerators?}} |
| 69 |
typedef enum q : short { s } R; |
| 70 |
|
| 71 |
struct s1 { |
| 72 |
int x; |
| 73 |
enum e:int : 1; // expected-error {{non-defining declaration of enumeration with a fixed underlying type is only permitted as a standalone declaration; missing list of enumerators?}} |
| 74 |
int y; |
| 75 |
}; |
| 76 |
|
| 77 |
enum forward; // expected-warning {{ISO C forbids forward references to 'enum' types}} |
| 78 |
extern enum forward fwd_val0; /* Constraint violation: incomplete type */ |
| 79 |
extern enum forward *fwd_ptr0; // expected-note {{previous}} |
| 80 |
extern int |
| 81 |
*fwd_ptr0; // expected-error {{redeclaration of 'fwd_ptr0' with a different type: 'int *' vs 'enum forward *'}} |
| 82 |
|
| 83 |
enum forward1 : int; |
| 84 |
extern enum forward1 fwd_val1; |
| 85 |
extern int fwd_val1; |
| 86 |
extern enum forward1 *fwd_ptr1; |
| 87 |
extern int *fwd_ptr1; |
| 88 |
|
| 89 |
enum ee1 : short; |
| 90 |
enum e : short f = 0; // expected-error {{non-defining declaration of enumeration with a fixed underlying type is only permitted as a standalone declaration; missing list of enumerators?}} |
| 91 |
enum g : short { yyy } h = yyy; |
| 92 |
|
| 93 |
enum ee2 : typeof ((enum ee3 : short { A })0, (short)0); |
| 94 |
|
| 95 |
enum not_actually_atomic : _Atomic(short) { // expected-error {{'_Atomic' qualifier ignored; operations involving the enumeration type will be non-atomic}} |
| 96 |
Surprise |
| 97 |
}; |
| 98 |
|
| 99 |
enum not_actually_const : const int { // expected-warning {{'const' qualifier in enumeration underlying type ignored}} |
| 100 |
SurpriseAgain |
| 101 |
}; |
| 102 |
|
| 103 |
enum not_actually_volatile : volatile int { // expected-warning {{'volatile' qualifier in enumeration underlying type ignored}} |
| 104 |
SurpriseOnceMore |
| 105 |
}; |
| 106 |
|
| 107 |
enum not_acually_const_or_volatile : const volatile int { // expected-warning {{'const' and 'volatile' qualifiers in enumeration underlying type ignored}} |
| 108 |
WhyTheSurprise |
| 109 |
}; |