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

Jordy Rose jediknil at belkadan.com
Tue May 15 18:51:53 PDT 2012

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...

(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.


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

More information about the cfe-commits mailing list