ThrowTheSwitch / CMock

CMock - Mock/stub generator for C
http://throwtheswitch.org
MIT License
672 stars 273 forks source link

reduce time of creating mock file #358

Closed lukzeg closed 1 year ago

lukzeg commented 3 years ago

Hi

I'm noticing strange problem when I'm creating mock files for file which body is for instance:

#ifndef ZUMZUM
#define ZUMZUM

#include <stddef.h>
#include <stdint.h>

#ifdef __cplusplus
#define NULLPTR 0
#define EXTERN_C_BEGIN extern "C"
{
#define EXTERN_C_END }
#else
#define NULLPTR (void *)NULL
#define EXTERN_C_BEGIN
#define EXTERN_C_END
#endif

#ifndef CHECK
# ifdef SOMETHING
#  if CHECK_SOMETHING == ZUMZUM_SOMETHING
#    define OMOM (1)
#  elif CHECK_SOMETHING == ZUMZUM_SOMETHING_EXTRA
#    define OMOM (2)
#  endif
# endif
#endif

void fun( void );

#endif

The cmock generation took:

real 0m17.064s user 0m16.453s sys 0m0.219s

Which is quietly long, for such short file. After digging in source code I noticed that we can speed up time of generation of mocks by switching places of two regex lib/cmock_header_parser.rb ln 233 from

    source.gsub!(/^\s*#.*/, '')
    source.gsub!(/extern\s+\"C\"\s*\{/, '')```

to
```   # remove preprocessor statements and extern "C"
    source.gsub!(/extern\s+\"C\"\s*\{/, '')
    source.gsub!(/^\s*#.*/, '')

decrease time from :

real 0m16.431s user 0m16.188s sys 0m0.234s

to: real 0m0.409s user 0m0.188s sys 0m0.219s

I didn't noticed any regression for whole project and generated mock files for ST 743 SDK. The files compared with diff mocks looks the same. I didn't notice any regression.