brintos

brintos / llvm-project-archived public Read only

0
0
Text · 21.6 KiB · 09f3206 Raw
470 lines · cpp
1// RUN: %clang_cc1 -fsyntax-only -pedantic -std=c++11 -verify=expected,cxx11-17 -triple x86_64-apple-darwin %s2// RUN: %clang_cc1 -fsyntax-only -pedantic -std=c++17 -verify=expected,cxx11-17 -triple x86_64-apple-darwin %s3// RUN: %clang_cc1 -fsyntax-only -pedantic -std=c++20 -verify -triple x86_64-apple-darwin %s4 5enum class E1 {6  Val1 = 1L7};8 9enum struct E2 {10  Val1 = '\0'11};12 13E1 v1 = Val1; // expected-error{{undeclared identifier}}14E1 v2 = E1::Val1;15 16static_assert(sizeof(E1) == sizeof(int), "bad size");17static_assert(sizeof(E1::Val1) == sizeof(int), "bad size");18static_assert(sizeof(E2) == sizeof(int), "bad size");19static_assert(sizeof(E2::Val1) == sizeof(int), "bad size");20 21E1 v3 = E2::Val1; // expected-error{{cannot initialize a variable}}22int x1 = E1::Val1; // expected-error{{cannot initialize a variable}}23 24enum E3 : char {25  Val2 = 126};27 28E3 v4 = Val2;29E1 v5 = Val2; // expected-error{{cannot initialize a variable}}30 31static_assert(sizeof(E3) == 1, "bad size");32 33int x2 = Val2;34 35int a1[Val2];36int a2[E1::Val1];37 38#if __cplusplus >= 201703L39// expected-error@-3 {{type 'E1' is not implicitly convertible to '__size_t' (aka 'unsigned long')}}40#else41// expected-error@-5 {{size of array has non-integer type}}42#endif43 44int* p1 = new int[Val2];45int* p2 = new int[E1::Val1];46 47#if __cplusplus >= 201703L48// expected-error@-3 {{converting 'E1' to incompatible type '__size_t'}}49#else50// expected-error@-5 {{array size expression must have integral or unscoped enumeration type, not 'E1'}}51#endif52 53enum class E4 {54  e1 = -2147483648, // ok55  e2 = 2147483647, // ok56  e3 = 2147483648 // expected-error{{enumerator value evaluates to 2147483648, which cannot be narrowed to type 'int'}}57                  // expected-warning@-1{{changes value}}58};59 60enum class E5 {61  e1 = 2147483647, // ok62  e2 // expected-error{{2147483648 is not representable in the underlying}}63};64 65enum class E6 : bool {66    e1 = false, e2 = true,67    e3 // expected-error{{2 is not representable in the underlying}}68};69 70enum E7 : bool {71    e1 = false, e2 = true,72    e3 // expected-error{{2 is not representable in the underlying}}73};74 75template <class T>76struct X {77  enum E : T {78    e1, e2,79    e3 // expected-error{{2 is not representable in the underlying}}80  };81};82 83X<bool> X2; // expected-note{{in instantiation of template}}84 85enum Incomplete1; // expected-error{{C++ forbids forward references}}86 87enum Complete1 : int;88Complete1 complete1;89 90enum class Complete2;91Complete2 complete2;92 93// All the redeclarations below are done twice on purpose. Tests that the type94// of the declaration isn't changed.95 96enum class Redeclare2; // expected-note{{previous declaration is here}} expected-note{{previous declaration is here}}97enum Redeclare2; // expected-error{{previously declared as scoped}}98enum Redeclare2; // expected-error{{previously declared as scoped}}99 100enum Redeclare3 : int; // expected-note{{previous declaration is here}} expected-note{{previous declaration is here}}101enum Redeclare3; // expected-error{{previously declared with fixed underlying type}}102enum Redeclare3; // expected-error{{previously declared with fixed underlying type}}103 104enum class Redeclare5;105enum class Redeclare5 : int; // ok106 107enum Redeclare6 : int;   // expected-note{{previous declaration is here}} expected-note{{previous declaration is here}}108enum Redeclare6 : short; // expected-error{{redeclared with different underlying type}}109enum Redeclare6 : short; // expected-error{{redeclared with different underlying type}}110 111enum class Redeclare7;         // expected-note{{previous declaration is here}} expected-note{{previous declaration is here}}112enum class Redeclare7 : short; // expected-error{{redeclared with different underlying type}}113enum class Redeclare7 : short; // expected-error{{redeclared with different underlying type}}114 115enum : long {116  long_enum_val = 10000117};118 119enum : long x; // expected-error{{unnamed enumeration must be a definition}}120 121void PR9333() {122  enum class scoped_enum { yes, no, maybe };123  scoped_enum e = scoped_enum::yes;124  if (e == scoped_enum::no) { }125}126 127namespace rdar9366066 {128  enum class X : unsigned { value };129 130  void f(X x) {131    x % X::value; // expected-error{{invalid operands to binary expression ('X' and 'rdar9366066::X')}}132                  // expected-note@-1{{no implicit conversion for scoped enum; consider casting to underlying type}}133                  // expected-note@-2{{no implicit conversion for scoped enum; consider casting to underlying type}}134    x % 8; // expected-error{{invalid operands to binary expression ('X' and 'int')}}135           // expected-note@-1{{no implicit conversion for scoped enum; consider casting to underlying type}}136  }137}138 139// Part 1 of PR10264140namespace test5 {141  namespace ns {142    typedef unsigned Atype;143    enum A : Atype;144  }145  enum ns::A : ns::Atype {146    x, y, z147  };148}149 150// Part 2 of PR10264151namespace test6 {152  enum A : unsigned;153  struct A::a; // expected-error {{incomplete type 'test6::A' named in nested name specifier}}154               // expected-error@-1{{forward declaration of struct cannot have a nested name specifier}}155  enum A::b; // expected-error {{incomplete type 'test6::A' named in nested name specifier}}156             // expected-error@-1{{forward declaration of enum cannot have a nested name specifier}}157  int A::c; // expected-error {{incomplete type 'test6::A' named in nested name specifier}}158  void A::d(); // expected-error {{incomplete type 'test6::A' named in nested name specifier}}159  void test() {160    (void) A::e; // expected-error {{incomplete type 'test6::A' named in nested name specifier}}161  }162}163 164namespace PR11484 {165  const int val = 104;166  enum class test1 { owner_dead = val, };167}168 169namespace N2764 {170  enum class E *x0a; // expected-error {{reference to enumeration must use 'enum' not 'enum class'}}171  enum E2 *x0b; // OK172  enum class E { a, b };173  enum E x1 = E::a; // ok174  enum class E x2 = E::a; // expected-error {{reference to enumeration must use 'enum' not 'enum class'}}175 176  enum F { a, b };177  enum F y1 = a; // ok178  enum class F y2 = a; // expected-error {{reference to enumeration must use 'enum' not 'enum class'}}179 180  struct S {181    friend enum class E; // expected-error {{reference to enumeration must use 'enum' not 'enum class'}}182                         // expected-warning@-1 {{elaborated enum specifier cannot be declared as a friend}}183                         // expected-note@-2 {{remove 'enum class' to befriend an enum}}184    friend enum class F; // expected-error {{reference to enumeration must use 'enum' not 'enum class'}}185                         // expected-warning@-1 {{elaborated enum specifier cannot be declared as a friend}}186                         // expected-note@-2 {{remove 'enum class' to befriend an enum}}187 188    friend enum G {}; // expected-error {{forward reference}} expected-error {{cannot define a type in a friend declaration}}189                      // expected-warning@-1 {{elaborated enum specifier cannot be declared as a friend}}190                      // expected-note@-2 {{remove 'enum' to befriend an enum}}191    friend enum class H {}; // expected-error {{forward reference}} expected-error {{cannot define a type in a friend declaration}}192                            // expected-warning@-1 {{elaborated enum specifier cannot be declared as a friend}}193                            // expected-note@-2 {{remove 'enum' to befriend an enum}}194    friend enum I : int {}; // expected-error {{forward reference}} expected-error {{cannot define a type in a friend declaration}}195                            // expected-warning@-1 {{elaborated enum specifier cannot be declared as a friend}}196                            // expected-note@-2 {{remove 'enum' to befriend an enum}}197 198    enum A : int;199    A a;200  } s;201 202  enum S::A : int {};203 204  enum class B;205}206 207enum class N2764::B {};208 209namespace PR12106 {210  template<typename E> struct Enum {211    Enum() : m_e(E::Last) {}212    E m_e;213  };214 215  enum eCOLORS { Last };216  Enum<eCOLORS> e;217}218 219namespace test7 {220  enum class E { e = (struct S*)0 == (struct S*)0 };221  S *p;222}223 224namespace test8 {225  template<typename T> struct S {226    enum A : int; // expected-note {{here}}227    enum class B; // expected-note {{here}}228    enum class C : int; // expected-note {{here}}229    enum class D : int; // expected-note {{here}}230  };231  template<typename T> enum S<T>::A { a }; // expected-error {{previously declared with fixed underlying type}}232  template<typename T> enum class S<T>::B : char { b }; // expected-error {{redeclared with different underlying}}233  template<typename T> enum S<T>::C : int { c }; // expected-error {{previously declared as scoped}}234  template<typename T> enum class S<T>::D : char { d }; // expected-error {{redeclared with different underlying}}235}236 237namespace test9 {238  template<typename T> struct S {239    enum class ET : T; // expected-note 2{{here}}240    enum class Eint : int; // expected-note 2{{here}}241  };242  template<> enum class S<int>::ET : int {};243  template<> enum class S<char>::ET : short {}; // expected-error {{different underlying type}}244  template<> enum class S<int>::Eint : short {}; // expected-error {{different underlying type}}245  template<> enum class S<char>::Eint : int {};246 247  template<typename T> enum class S<T>::ET : int {}; // expected-error {{different underlying type 'int' (was 'short')}}248  template<typename T> enum class S<T>::Eint : T {}; // expected-error {{different underlying type 'short' (was 'int')}}249 250  // The implicit instantiation of S<short> causes the implicit instantiation of251  // all declarations of member enumerations, so is ill-formed, even though we252  // never instantiate the definitions of S<short>::ET nor S<short>::Eint.253  S<short> s; // expected-note {{in instantiation of}}254}255 256namespace test10 {257  template<typename T> int f() {258    enum E : int;259    enum E : T; // expected-note {{here}}260    E x;261    enum E : int { e }; // expected-error {{different underlying}}262    x = e;263    return x;264  }265  int k = f<int>();266  int l = f<short>(); // expected-note {{here}}267 268  template<typename T> int g() {269    enum class E : int;270    enum class E : T; // expected-note {{here}}271    E x;272    enum class E : int { e }; // expected-error {{different underlying}}273    x = E::e;274    return (int)x;275  }276  int m = g<int>();277  int n = g<short>(); // expected-note {{here}}278}279 280namespace pr13128 {281  // This should compile cleanly282  class C {283    enum class E { C };284  };285}286 287namespace PR15633 {288  template<typename T> struct A {289    struct B {290      enum class E : T;291      enum class E2 : T;292    };293  };294  template<typename T> enum class A<T>::B::E { e };295  template class A<int>;296 297  struct B { enum class E; };298  template<typename T> enum class B::E { e }; // expected-error {{enumeration cannot be a template}}299}300 301namespace PR16900 {302  enum class A;303  A f(A a) { return -a; } // expected-error {{invalid argument type 'A' to unary expression}}304}305 306namespace PR18551 {307  enum class A { A };308  bool f() { return !A::A; } // expected-error {{invalid argument type 'PR18551::A' to unary expression}}309}310 311namespace rdar15124329 {312  enum class B : bool { F, T };313 314  const rdar15124329::B T1 = B::T;315  typedef B C;  const C T2 = B::T;316 317  static_assert(T1 != B::F, "");318  static_assert(T2 == B::T, "");319}320 321namespace PR18044 {322  enum class E { a };323 324  int E::e = 0; // expected-error {{does not refer into a class}}325  void E::f() {} // expected-error {{does not refer into a class}}326  struct E::S {}; // expected-error {{no struct named 'S'}}327  struct T : E::S {}; // expected-error {{expected class name}}328  enum E::E {}; // expected-error {{no enum named 'E'}}329  int E::*p; // expected-error {{does not point into a class}}330  using E::f; // expected-error {{no member named 'f'}}331 332  using E::a; // cxx11-17-warning {{using declaration naming a scoped enumerator is a C++20 extension}}333  E b = a;334}335 336namespace test11 {337  enum class E { a };338  typedef E E2;339  E2 f1() { return E::a; }340 341  bool f() { return !f1(); } // expected-error {{invalid argument type 'E2' (aka 'E') to unary expression}}342}343 344namespace PR35586 {345  enum C { R=-1, G, B };346  enum B { F = (enum C) -1, T}; // this should compile cleanly, it used to assert.347};348 349namespace test12 {350// Check that clang rejects this code without crashing in c++17.351enum class A;352enum class B;353A a;354B b{a}; // expected-error {{cannot initialize}}355}356 357namespace GH147736 {358template <typename Ty>359struct S {360  enum OhBoy : Ty { // expected-error 2 {{'_Atomic' qualifier ignored; operations involving the enumeration type will be non-atomic}}361    Unimportant362  } e;363};364 365// Okay, was previously rejected. The underlying type is int.366S<_Atomic(int)> s; // expected-warning {{'_Atomic' is a C11 extension}}367                   // expected-note@-1 {{in instantiation of template class 'GH147736::S<_Atomic(int)>' requested here}}368static_assert(__is_same(__underlying_type(S<_Atomic(long long)>::OhBoy), long long), ""); // expected-warning {{'_Atomic' is a C11 extension}}369                                                                                          // expected-note@-1 {{in instantiation of template class 'GH147736::S<_Atomic(long long)>' requested here}}370}371 372namespace GH24265 {373  enum class E_int { e };374  enum class E_long : long { e };375 376  void f() {377    E_int::e + E_long::e; // expected-error {{invalid operands to binary expression ('GH24265::E_int' and 'GH24265::E_long')}}378                          // expected-note@-1 {{no implicit conversion for scoped enum; consider casting to underlying type}}379                          // expected-note@-2 {{no implicit conversion for scoped enum; consider casting to underlying type}}380    E_int::e + 0; // expected-error {{invalid operands to binary expression ('GH24265::E_int' and 'int')}}381                  // expected-note@-1 {{no implicit conversion for scoped enum; consider casting to underlying type}}382 383    0 * E_int::e; // expected-error {{invalid operands to binary expression ('int' and 'GH24265::E_int')}}384                  // expected-note@-1 {{no implicit conversion for scoped enum; consider casting to underlying type}}385    0 / E_int::e; // expected-error {{invalid operands to binary expression ('int' and 'GH24265::E_int')}}386                  // expected-note@-1 {{no implicit conversion for scoped enum; consider casting to underlying type}}387    0 % E_int::e; // expected-error {{invalid operands to binary expression ('int' and 'GH24265::E_int')}}388                  // expected-note@-1 {{no implicit conversion for scoped enum; consider casting to underlying type}}389    0 + E_int::e; // expected-error {{invalid operands to binary expression ('int' and 'GH24265::E_int')}}390                  // expected-note@-1 {{no implicit conversion for scoped enum; consider casting to underlying type}}391    0 - E_int::e; // expected-error {{invalid operands to binary expression ('int' and 'GH24265::E_int')}}392                  // expected-note@-1 {{no implicit conversion for scoped enum; consider casting to underlying type}}393    0 << E_int::e; // expected-error {{invalid operands to binary expression ('int' and 'GH24265::E_int')}}394                   // expected-note@-1 {{no implicit conversion for scoped enum; consider casting to underlying type}}395    0 >> E_int::e; // expected-error {{invalid operands to binary expression ('int' and 'GH24265::E_int')}}396                   // expected-note@-1 {{no implicit conversion for scoped enum; consider casting to underlying type}}397 398    #if __cplusplus >= 202002L399    0 <=> E_int::e; // expected-error {{invalid operands to binary expression ('int' and 'GH24265::E_int')}}400                    // expected-note@-1 {{no implicit conversion for scoped enum; consider casting to underlying type}}401    #endif402 403    0 < E_int::e; // expected-error {{invalid operands to binary expression ('int' and 'GH24265::E_int')}}404                  // expected-note@-1 {{no implicit conversion for scoped enum; consider casting to underlying type}}405    0 > E_int::e; // expected-error {{invalid operands to binary expression ('int' and 'GH24265::E_int')}}406                  // expected-note@-1 {{no implicit conversion for scoped enum; consider casting to underlying type}}407    0 <= E_int::e; // expected-error {{invalid operands to binary expression ('int' and 'GH24265::E_int')}}408                   // expected-note@-1 {{no implicit conversion for scoped enum; consider casting to underlying type}}409    0 >= E_int::e; // expected-error {{invalid operands to binary expression ('int' and 'GH24265::E_int')}}410                   // expected-note@-1 {{no implicit conversion for scoped enum; consider casting to underlying type}}411    0 == E_int::e; // expected-error {{invalid operands to binary expression ('int' and 'GH24265::E_int')}}412                   // expected-note@-1 {{no implicit conversion for scoped enum; consider casting to underlying type}}413    0 != E_int::e; // expected-error {{invalid operands to binary expression ('int' and 'GH24265::E_int')}}414                   // expected-note@-1 {{no implicit conversion for scoped enum; consider casting to underlying type}}415    0 & E_int::e; // expected-error {{invalid operands to binary expression ('int' and 'GH24265::E_int')}}416                  // expected-note@-1 {{no implicit conversion for scoped enum; consider casting to underlying type}}417    0 ^ E_int::e; // expected-error {{invalid operands to binary expression ('int' and 'GH24265::E_int')}}418                  // expected-note@-1 {{no implicit conversion for scoped enum; consider casting to underlying type}}419    0 | E_int::e; // expected-error {{invalid operands to binary expression ('int' and 'GH24265::E_int')}}420                  // expected-note@-1 {{no implicit conversion for scoped enum; consider casting to underlying type}}421    0 && E_int::e; // expected-error {{value of type 'GH24265::E_int' is not contextually convertible to 'bool'}}422                   // expected-error@-1 {{invalid operands to binary expression ('int' and 'GH24265::E_int')}}423                   // expected-note@-2 {{no implicit conversion for scoped enum; consider casting to underlying type}}424    0 || E_int::e; // expected-error {{value of type 'GH24265::E_int' is not contextually convertible to 'bool'}}425                   // expected-error@-1 {{invalid operands to binary expression ('int' and 'GH24265::E_int')}}426                   // expected-note@-2 {{no implicit conversion for scoped enum; consider casting to underlying type}}427 428    int a;429    a *= E_int::e; // expected-error {{invalid operands to binary expression ('int' and 'GH24265::E_int')}}430                   // expected-note@-1 {{no implicit conversion for scoped enum; consider casting to underlying type}}431    a /= E_int::e; // expected-error {{invalid operands to binary expression ('int' and 'GH24265::E_int')}}432                   // expected-note@-1 {{no implicit conversion for scoped enum; consider casting to underlying type}}433    a %= E_int::e; // expected-error {{invalid operands to binary expression ('int' and 'GH24265::E_int')}}434                   // expected-note@-1 {{no implicit conversion for scoped enum; consider casting to underlying type}}435    a += E_int::e; // expected-error {{invalid operands to binary expression ('int' and 'GH24265::E_int')}}436                   // expected-note@-1 {{no implicit conversion for scoped enum; consider casting to underlying type}}437    a -= E_int::e; // expected-error {{invalid operands to binary expression ('int' and 'GH24265::E_int')}}438                   // expected-note@-1 {{no implicit conversion for scoped enum; consider casting to underlying type}}439    a <<= E_int::e; // expected-error {{invalid operands to binary expression ('int' and 'GH24265::E_int')}}440                    // expected-note@-1 {{no implicit conversion for scoped enum; consider casting to underlying type}}441    a >>= E_int::e; // expected-error {{invalid operands to binary expression ('int' and 'GH24265::E_int')}}442                    // expected-note@-1 {{no implicit conversion for scoped enum; consider casting to underlying type}}443    a &= E_int::e; // expected-error {{invalid operands to binary expression ('int' and 'GH24265::E_int')}}444                   // expected-note@-1 {{no implicit conversion for scoped enum; consider casting to underlying type}}445    a ^= E_int::e; // expected-error {{invalid operands to binary expression ('int' and 'GH24265::E_int')}}446                   // expected-note@-1 {{no implicit conversion for scoped enum; consider casting to underlying type}}447    a |= E_int::e; // expected-error {{invalid operands to binary expression ('int' and 'GH24265::E_int')}}448                   // expected-note@-1 {{no implicit conversion for scoped enum; consider casting to underlying type}}449 450    // TODO: These do not have the diagnostic yet451    E_int b;452    b *= 0; // expected-error {{invalid operands to binary expression ('E_int' and 'int')}}453    b /= 0; // expected-error {{invalid operands to binary expression ('E_int' and 'int')}}454    b %= 0; // expected-error {{invalid operands to binary expression ('E_int' and 'int')}}455    b += 0; // expected-error {{invalid operands to binary expression ('E_int' and 'int')}}456    b -= 0; // expected-error {{invalid operands to binary expression ('E_int' and 'int')}}457    b <<= 0; // expected-error {{invalid operands to binary expression ('E_int' and 'int')}}458    b >>= 0; // expected-error {{invalid operands to binary expression ('E_int' and 'int')}}459    b &= 0; // expected-error {{invalid operands to binary expression ('E_int' and 'int')}}460    b ^= 0; // expected-error {{invalid operands to binary expression ('E_int' and 'int')}}461    b |= 0; // expected-error {{invalid operands to binary expression ('E_int' and 'int')}}462 463    a = E_int::e; // expected-error {{assigning to 'int' from incompatible type 'GH24265::E_int'}}464    b = 0; // expected-error {{assigning to 'E_int' from incompatible type 'int'}}465 466    E_int c = 0; // expected-error {{cannot initialize a variable of type 'E_int' with an rvalue of type 'int'}}467    int d = E_int::e; // expected-error {{cannot initialize a variable of type 'int' with an rvalue of type 'GH24265::E_int'}}468  }469}470