[cfe-dev] Obtaining the parent of a given libclang CXCursor

Nick Beer nick.beer at ni.com
Wed Aug 1 06:44:30 PDT 2012


Hello David - 

Thanks for the information - especially regarding ipython.  I hadn't seen 
that before.

While clang_getCursorLexicalParent and clang_getCursorSemanticParent are 
helpful under some circumstances, what I'm looking for is a finer grain of 
control.  What I would really like is essentially the opposite of 
clang_visitChildren.  The project I'm working on requires me to be able to 
give various information about the thing that lies at a particular cursor 
location.  For instance, consider the following code:

#include <stddef.h>
void foo() {
    size_t a;
}

If I'm given the position (3, 5) and I ask for the cursor at that 
location, I'm given a TypeRef cursor for the size_t type.  However, what I 
would really like is the VarDecl that scopes that TypeRef.  In order to 
solve this problem generically, it seems I have to go all the way to 
global scope and then clang_visitChildren until I find the TypeRef, all 
while remembering where I was before.

If I were to get the FunctionDecl cursor and then visit its children, I 
would get the following tree:

FunctionDecl
CompoundStmt
    DeclStmt
        VarDecl
            TypeRef

What I really want is to be able to walk up the tree, instead of only 
down.

Thanks - 

- Nick



From:   David Röthlisberger <david at rothlis.net>
To:     Nick Beer <nick.beer at ni.com>
Cc:     cfe-dev at cs.uiuc.edu
Date:   08/01/2012 04:28 AM
Subject:        Re: [cfe-dev] Obtaining the parent of a given libclang 
CXCursor
Sent by:        David Rothlisberger <drothlis at gmail.com>



On 30 Jul 2012, at 19:41, Nick Beer wrote:
> given a position inside a function, I'd like to be able to get the
> cursor from that position, and then look at that cursor's parents until
> I find the function declaration.

Hi Nick

Have a look at clang_getCursorLexicalParent and
clang_getCursorSemanticParent.

By the way, ipython is an excellent way to explore this API. Here's how
I found the above:

   $ export PYTHONPATH=/path/to/clang/bindings/python
   $ ipython
   :::  import clang.cindex
   :::  index = clang.cindex.Index.create()
   :::  tu = index.parse('example.cpp')
   :::  cs = tu.cursor.get_children()
   :::  c = cs.next(); print c.spelling # Ignore the first few implicit 
typedefs
   :::  c = cs.next(); print c.spelling
   :::  c = cs.next(); print c.spelling
   :::  c = cs.next(); print c.spelling
   :::  cs = c.get_children()
   :::  c = cs.next()
   :::  c.<TAB>
   c.canonical                c.get_definition           c.result_type
   c.data                     c.get_usr                  c.semantic_parent
   c.displayname              c.hash                     c.spelling
   c.enum_type                c.is_definition c.translation_unit
   c.enum_value               c.is_static_method         c.type
   c.extent                   c.kind c.underlying_typedef_type
   c.from_location            c.lexical_parent           c.xdata
   c.from_result              c.location 
   c.get_children             c.objc_type_encoding 

Then look in clang/bindings/python/clang/cindex.py to find the names
of the corresponding C functions.

Hope this helps!

--Dave.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20120801/abec9559/attachment.html>


More information about the cfe-dev mailing list