c++ - clang rewriter: add structure definitions to invalid program -
i need rewrite body of c/c++ code inject structure definitions automatically when used. specifically, need recognize function bodies of form:
int func(struct struct_x_y *args) { /* access args->field here */ }
... , generate structure declaration prior function's body, e.g.:
struct struct_x_y { int field; }; int func(struct struct_x_y *args) { /* access args->field here */ }
when trying use clang's rewriter insert structure declaration (e.g. following this skeleton program), errors because original program text doesn't compile without these declarations -- function attempts access fields on undefined structure.
is possible use clang's rewriter on program isn't valid c++? can place further restriction -- function body invalid, rest of program fine.
(i can, of course, hack ugly solution uses regular expressions sort-of-detect function signature , generate structure, i'd rather harness power of clang's parser , rewriter.)
what ended doing making 2 passes through program. during first pass, used null diagnoser errors , warnings ignored. collected methods signatures indicated struct needs generated. after first pass done, generated structures , ran program through second pass, time diagnosers.
generally speaking, clang's ast immutable , doesn't allow tree modifications other pure text-based rewriting. approach above worked reasonably us. if curious context, part of iovisor bcc project.
Comments
Post a Comment