diff --git a/lib/cmock_header_parser.rb b/lib/cmock_header_parser.rb index a44cc20..d0e5a65 100644 --- a/lib/cmock_header_parser.rb +++ b/lib/cmock_header_parser.rb @@ -146,16 +146,17 @@ class CMockHeaderParser return args if (arg =~ /^\s*((\.\.\.)|(void))\s*$/) # we're done if we reach void by itself or ... arg_array = arg.split arg_elements = arg_array - @c_attributes # split up words and remove known attributes - args << { :type => (arg_type = arg_elements[0..-2].join(' ')), + args << { :type => arg_elements[0..-2].join(' '), :name => arg_elements[-1] }.merge(divine_ptr_and_const(arg)) end return args end - def divine_ptr(arg_type) - return false unless arg_type.include? '*' - return false if arg_type.gsub(/(const|char|\*|\s)+/,'').empty? + def divine_ptr(arg) + return false unless arg.include? '*' + # treat "const char *" and similar as a string, not a pointer + return false if /(^|\s)(const\s+)?char(\s+const)?\s*\*(?!.*\*)/ =~ arg return true end diff --git a/test/unit/cmock_header_parser_test.rb b/test/unit/cmock_header_parser_test.rb index 591cb57..1d13caa 100644 --- a/test/unit/cmock_header_parser_test.rb +++ b/test/unit/cmock_header_parser_test.rb @@ -1052,7 +1052,7 @@ describe CMockHeaderParser, "Verify CMockHeaderParser Module" do assert_equal(expected, result[:functions]) end - it "handle arrays and treat them as pointers" do + it "handle arrays and treat them as pointers or strings" do source = "void KeyOperated(CUSTOM_TYPE thing1[], int thing2 [ ], char thing3 [][2 ][ 3], int* thing4[4])" expected = [{:var_arg=>nil, :return=>{ :type => "void", @@ -1068,7 +1068,7 @@ describe CMockHeaderParser, "Verify CMockHeaderParser Module" do :contains_ptr? => true, :args=>[ {:type=>"CUSTOM_TYPE*", :name=>"thing1", :ptr? => true, :const? => false, :const_ptr? => false}, {:type=>"int*", :name=>"thing2", :ptr? => true, :const? => false, :const_ptr? => false}, - {:type=>"char*", :name=>"thing3", :ptr? => true, :const? => false, :const_ptr? => false}, #THIS one will likely change in the future when we improve multidimensional array support + {:type=>"char*", :name=>"thing3", :ptr? => false, :const? => false, :const_ptr? => false}, #THIS one will likely change in the future when we improve multidimensional array support {:type=>"int**", :name=>"thing4", :ptr? => true, :const? => false, :const_ptr? => false} #THIS one will likely change in the future when we improve multidimensional array support ], :args_string=>"CUSTOM_TYPE* thing1, int* thing2, char* thing3, int** thing4", @@ -1407,7 +1407,7 @@ describe CMockHeaderParser, "Verify CMockHeaderParser Module" do :contains_ptr? => true, :args=>[ {:type=>"sqlite3_stmt*", :name=>"cmock_arg2", :ptr? => true, :const? => false, :const_ptr? => false}, {:type=>"int", :name=>"cmock_arg3", :ptr? => false, :const? => false, :const_ptr? => false}, - {:type=>"char*", :name=>"cmock_arg4", :ptr? => true, :const? => true, :const_ptr? => false}, + {:type=>"char*", :name=>"cmock_arg4", :ptr? => false, :const? => true, :const_ptr? => false}, {:type=>"int", :name=>"n", :ptr? => false, :const? => false, :const_ptr? => false}, {:type=>"cmock_module_func_ptr1", :name=>"cmock_arg1", :ptr? => false, :const? => false, :const_ptr? => false} ], @@ -1544,4 +1544,35 @@ describe CMockHeaderParser, "Verify CMockHeaderParser Module" do end end + it "divines ptr correctly for string types" do + truth_table = [ + # argument ptr + [ "char s", false ], + [ "const char s", false ], + [ "char const s", false ], + [ "char *s", false ], + [ "const char *s", false ], + [ "char const *s", false ], + [ "char *const s", false ], + [ "const char *const s", false ], + [ "char const *const s", false ], + [ "char **s", true ], + [ "const char **s", true ], + [ "char const **s", true ], + [ "char *const *s", true ], + [ "const char *const *s", true ], + [ "char const *const *s", true ], + [ "char **const s", true ], + [ "const char **const s", true ], + [ "char const **const s", true ], + [ "char *const *const s", true ], + [ "const char *const *const s", true ], + [ "char const *const *const s", true ] + ] + + truth_table.each do |entry| + assert_equal(@parser.divine_ptr(entry[0]), entry[1]) + end + end + end