[PATCH] -Wassign-enum: compare unqualified types

David Blaikie dblaikie at gmail.com
Thu Dec 5 13:15:43 PST 2013


Strange that there are no examples of this suppression in the test
case - what documents/hints at this being the intended suppression
mechanism?

On Thu, Dec 5, 2013 at 12:52 PM, Dmitri Gribenko <gribozavr at gmail.com> wrote:
> 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'}}
>  }
>
> _______________________________________________
> cfe-commits mailing list
> cfe-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits
>




More information about the cfe-commits mailing list