[cfe-dev] C functions renamed with a macro not part of the main file?

Eli Friedman eli.friedman at gmail.com
Tue Oct 23 02:46:38 PDT 2012


On Tue, Oct 23, 2012 at 1:42 AM, Pascal Ognibene <pognibene at gmail.com> wrote:
> Hello,
>
> 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_cast<clang::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:
>
> #ifdef CHECK_ERRORS
> #define foo_my_func foo_my_func_safe
> #else
> #define foo_my_func foo_my_func_unsafe
> #endif
>
> 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?

See SourceManager::getExpansionLoc().

-Eli



More information about the cfe-dev mailing list