Skip to content

Commit 1b1218f

Browse files
committed
Removed including standard libraries from mock sources.
- Introduced CMock_memset and CMock_memcpy - Updated tests.
1 parent ed29ce3 commit 1b1218f

9 files changed

+37
-22
lines changed

lib/cmock_file_writer.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ def create_file(filename, subdir)
2222

2323
full_file_name_temp = "#{@config.mock_path}/#{subdir + '/' if subdir}#{filename}.new"
2424
full_file_name_done = "#{@config.mock_path}/#{subdir + '/' if subdir}#{filename}"
25+
$stderr.puts "Creating #{full_file_name_done.inspect}" unless (@config.verbosity < 2)
26+
2527
File.open(full_file_name_temp, 'w') do |file|
2628
yield(file, filename)
2729
end

lib/cmock_generator.rb

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ def create_using_statement(file, function)
100100
def create_mock_header_file(mock_project)
101101
if @include_inline == :include
102102
@file_writer.create_file(mock_project[:module_name] + (mock_project[:module_ext]), mock_project[:folder]) do |file, _filename|
103+
file << "/* Source File: #{mock_project[:source]} */\n"
104+
file << "/* Normalized source (without inlines). */\n"
103105
file << mock_project[:parsed_stuff][:normalized_source]
104106
end
105107
end
@@ -209,11 +211,6 @@ def create_mock_header_footer(header)
209211
def create_source_header_section(file, filename, mock_project)
210212
header_file = (mock_project[:folder] || '') + filename.gsub('.c', mock_project[:module_ext])
211213
file << "/* AUTOGENERATED FILE. DO NOT EDIT. */\n" unless mock_project[:parsed_stuff][:functions].empty?
212-
file << "#include <string.h>\n"
213-
file << "#include <stdlib.h>\n"
214-
unless @exclude_setjmp_h
215-
file << "#include <setjmp.h>\n"
216-
end
217214
file << "#include \"cmock.h\"\n"
218215
@includes_c_pre_header.each { |inc| file << "#include #{inc}\n" }
219216
file << "#include \"#{header_file}\"\n"
@@ -283,7 +280,7 @@ def create_mock_init_function(file, mock_project)
283280
def create_mock_destroy_function(file, mock_project)
284281
file << "void #{mock_project[:clean_name]}_Destroy(void)\n{\n"
285282
file << " CMock_Guts_MemFreeAll();\n"
286-
file << " memset(&Mock, 0, sizeof(Mock));\n"
283+
file << " CMock_memset(&Mock, 0, sizeof(Mock));\n"
287284
file << mock_project[:parsed_stuff][:functions].collect { |function| @plugins.run(:mock_destroy, function) }.join
288285

289286
unless @fail_on_unexpected_calls

lib/cmock_generator_plugin_return_thru_ptr.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def mock_implementation(function)
7070
lines << " if (cmock_call_instance->ReturnThruPtr_#{arg_name}_Used)\n"
7171
lines << " {\n"
7272
lines << " UNITY_TEST_ASSERT_NOT_NULL(#{arg_name}, cmock_line, CMockStringPtrIsNULL);\n"
73-
lines << " memcpy((void*)#{arg_name}, (void*)cmock_call_instance->ReturnThruPtr_#{arg_name}_Val,\n"
73+
lines << " CMock_memcpy((void*)#{arg_name}, (void*)cmock_call_instance->ReturnThruPtr_#{arg_name}_Val,\n"
7474
lines << " cmock_call_instance->ReturnThruPtr_#{arg_name}_Size);\n"
7575
lines << " }\n"
7676
end

