<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Apr 22, 2016 at 3:57 PM, Hal Finkel <span dir="ltr"><<a href="mailto:hfinkel@anl.gov">hfinkel@anl.gov</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><span class="gmail-">----- Original Message -----<br>
> From: "Richard Trieu via cfe-dev" <<a href="mailto:cfe-dev@lists.llvm.org">cfe-dev@lists.llvm.org</a>><br>
> To: "cfe-dev" <<a href="mailto:cfe-dev@lists.llvm.org">cfe-dev@lists.llvm.org</a>><br>
> Sent: Friday, April 22, 2016 5:47:59 PM<br>
> Subject: [cfe-dev] Which floating point to bool conversions should trigger    warnings?<br>
><br>
> I recently added (r267054) and then revert (r267234) some warnings<br>
> for floating point to bool conversions pending more discussion on<br>
> the topic. From my experience, bool conversions are tricky and a<br>
> source of many bugs, so a warning in this place would be good.<br>
><br>
><br>
> Bool behaves a little differently than other integer types. For<br>
> instance, 0.5 is converted to zero for integer types, except for<br>
> bool which it gets converted to true. Also, bool is the type for<br>
> conditionals and resulting type of logical operators.<br>
><br>
><br>
> However, at least one style guide says to use floating point to bool<br>
> conversion (<br>
> <a href="https://webkit.org/code-style-guidelines/#null-false-and-zero" rel="noreferrer">https://webkit.org/code-style-guidelines/#null-false-and-zero</a> )<br>
><br>
<br>
</span>I don't see where it says that.<br>
<br>
Regardless, I think we should always warn.<br>
<br>
 -Hal<br></blockquote><div><br></div><div>It specifies that checks against 0 should be written without the == 0.  For example:</div><div><br></div><div>  void check(float f) {</div><div>    if (f) { }  // This is the same as "f != 0.0"</div><div>    if (!f) { }  // This is the same as "f == 0.0"<br></div><div>  }</div><div><br></div><div>I don't intend to stop warning, just split the warning into pieces so users can better control which warnings are active.</div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex">
<div><div class="gmail-h5"><br>
><br>
> With that, I hope to get a little more discussion on this topic<br>
> before implementing the floating point to bool conversion warnings<br>
> again. The two factors for the warning are the source of the<br>
> floating point value and where the conversion happens, although<br>
> there may be other factors we need to take into account. Only the<br>
> source has been taken account so far since it is the easier way to<br>
> implement.<br>
><br>
><br>
> Source:<br>
><br>
> Exact floating point literals (0.0, 1.0)<br>
><br>
> Rounded floating point literals (0.5, 4.0)<br>
> Exact compiler time constant (5.0 - 4.0, kZero)<br>
> Inexact compiler time constant (1.0 / 2.0, kHugeNumber)<br>
> Run time values (getFloat())<br>
><br>
><br>
> Location:<br>
> Function Argument<br>
> Assigning/Initializing bool variable<br>
> Return statement of bool returning function<br>
> Condition of if statement, for loop, while loop, or do-while loop<br>
> Condition of conditional operator (?:)<br>
> Operand of logical operators(&&, ||, !)<br>
><br>
><br>
> Currently, exact floating point literals are not warned on, rounded<br>
> floating point literals are under -Wliteral-conversion, and<br>
> everything else is under -Wfloat-conversion.<br>
><br>
><br>
> Note that exclusionary groups may also be useful, for instance<br>
> "-Wfloat-conversion -Wno-some-bool-warning" if that helps filter out<br>
> some of the more noisy warnings.<br>
</div></div>> _______________________________________________<br>
> cfe-dev mailing list<br>
> <a href="mailto:cfe-dev@lists.llvm.org">cfe-dev@lists.llvm.org</a><br>
> <a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" rel="noreferrer">http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev</a><br>
><br>
<span class="gmail-HOEnZb"><font color="#888888"><br>
--<br>
Hal Finkel<br>
Assistant Computational Scientist<br>
Leadership Computing Facility<br>
Argonne National Laboratory<br>
</font></span></blockquote></div><br></div></div>