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

Richard legalize at xmission.com
Sun Mar 22 10:38:57 PDT 2015


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.
-- 
"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