]> granicus.if.org Git - clang/commitdiff
-Wassign-enum: compare unqualified types
authorDmitri Gribenko <gribozavr@gmail.com>
Thu, 5 Dec 2013 23:06:53 +0000 (23:06 +0000)
committerDmitri Gribenko <gribozavr@gmail.com>
Thu, 5 Dec 2013 23:06:53 +0000 (23:06 +0000)
This commit 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 used warn.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@196548 91177308-0d34-0410-b5e6-96231b3b80d8

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

index 077282c1bf17527c46c6367e67046d43719c1703..1d2ebad78810c8d4e324ee2f02d5bdba0bf2d097 100644 (file)
@@ -1149,7 +1149,7 @@ Sema::DiagnoseAssignmentEnum(QualType DstType, QualType SrcType,
     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)) {
@@ -1184,7 +1184,7 @@ Sema::DiagnoseAssignmentEnum(QualType DstType, QualType SrcType,
           EI++;
         if (EI == EIend || EI->first != RhsVal) {
           Diag(SrcExpr->getExprLoc(), diag::warn_not_in_enum_assignment)
-          << DstType;
+              << DstType.getUnqualifiedType();
         }
       }
     }
index 43eea0cef41fd8c31f7895957f92a3dc5cc96f91..edd4e37229276276f8bf391179c82aa69db6022c 100644 (file)
@@ -11,6 +11,24 @@ typedef enum CCTestEnum
 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 SilenceWithCast1 = 51; // expected-warning {{integer constant not in range of enumerated type 'CCTestEnum'}}
+static const CCTestEnum SilenceWithCast2 = (CCTestEnum) 51; // no-warning
+static const CCTestEnum SilenceWithCast3 = (const CCTestEnum) 51; // no-warning
+static const CCTestEnum SilenceWithCast4 = (const volatile CCTestEnum) 51; // no-warning
+
+void SilenceWithCastLocalVar() {
+  CCTestEnum SilenceWithCast1 = 51; // expected-warning {{integer constant not in range of enumerated type 'CCTestEnum'}}
+  CCTestEnum SilenceWithCast2 = (CCTestEnum) 51; // no-warning
+  CCTestEnum SilenceWithCast3 = (const CCTestEnum) 51; // no-warning
+  CCTestEnum SilenceWithCast4 = (const volatile CCTestEnum) 51; // no-warning
+
+  const CCTestEnum SilenceWithCast1c = 51; // expected-warning {{integer constant not in range of enumerated type 'CCTestEnum'}}
+  const CCTestEnum SilenceWithCast2c = (CCTestEnum) 51; // no-warning
+  const CCTestEnum SilenceWithCast3c = (const CCTestEnum) 51; // no-warning
+  const CCTestEnum SilenceWithCast4c = (const volatile CCTestEnum) 51; // no-warning
+}
+
 CCTestEnum foo(CCTestEnum r) {
   return 20; // expected-warning {{integer constant not in range of enumerated type 'CCTestEnum'}}
 }