<font size=2 face="sans-serif">Hello David - </font>
<br>
<br><font size=2 face="sans-serif">Thanks for the information - especially
regarding ipython. I hadn't seen that before.</font>
<br>
<br><font size=2 face="sans-serif">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:</font>
<br>
<br><tt><font size=2>#include <stddef.h></font></tt>
<br><tt><font size=2>void foo() {</font></tt>
<br><tt><font size=2> size_t a;</font></tt>
<br><tt><font size=2>}</font></tt>
<br>
<br><font size=2 face="sans-serif">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.</font>
<br>
<br><font size=2 face="sans-serif">If I were to get the FunctionDecl cursor
and then visit its children, I would get the following tree:</font>
<br>
<br><tt><font size=2>FunctionDecl</font></tt>
<br><tt><font size=2>CompoundStmt</font></tt>
<br><tt><font size=2> DeclStmt</font></tt>
<br><tt><font size=2> VarDecl</font></tt>
<br><tt><font size=2> TypeRef</font></tt>
<br>
<br><font size=2 face="sans-serif">What I really want is to be able to
walk up the tree, instead of only down.</font>
<br>
<br><font size=2 face="sans-serif">Thanks - </font>
<br>
<br><font size=2 face="sans-serif">- Nick</font>
<br>
<br>
<br>
<br><font size=1 color=#5f5f5f face="sans-serif">From:
</font><font size=1 face="sans-serif">David Röthlisberger
<david@rothlis.net></font>
<br><font size=1 color=#5f5f5f face="sans-serif">To:
</font><font size=1 face="sans-serif">Nick Beer <nick.beer@ni.com></font>
<br><font size=1 color=#5f5f5f face="sans-serif">Cc:
</font><font size=1 face="sans-serif">cfe-dev@cs.uiuc.edu</font>
<br><font size=1 color=#5f5f5f face="sans-serif">Date:
</font><font size=1 face="sans-serif">08/01/2012 04:28 AM</font>
<br><font size=1 color=#5f5f5f face="sans-serif">Subject:
</font><font size=1 face="sans-serif">Re: [cfe-dev]
Obtaining the parent of a given libclang CXCursor</font>
<br><font size=1 color=#5f5f5f face="sans-serif">Sent by:
</font><font size=1 face="sans-serif">David Rothlisberger
<drothlis@gmail.com></font>
<br>
<hr noshade>
<br>
<br>
<br><tt><font size=2>On 30 Jul 2012, at 19:41, Nick Beer wrote:<br>
> given a position inside a function, I'd like to be able to get the<br>
> cursor from that position, and then look at that cursor's parents
until<br>
> I find the function declaration.<br>
<br>
Hi Nick<br>
<br>
Have a look at clang_getCursorLexicalParent and<br>
clang_getCursorSemanticParent.<br>
<br>
By the way, ipython is an excellent way to explore this API. Here's how<br>
I found the above:<br>
<br>
$ export PYTHONPATH=/path/to/clang/bindings/python<br>
$ ipython<br>
::: import clang.cindex<br>
::: index = clang.cindex.Index.create()<br>
::: tu = index.parse('example.cpp')<br>
::: cs = tu.cursor.get_children()<br>
::: c = cs.next(); print c.spelling # Ignore the first few
implicit typedefs<br>
::: c = cs.next(); print c.spelling<br>
::: c = cs.next(); print c.spelling<br>
::: c = cs.next(); print c.spelling<br>
::: cs = c.get_children()<br>
::: c = cs.next()<br>
::: c.<TAB><br>
c.canonical c.get_definition
c.result_type<br>
c.data
c.get_usr
c.semantic_parent<br>
c.displayname c.hash
c.spelling<br>
c.enum_type c.is_definition
c.translation_unit<br>
c.enum_value c.is_static_method
c.type<br>
c.extent
c.kind
c.underlying_typedef_type<br>
c.from_location c.lexical_parent
c.xdata<br>
c.from_result c.location
<br>
c.get_children c.objc_type_encoding
<br>
<br>
Then look in clang/bindings/python/clang/cindex.py to find the names<br>
of the corresponding C functions.<br>
<br>
Hope this helps!<br>
<br>
--Dave.<br>
<br>
</font></tt>
<br>