lib/cmock_generator_utils.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def code_add_base_expectation(func_name, global_ordering_supported = true)
5151
lines = " CMOCK_MEM_INDEX_TYPE cmock_guts_index = CMock_Guts_MemNew(sizeof(CMOCK_#{func_name}_CALL_INSTANCE));\n"
5252
lines << " CMOCK_#{func_name}_CALL_INSTANCE* cmock_call_instance = (CMOCK_#{func_name}_CALL_INSTANCE*)CMock_Guts_GetAddressFor(cmock_guts_index);\n"
5353
lines << " UNITY_TEST_ASSERT_NOT_NULL(cmock_call_instance, cmock_line, CMockStringOutOfMemory);\n"
54-
lines << " memset(cmock_call_instance, 0, sizeof(*cmock_call_instance));\n"
54+
lines << " CMock_memset(cmock_call_instance, 0, sizeof(*cmock_call_instance));\n"
5555
lines << " Mock.#{func_name}_CallInstance = CMock_Guts_MemChain(Mock.#{func_name}_CallInstance, cmock_guts_index);\n"
5656
lines << " Mock.#{func_name}_IgnoreBool = (char)0;\n" if @ignore || @ignore_stateless
5757
lines << " cmock_call_instance->LineNumber = cmock_line;\n"
@@ -75,7 +75,7 @@ def code_assign_argument_quickly(dest, arg)
7575
else
7676
assert_expr = "sizeof(#{arg[:name]}) == sizeof(#{arg[:type]}) ? 1 : -1"
7777
comment = "/* add #{arg[:type]} to :treat_as_array if this causes an error */"
78-
" memcpy((void*)(&#{dest}), (void*)(&#{arg[:name]}),\n" \
78+
" CMock_memcpy((void*)(&#{dest}), (void*)(&#{arg[:name]}),\n" \
7979
" sizeof(#{arg[:type]}[#{assert_expr}])); #{comment}\n"
8080
end
8181
end

src/cmock.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,3 +229,20 @@ void CMock_Guts_MemFreeFinal(void)
229229
#endif
230230
}
231231

232+
void CMock_memset(void* ptr, int value, size_t num)
233+
{
234+
size_t i;
235+
for (i = 0; i < num; i++)
236+
{
237+
((char*)ptr)[i] = (char)value;
238+
}
239+
}
240+
241+
void CMock_memcpy(void* ptr, const void* src, size_t num)
242+
{
243+
size_t i;
244+
for (i = 0; i < num; i++)
245+
{
246+
((char*)ptr)[i] = ((char*)src)[i];
247+
}
248+
}

src/cmock.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,7 @@ CMOCK_MEM_INDEX_TYPE CMock_Guts_MemBytesFree(void) CMOCK_FUNCTION_ATTR(pure);
4343
CMOCK_MEM_INDEX_TYPE CMock_Guts_MemBytesUsed(void) CMOCK_FUNCTION_ATTR(pure);
4444
void CMock_Guts_MemFreeAll(void);
4545
void CMock_Guts_MemFreeFinal(void);
46+
void CMock_memset(void* ptr, int value, size_t num);
47+
void CMock_memcpy(void* ptr, const void* src, size_t num);
4648

4749
#endif /* end of CMOCK_FRAMEWORK_H */

