<br><br><div class="gmail_quote">Le 9 février 2012 01:00, David Blaikie <span dir="ltr"><<a href="mailto:dblaikie@gmail.com">dblaikie@gmail.com</a>></span> a écrit :<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div class="im">On Wed, Feb 8, 2012 at 3:33 PM, Joerg Sonnenberger<br>
<<a href="mailto:joerg@britannica.bec.de">joerg@britannica.bec.de</a>> wrote:<br>
> On Wed, Feb 08, 2012 at 03:04:44PM -0800, David Blaikie wrote:<br>
>> > The critical question is whether (a) a warning finds a useful number of<br>
>> > real bugs (b) doesn't have too many false positives and (c) has a<br>
>> > reasonable workaround for the cases of (b).<br>
>><br>
>> (a) is hard to say because this warning itself wasn't really intended<br>
>> to find current bugs - it was intended to ensure you find future bugs<br>
>> (when you add an enum value & then fail to update a switch)<br>
><br>
> Experience with large projects say that all likely bugs have been made<br>
> in the past already :)<br>
<br>
</div>Right - well, for whatever that's worth there were lots of real<br>
violations of this rule in LLVM - obviously because we actually tried<br>
to maintain this style/approach there weren't (m)any deliberate uses<br>
of defaults on covered switches.<br>
<br>
But yeah - more experience with other code bases is important, such as yours.<br>
<div class="im"><br>
>> (b) hard to say - like -Wparentheses and the original -Wswitch<br>
>> warning, this is more a convention designed to communicate extra<br>
>> information to the compiler to check semantics that aren't explicit in<br>
>> the code<br>
><br>
> I agree that this is an edge case.<br>
><br>
>> (c) The most immediate is to simply cast the switch condition<br>
>> expression to an integer type. This seems like it communicates clearly<br>
>> to the compiler "this isn't actually bounded by the enum type - I<br>
>> expect it to be any possible integer value". Alternatively, to do the<br>
>> error checking before the switch (assert(x >= first_enum && x <=<br>
>> last_enum) - and whenever the -Wswitch enum triggers on the following<br>
>> switch, it'd be quite close/easy to update the assert too).<br>
><br>
> So what is the correct integer type to use for the cast? Keep in mind<br>
> it depends on the values of the enum members.<br>
<br>
</div>std::underlying_type<e>::type<br>
<div class="im"><br>
> As such, casting doesn't<br>
> sound like the correct answer.<br>
<br>
</div>Sort of seems like it is - if you actually want to tolerate the value<br>
being outside the actual enum values that would communicate it clearly<br>
to the compiler.<br>
<div class="im"><br>
> Checking the range also doesn't work in<br>
> most interesting cases, since enums aren't always consecutive either.<br>
<br>
</div>That's fair.<br>
<div class="im"><br>
> More importantly, both cases just destroys the analysis potential again.<br>
> Just because I want to program defensively, doesn't mean the check isn't<br>
> desirable. Using __builtin_unreachable() as marker works if there is no<br>
> sane error recovery.<br>
<br>
</div>Still makes it hard to tell whether it's deliberate - when you do add<br>
a new enum value what does it mean? Should -Wswitch flag this, or is<br>
it just assumed that the user intended that the new enum value would<br>
never occur in this switch?<br>
<div class="im"><br>
> That is good enough for checks deep in the gusts of<br>
> code. It doesn't help for code on module borders.<br>
<br>
</div>If you don't have any control over the bounds/you expect it to have<br>
other values I'm still sort of seeing the cast to integral type to<br>
describe the situation you're in. I suppose you lose things like the<br>
ability to be warned about testing values that aren't any of the<br>
enums, or -Wswitch (though these scenarios don't get this<br>
functionality at the moment anyway), etc.<br>
<div class="im"><br>
> This seems<br>
> to call for a __builtin_invalid_case() :) Kind of like a semantically<br>
> stronger version of the branch prediction statements.<br>
<br>
</div>I'm not sure it's worth coming to that... ;)<br>
<span class="HOEnZb"><font color="#888888"><br>
- David<br>
</font></span><div class="HOEnZb"><div class="h5"><br></div></div></blockquote><div><br>Ah true, I had forgotten about the issue with defensive programming.<br><br>I had admit that my personal style is *not* to fall out from a switch over an enum (but return instead) so that a fallout is necessarily "invalid", but I can definitely understand that not all switches are so coded, many people like having big functions ;)<br>
<br>I will let the discussion of policy to people with a much larger experience on either CLang or very large code bases, and in exchange I'd like to spawn off the discussion of how to "ressucitate" the original goal of the -Wswitch warning:<br>
<br>=> detecting switches over an enum where one (a few ?) enumerators have been forgotten<br><br>At the moment -Wswitch is not triggered whenever there is a default clause. Still as we saw, this default clause can be there purely because the values of the enum are not necessarily limited to the enumerators (sigh), and may not indicate that *all other* values have been *consciously* discarded.<br>
<br>There are two things that hints that this is not conscious:<br>1. A single enumerator is missing<br>2. And the file containing the switch is (significantly, say a few seconds) older than the file containing the enum<br>
<br>I think the first point is quite reasonnable. Using a default clause to discard a *single* enumerator seems dubious, at worst it can simply be enumerated right before the default clause easily enough.<br><br>The second point restricts this first to protect from installed libraries (where all files are copied in a matter of second) and really center around human error. A simple *touch* on the file containing the switch solves the issue. With this protection in place, we might be willing to move from a *single* missing enumerator toward a *few* (10% ?) to cover the case where one adds several enumerators at once.<br>
<br>On the other hand, I do not know if we want to introduce a dependency on file timestamps to produce warnings, I would certainly understand a certain reluctance here!<br><br>-- Matthieu<br></div></div><br>