I need to use lightweight instrumentation tools (say, using some existing Python or maybe C pre-processor framework) to systematically insert statements in if-conditions of C functions:
For example, let foo be the C function:
int foo(int x){
if (x>3)
return x;
}
I need to transform it to
int foo(int x){
if (x>3){
print ("%d", x);
return x;
}
}
I have tried using LLVM, but it does not integrate better with other parts of the code. Here, I would prefer a Python/C preprocessor framework solution. Thanks for your ideas.
You could consider using GCC MELT or perhaps the GCC Python Plugin to customize the GCC compiler to transform its Gimple representation. It would be a complete solution, but I am not sure it qualifies as "lightweight" (because you need to understand the details of GCC internals), but neither is a Clang/LLVM approach. You'll probably need more than a week of work (notably to learn about the internals of GCC, or of Clang/LLVM)
BTW, your problem is less easy than what you believe, since
int foobar(int x) { return (x>3)?(x*6):(x*x); }
contains a conditional. Also, your original function is exactly equivalent to
int foo(int x){
while (x>3) // this while is done 0 or 1 time
return x;
}
and it is easy to find many other ways to express it.
So a purely syntactic approach would never work completely. Hence, you cannot hope for something "light".
(my opinion is that C code instrumentation is never a lightweight problem)