[cfe-dev] discriminating explicit boolean expressions from implicit boolean expressions in the AST

Richard legalize at xmission.com
Tue Mar 24 17:05:48 PDT 2015


In article <CAENS6EvT8Z0Y5Vn8bHh6XKHiBa8G1JSFLo3EL===wXExDVFKWA at mail.gmail.com>,
    David Blaikie <dblaikie at gmail.com> writes:

> On Sun, Mar 22, 2015 at 10:38 AM, Richard <legalize at xmission.com> wrote:
> 
> > [...]
> > 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).

Thanks David, I've prototyped this and it works pretty well.  I want
to make sure I cover all the different ways that an implicit bool
conversion can take place.  The ones I can think of were the
arithmetic expressions that are implicitly converted to bool by
comparing them to zero (integral types, pointers, floating-point
types) and objects (class, struct, union) that provide an operator
bool() that does the conversion.  For the former I am replacing
'if (e) return true; else return false;' with 'return e != 0;
and for the latter I am replacing with 'return bool(e);'.

Am I missing anything?
-- 
"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>



More information about the cfe-dev mailing list