libclang Discrepancy in getting the range of a Variable Declaration.

Hello,

I’m using libclang to search through CXCursors to match variable references to their definitions.

To do this, I’m storing a list of the definitions, and whenever I come across a reference, I call ‘clang_getCursorDefinition’ and compare for equality. For my equality check, I just see if the CXSourceRange’s of the two cursors are equal.

This works well unless I have a definition like:

int n, sum = 0;

In this case, when I encounter the definition CXCursor through my tree search (through clang_visitChildren), the range covers ‘sum = 0’. But when I call ‘clang_getCursorDefinition’ on a reference, the range for the returned CXCursor covers ‘int n, sum = 0’.

Is there a way to get the more precise range from a cursor referencing the variable? Or is there a simpler way to compare for equality between a definition cursor and a reference cursor for the same variable?

Thanks, Peter

Hello,

I'm using libclang to search through CXCursors to match variable references to their definitions.

To do this, I'm storing a list of the definitions, and whenever I come across a reference, I call 'clang_getCursorDefinition' and compare for equality. For my equality check, I just see if the CXSourceRange's of the two cursors are equal.

This works well unless I have a definition like:

    int n, sum = 0;

In this case, when I encounter the definition CXCursor through my tree search (through clang_visitChildren), the range covers 'sum = 0'. But when I call 'clang_getCursorDefinition' on a reference, the range for the returned CXCursor covers 'int n, sum = 0'.

Is there a way to get the more precise range from a cursor referencing the variable? Or is there a simpler way to compare for equality between a definition cursor and a reference cursor for the same variable?

I'm assuming clang_equalCursors() did not return the expected value ?
We should look into that first if that is the case.