Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

clang rewriter: add structure definitions to invalid program

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.)

like image 335
Sasha Goldshtein Avatar asked Nov 15 '25 11:11

Sasha Goldshtein


1 Answers

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.

like image 172
Sasha Goldshtein Avatar answered Nov 17 '25 02:11

Sasha Goldshtein



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!