[cfe-dev] RFC: visiting cursors "backwards" ?

Douglas Gregor dgregor at apple.com
Tue Feb 15 13:17:34 PST 2011

On Feb 15, 2011, at 12:40 PM, Stefan Seefeld wrote:

> libclang allows me to traverse sibling cursors.
> I have run into a use-case where I want to visit the previous sibling 
> cursor. How may I do that ?
> The use-case is this:
> Imagine code such as:
>   struct foo;
>   // Documentation for 'bar'
>   struct bar;
> In Synopsis, I'd like to associate the comment with the following 'bar' 
> declaration. Therefor, I'd like to tokenize the region between 'struct 
> bar' and the previous cursor, so I can read in the comment and store it 
> for later documentation extraction.
> I'd like to do this non-invasively, i.e. without the need for libclang 
> to be aware of even the concept of documentation. As long as it gives me 
> access to "all comments preceding cursor 'X'", I can work things out.

Personally, I'd rather see the "find the declaration to which this comment is attached" logic in the Clang core, exposed via libclang, rather than layered on top of Clang.

> Thus: Is there a way to locate the previous sibling cursor for any given 
> cursor ? (This may be a null cursor, if the given cursor is the first 
> child of its parent. That's fine, as in that case I would start from the 
> parent's start location itself.)

There's no way to do this, aside from walking through all of the parent's children again and keeping track of the previous child. We've generally avoided this since it will add another pointer to the size of every declaration node, and we try to keep the AST small.

	- Doug

More information about the cfe-dev mailing list