[cfe-dev] discriminating explicit boolean expressions from implicit boolean expressions in the AST
David Blaikie
dblaikie at gmail.com
Sun Mar 22 10:48:11 PDT 2015
On Sun, Mar 22, 2015 at 10:38 AM, Richard <legalize at xmission.com> wrote:
> When analyzing ternary operator and if statement conditional expressions,
> I'm trying to determine whether or not the expression is directly a boolean
> expression or an expression that is implicitly convertible to bool.
>
> Example:
>
> int i = 1;
> return (i > 10) ? true : false;
>
> I have a clang-tidy check that transforms this to:
>
> int i = 1;
> return i > 10;
>
> That works great when the conditional expression is an explicit boolean
> expression. When the conditional expression is implicitly converted
> to bool, then things get a little more interesting. Here is some actual
> code from llvm/lib/Support/APFloat.cpp b/lib/Support/APFloat.cpp:
>
> subtract ^= (sign ^ rhs.sign) ? true : false;
>
> 'sign' and 'rhs.sign' are both one bit fields of type 'unsigned int'.
> 'subtract' is a function parameter of type bool. The type of the
> conditional expression is implicitly converted to bool. The type of
> the right hand side of the xor-assign expression is bool.
>
> With the simplistic replacement strategy shown above, this becomes:
>
> subtract ^= (sign ^ rhs.sign);
>
> Now the type of the right hand side of the xor-assign expression is
> unsigned int.
>
> How can I identify these implicit boolean statements? When I enqure
> the type of the conditional expression in these cases by using
> Expr::getType(), they both tell me that they are bool.
>
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).
- David
> --
> "The Direct3D Graphics Pipeline" free book <
> http://tinyurl.com/d3d-pipeline>
> The Computer Graphics Museum <http://ComputerGraphicsMuseum.org>
> The Terminals Wiki <http://terminals.classiccmp.org>
> Legalize Adulthood! (my blog) <http://LegalizeAdulthood.wordpress.com>
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20150322/4ac08677/attachment.html>
More information about the cfe-dev
mailing list