[cfe-dev] Question about usage of Python bindings to clang

Jonathan Coe via cfe-dev cfe-dev at lists.llvm.org
Wed Jan 20 16:28:51 PST 2016


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

https://github.com/jbcoe/clang_cpp_code_model

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.

regards,

Jon

On 20 January 2016 at 14:11, Russell Keith-Magee via cfe-dev <
cfe-dev at lists.llvm.org> wrote:

> 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:
>
> https://gist.github.com/freakboy3742/5826ef09789dfba9042e
>
> 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.
>
> Yours,
> Russ Magee %-)
>
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20160121/fbd4361d/attachment.html>


More information about the cfe-dev mailing list