Don't assume that pointer-to-constant types have "const" removed.

1. Update treat_as table to include pointer-to-constant types.
2. Remove unnecessary casts in assignments and return statements.
3. Improve logic for adding "const" to types of function arguments.
4. It's no longer necessary to prepend "const" to function return type.
This commit is contained in:
John Lindgren
2017-09-12 15:54:47 -04:00
parent c725e4ddc6
commit 3b123fb533
11 changed files with 80 additions and 59 deletions
+4
View File
@@ -120,6 +120,8 @@ class CMockConfig
'UINT32' => 'HEX32',
'UINT32_T' => 'HEX32',
'void*' => 'HEX8_ARRAY',
'void const*' => 'HEX8_ARRAY',
'const void*' => 'HEX8_ARRAY',
'unsigned short' => 'HEX16',
'uint16' => 'HEX16',
'uint16_t' => 'HEX16',
@@ -131,6 +133,8 @@ class CMockConfig
'UINT8' => 'HEX8',
'UINT8_T' => 'HEX8',
'char*' => 'STRING',
'char const*' => 'STRING',
'const char*' => 'STRING',
'pCHAR' => 'STRING',
'cstring' => 'STRING',
'CSTRING' => 'STRING',
+1 -2
View File
@@ -247,10 +247,9 @@ class CMockGenerator
file << " if (cmock_call_instance->CallOrder < GlobalVerifyOrder)\n"
file << " UNITY_TEST_FAIL(cmock_line, CMockStringCalledLate);\n"
end
return_type = function[:return][:const?] ? "(const #{function[:return][:type]})" : ((function[:return][:type] =~ /cmock/) ? "(#{function[:return][:type]})" : '')
file << @plugins.run(:mock_implementation, function)
file << " UNITY_CLR_DETAILS();\n"
file << " return #{return_type}cmock_call_instance->ReturnVal;\n" unless (function[:return][:void?])
file << " return cmock_call_instance->ReturnVal;\n" unless (function[:return][:void?])
file << "}\n\n"
end
+4 -4
View File
@@ -27,8 +27,8 @@ class CMockGeneratorPluginArray
return nil unless function[:contains_ptr?]
args_call = function[:args].map{|m| m[:ptr?] ? "#{m[:name]}, #{m[:name]}_Depth" : "#{m[:name]}"}.join(', ')
args_string = function[:args].map do |m|
const_str = m[:const?] ? 'const ' : ''
m[:ptr?] ? "#{const_str}#{m[:type]} #{m[:name]}, int #{m[:name]}_Depth" : "#{const_str}#{m[:type]} #{m[:name]}"
type = @utils.arg_type_with_const(m)
m[:ptr?] ? "#{type} #{m[:name]}, int #{m[:name]}_Depth" : "#{type} #{m[:name]}"
end.join(', ')
if (function[:return][:void?])
return "#define #{function[:name]}_ExpectWithArray(#{args_call}) #{function[:name]}_CMockExpectWithArray(__LINE__, #{args_call})\n" +
@@ -44,8 +44,8 @@ class CMockGeneratorPluginArray
lines = []
func_name = function[:name]
args_string = function[:args].map do |m|
const_str = m[:const?] ? 'const ' : ''
m[:ptr?] ? "#{const_str}#{m[:type]} #{m[:name]}, int #{m[:name]}_Depth" : "#{const_str}#{m[:type]} #{m[:name]}"
type = @utils.arg_type_with_const(m)
m[:ptr?] ? "#{type} #{m[:name]}, int #{m[:name]}_Depth" : "#{type} #{m[:name]}"
end.join(', ')
call_string = function[:args].map{|m| m[:ptr?] ? "#{m[:name]}, #{m[:name]}_Depth" : m[:name]}.join(', ')
if (function[:return][:void?])
+9 -11
View File
@@ -33,7 +33,7 @@ class CMockGeneratorPluginCallback
def mock_function_declarations(function)
func_name = function[:name]
return_type = function[:return][:const?] ? "const #{function[:return][:type]}" : function[:return][:type]
return_type = function[:return][:type]
style = (@include_count ? 1 : 0) | (function[:args].empty? ? 0 : 2)
styles = [ "void", "int cmock_num_calls", function[:args_string], "#{function[:args_string]}, int cmock_num_calls" ]
"typedef #{return_type} (* CMOCK_#{func_name}_CALLBACK)(#{styles[style]});\nvoid #{func_name}_StubWithCallback(CMOCK_#{func_name}_CALLBACK Callback);\n"
@@ -41,7 +41,6 @@ class CMockGeneratorPluginCallback
def mock_implementation_for_callbacks_after_arg_check(function)
func_name = function[:name]
return_cast = function[:return][:const?] ? "(#{function[:return][:type]})" : ""
style = (@include_count ? 1 : 0) | (function[:args].empty? ? 0 : 2) | (function[:return][:void?] ? 0 : 4)
" if (Mock.#{func_name}_CallbackFunctionPointer != NULL)\n {\n" +
case(style)
@@ -49,16 +48,15 @@ class CMockGeneratorPluginCallback
when 1 then " Mock.#{func_name}_CallbackFunctionPointer(Mock.#{func_name}_CallbackCalls++);\n }\n"
when 2 then " Mock.#{func_name}_CallbackFunctionPointer(#{function[:args].map{|m| m[:name]}.join(', ')});\n }\n"
when 3 then " Mock.#{func_name}_CallbackFunctionPointer(#{function[:args].map{|m| m[:name]}.join(', ')}, Mock.#{func_name}_CallbackCalls++);\n }\n"
when 4 then " cmock_call_instance->ReturnVal = #{return_cast}Mock.#{func_name}_CallbackFunctionPointer();\n }\n"
when 5 then " cmock_call_instance->ReturnVal = #{return_cast}Mock.#{func_name}_CallbackFunctionPointer(Mock.#{func_name}_CallbackCalls++);\n }\n"
when 6 then " cmock_call_instance->ReturnVal = #{return_cast}Mock.#{func_name}_CallbackFunctionPointer(#{function[:args].map{|m| m[:name]}.join(', ')});\n }\n"
when 7 then " cmock_call_instance->ReturnVal = #{return_cast}Mock.#{func_name}_CallbackFunctionPointer(#{function[:args].map{|m| m[:name]}.join(', ')}, Mock.#{func_name}_CallbackCalls++);\n }\n"
when 4 then " cmock_call_instance->ReturnVal = Mock.#{func_name}_CallbackFunctionPointer();\n }\n"
when 5 then " cmock_call_instance->ReturnVal = Mock.#{func_name}_CallbackFunctionPointer(Mock.#{func_name}_CallbackCalls++);\n }\n"
when 6 then " cmock_call_instance->ReturnVal = Mock.#{func_name}_CallbackFunctionPointer(#{function[:args].map{|m| m[:name]}.join(', ')});\n }\n"
when 7 then " cmock_call_instance->ReturnVal = Mock.#{func_name}_CallbackFunctionPointer(#{function[:args].map{|m| m[:name]}.join(', ')}, Mock.#{func_name}_CallbackCalls++);\n }\n"
end
end
def mock_implementation_for_callbacks_without_arg_check(function)
func_name = function[:name]
return_cast = function[:return][:const?] ? "(#{function[:return][:type]})" : ""
style = (@include_count ? 1 : 0) | (function[:args].empty? ? 0 : 2) | (function[:return][:void?] ? 0 : 4)
" if (Mock.#{func_name}_CallbackFunctionPointer != NULL)\n {\n" +
case(style)
@@ -66,10 +64,10 @@ class CMockGeneratorPluginCallback
when 1 then " Mock.#{func_name}_CallbackFunctionPointer(Mock.#{func_name}_CallbackCalls++);\n return;\n }\n"
when 2 then " Mock.#{func_name}_CallbackFunctionPointer(#{function[:args].map{|m| m[:name]}.join(', ')});\n return;\n }\n"
when 3 then " Mock.#{func_name}_CallbackFunctionPointer(#{function[:args].map{|m| m[:name]}.join(', ')}, Mock.#{func_name}_CallbackCalls++);\n return;\n }\n"
when 4 then " return #{return_cast}Mock.#{func_name}_CallbackFunctionPointer();\n }\n"
when 5 then " return #{return_cast}Mock.#{func_name}_CallbackFunctionPointer(Mock.#{func_name}_CallbackCalls++);\n }\n"
when 6 then " return #{return_cast}Mock.#{func_name}_CallbackFunctionPointer(#{function[:args].map{|m| m[:name]}.join(', ')});\n }\n"
when 7 then " return #{return_cast}Mock.#{func_name}_CallbackFunctionPointer(#{function[:args].map{|m| m[:name]}.join(', ')}, Mock.#{func_name}_CallbackCalls++);\n }\n"
when 4 then " return Mock.#{func_name}_CallbackFunctionPointer();\n }\n"
when 5 then " return Mock.#{func_name}_CallbackFunctionPointer(Mock.#{func_name}_CallbackCalls++);\n }\n"
when 6 then " return Mock.#{func_name}_CallbackFunctionPointer(#{function[:args].map{|m| m[:name]}.join(', ')});\n }\n"
when 7 then " return Mock.#{func_name}_CallbackFunctionPointer(#{function[:args].map{|m| m[:name]}.join(', ')}, Mock.#{func_name}_CallbackCalls++);\n }\n"
end
end
+2 -3
View File
@@ -40,10 +40,9 @@ 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] =~ /cmock/) ? "(#{function[:return][:type]})" : '')
lines << " if (cmock_call_instance == NULL)\n return #{return_type}Mock.#{function[:name]}_FinalReturn;\n"
lines << " if (cmock_call_instance == NULL)\n return 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 cmock_call_instance->ReturnVal;\n }\n"
end
lines
end
+16 -3
View File
@@ -22,6 +22,19 @@ class CMockGeneratorUtils
@helpers = helpers
end
def self.arg_type_with_const(arg)
# Restore any "const" that was removed in header parsing
if arg[:type].include?('*')
arg[:const_ptr?] ? "#{arg[:type]} const" : arg[:type]
else
arg[:const?] ? "const #{arg[:type]}" : arg[:type]
end
end
def arg_type_with_const(arg)
self.class.arg_type_with_const(arg)
end
def code_verify_an_arg_expectation(function, arg)
if (@arrays)
case(@ptr_handling)
@@ -58,7 +71,7 @@ class CMockGeneratorUtils
def code_assign_argument_quickly(dest, arg)
if (arg[:ptr?] or @treat_as.include?(arg[:type]))
" #{dest} = #{arg[:const?] ? "(#{arg[:type]})" : ''}#{arg[:name]};\n"
" #{dest} = #{arg[:name]};\n"
else
" memcpy(&#{dest}, &#{arg[:name]}, sizeof(#{arg[:type]}));\n"
end
@@ -68,8 +81,8 @@ class CMockGeneratorUtils
if (function[:args_string] != "void")
if (@arrays)
args_string = function[:args].map do |m|
const_str = m[ :const? ] ? 'const ' : ''
m[:ptr?] ? "#{const_str}#{m[:type]} #{m[:name]}, int #{m[:name]}_Depth" : "#{const_str}#{m[:type]} #{m[:name]}"
type = arg_type_with_const(m)
m[:ptr?] ? "#{type} #{m[:name]}, int #{m[:name]}_Depth" : "#{type} #{m[:name]}"
end.join(', ')
"void CMockExpectParameters_#{function[:name]}(CMOCK_#{function[:name]}_CALL_INSTANCE* cmock_call_instance, #{args_string})\n{\n" +
function[:args].inject("") { |all, arg| all + code_add_an_arg_expectation(arg, (arg[:ptr?] ? "#{arg[:name]}_Depth" : 1) ) } +
+9 -9
View File
@@ -24,20 +24,20 @@ end
def test_return
{
:int => {:type => "int", :name => 'cmock_to_return', :ptr? => false, :const? => false, :void? => false, :str => 'int cmock_to_return'},
:int_ptr => {:type => "int*", :name => 'cmock_to_return', :ptr? => true, :const? => false, :void? => false, :str => 'int* cmock_to_return'},
:void => {:type => "void", :name => 'cmock_to_return', :ptr? => false, :const? => false, :void? => true, :str => 'void cmock_to_return'},
:string => {:type => "char*", :name => 'cmock_to_return', :ptr? => false, :const? => true, :void? => false, :str => 'const char* cmock_to_return'},
:int => {:type => "int", :name => 'cmock_to_return', :ptr? => false, :const? => false, :void? => false, :str => 'int cmock_to_return'},
:int_ptr => {:type => "int*", :name => 'cmock_to_return', :ptr? => true, :const? => false, :void? => false, :str => 'int* cmock_to_return'},
:void => {:type => "void", :name => 'cmock_to_return', :ptr? => false, :const? => false, :void? => true, :str => 'void cmock_to_return'},
:string => {:type => "const char*", :name => 'cmock_to_return', :ptr? => false, :const? => true, :void? => false, :str => 'const char* cmock_to_return'},
}
end
def test_arg
{
:int => {:type => "int", :name => 'MyInt', :ptr? => false, :const? => false},
:int_ptr => {:type => "int*", :name => 'MyIntPtr', :ptr? => true, :const? => false},
:mytype => {:type => "MY_TYPE", :name => 'MyMyType', :ptr? => false, :const? => true},
:mytype_ptr => {:type => "MY_TYPE*", :name => 'MyMyTypePtr', :ptr? => true, :const? => false},
:string => {:type => "char*", :name => 'MyStr', :ptr? => false, :const? => true},
:int => {:type => "int", :name => 'MyInt', :ptr? => false, :const? => false},
:int_ptr => {:type => "int*", :name => 'MyIntPtr', :ptr? => true, :const? => false},
:mytype => {:type => "MY_TYPE", :name => 'MyMyType', :ptr? => false, :const? => true},
:mytype_ptr => {:type => "MY_TYPE*", :name => 'MyMyTypePtr', :ptr? => true, :const? => false},
:string => {:type => "const char*", :name => 'MyStr', :ptr? => false, :const? => true},
}
end
+18 -10
View File
@@ -6,21 +6,29 @@
require File.expand_path(File.dirname(__FILE__)) + "/../test_helper"
require File.expand_path(File.dirname(__FILE__)) + '/../../lib/cmock_generator_plugin_array'
require File.expand_path(File.dirname(__FILE__)) + '/../../lib/cmock_generator_utils'
class UtilsStub
def helpers
{}
end
def arg_type_with_const(arg)
CMockGeneratorUtils.arg_type_with_const(arg)
end
def code_add_base_expectation(func)
"mock_retval_0"
end
end
describe CMockGeneratorPluginArray, "Verify CMockPGeneratorluginArray Module" do
before do
create_mocks :utils
#no strict ordering
@config = create_stub(
:when_ptr => :compare_data,
:enforce_strict_ordering => false,
:respond_to? => true )
@utils = create_stub(
:helpers => {},
:code_add_base_expectation => "mock_retval_0"
)
@utils = UtilsStub.new
@cmock_generator_plugin_array = CMockGeneratorPluginArray.new(@config, @utils)
end
@@ -88,10 +96,10 @@ describe CMockGeneratorPluginArray, "Verify CMockPGeneratorluginArray Module" do
it "add another mock function declaration for functions of style 'const char* func(const int* tofu)'" do
function = {:name => "Pine",
:args => [{ :type => "int*",
:name => "tofu",
:ptr? => true,
:const? => true,
:args => [{ :type => "const int*",
:name => "tofu",
:ptr? => true,
:const? => true,
}],
:return => test_return[:string],
:contains_ptr? => true }
@@ -21,9 +21,9 @@ describe CMockGeneratorPluginIgnoreArg, "Verify CMockGeneratorPluginIgnoreArg Mo
:name => "chicken",
:ptr? => false,
},
{ :type => "int*",
:name => "beef",
:ptr? => true,
{ :type => "const int*",
:name => "beef",
:ptr? => true,
:const? => true,
},
{ :type => "int*",
@@ -27,9 +27,9 @@ describe CMockGeneratorPluginReturnThruPtr, "Verify CMockGeneratorPluginReturnTh
:name => "chicken",
:ptr? => false,
},
{ :type => "int*",
:name => "beef",
:ptr? => true,
{ :type => "const int*",
:name => "beef",
:ptr? => true,
:const? => true,
},
{ :type => "int*",
@@ -52,7 +52,7 @@ describe CMockGeneratorPluginReturnThruPtr, "Verify CMockGeneratorPluginReturnTh
def complex_func_expect
@utils.expect :ptr_or_str?, false, ['int']
@utils.expect :ptr_or_str?, true, ['int*']
@utils.expect :ptr_or_str?, true, ['const int*']
@utils.expect :ptr_or_str?, true, ['int*']
end
+10 -10
View File
@@ -20,7 +20,7 @@ describe CMockGeneratorUtils, "Verify CMockGeneratorUtils Module" do
@config.expect :plugins, []
@config.expect :plugins, []
@config.expect :plugins, []
@config.expect :treat_as, {'int' => 'INT','short' => 'INT16','long' => 'INT','char' => 'INT8','char*' => 'STRING'}
@config.expect :treat_as, {'int' => 'INT','short' => 'INT16','long' => 'INT','char' => 'INT8','const char*' => 'STRING'}
@cmock_generator_utils_simple = CMockGeneratorUtils.new(@config, {:unity_helper => @unity_helper})
@config.expect :when_ptr, :smart
@@ -31,7 +31,7 @@ describe CMockGeneratorUtils, "Verify CMockGeneratorUtils Module" do
@config.expect :plugins, [:array, :cexception, :return_thru_ptr, :ignore_arg, :ignore]
@config.expect :plugins, [:array, :cexception, :return_thru_ptr, :ignore_arg, :ignore]
@config.expect :plugins, [:array, :cexception, :return_thru_ptr, :ignore_arg, :ignore]
@config.expect :treat_as, {'int' => 'INT','short' => 'INT16','long' => 'INT','char' => 'INT8','uint32_t' => 'HEX32','char*' => 'STRING'}
@config.expect :treat_as, {'int' => 'INT','short' => 'INT16','long' => 'INT','char' => 'INT8','uint32_t' => 'HEX32','const char*' => 'STRING'}
@cmock_generator_utils_complex = CMockGeneratorUtils.new(@config, {:unity_helper => @unity_helper, :A=>1, :B=>2})
end
@@ -99,8 +99,8 @@ describe CMockGeneratorUtils, "Verify CMockGeneratorUtils Module" do
arg1 = { :name => "Orange", :const? => false, :type => 'int', :ptr? => false }
expected1 = " cmock_call_instance->Expected_Orange = Orange;\n"
arg2 = { :name => "Lemon", :const? => true, :type => 'const char*', :ptr? => true }
expected2 = " cmock_call_instance->Expected_Lemon = (const char*)Lemon;\n"
arg2 = { :name => "Lemon", :const? => true, :type => 'const char*', :ptr? => false }
expected2 = " cmock_call_instance->Expected_Lemon = Lemon;\n"
arg3 = { :name => "Kiwi", :const? => false, :type => 'KIWI_T*', :ptr? => true }
expected3 = " cmock_call_instance->Expected_Kiwi = Kiwi;\n"
@@ -119,8 +119,8 @@ describe CMockGeneratorUtils, "Verify CMockGeneratorUtils Module" do
expected1 = " cmock_call_instance->Expected_Orange = Orange;\n" +
" cmock_call_instance->IgnoreArg_Orange = 0;\n"
arg2 = { :name => "Lemon", :const? => true, :type => 'const char*', :ptr? => true }
expected2 = " cmock_call_instance->Expected_Lemon = (const char*)Lemon;\n" +
arg2 = { :name => "Lemon", :const? => true, :type => 'const char*', :ptr? => false }
expected2 = " cmock_call_instance->Expected_Lemon = Lemon;\n" +
" cmock_call_instance->Expected_Lemon_Depth = Lemon_Depth;\n" +
" cmock_call_instance->IgnoreArg_Lemon = 0;\n"
@@ -154,7 +154,7 @@ describe CMockGeneratorUtils, "Verify CMockGeneratorUtils Module" do
expected = "void CMockExpectParameters_Melon(CMOCK_Melon_CALL_INSTANCE* cmock_call_instance, stuff)\n{\n" +
" cmock_call_instance->Expected_MyIntPtr = MyIntPtr;\n" +
" memcpy(&cmock_call_instance->Expected_MyMyType, &MyMyType, sizeof(MY_TYPE));\n" +
" cmock_call_instance->Expected_MyStr = (char*)MyStr;\n" +
" cmock_call_instance->Expected_MyStr = MyStr;\n" +
"}\n\n"
assert_equal(expected, @cmock_generator_utils_simple.code_add_argument_loader(function))
end
@@ -171,7 +171,7 @@ describe CMockGeneratorUtils, "Verify CMockGeneratorUtils Module" do
" cmock_call_instance->ReturnThruPtr_MyIntPtr_Used = 0;\n" +
" memcpy(&cmock_call_instance->Expected_MyMyType, &MyMyType, sizeof(MY_TYPE));\n" +
" cmock_call_instance->IgnoreArg_MyMyType = 0;\n" +
" cmock_call_instance->Expected_MyStr = (char*)MyStr;\n" +
" cmock_call_instance->Expected_MyStr = MyStr;\n" +
" cmock_call_instance->IgnoreArg_MyStr = 0;\n" +
"}\n\n"
assert_equal(expected, @cmock_generator_utils_complex.code_add_argument_loader(function))
@@ -231,7 +231,7 @@ describe CMockGeneratorUtils, "Verify CMockGeneratorUtils Module" do
" UNITY_TEST_ASSERT_EQUAL_STRING(cmock_call_instance->Expected_MyStr, MyStr, cmock_line, CMockStringMismatch);\n" +
" }\n"
@unity_helper.expect :nil?, false
@unity_helper.expect :get_helper, ['UNITY_TEST_ASSERT_EQUAL_STRING',''], ['char*']
@unity_helper.expect :get_helper, ['UNITY_TEST_ASSERT_EQUAL_STRING',''], ['const char*']
assert_equal(expected, @cmock_generator_utils_simple.code_verify_an_arg_expectation(function, arg))
end
@@ -311,7 +311,7 @@ describe CMockGeneratorUtils, "Verify CMockGeneratorUtils Module" do
" UNITY_TEST_ASSERT_EQUAL_STRING(cmock_call_instance->Expected_MyStr, MyStr, cmock_line, CMockStringMismatch);\n" +
" }\n"
@unity_helper.expect :nil?, false
@unity_helper.expect :get_helper, ['UNITY_TEST_ASSERT_EQUAL_STRING',''], ['char*']
@unity_helper.expect :get_helper, ['UNITY_TEST_ASSERT_EQUAL_STRING',''], ['const char*']
assert_equal(expected, @cmock_generator_utils_complex.code_verify_an_arg_expectation(function, arg))
end