Question about usage of Python bindings to clang

Hi all,

I’ve got some questions about the data returned by the Python bindings to clang.

When clang is invoked with “-Xclang -ast-dump -fsyntax-only” to dump the AST for a source file, the output of clang appears to provide much richer output than the Python bindings exposes. I’m guessing this is because the relevant details are deeply embedded in the cursor object returned by the Python API, but I’m not sure where I should be looking for this extra detail.

In particular, when clang returns a CXXConstructExpr, the Python API returns CursorKind.CALL_EXPR, which is the same node type returned by a normal function call - so it isn’t possible to differentiate a normal method call from a constructor.

Similarly, when clang returns a MaterializeTemporaryExpr or ImplicitCastExpr, the Python API returns a CursorKind.UNEXPOSED_EXPR.

How do you interrogate the Python API to determine the richer node types that clang is returning?

If it helps, I’ve posted full example code and output in a gist:

This contains three files:

  • a sample.cpp file with a simple class, and a function using that class

  • An AST dump of the file provided by clang

  • The AST produced by the Python bindings, generated by walking the tree, printing the node kind, type, return type and spelling, and then doing the same for each child.

Russ Magee %-)

I have worked on an early-stage python c++ code model which is built on top of the libclang python API.

The code-model is good enough to drive simple code generators and makes it simple to extract a lot of AST info. It is incomplete.

I don’t know what you’re trying to do, but maybe the code-model above could be useful.



Hi Jonathan,

Thanks - that looks similar to what I’ve already got:

The problem I’m hitting isn’t with the method declarations - those are pretty easy to sort out. My problem is with the contents of the COMPOUND_STMT type, which is the body of the methods, and the CALL_EXPR node type in particular.

Russ Magee %-)