From: Francois Pichet Date: Mon, 18 Oct 2010 15:01:13 +0000 (+0000) Subject: Microsoft enum extensions. 2 things will change on -fms-extensions: X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=842e7a22c6a0fbf0ccdb4bb9308d92ca9e9621f6;p=clang Microsoft enum extensions. 2 things will change on -fms-extensions: 1. enum underlying type is int by default. 2. Error "enumerator value is not representable in the underlying type"is a ExtWarning git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@116704 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td index 0b82c03da2..4d394a8c6d 100644 --- a/include/clang/Basic/DiagnosticSemaKinds.td +++ b/include/clang/Basic/DiagnosticSemaKinds.td @@ -839,6 +839,9 @@ def err_enum_invalid_underlying : Error< "non-integral type %0 is an invalid underlying type">; def err_enumerator_too_large : Error< "enumerator value is not representable in the underlying type %0">; +def ext_enumerator_too_large : ExtWarn< + "enumerator value is not representable in the underlying type %0">, + InGroup; def err_enumerator_wrapped : Error< "enumerator value %0 is not representable in the underlying type %1">; def err_enum_redeclare_type_mismatch : Error< diff --git a/lib/Sema/SemaDecl.cpp b/lib/Sema/SemaDecl.cpp index c727c64370..a65c69ddd5 100644 --- a/lib/Sema/SemaDecl.cpp +++ b/lib/Sema/SemaDecl.cpp @@ -5526,7 +5526,9 @@ Decl *Sema::ActOnTag(Scope *S, unsigned TagSpec, TagUseKind TUK, // Recover by falling back to int. EnumUnderlying = Context.IntTy.getTypePtr(); } - } + } else if (getLangOptions().Microsoft) + // Microsoft enums are always of int type. + EnumUnderlying = Context.IntTy.getTypePtr(); } DeclContext *SearchDC = CurContext; @@ -7080,10 +7082,14 @@ EnumConstantDecl *Sema::CheckEnumConstant(EnumDecl *Enum, // C++0x [dcl.enum]p5: // ... if the initializing value of an enumerator cannot be // represented by the underlying type, the program is ill-formed. - if (!isRepresentableIntegerValue(Context, EnumVal, EltTy)) - Diag(IdLoc, diag::err_enumerator_too_large) - << EltTy; - else + if (!isRepresentableIntegerValue(Context, EnumVal, EltTy)) { + if (getLangOptions().Microsoft) { + Diag(IdLoc, diag::ext_enumerator_too_large) << EltTy; + ImpCastExprToType(Val, EltTy, CK_IntegralCast); + } else + Diag(IdLoc, diag::err_enumerator_too_large) + << EltTy; + } else ImpCastExprToType(Val, EltTy, CK_IntegralCast); } else { diff --git a/test/Sema/MicrosoftExtensions.c b/test/Sema/MicrosoftExtensions.c index 96c5639810..47071a383d 100644 --- a/test/Sema/MicrosoftExtensions.c +++ b/test/Sema/MicrosoftExtensions.c @@ -19,3 +19,15 @@ struct D { int l; int D[]; }; + + +enum ENUM1; // expected-warning {{forward references to 'enum' types are a Microsoft extension}} +enum ENUM1 var1 = 3; +enum ENUM1* var2 = 0; + + +enum ENUM2 { + ENUM2_a = (enum ENUM2) 4, + ENUM2_b = 0x9FFFFFFF, // expected-warning {{enumerator value is not representable in the underlying type 'int'}} + ENUM2_c = 0x100000000 // expected-warning {{enumerator value is not representable in the underlying type 'int'}} +}; diff --git a/test/SemaCXX/MicrosoftExtensions.cpp b/test/SemaCXX/MicrosoftExtensions.cpp index 7cc1675f2e..cfb27bcf21 100644 --- a/test/SemaCXX/MicrosoftExtensions.cpp +++ b/test/SemaCXX/MicrosoftExtensions.cpp @@ -87,4 +87,13 @@ void m1() { //MSVC allows forward enum declaration enum ENUM; // expected-warning {{forward references to 'enum' types are a Microsoft extension}} -ENUM *var; +ENUM *var = 0; +ENUM var2 = (ENUM)3; +enum ENUM1* var3 = 0;// expected-warning {{forward references to 'enum' types are a Microsoft extension}} + + +enum ENUM2 { + ENUM2_a = (enum ENUM2) 4, + ENUM2_b = 0x9FFFFFFF, // expected-warning {{enumerator value is not representable in the underlying type 'int'}} + ENUM2_c = 0x100000000 // expected-warning {{enumerator value is not representable in the underlying type 'int'}} +};