[cfe-dev] Determining macros used in a function or SourceRange (using clang plugin)

Eric Bayer via cfe-dev cfe-dev at lists.llvm.org
Fri Sep 23 14:20:36 PDT 2016


Hi,

I have a project that I've been working on to determine dependencies in 
code and mock/stub them.  Mostly I'm using a recursive visitor to 
datamine these things.  I would really like to be able to figure out 
what macros are used in a function as well (or SourceRange would be even 
more helpful.)   Just to be clear I'm doing this as a clang frontend 
plugin (currently stopping at syntax check).  I know that I can capture 
these PPCallbacks and store away their SourceLocations etc during 
preprocessing, but based on what is done in error messages I would 
imagine that the information is there somewhere already... I'd rather 
not reinvent the wheel if something else will work already.

For example:

#define BAR "%s"
#define FOO(x) printf( BAR, x )
#define DOY 3
void func1() {
     const char *lvar="func1";
     FOO(lvar);
}
void func2() {
     printf("%d", DOY);
}

What I want to be able to do is isolate func1() and determine that FOO 
and BAR were both used and what macro they were pointing to at the time 
they were used.  What I'm hoping is that there is way to iterate the 
SourceRange to find where the text pieces came from (with enough info to 
know what the macro name/definition was).  I didn't see an obvious way 
to do this.  Then again, maybe I'm going down the wrong path and there's 
an entirely easier direction?

Anyhow, even some hints in the right direction would be greatly appreciated.

Thanks in advance,

    -Eric





More information about the cfe-dev mailing list