From 1b1218f16ff26f3dba5952f22448811df88a9fc3 Mon Sep 17 00:00:00 2001 From: "Pascal J. Bourguignon" Date: Thu, 10 Aug 2023 12:44:31 +0200 Subject: [PATCH] Removed including standard libraries from mock sources. - Introduced CMock_memset and CMock_memcpy - Updated tests. --- lib/cmock_file_writer.rb | 2 ++ lib/cmock_generator.rb | 9 +++------ lib/cmock_generator_plugin_return_thru_ptr.rb | 2 +- lib/cmock_generator_utils.rb | 4 ++-- src/cmock.c | 17 +++++++++++++++++ src/cmock.h | 2 ++ test/unit/cmock_generator_main_test.rb | 7 ++----- ...ock_generator_plugin_return_thru_ptr_test.rb | 2 +- test/unit/cmock_generator_utils_test.rb | 14 +++++++------- 9 files changed, 37 insertions(+), 22 deletions(-) diff --git a/lib/cmock_file_writer.rb b/lib/cmock_file_writer.rb index f30c44b2..d7849d08 100644 --- a/lib/cmock_file_writer.rb +++ b/lib/cmock_file_writer.rb @@ -22,6 +22,8 @@ def create_file(filename, subdir) full_file_name_temp = "#{@config.mock_path}/#{subdir + '/' if subdir}#{filename}.new" full_file_name_done = "#{@config.mock_path}/#{subdir + '/' if subdir}#{filename}" + $stderr.puts "Creating #{full_file_name_done.inspect}" unless (@config.verbosity < 2) + File.open(full_file_name_temp, 'w') do |file| yield(file, filename) end diff --git a/lib/cmock_generator.rb b/lib/cmock_generator.rb index c3896dfb..815e1b81 100644 --- a/lib/cmock_generator.rb +++ b/lib/cmock_generator.rb @@ -100,6 +100,8 @@ def create_using_statement(file, function) def create_mock_header_file(mock_project) if @include_inline == :include @file_writer.create_file(mock_project[:module_name] + (mock_project[:module_ext]), mock_project[:folder]) do |file, _filename| + file << "/* Source File: #{mock_project[:source]} */\n" + file << "/* Normalized source (without inlines). */\n" file << mock_project[:parsed_stuff][:normalized_source] end end @@ -209,11 +211,6 @@ def create_mock_header_footer(header) def create_source_header_section(file, filename, mock_project) header_file = (mock_project[:folder] || '') + filename.gsub('.c', mock_project[:module_ext]) file << "/* AUTOGENERATED FILE. DO NOT EDIT. */\n" unless mock_project[:parsed_stuff][:functions].empty? - file << "#include \n" - file << "#include \n" - unless @exclude_setjmp_h - file << "#include \n" - end file << "#include \"cmock.h\"\n" @includes_c_pre_header.each { |inc| file << "#include #{inc}\n" } file << "#include \"#{header_file}\"\n" @@ -283,7 +280,7 @@ def create_mock_init_function(file, mock_project) def create_mock_destroy_function(file, mock_project) file << "void #{mock_project[:clean_name]}_Destroy(void)\n{\n" file << " CMock_Guts_MemFreeAll();\n" - file << " memset(&Mock, 0, sizeof(Mock));\n" + file << " CMock_memset(&Mock, 0, sizeof(Mock));\n" file << mock_project[:parsed_stuff][:functions].collect { |function| @plugins.run(:mock_destroy, function) }.join unless @fail_on_unexpected_calls diff --git a/lib/cmock_generator_plugin_return_thru_ptr.rb b/lib/cmock_generator_plugin_return_thru_ptr.rb index 96b20035..4739684e 100644 --- a/lib/cmock_generator_plugin_return_thru_ptr.rb +++ b/lib/cmock_generator_plugin_return_thru_ptr.rb @@ -70,7 +70,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 << " CMock_memcpy((void*)#{arg_name}, (void*)cmock_call_instance->ReturnThruPtr_#{arg_name}_Val,\n" lines << " cmock_call_instance->ReturnThruPtr_#{arg_name}_Size);\n" lines << " }\n" end diff --git a/lib/cmock_generator_utils.rb b/lib/cmock_generator_utils.rb index ecbc37e5..5af37afe 100644 --- a/lib/cmock_generator_utils.rb +++ b/lib/cmock_generator_utils.rb @@ -51,7 +51,7 @@ def code_add_base_expectation(func_name, global_ordering_supported = true) lines = " CMOCK_MEM_INDEX_TYPE cmock_guts_index = CMock_Guts_MemNew(sizeof(CMOCK_#{func_name}_CALL_INSTANCE));\n" lines << " CMOCK_#{func_name}_CALL_INSTANCE* cmock_call_instance = (CMOCK_#{func_name}_CALL_INSTANCE*)CMock_Guts_GetAddressFor(cmock_guts_index);\n" lines << " UNITY_TEST_ASSERT_NOT_NULL(cmock_call_instance, cmock_line, CMockStringOutOfMemory);\n" - lines << " memset(cmock_call_instance, 0, sizeof(*cmock_call_instance));\n" + lines << " CMock_memset(cmock_call_instance, 0, sizeof(*cmock_call_instance));\n" lines << " Mock.#{func_name}_CallInstance = CMock_Guts_MemChain(Mock.#{func_name}_CallInstance, cmock_guts_index);\n" lines << " Mock.#{func_name}_IgnoreBool = (char)0;\n" if @ignore || @ignore_stateless lines << " cmock_call_instance->LineNumber = cmock_line;\n" @@ -75,7 +75,7 @@ def code_assign_argument_quickly(dest, arg) else assert_expr = "sizeof(#{arg[:name]}) == sizeof(#{arg[:type]}) ? 1 : -1" comment = "/* add #{arg[:type]} to :treat_as_array if this causes an error */" - " memcpy((void*)(&#{dest}), (void*)(&#{arg[:name]}),\n" \ + " CMock_memcpy((void*)(&#{dest}), (void*)(&#{arg[:name]}),\n" \ " sizeof(#{arg[:type]}[#{assert_expr}])); #{comment}\n" end end diff --git a/src/cmock.c b/src/cmock.c index e6a11971..94d73043 100644 --- a/src/cmock.c +++ b/src/cmock.c @@ -229,3 +229,20 @@ void CMock_Guts_MemFreeFinal(void) #endif } +void CMock_memset(void* ptr, int value, size_t num) +{ + size_t i; + for (i = 0; i < num; i++) + { + ((char*)ptr)[i] = (char)value; + } +} + +void CMock_memcpy(void* ptr, const void* src, size_t num) +{ + size_t i; + for (i = 0; i < num; i++) + { + ((char*)ptr)[i] = ((char*)src)[i]; + } +} diff --git a/src/cmock.h b/src/cmock.h index 45bab182..1f943fcf 100644 --- a/src/cmock.h +++ b/src/cmock.h @@ -43,5 +43,7 @@ CMOCK_MEM_INDEX_TYPE CMock_Guts_MemBytesFree(void) CMOCK_FUNCTION_ATTR(pure); CMOCK_MEM_INDEX_TYPE CMock_Guts_MemBytesUsed(void) CMOCK_FUNCTION_ATTR(pure); void CMock_Guts_MemFreeAll(void); void CMock_Guts_MemFreeFinal(void); +void CMock_memset(void* ptr, int value, size_t num); +void CMock_memcpy(void* ptr, const void* src, size_t num); #endif /* end of CMOCK_FRAMEWORK_H */ diff --git a/test/unit/cmock_generator_main_test.rb b/test/unit/cmock_generator_main_test.rb index 1a66c0a5..f7229ba8 100644 --- a/test/unit/cmock_generator_main_test.rb +++ b/test/unit/cmock_generator_main_test.rb @@ -343,9 +343,6 @@ def helper_create_header_top_with_opt_includes_form_config_and_plugin(ext) { :name => "tres", :args => [] } ] expected = [ "/* AUTOGENERATED FILE. DO NOT EDIT. */\n", - "#include \n", - "#include \n", - "#include \n", "#include \"cmock.h\"\n", "#include \"MockPoutPoutFish.h\"\n", "\n", @@ -486,7 +483,7 @@ def helper_create_header_top_with_opt_includes_form_config_and_plugin(ext) output = [] expected = [ "void MockPoutPoutFish_Destroy(void)\n{\n", " CMock_Guts_MemFreeAll();\n", - " memset(&Mock, 0, sizeof(Mock));\n", + " CMock_memset(&Mock, 0, sizeof(Mock));\n", "}\n\n" ] @@ -503,7 +500,7 @@ def helper_create_header_top_with_opt_includes_form_config_and_plugin(ext) output = [] expected = [ "void MockPoutPoutFish_Destroy(void)\n{\n", " CMock_Guts_MemFreeAll();\n", - " memset(&Mock, 0, sizeof(Mock));\n", + " CMock_memset(&Mock, 0, sizeof(Mock));\n", " uno", " GlobalExpectCount = 0;\n", " GlobalVerifyOrder = 0;\n", diff --git a/test/unit/cmock_generator_plugin_return_thru_ptr_test.rb b/test/unit/cmock_generator_plugin_return_thru_ptr_test.rb index 03fc5e4e..70a796cd 100644 --- a/test/unit/cmock_generator_plugin_return_thru_ptr_test.rb +++ b/test/unit/cmock_generator_plugin_return_thru_ptr_test.rb @@ -125,7 +125,7 @@ def complex_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" + + " CMock_memcpy((void*)tofu, (void*)cmock_call_instance->ReturnThruPtr_tofu_Val,\n" + " cmock_call_instance->ReturnThruPtr_tofu_Size);\n" + " }\n" diff --git a/test/unit/cmock_generator_utils_test.rb b/test/unit/cmock_generator_utils_test.rb index 15356638..a1268218 100644 --- a/test/unit/cmock_generator_utils_test.rb +++ b/test/unit/cmock_generator_utils_test.rb @@ -61,7 +61,7 @@ " CMOCK_MEM_INDEX_TYPE cmock_guts_index = CMock_Guts_MemNew(sizeof(CMOCK_Apple_CALL_INSTANCE));\n" + " CMOCK_Apple_CALL_INSTANCE* cmock_call_instance = (CMOCK_Apple_CALL_INSTANCE*)CMock_Guts_GetAddressFor(cmock_guts_index);\n" + " UNITY_TEST_ASSERT_NOT_NULL(cmock_call_instance, cmock_line, CMockStringOutOfMemory);\n" + - " memset(cmock_call_instance, 0, sizeof(*cmock_call_instance));\n" + + " CMock_memset(cmock_call_instance, 0, sizeof(*cmock_call_instance));\n" + " Mock.Apple_CallInstance = CMock_Guts_MemChain(Mock.Apple_CallInstance, cmock_guts_index);\n" + " cmock_call_instance->LineNumber = cmock_line;\n" output = @cmock_generator_utils_simple.code_add_base_expectation("Apple") @@ -73,7 +73,7 @@ " CMOCK_MEM_INDEX_TYPE cmock_guts_index = CMock_Guts_MemNew(sizeof(CMOCK_Apple_CALL_INSTANCE));\n" + " CMOCK_Apple_CALL_INSTANCE* cmock_call_instance = (CMOCK_Apple_CALL_INSTANCE*)CMock_Guts_GetAddressFor(cmock_guts_index);\n" + " UNITY_TEST_ASSERT_NOT_NULL(cmock_call_instance, cmock_line, CMockStringOutOfMemory);\n" + - " memset(cmock_call_instance, 0, sizeof(*cmock_call_instance));\n" + + " CMock_memset(cmock_call_instance, 0, sizeof(*cmock_call_instance));\n" + " Mock.Apple_CallInstance = CMock_Guts_MemChain(Mock.Apple_CallInstance, cmock_guts_index);\n" + " Mock.Apple_IgnoreBool = (char)0;\n" + " cmock_call_instance->LineNumber = cmock_line;\n" + @@ -88,7 +88,7 @@ " CMOCK_MEM_INDEX_TYPE cmock_guts_index = CMock_Guts_MemNew(sizeof(CMOCK_Apple_CALL_INSTANCE));\n" + " CMOCK_Apple_CALL_INSTANCE* cmock_call_instance = (CMOCK_Apple_CALL_INSTANCE*)CMock_Guts_GetAddressFor(cmock_guts_index);\n" + " UNITY_TEST_ASSERT_NOT_NULL(cmock_call_instance, cmock_line, CMockStringOutOfMemory);\n" + - " memset(cmock_call_instance, 0, sizeof(*cmock_call_instance));\n" + + " CMock_memset(cmock_call_instance, 0, sizeof(*cmock_call_instance));\n" + " Mock.Apple_CallInstance = CMock_Guts_MemChain(Mock.Apple_CallInstance, cmock_guts_index);\n" + " Mock.Apple_IgnoreBool = (char)0;\n" + " cmock_call_instance->LineNumber = cmock_line;\n" + @@ -108,7 +108,7 @@ expected3 = " cmock_call_instance->Expected_Kiwi = Kiwi;\n" arg4 = { :name => "Lime", :const? => false, :type => 'LIME_T', :ptr? => false } - expected4 = " memcpy((void*)(&cmock_call_instance->Expected_Lime), (void*)(&Lime),\n" + + expected4 = " CMock_memcpy((void*)(&cmock_call_instance->Expected_Lime), (void*)(&Lime),\n" + " sizeof(LIME_T[sizeof(Lime) == sizeof(LIME_T) ? 1 : -1])); /* add LIME_T to :treat_as_array if this causes an error */\n" assert_equal(expected1, @cmock_generator_utils_simple.code_add_an_arg_expectation(arg1)) @@ -134,7 +134,7 @@ " cmock_call_instance->ReturnThruPtr_Kiwi_Used = 0;\n" arg4 = { :name => "Lime", :const? => false, :type => 'LIME_T', :ptr? => false } - expected4 = " memcpy((void*)(&cmock_call_instance->Expected_Lime), (void*)(&Lime),\n" + + expected4 = " CMock_memcpy((void*)(&cmock_call_instance->Expected_Lime), (void*)(&Lime),\n" + " sizeof(LIME_T[sizeof(Lime) == sizeof(LIME_T) ? 1 : -1])); /* add LIME_T to :treat_as_array if this causes an error */\n" + " cmock_call_instance->IgnoreArg_Lime = 0;\n" @@ -158,7 +158,7 @@ expected = "void CMockExpectParameters_Melon(CMOCK_Melon_CALL_INSTANCE* cmock_call_instance, stuff);\n" + "void CMockExpectParameters_Melon(CMOCK_Melon_CALL_INSTANCE* cmock_call_instance, stuff)\n{\n" + " cmock_call_instance->Expected_MyIntPtr = MyIntPtr;\n" + - " memcpy((void*)(&cmock_call_instance->Expected_MyMyType), (void*)(&MyMyType),\n" + + " CMock_memcpy((void*)(&cmock_call_instance->Expected_MyMyType), (void*)(&MyMyType),\n" + " sizeof(MY_TYPE[sizeof(MyMyType) == sizeof(MY_TYPE) ? 1 : -1])); /* add MY_TYPE to :treat_as_array if this causes an error */\n" + " cmock_call_instance->Expected_MyStr = MyStr;\n" + "}\n\n" @@ -176,7 +176,7 @@ " cmock_call_instance->Expected_MyIntPtr_Depth = MyIntPtr_Depth;\n" + " cmock_call_instance->IgnoreArg_MyIntPtr = 0;\n" + " cmock_call_instance->ReturnThruPtr_MyIntPtr_Used = 0;\n" + - " memcpy((void*)(&cmock_call_instance->Expected_MyMyType), (void*)(&MyMyType),\n" + + " CMock_memcpy((void*)(&cmock_call_instance->Expected_MyMyType), (void*)(&MyMyType),\n" + " sizeof(MY_TYPE[sizeof(MyMyType) == sizeof(MY_TYPE) ? 1 : -1])); /* add MY_TYPE to :treat_as_array if this causes an error */\n" + " cmock_call_instance->IgnoreArg_MyMyType = 0;\n" + " cmock_call_instance->Expected_MyStr = MyStr;\n" +