ThrowTheSwitch / CMock

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

Mocking functions with array parameter causes conflicting type error #213

Open oliverbl opened 5 years ago

oliverbl commented 5 years ago

I have some functions which are like the following:

void foo(uint8_t data[PARAM_1][PARAM_2]);

now cmock will generate the following mock function for it: void foo(uint8_t *data);

This causes an error while compiling:

conflicting types for 'foo'

So at the moment I cant use cmock

Im using gcc 4.8.3 as the compiler

huska6 commented 5 years ago

Hello, I'm facing exactly the same issue. Any help would be welcomed.

aadavids commented 5 years ago

I am also having this issue, any advice?

alex116 commented 5 years ago

I have created a small example to reproduce it quickly mock_fail_func_sig.zip

alex116 commented 5 years ago

vendor/ceedling/vendor/cmock/lib/cmock_header_parser.rb around line 225:

arg_list.gsub!(/(\w+)(?:\s*\[\s*\(*[\s\d\w+-]*\)*\s*\])+/,'*\1') # magically turn brackets into asterisks, also match for parentheses that come from macros

magically causes compile errors

alex116 commented 5 years ago

so here's how you can "fix"/hack it for now:

first of all I replaced that line from my previous comment with like 6 of these lines. arg_list.gsub!(/(\w+)(?:\s*\[\s*\(*[\s\d\w+-]*\)*\s*\])/,'*\1') If you note, the "+" is missing in my version. I removed it so it doesn't replace all brackets, it just replaces one bracket at the time with one "*" for each replacing it with 6 lines probably fails if you use more than 6 sets of brackets in one argument. maybe some maintainer can implement this better?

also, in the cmock_header_parser.rb around line 270 or so in function parse_declaration(declaration), after the "if else end" part where it says "#check for var args", I added a line: decl[:original_args] = args.dup before the call to "clean_args". That way I have the original argument saved.

Then in cmock_generator.rb on line 229 in function create_mock_implementation I replaced #args_string = function[:args_string] args_string = function[:original_args]

that way the generated .c mock file has the original args and the .h uses the **

alex116 commented 5 years ago

if you feel bold you may apply this patch:

function_signatures.txt

from your project root: patch -s -p0 < function_signatures.txt

alex116 commented 5 years ago

the regex is making me nauseous. arg_list.gsub!(/(\w+)(?:\s*\[\s*\(*[\s\d\w+-]*\)*\s*\])/,'*\1') Somebody please fix it, for the love of science!

alex116 commented 5 years ago

I forgot to mention, it caused problems when generating *.c files where the argument contained enums. for now I fixed it for me like this:

if function[:original_args].index("[") != nil args_string = function[:original_args] else args_string = function[:args_string] end

oliverbl commented 5 years ago

I forgot to mention, it caused problems when generating *.c files where the argument contained enums. for now I fixed it for me like this:

if function[:original_args].index("[") != nil args_string = function[:original_args] else args_string = function[:args_string] end

In which file did you change this? Thanks for your fix

Edit: Oh wait, I got it..

tbayley-dyson commented 2 years ago

This issue affects me building Ceedling unit tests with GCC compiler version 11.2.0 on Linux. To suppress the build warning, I have added the following compiler flag for unit test builds: Wno-array-parameter

ajpenner commented 1 year ago

Is this feature a candidate for ceedling 0.32? It would be helpful to avoid suppressing gcc warnings, unless absolutely necessary.

droopymiller commented 1 month ago

Hi, I'm running into this issue. Is there a recommended work around or fix at this point? Is the best current solution what @alex116 shared? Thanks!