Identifying Cursors from included files.

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.

Thanks, Peter

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:

You might want to start looking here:

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.