Skip to content

Commit

Permalink
Merge pull request ThrowTheSwitch#453 from NovaNekmit/NovaNekmit-patch-1
Browse files Browse the repository at this point in the history
Make ReturnMemThruPtr pointers const
  • Loading branch information
mvandervoord authored Nov 23, 2023
2 parents c548629 + 6d9236d commit 9192a95
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 11 deletions.
13 changes: 9 additions & 4 deletions lib/cmock_generator_plugin_return_thru_ptr.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,18 @@ def initialize(config, utils)
@config = config
end

def ptr_to_const(arg_type)
# replace last "*" with " const*"
arg_type.gsub(/(.*)\*/, '\1 const*')
end

def instance_typedefs(function)
lines = ''
function[:args].each do |arg|
next unless @utils.ptr_or_str?(arg[:type]) && !(arg[:const?])

lines << " char ReturnThruPtr_#{arg[:name]}_Used;\n"
lines << " #{arg[:type]} ReturnThruPtr_#{arg[:name]}_Val;\n"
lines << " #{ptr_to_const(arg[:type])} ReturnThruPtr_#{arg[:name]}_Val;\n"
lines << " size_t ReturnThruPtr_#{arg[:name]}_Size;\n"
end
lines
Expand Down Expand Up @@ -47,7 +52,7 @@ def mock_function_declarations(function)
lines << " #{function[:name]}_CMockReturnMemThruPtr_#{arg[:name]}(__LINE__, #{arg[:name]}, cmock_len * sizeof(*#{arg[:name]}))\n"
lines << "#define #{function[:name]}_ReturnMemThruPtr_#{arg[:name]}(#{arg[:name]}, cmock_size)"
lines << " #{function[:name]}_CMockReturnMemThruPtr_#{arg[:name]}(__LINE__, #{arg[:name]}, cmock_size)\n"
lines << "void #{function[:name]}_CMockReturnMemThruPtr_#{arg[:name]}(UNITY_LINE_TYPE cmock_line, #{arg[:type]} #{arg[:name]}, size_t cmock_size);\n"
lines << "void #{function[:name]}_CMockReturnMemThruPtr_#{arg[:name]}(UNITY_LINE_TYPE cmock_line, #{ptr_to_const(arg[:type])} #{arg[:name]}, size_t cmock_size);\n"
end
lines
end
Expand All @@ -59,7 +64,7 @@ def mock_interfaces(function)
arg_name = arg[:name]
next unless @utils.ptr_or_str?(arg[:type]) && !(arg[:const?])

lines << "void #{func_name}_CMockReturnMemThruPtr_#{arg_name}(UNITY_LINE_TYPE cmock_line, #{arg[:type]} #{arg_name}, size_t cmock_size)\n"
lines << "void #{func_name}_CMockReturnMemThruPtr_#{arg_name}(UNITY_LINE_TYPE cmock_line, #{ptr_to_const(arg[:type])} #{arg_name}, size_t cmock_size)\n"
lines << "{\n"
lines << " CMOCK_#{func_name}_CALL_INSTANCE* cmock_call_instance = " \
"(CMOCK_#{func_name}_CALL_INSTANCE*)CMock_Guts_GetAddressFor(CMock_Guts_MemEndOfChain(Mock.#{func_name}_CallInstance));\n"
Expand All @@ -81,7 +86,7 @@ def mock_implementation(function)
lines << " if (cmock_call_instance->ReturnThruPtr_#{arg_name}_Used)\n"
lines << " {\n"
lines << " UNITY_TEST_ASSERT_NOT_NULL(#{arg_name}, cmock_line, CMockStringPtrIsNULL);\n"
lines << " memcpy((void*)#{arg_name}, (void*)cmock_call_instance->ReturnThruPtr_#{arg_name}_Val,\n"
lines << " memcpy((void*)#{arg_name}, (const void*)cmock_call_instance->ReturnThruPtr_#{arg_name}_Val,\n"
lines << " cmock_call_instance->ReturnThruPtr_#{arg_name}_Size);\n"
lines << " }\n"
end
Expand Down
44 changes: 37 additions & 7 deletions test/unit/cmock_generator_plugin_return_thru_ptr_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
:return => test_return[:string],
:contains_ptr? => false}

