[cfe-dev] Distinguishing between implicit and explicit bool to int casts

Michael Layzell via cfe-dev cfe-dev at lists.llvm.org
Mon May 9 13:15:46 PDT 2016


I was trying to write a clang static analysis for detecting implicit bool
to int casts. Namely, I wanted to allow forms like:

int x = (int) true;

But disallow forms like:

int x = true;

Unfortunately, it seems like both of these generate implicit cast AST
nodes, namely I get the following AST tree for the explicitly casted form:

    `-DeclStmt 0x7fae2407a118 <line:51:3, col:30>
      `-VarDecl 0x7fae2407a050 <col:3, col:26> col:7 x 'int' cinit
        `-CStyleCastExpr 0x7fae2407a0f0 <col:21, col:26> 'int' <NoOp>
          `-ImplicitCastExpr 0x7fae2407a0d8 <col:26> 'int' <IntegralCast>
            `-CXXBoolLiteralExpr 0x7fae2407a0b0 <col:26> '_Bool' true

How should I distinguish between the two forms reliably, such that explicit
casts are never matched, but implicit casts are matched, when an explicit
cast still inserts an implicit cast, and then just becomes a NoOp?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20160509/1e3c7bbd/attachment.html>


More information about the cfe-dev mailing list