Code instrumentation in Clang?

I'm trying to get up to speed on using Clang by doing a bit of dynamic
code analysis by instrumenting C (and maybe C++) code where I take a
source file and generate an instrumented output. I'd like to add a
function call at the beginning of any block to mark statement coverage
and also change all boolean expressions to call some function so I can
track that too. For example:
  foo = a && (b || c);
would become something like:
  foo = EXPR_AND(a, EXPR_OR(b, c));
and thus I can track all combinations of conditions that occur. I'd
need to track where in the source code I am too, and save that info
seperately. This is a bit beyond what gcov provides.
I'm gussing that using a RecursiveASTVisitor would be the best
approach, but whatever approach I take, is there an easy way to output
the C code for each node I visit?
Any suggestions of what to look at to accomplish something like this
would be most appreciated!

Thanks!
Robert

Why would you want to add instrumentation in Clang and not in LLVM?
For an example of LLVM instrumentation pass, take a look at lib/Transforms/Instrumentation/AddressSanitizer.cpp.

–kcc

I would like to instrument the C/C++ source so that it can be compiled
under most any compiler, not just Clang. The only way I see to do
that would be either through libclang or maybe with a recursive AST
visitor. I was hoping to find a good example to work from, assuming I
need a Rewriter. If there's something I'm missing, feel free to
correct me!

Thanks,
Robert

Robert Ankeney wrote:

I'm trying to get up to speed on using Clang by doing a bit of dynamic
code analysis by instrumenting C (and maybe C++) code where I take a
source file and generate an instrumented output. I'd like to add a
function call at the beginning of any block to mark statement coverage
and also change all boolean expressions to call some function so I can
track that too. For example:
foo = a && (b || c);
would become something like:
foo = EXPR_AND(a, EXPR_OR(b, c));
and thus I can track all combinations of conditions that occur. I'd
need to track where in the source code I am too, and save that info
seperately. This is a bit beyond what gcov provides.
I'm gussing that using a RecursiveASTVisitor would be the best
approach, but whatever approach I take, is there an easy way to output
the C code for each node I visit?
Any suggestions of what to look at to accomplish something like this
would be most appreciated!

TestCocoon does exactly that:

http://www.testcocoon.org/

Thanks,

Steve.

Thanks for the pointer. TestCocoon looks promising, though I'm
looking at performing Modified Condition/Decision Coverage (MC/DC) and
perhaps perform some metrics. In any case, I was looking at doing
something to get more involved in Clang. I'll have a look at
TestCocoon and maybe I can contribute there too.

Robert

TestCocoon page now is replaced by http://www.froglogic.com/squish/coco/
But it seems a commencial tool. No source code.