# void Pine(int chicken, const int beef, int *tofu)
# void Pine(int chicken, const int beef, int *tofu, const char** buffer)
@complex_func = {:name => "Pine",
:args => [{ :type => "int",
:name => "chicken",
Expand All @@ -35,6 +35,10 @@
{ :type => "int*",
:name => "tofu",
:ptr? => true,
},
{ :type => "char**",
:name => "bean_buffer",
:ptr? => true,
}],
:return => test_return[:void],
:contains_ptr? => true }
Expand Down Expand Up @@ -66,6 +70,7 @@ def complex_func_expect
@utils.expect :ptr_or_str?, false, ['int']
@utils.expect :ptr_or_str?, true, ['const int*']
@utils.expect :ptr_or_str?, true, ['int*']
@utils.expect :ptr_or_str?, true, ['char**']
end

def void_ptr_func_expect
Expand All @@ -88,11 +93,14 @@ def void_ptr_func_expect
assert_equal("", returned)
end

it "add to tyepdef structure mock needs of functions of style 'void func(int chicken, int* pork)'" do
it "add to tyepdef structure mock needs of functions of style 'void func(int chicken, const int beef, int* pork, char** bean_buffer)'" do
complex_func_expect()
expected = " char ReturnThruPtr_tofu_Used;\n" +
" int* ReturnThruPtr_tofu_Val;\n" +
" size_t ReturnThruPtr_tofu_Size;\n"
" int const* ReturnThruPtr_tofu_Val;\n" +
" size_t ReturnThruPtr_tofu_Size;\n" +
" char ReturnThruPtr_bean_buffer_Used;\n" +
" char* const* ReturnThruPtr_bean_buffer_Val;\n" +
" size_t ReturnThruPtr_bean_buffer_Size;\n"
returned = @cmock_generator_plugin_return_thru_ptr.instance_typedefs(@complex_func)
assert_equal(expected, returned)
end
Expand All @@ -113,7 +121,14 @@ def void_ptr_func_expect
" Pine_CMockReturnMemThruPtr_tofu(__LINE__, tofu, cmock_len * sizeof(*tofu))\n" +
"#define Pine_ReturnMemThruPtr_tofu(tofu, cmock_size)" +
" Pine_CMockReturnMemThruPtr_tofu(__LINE__, tofu, cmock_size)\n" +
"void Pine_CMockReturnMemThruPtr_tofu(UNITY_LINE_TYPE cmock_line, int* tofu, size_t cmock_size);\n"
"void Pine_CMockReturnMemThruPtr_tofu(UNITY_LINE_TYPE cmock_line, int const* tofu, size_t cmock_size);\n"+
"#define Pine_ReturnThruPtr_bean_buffer(bean_buffer)" +
" Pine_CMockReturnMemThruPtr_bean_buffer(__LINE__, bean_buffer, sizeof(char*))\n" +
"#define Pine_ReturnArrayThruPtr_bean_buffer(bean_buffer, cmock_len)" +
" Pine_CMockReturnMemThruPtr_bean_buffer(__LINE__, bean_buffer, cmock_len * sizeof(*bean_buffer))\n" +
"#define Pine_ReturnMemThruPtr_bean_buffer(bean_buffer, cmock_size)" +
" Pine_CMockReturnMemThruPtr_bean_buffer(__LINE__, bean_buffer, cmock_size)\n" +
"void Pine_CMockReturnMemThruPtr_bean_buffer(UNITY_LINE_TYPE cmock_line, char* const* bean_buffer, size_t cmock_size);\n"

returned = @cmock_generator_plugin_return_thru_ptr.mock_function_declarations(@complex_func)
assert_equal(expected, returned)
Expand Down Expand Up @@ -146,14 +161,23 @@ def void_ptr_func_expect
complex_func_expect();

expected =
"void Pine_CMockReturnMemThruPtr_tofu(UNITY_LINE_TYPE cmock_line, int* tofu, size_t cmock_size)\n" +
"void Pine_CMockReturnMemThruPtr_tofu(UNITY_LINE_TYPE cmock_line, int const* tofu, size_t cmock_size)\n" +
"{\n" +
" CMOCK_Pine_CALL_INSTANCE* cmock_call_instance = " +
"(CMOCK_Pine_CALL_INSTANCE*)CMock_Guts_GetAddressFor(CMock_Guts_MemEndOfChain(Mock.Pine_CallInstance));\n" +
" UNITY_TEST_ASSERT_NOT_NULL(cmock_call_instance, cmock_line, CMockStringPtrPreExp);\n" +
" cmock_call_instance->ReturnThruPtr_tofu_Used = 1;\n" +
" cmock_call_instance->ReturnThruPtr_tofu_Val = tofu;\n" +
" cmock_call_instance->ReturnThruPtr_tofu_Size = cmock_size;\n" +
"}\n\n" +
"void Pine_CMockReturnMemThruPtr_bean_buffer(UNITY_LINE_TYPE cmock_line, char* const* bean_buffer, size_t cmock_size)\n" +
"{\n" +
" CMOCK_Pine_CALL_INSTANCE* cmock_call_instance = " +
"(CMOCK_Pine_CALL_INSTANCE*)CMock_Guts_GetAddressFor(CMock_Guts_MemEndOfChain(Mock.Pine_CallInstance));\n" +
" UNITY_TEST_ASSERT_NOT_NULL(cmock_call_instance, cmock_line, CMockStringPtrPreExp);\n" +
" cmock_call_instance->ReturnThruPtr_bean_buffer_Used = 1;\n" +
" cmock_call_instance->ReturnThruPtr_bean_buffer_Val = bean_buffer;\n" +
" cmock_call_instance->ReturnThruPtr_bean_buffer_Size = cmock_size;\n" +
"}\n\n"

returned = @cmock_generator_plugin_return_thru_ptr.mock_interfaces(@complex_func).join("")
Expand All @@ -167,8 +191,14 @@ def void_ptr_func_expect
" if (cmock_call_instance->ReturnThruPtr_tofu_Used)\n" +
" {\n" +
" UNITY_TEST_ASSERT_NOT_NULL(tofu, cmock_line, CMockStringPtrIsNULL);\n" +
" memcpy((void*)tofu, (void*)cmock_call_instance->ReturnThruPtr_tofu_Val,\n" +
" memcpy((void*)tofu, (const void*)cmock_call_instance->ReturnThruPtr_tofu_Val,\n" +
" cmock_call_instance->ReturnThruPtr_tofu_Size);\n" +
" }\n" +
" if (cmock_call_instance->ReturnThruPtr_bean_buffer_Used)\n" +
" {\n" +
" UNITY_TEST_ASSERT_NOT_NULL(bean_buffer, cmock_line, CMockStringPtrIsNULL);\n" +
" memcpy((void*)bean_buffer, (const void*)cmock_call_instance->ReturnThruPtr_bean_buffer_Val,\n" +
" cmock_call_instance->ReturnThruPtr_bean_buffer_Size);\n" +
" }\n"

returned = @cmock_generator_plugin_return_thru_ptr.mock_implementation(@complex_func).join("")
Expand Down

0 comments on commit 9192a95

Please sign in to comment.