Distinguishing between implicit and explicit bool to int casts

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Distinguishing between implicit and explicit bool to int casts

Neil Nelson via cfe-dev
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?

_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: Distinguishing between implicit and explicit bool to int casts

Neil Nelson via cfe-dev
In C++ at least, there seems to be a difference:

|-VarDecl 0x7da9f60 <cast.cc:1:1, col:14> col:5 x 'int' cinit
| `-CStyleCastExpr 0x7daa040 <col:9, col:14> 'int' <NoOp>
|   `-ImplicitCastExpr 0x7daa028 <col:14> 'int' <IntegralCast>
|     `-CXXBoolLiteralExpr 0x7daa000 <col:14> '_Bool' true
`-VarDecl 0x7daa078 <line:2:1, col:9> col:5 y 'int' cinit
  `-ImplicitCastExpr 0x7daa0f0 <col:9> 'int' <IntegralCast>
    `-CXXBoolLiteralExpr 0x7daa0d8 <col:9> '_Bool' true

One has a CStyleCastExpr, one doesn't.

On Mon, May 9, 2016 at 1:15 PM, Michael Layzell via cfe-dev <[hidden email]> wrote:
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?

_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev



_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: Distinguishing between implicit and explicit bool to int casts

Neil Nelson via cfe-dev
It's a bit of a quirk of Clang's AST representation, to be sure - but, yeah, basically I think you have to check that there's no explicit cast around the implicit cast to find the things you're looking for.

On Fri, May 13, 2016 at 11:50 AM, Michael Layzell <[hidden email]> wrote:
Indeed, there is a difference, but I don't understand why the implicit cast is occurring. I could theoretically look for the pattern of the no-op CStyleCastExpr around the ImplicitCastExpr, but that seems like a bit of a hack. The cast isn't really implicit in this situation I would think.

On Mon, May 9, 2016 at 1:18 PM, David Blaikie <[hidden email]> wrote:
In C++ at least, there seems to be a difference:

|-VarDecl 0x7da9f60 <cast.cc:1:1, col:14> col:5 x 'int' cinit
| `-CStyleCastExpr 0x7daa040 <col:9, col:14> 'int' <NoOp>
|   `-ImplicitCastExpr 0x7daa028 <col:14> 'int' <IntegralCast>
|     `-CXXBoolLiteralExpr 0x7daa000 <col:14> '_Bool' true
`-VarDecl 0x7daa078 <line:2:1, col:9> col:5 y 'int' cinit
  `-ImplicitCastExpr 0x7daa0f0 <col:9> 'int' <IntegralCast>
    `-CXXBoolLiteralExpr 0x7daa0d8 <col:9> '_Bool' true

One has a CStyleCastExpr, one doesn't.

On Mon, May 9, 2016 at 1:15 PM, Michael Layzell via cfe-dev <[hidden email]> wrote:
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?

_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev





_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev