[cfe-dev] Hacking Clang to generate mock objects

Chandler Carruth chandlerc at google.com
Thu Sep 1 23:21:03 PDT 2011


On Thu, Sep 1, 2011 at 3:36 PM, Dave Elcock <dave.elcock at gmail.com> wrote:

> 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 (http://www.hippomocks.com/wiki/), 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?


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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20110901/9d1481a0/attachment.html>


More information about the cfe-dev mailing list