[cfe-dev] [libclang] Is there any method like get_children (python cindex) available for libclang or alternative to print the tree structure by the traversal method?
carlosviansi at gmail.com
Sun Jun 17 09:45:41 PDT 2012
Hi David, thanks for replying.
Yes I did narrow down the problem to a more specific line, if I run this
code which was provided at  and I adapted a small piece of :
def callexpr_visitor(node, parent, userdata):
if node.kind == clang.cindex.CursorKind.CALL_EXPR:
print 'Found %s [line=%s, col=%s]' % (
return 2 # means continue visiting recursively
index = clang.cindex.Index.create()
tu = index.parse(sys.argv)
print 'Translation unit:', tu.spelling
*f = tu.get_includes()
Adding the method in red makes it crash, That is, the tu.get_includes
crashes. I look at the *cindex.py* but I cant figured what is wrong it
seemed to me like a normal use of the method.
The reasons I stayed on C was because I saw few comments that python was
still catching up on making available the methods provided by libclang, and
given that libclang itself is not expected to provide all the features from
clang itself, I saw myself going more far away from the functionalities
going that way (I dont know C++ which is what kept me away from Clang AST
usage). But the strongest reason was the problem itself that happened
(otherwise I would have just used that code for my purposes).
2012/6/17 David Röthlisberger <david at rothlis.net>
> On 17 Jun 2012, at 08:24, Carlos Andrade wrote:
> > I managed to create a transversal using libclang on C, map it to code
> and get its types but now I am stuck trying to output it like a tree like
> it is done at . For some unknown reason  crashes here with the
> warning "Python quit unexpectedly while using libclang.dylib plug-in.
> (Running on an OS Lion).
> >  https://gist.github.com/2503232
> Can you narrow down the failure to a specific line of python + line of
> the input translation unit? I suspect it might be something to do with
> accessing a property of an invalid type.
> > On my C version I can get each node being printed, but what I wanted is
> what  does: Print the structure of the tree (who is child of who) and I
> was trying to do so by tying to verify if the child is NULL but I cant find
> any method available to do so.  Seems to suggest this doesnt even exist
> on libclang C.
> > 
> Have you considered writing your program in python? I found that using
> the ipython shell (with its tab-completion) was really helpful for
> exploring the libclang API.
> (I apologise for not actually answering your question, but I don't know
> enough about the libclang C interface.)
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the cfe-dev