Skip to content

Commit

Permalink
Merge pull request #144 from jlindgren90/master
Browse files Browse the repository at this point in the history
Merge pull request #136 and fix merge conflicts (Thanks @jlindgren90 and @phonetagger !)
  • Loading branch information
mvandervoord authored Sep 11, 2017
2 parents f2ea428 + ef04f4a commit 50adf82
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 7 deletions.
19 changes: 14 additions & 5 deletions lib/cmock_header_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@

class CMockHeaderParser

attr_accessor :funcs, :c_attributes, :treat_as_void, :treat_externs
attr_accessor :funcs, :c_attr_noconst, :c_attributes, :treat_as_void, :treat_externs

def initialize(cfg)
@funcs = []
@c_strippables = cfg.strippables
@c_attributes = (['const'] + cfg.attributes).uniq
@c_attr_noconst = cfg.attributes.uniq - ['const']
@c_attributes = ['const'] + c_attr_noconst
@c_calling_conventions = cfg.c_calling_conventions.uniq
@treat_as_void = (['void'] + cfg.treat_as_void).uniq
@declaration_parse_matcher = /([\d\w\s\*\(\),\[\]]+??)\(([\d\w\s\*\(\),\.\[\]+-]*)\)$/m
Expand Down Expand Up @@ -144,11 +145,19 @@ def parse_args(arg_list)
arg_list.split(',').each do |arg|
arg.strip!
return args if (arg =~ /^\s*((\.\.\.)|(void))\s*$/) # we're done if we reach void by itself or ...

# Split up words and remove known attributes, but in case of pointer args, don't remove any
# 'const' from the type that it points to, since that may change the underlying assembly-code
# pointer type on some embedded platforms, making it point to RAM instead of ROM. (I.e. For
# pointer args, remove 'const' only when it applies to the pointer itself. For non-pointer
# args, remove 'const' regardless.)
#
arg_array = arg.split
arg_elements = arg_array - @c_attributes # split up words and remove known attributes
args << { :type => arg_elements[0..-2].join(' '),
ptr_const_info = divine_ptr_and_const(arg)
arg_elements = arg_array - (arg.include?('*') ? @c_attr_noconst : @c_attributes)
args << { :type => arg_elements[0..(ptr_const_info[:const_ptr?] ? -3 : -2)].join(' '),
:name => arg_elements[-1]
}.merge(divine_ptr_and_const(arg))
}.merge(ptr_const_info)
end
return args
end
Expand Down
67 changes: 65 additions & 2 deletions test/unit/cmock_header_parser_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -861,6 +861,69 @@
assert_equal(expected, @parser.parse("module", source)[:functions])
end

it "properly parse const and pointer argument types with no arg names" do

source = "void foo(int const*, int*const, const int*, const int*const, int const*const, int*, int, const int);\n"

expected = [{ :name => "foo",
:modifier => "",
:return => { :type => "void",
:name => "cmock_to_return",
:str => "void cmock_to_return",
:void? => true,
:ptr? => false,
:const? => false,
:const_ptr? => false
},
:var_arg => nil,
:args_string => "int const* cmock_arg1, int* const cmock_arg2, const int* cmock_arg3, const int* const cmock_arg4, " +
"int const* const cmock_arg5, int* cmock_arg6, int cmock_arg7, const int cmock_arg8",
:args => [{ :type=>"int const*", :name => "cmock_arg1", :ptr? => true, :const? => true, :const_ptr? => false },
{ :type=>"int*", :name => "cmock_arg2", :ptr? => true, :const? => false, :const_ptr? => true },
{ :type=>"const int*", :name => "cmock_arg3", :ptr? => true, :const? => true, :const_ptr? => false },
{ :type=>"const int*", :name => "cmock_arg4", :ptr? => true, :const? => true, :const_ptr? => true },
{ :type=>"int const*", :name => "cmock_arg5", :ptr? => true, :const? => true, :const_ptr? => true },
{ :type=>"int*", :name => "cmock_arg6", :ptr? => true, :const? => false, :const_ptr? => false },
{ :type=>"int", :name => "cmock_arg7", :ptr? => false, :const? => false, :const_ptr? => false },
{ :type=>"int", :name => "cmock_arg8", :ptr? => false, :const? => true, :const_ptr? => false }],
:args_call => "cmock_arg1, cmock_arg2, cmock_arg3, cmock_arg4, cmock_arg5, cmock_arg6, cmock_arg7, cmock_arg8",
:contains_ptr? => true
}]
assert_equal(expected, @parser.parse("module", source)[:functions])
end

it "properly parse const and pointer argument types with arg names" do

source = "void bar(int const* param1, int*const param2, const int* param3, const int*const param4,\n" +
" int const*const param5, int*param6, int param7, const int param8);\n"

expected = [{ :name => "bar",
:modifier => "",
:return => { :type => "void",
:name => "cmock_to_return",
:str => "void cmock_to_return",
:void? => true,
:ptr? => false,
:const? => false,
:const_ptr? => false
},
:var_arg => nil,
:args_string => "int const* param1, int* const param2, const int* param3, const int* const param4, " +
"int const* const param5, int* param6, int param7, const int param8",
:args => [{ :type=>"int const*", :name => "param1", :ptr? => true, :const? => true, :const_ptr? => false },
{ :type=>"int*", :name => "param2", :ptr? => true, :const? => false, :const_ptr? => true },
{ :type=>"const int*", :name => "param3", :ptr? => true, :const? => true, :const_ptr? => false },
{ :type=>"const int*", :name => "param4", :ptr? => true, :const? => true, :const_ptr? => true },
{ :type=>"int const*", :name => "param5", :ptr? => true, :const? => true, :const_ptr? => true },
{ :type=>"int*", :name => "param6", :ptr? => true, :const? => false, :const_ptr? => false },
{ :type=>"int", :name => "param7", :ptr? => false, :const? => false, :const_ptr? => false },
{ :type=>"int", :name => "param8", :ptr? => false, :const? => true, :const_ptr? => false }],
:args_call => "param1, param2, param3, param4, param5, param6, param7, param8",
:contains_ptr? => true
}]
assert_equal(expected, @parser.parse("module", source)[:functions])
end

it "properly detect typedef'd variants of void and use those" do

source = "typedef (void) FUNKY_VOID_T;\n" +
Expand Down Expand Up @@ -937,7 +1000,7 @@
:name=>"Penny",
:modifier=>"",
:contains_ptr? => true,
:args=>[ {:type=>"struct _KeepYourHeadUp_*", :name=>"BillyBuddy", :ptr? => true, :const? => true, :const_ptr? => true} ],
:args=>[ {:type=>"struct const _KeepYourHeadUp_*", :name=>"BillyBuddy", :ptr? => true, :const? => true, :const_ptr? => true} ],
:args_string=>"struct const _KeepYourHeadUp_* const BillyBuddy",
:args_call=>"BillyBuddy"
},
Expand Down Expand Up @@ -1440,7 +1503,7 @@
: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? => false, :const? => true, :const_ptr? => false},
{:type=>"const 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}
],
Expand Down

0 comments on commit 50adf82

Please sign in to comment.