test/unit/cmock_generator_main_test.rb

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -343,9 +343,6 @@ def helper_create_header_top_with_opt_includes_form_config_and_plugin(ext)
343343
{ :name => "tres", :args => [] }
344344
]
345345
expected = [ "/* AUTOGENERATED FILE. DO NOT EDIT. */\n",
346-
"#include <string.h>\n",
347-
"#include <stdlib.h>\n",
348-
"#include <setjmp.h>\n",
349346
"#include \"cmock.h\"\n",
350347
"#include \"MockPoutPoutFish.h\"\n",
351348
"\n",
@@ -486,7 +483,7 @@ def helper_create_header_top_with_opt_includes_form_config_and_plugin(ext)
486483
output = []
487484
expected = [ "void MockPoutPoutFish_Destroy(void)\n{\n",
488485
" CMock_Guts_MemFreeAll();\n",
489-
" memset(&Mock, 0, sizeof(Mock));\n",
486+
" CMock_memset(&Mock, 0, sizeof(Mock));\n",
490487
"}\n\n"
491488
]
492489

@@ -503,7 +500,7 @@ def helper_create_header_top_with_opt_includes_form_config_and_plugin(ext)
503500
output = []
504501
expected = [ "void MockPoutPoutFish_Destroy(void)\n{\n",
505502
" CMock_Guts_MemFreeAll();\n",
506-
" memset(&Mock, 0, sizeof(Mock));\n",
503+
" CMock_memset(&Mock, 0, sizeof(Mock));\n",
507504
" uno",
508505
" GlobalExpectCount = 0;\n",
509506
" GlobalVerifyOrder = 0;\n",

test/unit/cmock_generator_plugin_return_thru_ptr_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ def complex_func_expect
125125
" if (cmock_call_instance->ReturnThruPtr_tofu_Used)\n" +
126126
" {\n" +
127127
" UNITY_TEST_ASSERT_NOT_NULL(tofu, cmock_line, CMockStringPtrIsNULL);\n" +
128-
" memcpy((void*)tofu, (void*)cmock_call_instance->ReturnThruPtr_tofu_Val,\n" +
128+
" CMock_memcpy((void*)tofu, (void*)cmock_call_instance->ReturnThruPtr_tofu_Val,\n" +
129129
" cmock_call_instance->ReturnThruPtr_tofu_Size);\n" +
130130
" }\n"
131131

test/unit/cmock_generator_utils_test.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
" CMOCK_MEM_INDEX_TYPE cmock_guts_index = CMock_Guts_MemNew(sizeof(CMOCK_Apple_CALL_INSTANCE));\n" +
6262
" CMOCK_Apple_CALL_INSTANCE* cmock_call_instance = (CMOCK_Apple_CALL_INSTANCE*)CMock_Guts_GetAddressFor(cmock_guts_index);\n" +
6363
" UNITY_TEST_ASSERT_NOT_NULL(cmock_call_instance, cmock_line, CMockStringOutOfMemory);\n" +
64-
" memset(cmock_call_instance, 0, sizeof(*cmock_call_instance));\n" +
64+
" CMock_memset(cmock_call_instance, 0, sizeof(*cmock_call_instance));\n" +
6565
" Mock.Apple_CallInstance = CMock_Guts_MemChain(Mock.Apple_CallInstance, cmock_guts_index);\n" +
6666
" cmock_call_instance->LineNumber = cmock_line;\n"
6767
output = @cmock_generator_utils_simple.code_add_base_expectation("Apple")
@@ -73,7 +73,7 @@
7373
" CMOCK_MEM_INDEX_TYPE cmock_guts_index = CMock_Guts_MemNew(sizeof(CMOCK_Apple_CALL_INSTANCE));\n" +
7474
" CMOCK_Apple_CALL_INSTANCE* cmock_call_instance = (CMOCK_Apple_CALL_INSTANCE*)CMock_Guts_GetAddressFor(cmock_guts_index);\n" +
7575
" UNITY_TEST_ASSERT_NOT_NULL(cmock_call_instance, cmock_line, CMockStringOutOfMemory);\n" +
76-
" memset(cmock_call_instance, 0, sizeof(*cmock_call_instance));\n" +
76+
" CMock_memset(cmock_call_instance, 0, sizeof(*cmock_call_instance));\n" +
7777
" Mock.Apple_CallInstance = CMock_Guts_MemChain(Mock.Apple_CallInstance, cmock_guts_index);\n" +
7878
" Mock.Apple_IgnoreBool = (char)0;\n" +
7979
" cmock_call_instance->LineNumber = cmock_line;\n" +
@@ -88,7 +88,7 @@
8888
" CMOCK_MEM_INDEX_TYPE cmock_guts_index = CMock_Guts_MemNew(sizeof(CMOCK_Apple_CALL_INSTANCE));\n" +
8989
" CMOCK_Apple_CALL_INSTANCE* cmock_call_instance = (CMOCK_Apple_CALL_INSTANCE*)CMock_Guts_GetAddressFor(cmock_guts_index);\n" +
9090
" UNITY_TEST_ASSERT_NOT_NULL(cmock_call_instance, cmock_line, CMockStringOutOfMemory);\n" +
91-
" memset(cmock_call_instance, 0, sizeof(*cmock_call_instance));\n" +
91+
" CMock_memset(cmock_call_instance, 0, sizeof(*cmock_call_instance));\n" +
9292
" Mock.Apple_CallInstance = CMock_Guts_MemChain(Mock.Apple_CallInstance, cmock_guts_index);\n" +
9393
" Mock.Apple_IgnoreBool = (char)0;\n" +
9494
" cmock_call_instance->LineNumber = cmock_line;\n" +
@@ -108,7 +108,7 @@
108108
expected3 = " cmock_call_instance->Expected_Kiwi = Kiwi;\n"
109109

110110
arg4 = { :name => "Lime", :const? => false, :type => 'LIME_T', :ptr? => false }
111-
expected4 = " memcpy((void*)(&cmock_call_instance->Expected_Lime), (void*)(&Lime),\n" +
111+
expected4 = " CMock_memcpy((void*)(&cmock_call_instance->Expected_Lime), (void*)(&Lime),\n" +
112112
" sizeof(LIME_T[sizeof(Lime) == sizeof(LIME_T) ? 1 : -1])); /* add LIME_T to :treat_as_array if this causes an error */\n"
113113

114114
assert_equal(expected1, @cmock_generator_utils_simple.code_add_an_arg_expectation(arg1))
@@ -134,7 +134,7 @@
134134
" cmock_call_instance->ReturnThruPtr_Kiwi_Used = 0;\n"
135135

136136
arg4 = { :name => "Lime", :const? => false, :type => 'LIME_T', :ptr? => false }
137-
expected4 = " memcpy((void*)(&cmock_call_instance->Expected_Lime), (void*)(&Lime),\n" +
137+
expected4 = " CMock_memcpy((void*)(&cmock_call_instance->Expected_Lime), (void*)(&Lime),\n" +
138138
" sizeof(LIME_T[sizeof(Lime) == sizeof(LIME_T) ? 1 : -1])); /* add LIME_T to :treat_as_array if this causes an error */\n" +
139139
" cmock_call_instance->IgnoreArg_Lime = 0;\n"
140140

@@ -158,7 +158,7 @@
158158
expected = "void CMockExpectParameters_Melon(CMOCK_Melon_CALL_INSTANCE* cmock_call_instance, stuff);\n" +
159159
"void CMockExpectParameters_Melon(CMOCK_Melon_CALL_INSTANCE* cmock_call_instance, stuff)\n{\n" +
160160
" cmock_call_instance->Expected_MyIntPtr = MyIntPtr;\n" +
161-
" memcpy((void*)(&cmock_call_instance->Expected_MyMyType), (void*)(&MyMyType),\n" +
161+
" CMock_memcpy((void*)(&cmock_call_instance->Expected_MyMyType), (void*)(&MyMyType),\n" +
162162
" sizeof(MY_TYPE[sizeof(MyMyType) == sizeof(MY_TYPE) ? 1 : -1])); /* add MY_TYPE to :treat_as_array if this causes an error */\n" +
163163
" cmock_call_instance->Expected_MyStr = MyStr;\n" +
164164
"}\n\n"
@@ -176,7 +176,7 @@
176176
" cmock_call_instance->Expected_MyIntPtr_Depth = MyIntPtr_Depth;\n" +
177177
" cmock_call_instance->IgnoreArg_MyIntPtr = 0;\n" +
178178
" cmock_call_instance->ReturnThruPtr_MyIntPtr_Used = 0;\n" +
179-
" memcpy((void*)(&cmock_call_instance->Expected_MyMyType), (void*)(&MyMyType),\n" +
179+
" CMock_memcpy((void*)(&cmock_call_instance->Expected_MyMyType), (void*)(&MyMyType),\n" +
180180
" sizeof(MY_TYPE[sizeof(MyMyType) == sizeof(MY_TYPE) ? 1 : -1])); /* add MY_TYPE to :treat_as_array if this causes an error */\n" +
181181
" cmock_call_instance->IgnoreArg_MyMyType = 0;\n" +
182182
" cmock_call_instance->Expected_MyStr = MyStr;\n" +

0 commit comments

Comments
 (0)