Search code examples
clangllvmllvm-clanglibtooling

How to exculde build-in / system function during function analysis by clang libtooling


I'm trying to analyze functions by using clang libtooling. Here is the source code that I want to analyze:

#include <stdio.h>

int main(){
    int a = 100;
    printf("a==%d", a);
}

when I run my tool to get all the function decl in above files, I found there are a lot of build-in / system functions, like:

decls: 
_IO_cookie_init
 __underflow
 __uflow
 __overflow
 _IO_getc
 _IO_putc
 _IO_feof
 _IO_ferror
 _IO_peekc_locked
 _IO_flockfile
 _IO_funlockfile
 _IO_ftrylockfile
 _IO_vfscanf
 _IO_vfprintf
 _IO_padn
 _IO_sgetn
 _IO_seekoff
 _IO_seekpos
 _IO_free_backup_area
 remove
 rename
 renameat
 tmpfile
 tmpfile64
 tmpnam
 tmpnam_r
 tempnam
 fclose
 fflush
 fflush_unlocked
 fcloseall
 fopen

(I think they are introduced by the header file "stdio.h" )

my question is: How can I get rid of all these built-in/system functions from the "stdio.h" file, or other (system) header files?

Thanks in advance!!!


Solution

  • When you visit a function, check if its location (startLoc or endLoc) is in system header using SourceManagers api 'isInSystemHeader(loc)'

    e.g.:

    Bool VisitFunctionDecl(FunctionDecl * D)
    {
        If(sourceManager.isInSystemHeader(D->getLocStart()))
            return true;
    }
    

    Thanks, Hemant