From 55462aef40899193c1a36736342dea8027e04141 Mon Sep 17 00:00:00 2001 From: John Lindgren Date: Fri, 1 Sep 2017 12:30:11 -0400 Subject: [PATCH] Fix parsing of declarations like "int const* doStuff(void)". The "const" in this case was not correctly stripped from the return type, leading to a double const in generated typedefs, i.e.: typedef const int const* (* doStuff_CALLBACK)(int cmock_num_calls); Add a test for these cases. --- lib/cmock_header_parser.rb | 6 ++--- test/unit/cmock_header_parser_test.rb | 33 +++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/lib/cmock_header_parser.rb b/lib/cmock_header_parser.rb index d0e5a65..4ade8cc 100644 --- a/lib/cmock_header_parser.rb +++ b/lib/cmock_header_parser.rb @@ -228,8 +228,8 @@ class CMockHeaderParser #process function attributes, return type, and name descriptors = regex_match[1] - descriptors.gsub!(/\s+\*/,'*') #remove space to place asterisks with return type (where they belong) - descriptors.gsub!(/\*(\w)/,'* \1') #pull asterisks away from function name to place asterisks with return type (where they belong) + descriptors.gsub!(/(\w)\*/,'\1 *') #pull asterisks away from preceding word + descriptors.gsub!(/\*(\w)/,'* \1') #pull asterisks away from following word descriptors = descriptors.split #array of all descriptor strings #grab name @@ -249,7 +249,7 @@ class CMockHeaderParser end end decl[:modifier] = decl[:modifier].join(' ') - rettype = rettype.join(' ') + rettype = rettype.join(' ').gsub(/\s+\*/,'*') #remove space before asterisks rettype = 'void' if (@local_as_void.include?(rettype.strip)) decl[:return] = { :type => rettype, :name => 'cmock_to_return', diff --git a/test/unit/cmock_header_parser_test.rb b/test/unit/cmock_header_parser_test.rb index 1d13caa..7375c50 100644 --- a/test/unit/cmock_header_parser_test.rb +++ b/test/unit/cmock_header_parser_test.rb @@ -805,6 +805,39 @@ describe CMockHeaderParser, "Verify CMockHeaderParser Module" do assert_equal(expected, @parser.parse("module", source)[:functions]) end + it "should properly handle const before or after return type" do + + sources = [ + "const int * PorkRoast(void);\n", + "int const * PorkRoast(void);\n", + "const int* PorkRoast(void);\n", + "int const* PorkRoast(void);\n", + "const int *PorkRoast(void);\n", + "int const *PorkRoast(void);\n" + ] + + expected = [{ :var_arg=>nil, + :name=>"PorkRoast", + :return=> { :type => "int*", + :name => 'cmock_to_return', + :ptr? => true, + :const? => true, + :const_ptr? => false, + :str => "int* cmock_to_return", + :void? => false + }, + :modifier=>"const", + :contains_ptr? => false, + :args=>[], + :args_string=>"void", + :args_call=>"" + }] + + sources.each do |source| + assert_equal(expected, @parser.parse("module", source)[:functions]) + end + end + it "should properly handle const applied after asterisk in return type (not legal C, but sometimes used)" do source = "int * const PorkRoast(void);\n"