Can I use Clang lib to read function's parameter values which are from preprossing macro?

20 Views Asked by At

I am new to LLVM and Clang internals, hope you could help confirming my plan.

I have the following code:

util.h:

#define LOG_INFO(name, message, ...)     logging(LOG_INFO, state_name, __FILE__, __LINE__, __func__, message, ##__VA_ARGS__)

   
void logging(int priority, const char *name, const char *file_name, int line, const char *function, const char *format, ...);

And here is app.c which uses the macro LOG_INFO:

void foo()
{
    // dummy line
    /*  
     * dummy lines
     *
     */
    LOG_INFO("state100", "the %s dump at foo()", "first");

    int i;
    for (i = 0; i < 6; ++i) {
        LOG_WARNING("state150", "the %d loop", i); 
        LOG_DEBUG("", "i=%d\n", i); 
    }   

    LOG_ERR("state300", "garbage:%s - %d - %ld", "haha", 123, 123000);
}

void bar()
{
    LOG_INFO("state200", "the %s dump at function %s", "first", "bar");
}

int main(int argc, char **argv)
{
    (void)argc;
    (void)argv;

    foo();

    bar();

    LOG_INFO("state900", "Great job!");

    return 0;
}

My plan is to use Clang lib to go over the AST of app.c:

  1. find all the calling of logging()
  2. print out all the values of file_name, line and function at each logging() calling

For #1, since AST is after preprocessing - am I correct? - so AST has logging() instead of LOG_INFO.

For #2, again since AST is after preprocessing, I will be able to access all the parameter values of logging().

Is my understanding right?

0

There are 0 best solutions below