(clang) How to parse macros themselves, getting an ast where possible? -


hi i'm using clang extract information c files. , i'm trying extract values of macros.

e.g. i'd want value '13' or ast (+ (* 3 4) 1):

#define some_constant 3*4+1 

or macro function, i'd want ast e.g. (some_macrofunc (x y) (+ (add4 x) (* y 9))) :

int add4(int q) {return q+4;} #define some_macrofunc(x,y) add4(x)+y*9 

so far i've managed iterate through macros via 'preprocessor' class's macro_begin() , macro_end() functions.

then i've gotten macro names, , 'macroinfo' class i've been able whether macro functionlike (including param names) or not. i've got access tokens in macro, able token kind e.g: string_literal, identifier, comma, l_paren, r_paren, etc.

so 2 things:

  1. how access actual value of tokens, rather kinds.

  2. is there way generate ast macros given tokens? 1 way thought parse source code, extract macros, , using names, add code including macros source , reparse ast.

e.g. like:

char *tempsome_constant = some_constant;     void tempsome_macrofunc(char *x, char *y) {some_macrofunc(x,y);} 

though method seems hacky, , have trouble macros aren't statement or expression like.

thanks.

edit: clarify want expanded body (until no macros left, non macro tokens) of each macro.

edit2 solved what:

if anyones interested intend expand body of macro manually.

"preprocessor.getspelling(token)" token value.

"preprocessor.getidentifiertable().get(stringref(spelling))" identinfo token.

and using "clang\lib\lex\ppmacroexpansion.cpp" reference.

still thinking how pass parser without reparsing whole source tree, shouldn't difficult figure out.

thanks ira baxter discussion, helped me iron out problem.

i working on similar. use clang front end collecting context (w.r.t. class, function etc.) in macro defined , use (pseudo) expression parser figure out if macro-body valid expression or not. ultimate goal transform macro c++ declaration. got paper accepted icsm-2012 explains how achieve this.

the tools -the demacrofier- used rid of macros hosted here

ira baxter's examples insightful in way macros used. however, %age of macros less \ref(an empirical analysis of c preprocessor use ernst et al.). currently, focusing more on common cases.


Comments

Popular posts from this blog

How do you change vbulletin's home page to the forums instead of the default (CMS or Activity Stream)? -

c++ - Troubles when compiling phash program -