[cfe-dev] Identifying Cursors from included files.

Jacob Carlborg doob at me.com
Sat Jan 5 04:04:23 PST 2013


On 2013-01-05 11:23, Peter Conn wrote:
> Hello
>
> When I'm looking through cursors using 'clang_visitChildren', how do I
> check whether that cursor comes from an included file.
>
> I have tried:
> CXFile cxf;
> clang_getSpellingLocation(clang_getRangeStart(clang_getCursorExtent(cursor),
> &cxf, NULL, NULL, NULL);
>
> But clang_getCString(clang_getFileName(cxf)) will always return the name
> of the main file.
>
> I have also tried:
> getTranslationUnitSpelling(clang_Cursor_getTranslationUnit(cursor))
>
> and the same applies here.
>
> I would like to be able to distinguish between cursors that are present
> in the original file, and those from included files.

This is how I do it:

auto loc = clang_getCursorLocation(cursor);

CXFile file;
unsigned line;
unsigned column;
unsigned offset;

clang_getSpellingLocation(loc, &file, &line, &column, &offset);

if (file != mainFile)
{
     // cursor is from included file
}

You can get "mainFile" using something like this:

auto mainFileName = "foo.c"; // you will probably get this from the 
command line or somewhere else

auto mainFile = clang_getFile(translationUnit, mainFileName);

You can have a look at my project, DStep:

https://github.com/jacob-carlborg/dstep

You might want to start looking here:

https://github.com/jacob-carlborg/dstep/blob/master/dstep/translator/Translator.d#L63

And here:

https://github.com/jacob-carlborg/dstep/blob/master/dstep/translator/Translator.d#L166

It's written in D with a few wrappers around the Clang C API.

-- 
/Jacob Carlborg




More information about the cfe-dev mailing list