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

David Blaikie dblaikie at gmail.com
Wed Mar 25 10:48:19 PDT 2015


On Tue, Mar 24, 2015 at 5:05 PM, Richard <legalize at xmission.com> wrote:

>
> 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;
>

Maybe special case chars for "return e != '\0';" ? hard to tell if it's
chars being used as bytes or chars being used as textual characters,
though... - and hard to tell what the common codebase convention is as to
whether "return e;" is better than "return e != 0;" etc... - I'd be sort of
inclined to just use the expression directly if it's valid (eg: for builtin
types and implicit conversion operators).


> and for the latter I am replacing with 'return bool(e);'.
>

If the type's conversion operator is implicit, it might be fine to "return
e;"
If the type's conversion operator is explicit, it might be more suitable to
use static_cast<bool>. There is "return !!e;" but that's lame... otherwise
there's probably more explicit type-specific ways to test the object, but
you won't be able to suggest those automatically. (eg: "return o !=
nullptr;" for a unique_ptr, etc)


> 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>
> _______________________________________________
> 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/20150325/e03d328d/attachment.html>


More information about the cfe-dev mailing list