<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Sun, Mar 22, 2015 at 10:38 AM, Richard <span dir="ltr"><<a href="mailto:legalize@xmission.com" target="_blank">legalize@xmission.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">When analyzing ternary operator and if statement conditional expressions,<br>
I'm trying to determine whether or not the expression is directly a boolean<br>
expression or an expression that is implicitly convertible to bool.<br>
<br>
Example:<br>
<br>
        int i = 1;<br>
        return (i > 10) ? true : false;<br>
<br>
I have a clang-tidy check that transforms this to:<br>
<br>
        int i = 1;<br>
        return i > 10;<br>
<br>
That works great when the conditional expression is an explicit boolean<br>
expression.  When the conditional expression is implicitly converted<br>
to bool, then things get a little more interesting.  Here is some actual<br>
code from llvm/lib/Support/APFloat.cpp b/lib/Support/APFloat.cpp:<br>
<br>
  subtract ^= (sign ^ rhs.sign) ? true : false;<br>
<br>
'sign' and 'rhs.sign' are both one bit fields of type 'unsigned int'.<br>
'subtract' is a function parameter of type bool.  The type of the<br>
conditional expression is implicitly converted to bool.  The type of<br>
the right hand side of the xor-assign expression is bool.<br>
<br>
With the simplistic replacement strategy shown above, this becomes:<br>
<br>
  subtract ^= (sign ^ rhs.sign);<br>
<br>
Now the type of the right hand side of the xor-assign expression is<br>
unsigned int.<br>
<br>
How can I identify these implicit boolean statements?  When I enqure<br>
the type of the conditional expression in these cases by using<br>
Expr::getType(), they both tell me that they are bool.<br></blockquote><div><br>If you ast-dump (or is it dump-ast? I forget) the clang invocation, you should see the AST that builds these and I believe you'll see that the RHS is an ImplicitConversionExpr or something like that, which is what's hiding the underlying type. There are utility functions for stripping implicit casts and parentheses (utilities are probably in ASTContext).<br><br>- David<br> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<span class="HOEnZb"><font color="#888888">--<br>
"The Direct3D Graphics Pipeline" free book <<a href="http://tinyurl.com/d3d-pipeline" target="_blank">http://tinyurl.com/d3d-pipeline</a>><br>
     The Computer Graphics Museum <<a href="http://ComputerGraphicsMuseum.org" target="_blank">http://ComputerGraphicsMuseum.org</a>><br>
         The Terminals Wiki <<a href="http://terminals.classiccmp.org" target="_blank">http://terminals.classiccmp.org</a>><br>
  Legalize Adulthood! (my blog) <<a href="http://LegalizeAdulthood.wordpress.com" target="_blank">http://LegalizeAdulthood.wordpress.com</a>><br>
_______________________________________________<br>
cfe-dev mailing list<br>
<a href="mailto:cfe-dev@cs.uiuc.edu">cfe-dev@cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev</a><br>
</font></span></blockquote></div><br></div></div>