<div class="gmail_quote">On Tue, May 15, 2012 at 6:51 PM, Jordy Rose <span dir="ltr"><<a href="mailto:jediknil@belkadan.com" target="_blank">jediknil@belkadan.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
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...<br>
<br>
+    // Keep track of whether every enum element is the same value.<br>
+    if (AllElementsEqual && i > 0) {<br>
+      if (InitVal.getBitWidth() > LastVal.getBitWidth())<br>
+        AllElementsEqual = InitVal == LastVal.extend(InitVal.getBitWidth());<br>
+      else if (InitVal.getBitWidth() < LastVal.getBitWidth())<br>
+        AllElementsEqual = InitVal.extend(LastVal.getBitWidth()) == LastVal;<br>
+      else<br>
+        AllElementsEqual = InitVal == LastVal;<br>
+    }<br>
<br>
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...<br></blockquote><div><br></div><div>I tried sticking some negative values in there, but didn't see any crashing.   Do you have an example of this happening?</div>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
(This is the sort of problem that APSIntType in the static analyzer Core library is supposed to make simpler.)<br>
<br>
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.<br>

<br>
Jordy<br>
<div><div class="h5"><br>
<br>
On May 15, 2012, at 19:10, Richard Trieu wrote:<br>
<br>
> 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:<br>
><br>
> enum A {<br>
>   FIRST = 1,<br>
>   SECOND = 1<br>
> };<br>
</div></div>> <unique-enum.patch>_______________________________________________<br>
> cfe-commits mailing list<br>
> <a href="mailto:cfe-commits@cs.uiuc.edu">cfe-commits@cs.uiuc.edu</a><br>
> <a href="http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits</a><br>
<br>
</blockquote></div><br>