[PATCH] -Wassign-enum: compare unqualified types
Dmitri Gribenko
gribozavr at gmail.com
Thu Dec 5 12:52:36 PST 2013
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'}}
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D2341.1.patch
Type: text/x-patch
Size: 1223 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20131205/3000a4ea/attachment.bin>
More information about the cfe-commits
mailing list