[cfe-commits] [Patch] Add a warning to catch enums with all elements having the same value

Richard Trieu rtrieu at google.com
Tue May 15 19:25:40 PDT 2012


On Tue, May 15, 2012 at 6:51 PM, Jordy Rose <jediknil at belkadan.com> wrote:

> Interesting. This seems hard to do by accident, but there are very few
> circumstances I can think of where you'd do it on purpose, so...
>
> +    // Keep track of whether every enum element is the same value.
> +    if (AllElementsEqual && i > 0) {
> +      if (InitVal.getBitWidth() > LastVal.getBitWidth())
> +        AllElementsEqual = InitVal ==
> LastVal.extend(InitVal.getBitWidth());
> +      else if (InitVal.getBitWidth() < LastVal.getBitWidth())
> +        AllElementsEqual = InitVal.extend(LastVal.getBitWidth()) ==
> LastVal;
> +      else
> +        AllElementsEqual = InitVal == LastVal;
> +    }
>
> This will crash if the two values have the same bit width, but different
> signedness. I'm not sure if you can do that with enums, but...
>

I tried sticking some negative values in there, but didn't see any
crashing.   Do you have an example of this happening?

>
> (This is the sort of problem that APSIntType in the static analyzer Core
> library is supposed to make simpler.)
>
> Also, as is right now the two tests can be embedded in the same file,
> using "-x c++". However, if we want to test this with C++11's
> fixed-underlying-type enums, that's a case for keeping separate files.
>
> Jordy
>
>
> On May 15, 2012, at 19:10, Richard Trieu wrote:
>
> > Add -Wunique-enum which will warn on enums with at least 2 elements such
> that all elements are the same value.  This will catch enums such as:
> >
> > enum A {
> >   FIRST = 1,
> >   SECOND = 1
> > };
> > <unique-enum.patch>_______________________________________________
> > cfe-commits mailing list
> > cfe-commits at cs.uiuc.edu
> > http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20120515/491de907/attachment.html>


More information about the cfe-commits mailing list