From 3d4ddfdc4cd7fab87b5b45b5799ea3a5320024e2 Mon Sep 17 00:00:00 2001 From: Mark VanderVoord Date: Tue, 16 Jun 2026 15:37:14 -0400 Subject: [PATCH] Fix const pointer handling in return values --- lib/cmock_generator.rb | 19 +++++++++++++------ lib/cmock_header_parser.rb | 3 ++- test/unit/cmock_header_parser_test.rb | 2 +- 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/lib/cmock_generator.rb b/lib/cmock_generator.rb index 24e6289..dd0b9b1 100644 --- a/lib/cmock_generator.rb +++ b/lib/cmock_generator.rb @@ -296,11 +296,20 @@ class CMockGenerator file << "}\n\n" end + def function_return_type(function) + # When const_ptr? is true, the "const" in :modifier belongs after the asterisk (e.g. "int* const"), + # not as a prefix (which would incorrectly produce "const int*"). + ret = if function[:return][:const_ptr?] + "#{function[:return][:type]} const" + else + (function[:modifier].empty? ? '' : "#{function[:modifier]} ") + function[:return][:type] + end + ret + (function[:c_calling_convention] ? " #{function[:c_calling_convention]}" : '') + end + def create_mock_implementation(file, function) # prepare return value and arguments - function_mod_and_rettype = (function[:modifier].empty? ? '' : "#{function[:modifier]} ") + - (function[:return][:type]) + - (function[:c_calling_convention] ? " #{function[:c_calling_convention]}" : '') + function_mod_and_rettype = function_return_type(function) args_string = function[:args_string] args_string += ", #{function[:var_arg]}" unless function[:var_arg].nil? @@ -359,9 +368,7 @@ class CMockGenerator def create_function_skeleton(file, function, existing) # prepare return value and arguments - function_mod_and_rettype = (function[:modifier].empty? ? '' : "#{function[:modifier]} ") + - (function[:return][:type]) + - (function[:c_calling_convention] ? " #{function[:c_calling_convention]}" : '') + function_mod_and_rettype = function_return_type(function) args_string = function[:args_string] args_string += ", #{function[:var_arg]}" unless function[:var_arg].nil? diff --git a/lib/cmock_header_parser.rb b/lib/cmock_header_parser.rb index 803a5dc..f587263 100644 --- a/lib/cmock_header_parser.rb +++ b/lib/cmock_header_parser.rb @@ -567,9 +567,10 @@ class CMockHeaderParser rettype = parsed[:type] rettype = 'void' if @local_as_void.include?(rettype.strip) + retstr = parsed[:const_ptr?] ? "#{rettype} const" : rettype decl[:return] = { :type => rettype, :name => 'cmock_to_return', - :str => "#{rettype} cmock_to_return", + :str => "#{retstr} cmock_to_return", :void? => (rettype == 'void'), :ptr? => parsed[:ptr?] || false, :const? => parsed[:const?] || false, diff --git a/test/unit/cmock_header_parser_test.rb b/test/unit/cmock_header_parser_test.rb index e47ef20..05e3c02 100644 --- a/test/unit/cmock_header_parser_test.rb +++ b/test/unit/cmock_header_parser_test.rb @@ -1038,7 +1038,7 @@ describe CMockHeaderParser, "Verify CMockHeaderParser Module" do :ptr? => true, :const? => false, :const_ptr? => true, - :str => "int* cmock_to_return", + :str => "int* const cmock_to_return", :void? => false }, :modifier=>"const",