brintos

brintos / llvm-project-archived public Read only

0
0
Text · 3.1 KiB · 36717f3 Raw
75 lines · c
1// RUN: %clang_cc1 -fsyntax-only -verify -Wimplicit-int-enum-cast %s2// RUN: %clang_cc1 -fsyntax-only -verify -Wc++-compat %s3// RUN: %clang_cc1 -fsyntax-only -verify=cxx -x c++ %s4// RUN: %clang_cc1 -fsyntax-only -verify=good -Wno-implicit-enum-enum-cast %s5// RUN: %clang_cc1 -fsyntax-only -verify=good -Wc++-compat -Wno-implicit-enum-enum-cast -Wno-implicit-int-enum-cast %s6// good-no-diagnostics7 8enum E1 {9  E1_Zero,10  E1_One11};12 13enum E2 {14  E2_Zero15};16 17struct S {18  enum E1 e;19} s = { 12 }; // expected-warning {{implicit conversion from 'int' to enumeration type 'enum E1' is invalid in C++}} \20                 cxx-error {{cannot initialize a member subobject of type 'enum E1' with an rvalue of type 'int'}}21 22enum E1 foo(void) {23  int x;24  enum E1 e = 12; // expected-warning {{implicit conversion from 'int' to enumeration type 'enum E1' is invalid in C++}} \25                     cxx-error {{cannot initialize a variable of type 'enum E1' with an rvalue of type 'int'}}26 27  // Enum to integer is fine.28  x = e;29 30  // Integer to enum is not fine.31  e = x;    // expected-warning {{implicit conversion from 'int' to enumeration type 'enum E1' is invalid in C++}} \32               cxx-error {{assigning to 'enum E1' from incompatible type 'int'}}33  return x; // expected-warning {{implicit conversion from 'int' to enumeration type 'enum E1' is invalid in C++}} \34               cxx-error {{cannot initialize return object of type 'enum E1' with an lvalue of type 'int'}}35}36 37// Returning with the correct types is fine.38enum E1 bar(void) {39  return E1_Zero;40}41 42// Enum to different-enum conversion is also a C++ incompatibility, but is43// handled via a more general diagnostic, -Wimplicit-enum-enum-cast, which is44// on by default.45enum E1 quux(void) {46  enum E1 e1 = E2_Zero; // expected-warning {{implicit conversion from enumeration type 'enum E2' to different enumeration type 'enum E1'}} \47                           cxx-error {{cannot initialize a variable of type 'enum E1' with an rvalue of type 'E2'}}48  e1 = E2_Zero;         // expected-warning {{implicit conversion from enumeration type 'enum E2' to different enumeration type 'enum E1'}}   \49                           cxx-error {{assigning to 'enum E1' from incompatible type 'E2'}}50  return E2_Zero;       // expected-warning {{implicit conversion from enumeration type 'enum E2' to different enumeration type 'enum E1'}} \51                           cxx-error {{cannot initialize return object of type 'enum E1' with an rvalue of type 'E2'}}52}53 54enum E1 comma1(void) {55  return ((void)0, E1_One);56}57 58enum E1 comma2(void) {59  enum E1 x;60  return61    (x = 12,  // expected-warning {{implicit conversion from 'int' to enumeration type 'enum E1' is invalid in C++}} \62                 cxx-error {{assigning to 'enum E1' from incompatible type 'int'}}63    E1_One);64}65 66enum E1 comma3(void) {67  enum E1 x;68  return ((void)0, foo()); // Okay, no conversion in C++69}70 71enum E1 comma4(void) {72  return ((void)1, 2); // expected-warning {{implicit conversion from 'int' to enumeration type 'enum E1' is invalid in C++}} \73                          cxx-error {{cannot initialize return object of type 'enum E1' with an rvalue of type 'int'}}74}75