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

David Röthlisberger david at rothlis.net
Wed Aug 1 08:54:26 PDT 2012


On 1 Aug 2012, at 14:44, Nick Beer wrote:
> 
> Consider the following code: 
> 
> #include <stddef.h> 
> void foo() { 
>     size_t a; 
> } 
> 
> 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. 

I'm afraid I'm stumped. My suggestion of using
clang_getCursor{Lexical,Semantic}Parent won't work. With the example
code you gave, each cursor's parents are: (Lexical Parent, Semantic
Parent)

FunctionDecl  [TranslationUnit, TranslationUnit]
  CompoundStmt  [None, FunctionDecl]
    DeclStmt  [None, None]
      VarDecl  [FunctionDecl, FunctionDecl]
        TypeRef  [None, None]

I don't really know the AST API, so I can't say whether the above parent
values are correct. My (naive) expectation was that the lexical parent
was always the cursor immediately preceding in that hierarchy, but maybe
there's a reason it isn't so.

Cheers,
Dave.





More information about the cfe-dev mailing list