ThrowTheSwitch / CMock

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

Weird mock generated when there is a structure with the function pointers in it #399

Open GlebPlekhotko opened 2 years ago

GlebPlekhotko commented 2 years ago

Hello!


I have a header file in my project which contains structure declaration in it. Some fields there are pointers to the functions. See the following example:

typedef struct BasicListStruct {
    const uint16_t freeBytes;
    const uint16_t itemsNumber;

    void* (*append)(struct BasicListStruct *this, uint16_t itemSize);
    int32_t (*clear)(struct BasicListStruct *this);
    void* (*fetch)(struct BasicListStruct *this, uint16_t itemIndex);
    void* (*insert)(struct BasicListStruct *this, uint16_t itemIndex, uint16_t itemSize);
    int32_t (*remove)(struct BasicListStruct *this, uint16_t itemIndex);
} BasicList;

int32_t basicListInit(BasicList *this, uint16_t sizeInBytes, uint16_t headerSizeInBytes);

If feed such header to the CMock then:

  1. It takes much more time to generate a mock. I believe, it is about 30 seconds contrary to almost instant in other cases.
  2. Outcome cannot be compiled. GCC compiler reports "error: macro names must be identifiers".

If open a generated file, one may see something like this:

#define *_Expect(this, itemSize) *_CMockExpect(__LINE__, this, itemSize)
void *_CMockExpect(UNITY_LINE_TYPE cmock_line, * append)(struct BasicListStruct* this, uint16_t itemSize);

So, it's no surprise the compiler cannot deal with it.


A little study showed, that it is "void*" return value type for some functions is the root cause of this issue. It somehow confuses CMock and makes it generate aforementioned weird stuff. Moreover, any other "pointer to" type does the same job.

It is possible to work around this issue by declaring a custom "typedef" for either a structure member function or return type. But maybe you will find some time to fix it natively.


Thanks for great tool and good luck!