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

Pascal Ognibene pognibene at gmail.com
Tue Oct 23 01:42:11 PDT 2012


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?

Thank you for your help,

Pascal
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20121023/ed82fc10/attachment.html>


More information about the cfe-dev mailing list