From 50de50d2391f7a8a6c6d32031ad0c30471c62857 Mon Sep 17 00:00:00 2001 From: Lukasz Zeglinski Date: Sun, 9 May 2021 22:09:47 +0200 Subject: [PATCH 1/4] Switch places in the extern C regex to speed up mock generation --- lib/cmock_header_parser.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/cmock_header_parser.rb b/lib/cmock_header_parser.rb index 9730bf4..44fd015 100644 --- a/lib/cmock_header_parser.rb +++ b/lib/cmock_header_parser.rb @@ -245,8 +245,8 @@ class CMockHeaderParser source.gsub!(/__attribute(?:__)?\s*\(\(+.*\)\)+/, '') # remove preprocessor statements and extern "C" - source.gsub!(/^\s*#.*/, '') source.gsub!(/extern\s+\"C\"\s*\{/, '') + source.gsub!(/^\s*#.*/, '') # enums, unions, structs, and typedefs can all contain things (e.g. function pointers) that parse like function prototypes, so yank them # forward declared structs are removed before struct definitions so they don't mess up real thing later. we leave structs keywords in function prototypes From 7100ef0e6227d0234650bde8bae9e410bcb7a0a0 Mon Sep 17 00:00:00 2001 From: Ivan Siutsou Date: Sun, 7 Nov 2021 21:47:51 +0200 Subject: [PATCH 2/4] Fix parsing of calling conventions in function pointers --- lib/cmock_header_parser.rb | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/lib/cmock_header_parser.rb b/lib/cmock_header_parser.rb index 9730bf4..e9a7302 100644 --- a/lib/cmock_header_parser.rb +++ b/lib/cmock_header_parser.rb @@ -484,17 +484,18 @@ class CMockHeaderParser arg_list.gsub!(/\*(\w)/, '* \1') # scan argument list for function pointers and replace them with custom types - arg_list.gsub!(/([\w\s\*]+)\(+\s*\*[\*\s]*([\w\s]*)\s*\)+\s*\(((?:[\w\s\*]*,?)*)\s*\)*/) do |_m| + 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}" funcret = Regexp.last_match(1).strip - funcname = Regexp.last_match(2).strip - funcargs = Regexp.last_match(3).strip + funcdecl = Regexp.last_match(2).strip + funcname = Regexp.last_match(3).strip + funcargs = Regexp.last_match(4).strip funconst = '' if funcname.include? 'const' funcname.gsub!('const', '').strip! funconst = 'const ' end - parse_project[:typedefs] << "typedef #{funcret}(*#{functype})(#{funcargs});" + parse_project[:typedefs] << "typedef #{funcret}(#{funcdecl} *#{functype})(#{funcargs});" funcname = "cmock_arg#{c += 1}" if funcname.empty? "#{functype} #{funconst}#{funcname}" end From 8b2e897d35772003b64d61ed5056e8af0ebee337 Mon Sep 17 00:00:00 2001 From: Ivan Siutsou Date: Sun, 7 Nov 2021 23:04:44 +0200 Subject: [PATCH 3/4] The same output witout convention --- lib/cmock_header_parser.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/cmock_header_parser.rb b/lib/cmock_header_parser.rb index e9a7302..e9d7d08 100644 --- a/lib/cmock_header_parser.rb +++ b/lib/cmock_header_parser.rb @@ -495,7 +495,10 @@ class CMockHeaderParser funcname.gsub!('const', '').strip! funconst = 'const ' end - parse_project[:typedefs] << "typedef #{funcret}(#{funcdecl} *#{functype})(#{funcargs});" + if funcdecl != '' + funcdecl += ' ' + end + parse_project[:typedefs] << "typedef #{funcret}(#{funcdecl}*#{functype})(#{funcargs});" funcname = "cmock_arg#{c += 1}" if funcname.empty? "#{functype} #{funconst}#{funcname}" end From 202721e5612702b7f89c170ebedb93bb765b48eb Mon Sep 17 00:00:00 2001 From: Dani Martin <32535596+danimartin82@users.noreply.github.com> Date: Fri, 17 Dec 2021 13:18:19 +0100 Subject: [PATCH 4/4] Fix skeleton file creation --- lib/cmock_generator.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/cmock_generator.rb b/lib/cmock_generator.rb index 6ed5110..5311c7d 100644 --- a/lib/cmock_generator.rb +++ b/lib/cmock_generator.rb @@ -134,7 +134,7 @@ class CMockGenerator def create_skeleton_source_file(mock_project) filename = "#{@config.mock_path}/#{@subdir + '/' if @subdir}#{mock_project[:module_name]}.c" existing = File.exist?(filename) ? File.read(filename) : '' - @file_writer.append_file(mock_project[:module_name] + '.c', @subdir) do |file, fullname| + @file_writer.create_file(mock_project[:module_name] + '.c', @subdir) do |file, fullname| blank_project = mock_project.clone blank_project[:parsed_stuff] = { :functions => [] } create_source_header_section(file, fullname, blank_project) if existing.empty?