Hi doug.gregor,

This patch changes -Wassign-enum to compare unqualified types.  One could think 
that this does not matter much, because who wants a value of enum type that is 
const-qualified?  But this breaks the intended pattern to silence this warning 
with an explicit cast:

static const enum Foo z = (enum Foo) 42;

In this case, source type is 'enum Foo', and destination type is 'const enum 
Foo', and if we compare qualified types, they don't match, so we warn.


http://llvm-reviews.chandlerc.com/D2341

Files:
  lib/Sema/SemaStmt.cpp
  test/Sema/warn-outof-range-assign-enum.c

Index: lib/Sema/SemaStmt.cpp
===================================================================
--- lib/Sema/SemaStmt.cpp
+++ lib/Sema/SemaStmt.cpp
@@ -1118,7 +1118,7 @@
     return;
 
   if (const EnumType *ET = DstType->getAs<EnumType>())
-    if (!Context.hasSameType(SrcType, DstType) &&
+    if (!Context.hasSameUnqualifiedType(SrcType, DstType) &&
         SrcType->isIntegerType()) {
       if (!SrcExpr->isTypeDependent() && !SrcExpr->isValueDependent() &&
           SrcExpr->isIntegerConstantExpr(Context)) {
Index: test/Sema/warn-outof-range-assign-enum.c
===================================================================
--- test/Sema/warn-outof-range-assign-enum.c
+++ test/Sema/warn-outof-range-assign-enum.c
@@ -11,6 +11,9 @@
 CCTestEnum test = 50; // expected-warning {{integer constant not in range of 
enumerated type 'CCTestEnum'}}
 CCTestEnum test1 = -50; // expected-warning {{integer constant not in range of 
enumerated type 'CCTestEnum'}}
 
+// Explicit cast should silence the warning.
+static const CCTestEnum SilenceWithCast = (CCTestEnum) 51; // no-warning
+
 CCTestEnum foo(CCTestEnum r) {
   return 20; // expected-warning {{integer constant not in range of enumerated 
type 'CCTestEnum'}}
 }
Index: lib/Sema/SemaStmt.cpp
===================================================================
--- lib/Sema/SemaStmt.cpp
+++ lib/Sema/SemaStmt.cpp
@@ -1118,7 +1118,7 @@
     return;
 
   if (const EnumType *ET = DstType->getAs<EnumType>())
-    if (!Context.hasSameType(SrcType, DstType) &&
+    if (!Context.hasSameUnqualifiedType(SrcType, DstType) &&
         SrcType->isIntegerType()) {
       if (!SrcExpr->isTypeDependent() && !SrcExpr->isValueDependent() &&
           SrcExpr->isIntegerConstantExpr(Context)) {
Index: test/Sema/warn-outof-range-assign-enum.c
===================================================================
--- test/Sema/warn-outof-range-assign-enum.c
+++ test/Sema/warn-outof-range-assign-enum.c
@@ -11,6 +11,9 @@
 CCTestEnum test = 50; // expected-warning {{integer constant not in range of enumerated type 'CCTestEnum'}}
 CCTestEnum test1 = -50; // expected-warning {{integer constant not in range of enumerated type 'CCTestEnum'}}
 
+// Explicit cast should silence the warning.
+static const CCTestEnum SilenceWithCast = (CCTestEnum) 51; // no-warning
+
 CCTestEnum foo(CCTestEnum r) {
   return 20; // expected-warning {{integer constant not in range of enumerated type 'CCTestEnum'}}
 }
_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits

Reply via email to