gmock generator based on libclang

678 views
Skip to first unread message

Krzysztof Jusiak

unread,
Jul 5, 2013, 4:00:57 PM7/5/13
to googl...@googlegroups.com
Hi, if someone is looking for a mock generator based on libclang, which could be used in huge projects and be easily integrated with the build system, I have recently written one:


its written in python (tested with 2.7 and 3.2) using libclang and python bindings (tested with 3.2)

features:
    * its reliable, since its using clang compiler
    * its fast (tested on project ~200 kloc -> generation of mocs took 3-5 seconds on common laptop)
    * output file might be easily adopted to the project via configuration file
    * easy integration with the project build system -> generate mocks files for each interface, limited to the project (for example via project namespace), from given files
    * generate pretty output (one mock per file)
    * handle c++ operators:

           virtual int operator()(int, double);
      ...
           virtual int operator()(int arg0, double arg1) {  return function_call_or_cast_operator(arg0, arg1); 
           MOCK_METHOD2(function_call_or_cast_operator, int(int, double));
    * easy to extend (~200 lines of code)

usage:
    ./gmock.py <config_file> <dir_for_generated_mocks> <limit_to_interfaces_within_decl> files...

integration with build system:
     find project -iname "*.h" -or -iname "*.hpp" | xargs gmock.py "project/conf/gmock.conf" "project/generated/mocks" "Project"

example output:
      project/generated/mocks/I1Mock.hpp
      project/generated/mocks/I2Mock.hpp
      ...

hopefully, it will be useful for you as well,
Chris

Zhanyong Wan (λx.x x)

unread,
Jul 7, 2013, 6:32:21 PM7/7/13
to Krzysztof Jusiak, Google C++ Mocking Framework
Thanks for sharing, Krzysztof!



--
 
---
You received this message because you are subscribed to the Google Groups "Google C++ Mocking Framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to googlemock+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
Zhanyong

Manuel Klimek

unread,
Jul 8, 2013, 1:38:28 AM7/8/13
to Krzysztof Jusiak, Google C++ Mocking Framework
Hi Krzysztof,

does this work with the compilation database support from libclang? Otherwise, how do you specify the compile command lines (include paths etc).

Cheers,
/Manuel


Krzysztof Jusiak

unread,
Jul 8, 2013, 11:21:23 AM7/8/13
to googl...@googlegroups.com, Krzysztof Jusiak
Hi Manuel,

so basically clang options might be given after '--', example:
    ./gmock.py -d "test/mocks" file1.hpp file2.hpp -- -D LINUX -Iproject/externals

generally mock generation its more syntax driven, so whole knowledge of types isnt necessary (all includes dont have to be visible for the script),
on the other hand knowledge of defines might be necessary, but such possibility is available using '--' as a separator in command line arguments

thanks for pointing it out tho :)

Chris

Manuel Klimek

unread,
Jul 8, 2013, 12:52:44 PM7/8/13
to Krzysztof Jusiak, Google C++ Mocking Framework
On Mon, Jul 8, 2013 at 5:21 PM, Krzysztof Jusiak <krzy...@jusiak.net> wrote:
Hi Manuel,

so basically clang options might be given after '--', example:
    ./gmock.py -d "test/mocks" file1.hpp file2.hpp -- -D LINUX -Iproject/externals

generally mock generation its more syntax driven, so whole knowledge of types isnt necessary (all includes dont have to be visible for the script),
on the other hand knowledge of defines might be necessary, but such possibility is available using '--' as a separator in command line arguments

thanks for pointing it out tho :)

Cool. The problem is that we often have code where there are user defined types in the interface, which I think would be needed to get a decent AST. libclang has the ability to use a compilation database so the user doesn't need to specify any command line flags (because that's quickly tedious), and it automatically does TheRightThing... 


Cheers,
/Manuel

Krzysztof Jusiak

unread,
Jul 9, 2013, 8:15:56 AM7/9/13
to googl...@googlegroups.com, Krzysztof Jusiak
yea, true that, what can be automated, should be automated :) ill take a look then, libclang supports it so it shouldnt be that hard, but oh, we shall see,
anyway im not sure if I got your case, so you do have user defined types in the interfaces which are given via compilation flags and there are different for different files ?

Manuel Klimek

unread,
Jul 9, 2013, 8:25:23 AM7/9/13
to Krzysztof Jusiak, Google C++ Mocking Framework
On Tue, Jul 9, 2013 at 2:15 PM, Krzysztof Jusiak <krzy...@jusiak.net> wrote:
yea, true that, what can be automated, should be automated :) ill take a look then, libclang supports it so it shouldnt be that hard, but oh, we shall see,

Cool.
 
anyway im not sure if I got your case, so you do have user defined types in the interfaces which are given via compilation flags and there are different for different files ?

No, just interfaces where the methods return types that come in via #includes of other files. For clang to generate a correct AST it must be able to find those includes. In our projects include paths are a pretty complex setup.
Reply all
Reply to author
Forward
0 new messages