Stop casting unless necessary for custom types or const when returning values.

This commit is contained in:
Mark VanderVoord
2015-08-18 07:45:45 -04:00
parent 2cde629c05
commit f17d1a933b
7 changed files with 42 additions and 17 deletions
+4 -4
View File
@@ -23,7 +23,7 @@ class CMockGenerator
@includes_h_post_orig_header = (@config.includes_h_post_orig_header || []).map{|h| h =~ /</ ? h : "\"#{h}\""}
@includes_c_pre_header = (@config.includes_c_pre_header || []).map{|h| h =~ /</ ? h : "\"#{h}\""}
@includes_c_post_header = (@config.includes_c_post_header || []).map{|h| h =~ /</ ? h : "\"#{h}\""}
here = File.dirname __FILE__
unity_path_in_ceedling = "#{here}/../../unity" # path to Unity from within Ceedling
unity_path_in_cmock = "#{here}/../vendor/unity" # path to Unity from within CMock
@@ -34,7 +34,7 @@ class CMockGenerator
else
raise "Failed to find an instance of Unity to pull in type_sanitizer module!"
end
end
def create_mock(module_name, parsed_stuff)
@@ -211,9 +211,9 @@ class CMockGenerator
file << " UNITY_TEST_FAIL(cmock_line, \"Function '#{function[:name]}' called later than expected.\");\n"
# file << " UNITY_TEST_ASSERT((cmock_call_instance->CallOrder == ++GlobalVerifyOrder), cmock_line, \"Out of order function calls. Function '#{function[:name]}'\");\n"
end
return_type_cast = function[:return][:const?] ? "(const #{function[:return][:type]})" : ''
return_type = function[:return][:const?] ? "(const #{function[:return][:type]})" : ((function[:return][:type] =~ /cmock/) ? "(#{function[:return][:type]})" : '')
file << @plugins.run(:mock_implementation, function)
file << " return #{return_type_cast}cmock_call_instance->ReturnVal;\n" unless (function[:return][:void?])
file << " return #{return_type}cmock_call_instance->ReturnVal;\n" unless (function[:return][:void?])
file << "}\n\n"
end
+6 -6
View File
@@ -2,19 +2,19 @@
# CMock Project - Automatic Mock Generation for C
# Copyright (c) 2007 Mike Karlesky, Mark VanderVoord, Greg Williams
# [Released under MIT License. Please refer to license.txt for details]
# ==========================================
# ==========================================
class CMockGeneratorPluginCallback
attr_accessor :include_count
attr_reader :priority
attr_reader :config, :utils
def initialize(config, utils)
@config = config
@utils = utils
@priority = 6
@include_count = @config.callback_include_count
if (@config.callback_after_arg_check)
alias :mock_implementation :mock_implementation_for_callbacks
@@ -54,14 +54,14 @@ class CMockGeneratorPluginCallback
when 7 then " return Mock.#{func_name}_CallbackFunctionPointer(#{function[:args].map{|m| m[:name]}.join(', ')}, Mock.#{func_name}_CallbackCalls++);\n }\n"
end
end
def nothing(function)
return ""
end
def mock_interfaces(function)
func_name = function[:name]
"void #{func_name}_StubWithCallback(CMOCK_#{func_name}_CALLBACK Callback)\n{\n" +
"void #{func_name}_StubWithCallback(CMOCK_#{func_name}_CALLBACK Callback)\n{\n" +
" Mock.#{func_name}_CallbackFunctionPointer = Callback;\n}\n\n"
end
@@ -69,7 +69,7 @@ class CMockGeneratorPluginCallback
" Mock.#{function[:name]}_CallbackFunctionPointer = NULL;\n" +
" Mock.#{function[:name]}_CallbackCalls = 0;\n"
end
def mock_verify(function)
func_name = function[:name]
" if (Mock.#{func_name}_CallbackFunctionPointer != NULL)\n Mock.#{func_name}_CallInstance = CMOCK_GUTS_NONE;\n"
@@ -45,8 +45,8 @@ class CMockGeneratorPluginExpectAnyArgs
else
retval = function[:return].merge( { :name => "cmock_call_instance->ReturnVal"} )
lines << " " + @utils.code_assign_argument_quickly("Mock.#{function[:name]}_FinalReturn", retval) unless (retval[:void?])
return_type_cast = function[:return][:const?] ? "(const #{function[:return][:type]})" : ''
lines << " return #{return_type_cast}cmock_call_instance->ReturnVal;\n }\n"
return_type = function[:return][:const?] ? "(const #{function[:return][:type]})" : ((function[:return][:type] =~ /cmock/) ? "(#{function[:return][:type]})" : '')
lines << " return #{return_type}cmock_call_instance->ReturnVal;\n }\n"
end
lines
end
+3 -3
View File
@@ -39,10 +39,10 @@ class CMockGeneratorPluginIgnore
lines << " return;\n }\n"
else
retval = function[:return].merge( { :name => "cmock_call_instance->ReturnVal"} )
return_type = function[:return][:const?] ? "const #{function[:return][:type]}" : function[:return][:type]
lines << " if (cmock_call_instance == NULL)\n return (#{return_type})Mock.#{function[:name]}_FinalReturn;\n"
return_type = function[:return][:const?] ? "(const #{function[:return][:type]})" : ((function[:return][:type] =~ /cmock/) ? "(#{function[:return][:type]})" : '')
lines << " if (cmock_call_instance == NULL)\n return #{return_type}Mock.#{function[:name]}_FinalReturn;\n"
lines << " " + @utils.code_assign_argument_quickly("Mock.#{function[:name]}_FinalReturn", retval) unless (retval[:void?])
lines << " return (#{return_type})cmock_call_instance->ReturnVal;\n }\n"
lines << " return #{return_type}cmock_call_instance->ReturnVal;\n }\n"
end
lines
end
+2
View File
@@ -45,3 +45,5 @@ unsigned int ** ptr_ptr_return4(unsigned int ** a);
extern unsigned long int incredible_descriptors(register const unsigned short a);
int32_t example_c99_type(int32_t param1);
void I2CIntRegister(uint32_t ui32Base, void (*pfnHandler)(void));
@@ -66,9 +66,9 @@ describe CMockGeneratorPluginIgnore, "Verify CMockGeneratorPluginIgnore Module"
expected = [" if (Mock.Fungus_IgnoreBool)\n",
" {\n",
" if (cmock_call_instance == NULL)\n",
" return (int)Mock.Fungus_FinalReturn;\n",
" return Mock.Fungus_FinalReturn;\n",
" mock_retval_0",
" return (int)cmock_call_instance->ReturnVal;\n",
" return cmock_call_instance->ReturnVal;\n",
" }\n"
].join
returned = @cmock_generator_plugin_ignore.mock_implementation_precheck(function)
+23
View File
@@ -1009,6 +1009,29 @@ describe CMockHeaderParser, "Verify CMockHeaderParser Module" do
assert_equal(typedefs, result[:typedefs])
end
it "extract functions containing a function pointer with a void" do
source = "void FunkyTurkey(void (*func_ptr)(void))"
expected = [{ :var_arg=>nil,
:return=>{ :type => "void",
:name => 'cmock_to_return',
:ptr? => false,
:const? => false,
:str => "void cmock_to_return",
:void? => true
},
:name=>"FunkyTurkey",
:modifier=>"",
:contains_ptr? => false,
:args=>[ {:type=>"cmock_module_func_ptr1", :name=>"func_ptr", :ptr? => false, :const? => false}
],
:args_string=>"cmock_module_func_ptr1 func_ptr",
:args_call=>"func_ptr" }]
typedefs = ["typedef void(*cmock_module_func_ptr1)(void);"]
result = @parser.parse("module", source)
assert_equal(expected, result[:functions])
assert_equal(typedefs, result[:typedefs])
end
it "extract functions containing a function pointer with an implied void" do
source = "void FunkyTurkey(unsigned int (*func_ptr)())"
expected = [{ :var_arg=>nil,