C functions renamed with a macro not part of the main file?


I’m working on a parser for C headers. I need to find all functions declared in a single header, but this header may include additional headers and I want to ignore functions from these additional headers. I use a recursive AST visitor to find out all Decls in the header.
To make a difference between functions declared in my header, or declared in included headers,
I use:

if ( d->getKind() == clang::Decl::Function )
clang::FunctionDecl *fdecl =
clang::dyn_castclang::FunctionDecl ( d );

clang::SourceLocation loc = d->getLocation();
if ( ast->getSourceManager().isFromMainFile ( loc ) )
// function declaration is in the main file, keep it

This works well, but if I have some code like:

#define foo_my_func foo_my_func_safe
#define foo_my_func foo_my_func_unsafe

void foo_my_func(void);

Then the renamed function foo_my_func_unsafe is found in the file…
But ast->getSourceManager().isFromMainFile ( loc ) returns false. The
function is hence filtered even though it’s actually declared in the main header.

What’s the best way to check if a function (or a class, or any decl) is
declared in the main file (the single C header in my case), taking in account this
macro renaming case?

Thank you for your help,


See SourceManager::getExpansionLoc().