Hacking Clang to generate mock objects

Hello.

I've recently discovered Clang and I've come up with the hair-brained
idea of playing/hacking with it to achieve something akin to
HippoMocks (hippomocks.com), essentially using an
abstract class as a template, and generating stubs and implementations
for pure virtual methods on-the-fly.

I'm imagining writing some sort of extension that could modify vtables
etc, but be inserted into Clang with a command line switch a la LLVM's
transformation passes. I reckon I need to do that before it gets as
far as LLVM itself, just so there is richer type information still
around.

Before I go wasting any more time, is this just plain bonkers? Is it
going to be possible as an external modification, or am I going to be
stuck with a hacked clang executable/libs?

If I'm in with a chance, where would I start looking for documentation
about how Clang builds up vtables etc, and how it's all stitched
together? Is doxygen the best source of that sort of information, or
have I missed some key architecture docs?

Cheers for any help,
Dave.

I’m not quite sure why you’re approaching this problem from the direction you are. Why not generate source code for the mocks instead of trying to synthesize generated code for them based on the original code?

For reference, GoogleMock (which you should look at if you haven’t) creates mock interfaces very easily. The complexity is exactly bounded by the size of the mocked interface, without regard for the complexity of implementation. There is even a (somewhat hacky) python script to generate the mock source code from an existing class.

I could imagine writing a Clang-based tool that examined an AST to generate the mock instead of using Python. It would be less hacky and handle horrible corner cases of the language better. I’m fairly confident that the existing libraries etc in Clang are sufficient to do this.

That said, I work at Google and we have a lot of experience using GoogleMock. I don’t recall ever hearing an engineer using it and complaining that the hacky python script couldn’t generate their mock. Most users I talked to didn’t even know that the python script existed, because they never really needed it. The mock is so easy to implement, they just did it. :: shrug :: I guess in my experience this is not the hard part about mocks and using them in testing. maybe HippoMocks need this type of functionality more.

Anyways, if you want to talk more about the Clang tool for this, by all means carry on. =D Happy to help there. If you want talk more about mocking frameworks etc, or why this tool would be useful or not, feel free to email me off-list, or mail the GoogleMock list.

I'm not quite sure why you're approaching this problem from the direction
you are. Why not generate *source code* for the mocks instead of trying to
synthesize generated code for them based on the original code?

Yep, that would do it for sure, I just wondered if it was possible to
do it without the intermediate step of generating code, and going
straight to inserting extra AST or whatever mid-compile. I'm keen to
make it feel more like .net or java reflection at play than the
"inconvenience" of having to generate more source code (how very 20th
century). Basically, I miss the likes of RhinoMocks for .net and want
a similar level of simplicity and neat tests for C++.

For reference, GoogleMock (which you should look at if you haven't) creates
mock interfaces very easily. The complexity is exactly bounded by the size
of the mocked interface, without regard for the complexity of
implementation. There is even a (somewhat hacky) python script to generate
the mock source code from an existing class.

I'm sure GoogleMock could handle it too, but I've never been that keen
on it. It's a bit too verbose, and it gets tiring mocking every method
explicitly (is that what the python script takes care of?). I'm also
not a big fan of the macro approach, but I guess there's not much of
an alternative with C++, so I'm just exploring other possibilities. If
nothing else, GoogleMock tests look ugly to me, aggressive even!
That's petty, I know.

I could imagine writing a Clang-based tool that examined an AST to generate
the mock instead of using Python. It would be less hacky and handle horrible
corner cases of the language better. I'm fairly confident that the existing
libraries etc in Clang are sufficient to do this.

Yes, I can see it's probably going to fall back to this if I ever
actually write any code, but I'm exploring my "dream" solution first.

Anyways, if you want to talk more about the Clang tool for this, by all
means carry on. =D Happy to help there.

Great, thanks. You could start by pointing me towards some
documentation! Having perused clang.llvm.org, I can't help feeling
there's some missing general architecture docs. There's a good deal of
info in Doxygen and the code itself, but something less detailed to
get me started would be helpful. Is Doxygen my best bet?

As for the "dream" option, where would I go about injecting this
imaginary "pass" to generate the mocks. Is there something similar to
LLVM's transform passes I can hook into?

Cheers,
Dave.