Skip to content

Commit

Permalink
Address issue #196 to better deal with passing pointer types
Browse files Browse the repository at this point in the history
  • Loading branch information
mvandervoord committed Oct 30, 2019
1 parent 9a0bf38 commit 7615806
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
8 changes: 7 additions & 1 deletion lib/cmock_generator_plugin_return_thru_ptr.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,13 @@ def mock_function_declarations(function)
function[:args].each do |arg|
if (@utils.ptr_or_str?(arg[:type]) and not arg[:const?])
lines << "#define #{function[:name]}_ReturnThruPtr_#{arg[:name]}(#{arg[:name]})"
lines << " #{function[:name]}_CMockReturnMemThruPtr_#{arg[:name]}(__LINE__, #{arg[:name]}, sizeof(*#{arg[:name]}))\n"
# If the pointer type actually contains an asterisk, we can do sizeof the type (super safe), otherwise
# we need to do a sizeof the dereferenced pointer (which could be a problem if give the wrong size
if (arg[:type][-1] == '*')
lines << " #{function[:name]}_CMockReturnMemThruPtr_#{arg[:name]}(__LINE__, #{arg[:name]}, sizeof(#{arg[:type][0..-2]}))\n"
else
lines << " #{function[:name]}_CMockReturnMemThruPtr_#{arg[:name]}(__LINE__, #{arg[:name]}, sizeof(*#{arg[:name]}))\n"
end
lines << "#define #{function[:name]}_ReturnArrayThruPtr_#{arg[:name]}(#{arg[:name]}, cmock_len)"
lines << " #{function[:name]}_CMockReturnMemThruPtr_#{arg[:name]}(__LINE__, #{arg[:name]}, (int)(cmock_len * (int)sizeof(*#{arg[:name]})))\n"
lines << "#define #{function[:name]}_ReturnMemThruPtr_#{arg[:name]}(#{arg[:name]}, cmock_size)"
Expand Down
2 changes: 1 addition & 1 deletion test/unit/cmock_generator_plugin_return_thru_ptr_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def complex_func_expect

expected =
"#define Pine_ReturnThruPtr_tofu(tofu)" +
" Pine_CMockReturnMemThruPtr_tofu(__LINE__, tofu, sizeof(*tofu))\n" +
" Pine_CMockReturnMemThruPtr_tofu(__LINE__, tofu, sizeof(int))\n" +
"#define Pine_ReturnArrayThruPtr_tofu(tofu, cmock_len)" +
" Pine_CMockReturnMemThruPtr_tofu(__LINE__, tofu, (int)(cmock_len * (int)sizeof(*tofu)))\n" +
"#define Pine_ReturnMemThruPtr_tofu(tofu, cmock_size)" +
Expand Down

0 comments on commit 7615806

Please sign in to comment.