I need to rewrite a body of C/C++ code to inject structure definitions automatically when they are used. Specifically, I need to recognize function bodies of the form:
int func(struct STRUCT_x_y *args) { /* access args->field here */ }
... and generate the structure declaration prior to the function's body, e.g.:
struct STRUCT_x_y {
int field;
};
int func(struct STRUCT_x_y *args) { /* access args->field here */ }
When trying to use Clang's rewriter to insert the structure declaration (e.g. following this skeleton program), I get errors because the original program text doesn't compile without these declarations -- the function attempts to access fields on the undefined structure.
Is it possible to use Clang's rewriter on a program that isn't valid C++? I can even place a further restriction -- the function body is invalid, but the rest of the program is fine.
(I can, of course, hack together some ugly solution that uses regular expressions to sort-of-detect the function signature and generate the structure, but I'd really rather harness the power of Clang's parser and rewriter.)
What I ended up doing is just making two passes through the program. During the first pass, I used a null diagnoser so that all errors and warnings are ignored. I collected the methods whose signatures indicated that a struct needs to be generated. After the first pass was done, I generated all the structures and ran the program through a second pass, this time with the diagnosers.
Generally speaking, Clang's AST is immutable and doesn't really allow tree modifications other than the pure text-based rewriting. But the approach above worked reasonably well for us. If you are curious about context, this is part of the IOVisor BCC project.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With