From aab0724859c8ef8fa80fa01c94759923879ec779 Mon Sep 17 00:00:00 2001 From: mkarlesky Date: Thu, 28 May 2009 04:02:20 +0000 Subject: [PATCH] moved removal of 'const' to prototype parser handling from mock generation to control format (whitespace) of type strings git-svn-id: http://cmock.svn.sourceforge.net/svnroot/cmock/trunk@117 bf332499-1b4d-0410-844d-d2d48d5cc64c --- lib/cmock_function_prototype_node_classes.rb | 49 +++++++++---------- lib/cmock_generator_plugin_expect.rb | 13 ++--- .../cmock_function_prototype_parser_test.rb | 18 +++---- 3 files changed, 37 insertions(+), 43 deletions(-) diff --git a/lib/cmock_function_prototype_node_classes.rb b/lib/cmock_function_prototype_node_classes.rb index a2823d6..bb07c69 100644 --- a/lib/cmock_function_prototype_node_classes.rb +++ b/lib/cmock_function_prototype_node_classes.rb @@ -2,11 +2,8 @@ module CMockFunctionPrototype module FunctionPrototypeUtils - # handles '*' and '[]' (they're both pointers after all) - def normalize_ptr(ptr_string) - ptr_string.gsub!(/\s+\*/, '*') - ptr_string.gsub!(/\*(\w)/, '* \1') - return ptr_string + def replace_brackets(string, replace='') + return string.gsub(/(\s*\[\s*[0-9]*\])+/, replace) end def make_cmock_arg_name(index) @@ -135,7 +132,7 @@ module CMockFunctionPrototype if (arg.class == CMockFunctionPrototype::TypeWithNameNode) list << arg.type_and_smart_name_string(index) elsif (arg.class == CMockFunctionPrototype::FunctionPointerNode) - list << arg.type_and_smart_name_string(index) + list << arg.type_and_smart_name_string(index) elsif (arg.class == CMockFunctionPrototype::VarArgNode) @var_arg_found = true # consume var args @@ -155,9 +152,9 @@ module CMockFunctionPrototype arguments.elements.each_with_index do |element, index| arg = element.argument if (arg.class == CMockFunctionPrototype::TypeWithNameNode) - list << arg.type_and_smart_name_token_hash(index) + list << arg.type_and_smart_name_hash(index) elsif (arg.class == CMockFunctionPrototype::FunctionPointerNode) - list << arg.type_and_smart_name_token_hash(index, self.parent.name.text_value) + list << arg.type_and_smart_name_hash(index, self.parent.name.text_value) elsif (arg.class == CMockFunctionPrototype::VarArgNode) # consume var args elsif (arg.class == CMockFunctionPrototype::VoidNode) @@ -205,7 +202,7 @@ module CMockFunctionPrototype return "#{return_type.text_value} (*#{func_ptr_name})#{name_and_args.argument_list.normalized_argument_list}" end - def type_and_smart_name_token_hash(arg_list_index, function_name) + def type_and_smart_name_hash(arg_list_index, function_name) name_and_args = get_deepest_name_and_args_node typename = make_function_pointer_param_typedef_name(arg_list_index, function_name) @@ -238,14 +235,14 @@ module CMockFunctionPrototype include FunctionPrototypeUtils def text_value - return "#{normalize_ptr(type.text_value)} #{name.text_value}" if not name.text_value.blank? - return "#{normalize_ptr(type.text_value)}" + return "#{type.text_value} #{name.text_value}" if not name.text_value.blank? + return "#{type.text_value}" end def type_and_smart_name_string(arg_list_index) if (name.text_value.blank?) if (type.brackets?) - return "#{type.replace_brackets} #{make_cmock_arg_name(arg_list_index)}#{type.get_brackets}" + return "#{replace_brackets(type.text_value)} #{make_cmock_arg_name(arg_list_index)}#{type.get_brackets}" end return "#{type.text_value} #{make_cmock_arg_name(arg_list_index)}" end @@ -253,19 +250,19 @@ module CMockFunctionPrototype return "#{type.text_value} #{name.text_value}" end - def type_and_smart_name_token_hash(arg_list_index) + def type_and_smart_name_hash(arg_list_index) if (name.text_value.blank?) if (type.brackets?) - return { :type => type.replace_brackets('*'), :name => make_cmock_arg_name(arg_list_index) } + return { :type => replace_brackets(type.text_value_no_const, '*'), :name => make_cmock_arg_name(arg_list_index) } end - return { :type => type.text_value, :name => make_cmock_arg_name(arg_list_index) } + return { :type => type.text_value_no_const, :name => make_cmock_arg_name(arg_list_index) } end if (name.brackets?) - return { :type => "#{type.text_value}*", :name => name.replace_brackets } + return { :type => "#{type.text_value_no_const}*", :name => replace_brackets(name.text_value) } end - return { :type => type.text_value, :name => name.text_value } + return { :type => type.text_value_no_const, :name => name.text_value } end end @@ -282,10 +279,6 @@ module CMockFunctionPrototype def get_brackets return brackets.text_value end - - def replace_brackets(replace='') - return text_value.gsub(/(\s*\[\s*[0-9]*\])+/, replace) - end end @@ -300,7 +293,15 @@ module CMockFunctionPrototype include FunctionPrototypeUtils def text_value - return normalize_ptr(super.gsub(/\s+/, ' ')).strip + type = super + type.gsub!(/\s+/, ' ') # remove extra spaces + type.gsub!(/\s\*/, '*') # remove space preceding '*' + type.gsub!(/const\*/, 'const *') # space out 'const' & '*' + return type.strip + end + + def text_value_no_const + return text_value.gsub(/\s*const\s*/, '') end def brackets? @@ -310,10 +311,6 @@ module CMockFunctionPrototype def get_brackets return brackets.text_value end - - def replace_brackets(replace='') - return text_value.gsub(/(\s*\[\s*[0-9]*\])+/, replace) - end end diff --git a/lib/cmock_generator_plugin_expect.rb b/lib/cmock_generator_plugin_expect.rb index 2c74baf..0ab12f2 100644 --- a/lib/cmock_generator_plugin_expect.rb +++ b/lib/cmock_generator_plugin_expect.rb @@ -30,10 +30,9 @@ class CMockGeneratorPluginExpect end function[:args].each do |arg| - type = arg[:type].gsub(/\s*const\s*/, ' ').strip - lines << [ "#{@tab}#{type} *#{function[:name]}_Expected_#{arg[:name]};\n", - "#{@tab}#{type} *#{function[:name]}_Expected_#{arg[:name]}_Head;\n", - "#{@tab}#{type} *#{function[:name]}_Expected_#{arg[:name]}_Tail;\n" ] + lines << [ "#{@tab}#{arg[:type]} *#{function[:name]}_Expected_#{arg[:name]};\n", + "#{@tab}#{arg[:type]} *#{function[:name]}_Expected_#{arg[:name]}_Head;\n", + "#{@tab}#{arg[:type]} *#{function[:name]}_Expected_#{arg[:name]}_Tail;\n" ] end lines.flatten end @@ -72,8 +71,7 @@ class CMockGeneratorPluginExpect end function[:args].each do |arg| - arg_return_type = arg[:type].gsub(/\s*const\s*/, ' ').strip - lines << @utils.code_verify_an_arg_expectation(function, arg_return_type, arg[:name]) + lines << @utils.code_verify_an_arg_expectation(function, arg[:type], arg[:name]) end lines.flatten end @@ -86,8 +84,7 @@ class CMockGeneratorPluginExpect lines << "void ExpectParameters_#{function[:name]}(#{function[:args_string]})\n" lines << "{\n" function[:args].each do |arg| - type = arg[:type].gsub(/\s*const\s*/, ' ').strip - lines << @utils.code_add_an_arg_expectation(function, type, arg[:name]) + lines << @utils.code_add_an_arg_expectation(function, arg[:type], arg[:name]) end lines << "}\n\n" end diff --git a/test/unit/cmock_function_prototype_parser_test.rb b/test/unit/cmock_function_prototype_parser_test.rb index 348a54a..f1bd530 100644 --- a/test/unit/cmock_function_prototype_parser_test.rb +++ b/test/unit/cmock_function_prototype_parser_test.rb @@ -123,11 +123,11 @@ class CMockFunctionPrototypeParserTest < Test::Unit::TestCase parsed = @parser.parse("void foo_bar(const unsigned int const_param, int int_param, int integer, char character, int* const constant)") assert_equal('const unsigned int const_param, int int_param, int integer, char character, int* const constant', parsed.get_argument_list) assert_equal([ - {:type => 'const unsigned int', :name => 'const_param'}, + {:type => 'unsigned int', :name => 'const_param'}, {:type => 'int', :name => 'int_param'}, {:type => 'int', :name => 'integer'}, {:type => 'char', :name => 'character'}, - {:type => 'int* const', :name => 'constant'}], + {:type => 'int*', :name => 'constant'}], parsed.get_arguments) assert_nil(parsed.get_var_arg) @@ -135,19 +135,19 @@ class CMockFunctionPrototypeParserTest < Test::Unit::TestCase assert_equal('signed char* abc, const unsigned long int xyz_123, unsigned int const abc_123, long long arm_of_the_law', parsed.get_argument_list) assert_equal([ {:type => 'signed char*', :name => 'abc'}, - {:type => 'const unsigned long int', :name => 'xyz_123'}, - {:type => 'unsigned int const', :name => 'abc_123'}, + {:type => 'unsigned long int', :name => 'xyz_123'}, + {:type => 'unsigned int', :name => 'abc_123'}, {:type => 'long long', :name => 'arm_of_the_law'}], parsed.get_arguments) assert_nil(parsed.get_var_arg) parsed = @parser.parse("void foo_bar(CUSTOM_TYPE abc, CUSTOM_TYPE* xyz_123, CUSTOM_TYPE const abcxyz, struct CUSTOM_TYPE const * const abc123)") - assert_equal('CUSTOM_TYPE abc, CUSTOM_TYPE* xyz_123, CUSTOM_TYPE const abcxyz, struct CUSTOM_TYPE const* const abc123', parsed.get_argument_list) + assert_equal('CUSTOM_TYPE abc, CUSTOM_TYPE* xyz_123, CUSTOM_TYPE const abcxyz, struct CUSTOM_TYPE const * const abc123', parsed.get_argument_list) assert_equal([ {:type => 'CUSTOM_TYPE', :name => 'abc'}, {:type => 'CUSTOM_TYPE*', :name => 'xyz_123'}, - {:type => 'CUSTOM_TYPE const', :name => 'abcxyz'}, - {:type => 'struct CUSTOM_TYPE const* const', :name => 'abc123'}], + {:type => 'CUSTOM_TYPE', :name => 'abcxyz'}, + {:type => 'struct CUSTOM_TYPE*', :name => 'abc123'}], parsed.get_arguments) assert_nil(parsed.get_var_arg) @@ -319,9 +319,9 @@ class CMockFunctionPrototypeParserTest < Test::Unit::TestCase parsed.get_argument_list) assert_equal( [{:type => 'FUNC_PTR_FOO_BAR_PARAM_1_T', :name => 'cmock_arg1'}, - {:type => 'char* const', :name => 'cmock_arg2'}, + {:type => 'char*', :name => 'cmock_arg2'}, {:type => 'unsigned int', :name => 'c'}, - {:type => 'long const', :name => 'cmock_arg4'}, + {:type => 'long', :name => 'cmock_arg4'}, {:type => 'CUSTOM_THING', :name => 'cmock_arg5'}, {:type => 'int*', :name => 'cmock_arg6'}, {:type => 'char*', :name => 'cmock_arg7'}],