Fixed handling of pointers that look like function pointers but are not (Fixed #349)

This commit is contained in:
Mark VanderVoord
2026-06-26 13:00:26 -04:00
parent 783bbde34d
commit 9e8d1f93ef
2 changed files with 57 additions and 0 deletions
+4
View File
@@ -565,6 +565,10 @@ class CMockHeaderParser
# pull asterisks away from arg to place asterisks with type (where they belong)
arg_list.gsub!(/\*(\w)/, '* \1')
# normalize parenthesized pointer arguments like int (* numb) -> int * numb
# negative lookahead prevents matching function pointers (*name)(args) and pointer-to-arrays (*name)[dims]
arg_list.gsub!(/\(\s*\*\s*((?:const\s+)?\w+)\s*\)(?!\s*[(\[])/, '* \1')
# scan argument list for function pointers and replace them with custom types
arg_list.gsub!(/([\w\s*]+)\(+([\w\s]*)\*[*\s]*([\w\s]*)\s*\)+\s*\(((?:[\w\s*]*,?)*)\s*\)*/) do |_m|
functype = "cmock_#{parse_project[:module_name]}_func_ptr#{parse_project[:typedefs].size + 1}"
+53
View File
@@ -1845,6 +1845,59 @@ describe CMockHeaderParser, "Verify CMockHeaderParser Module" do
assert_equal(typedefs, result[:typedefs])
end
it "extract functions containing a parenthesized pointer argument" do
source = "void func(char *str, int (* numb))"
expected = [{ :var_arg=>nil,
:return=>{ :type => "void",
:name => 'cmock_to_return',
:ptr? => false,
:const? => false,
:const_ptr? => false,
:str => "void cmock_to_return",
:void? => true
},
:name=>"func",
:unscoped_name=>"func",
:namespace=>[],
:class=>nil,
:modifier=>"",
:contains_ptr? => true,
:args=>[ {:type=>"char*", :name=>"str", :ptr? => false, :string? => true, :const? => false, :const_ptr? => false},
{:type=>"int*", :name=>"numb", :ptr? => true, :string? => false, :const? => false, :const_ptr? => false}
],
:args_string=>"char* str, int* numb",
:args_call=>"str, numb" }]
result = @parser.parse("module", source)
assert_equal(expected, result[:functions])
assert_equal([], result[:typedefs])
end
it "extract functions containing a parenthesized const pointer argument" do
source = "void func(int (* const numb))"
expected = [{ :var_arg=>nil,
:return=>{ :type => "void",
:name => 'cmock_to_return',
:ptr? => false,
:const? => false,
:const_ptr? => false,
:str => "void cmock_to_return",
:void? => true
},
:name=>"func",
:unscoped_name=>"func",
:namespace=>[],
:class=>nil,
:modifier=>"",
:contains_ptr? => true,
:args=>[ {:type=>"int*", :name=>"numb", :ptr? => true, :string? => false, :const? => false, :const_ptr? => true}
],
:args_string=>"int* const numb",
:args_call=>"numb" }]
result = @parser.parse("module", source)
assert_equal(expected, result[:functions])
assert_equal([], result[:typedefs])
end
it "extract functions with varargs" do
source = "int XFiles(int Scully, int Mulder, ...);\n"
expected = [{ :var_arg=>"...",