From 6e58316fd02420b1cc0bfbaa38d240cc0f201992 Mon Sep 17 00:00:00 2001 From: Martin Babutzka Date: Thu, 12 Jan 2023 13:54:50 +0100 Subject: [PATCH] Add the parameter to additionally pass the original header file to the CMock parse function. This allows the treat_inlines to operate on the original header instead of the preprocessed header which is crucial to get a correct copy of the original header (with the keywords static/inline stripped). This needs the appropriate ceedling version to work. CMock called directly will continue to work since an illegal path is provided (""). No error is triggered, if the treat_inlines feature is deactivated. --- lib/cmock.rb | 10 +++++----- lib/cmock_header_parser.rb | 18 ++++++++++++------ 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/lib/cmock.rb b/lib/cmock.rb index 72f86418..087a3df0 100644 --- a/lib/cmock.rb +++ b/lib/cmock.rb @@ -27,9 +27,9 @@ def initialize(options = nil) @silent = (cm_config.verbosity < 2) end - def setup_mocks(files, folder = nil) + def setup_mocks(files, orig_file = nil, folder = nil) [files].flatten.each do |src| - generate_mock(src, folder) + generate_mock(src, orig_file, folder) end end @@ -41,11 +41,11 @@ def setup_skeletons(files) private ############################### - def generate_mock(src, folder) + def generate_mock(src, orig_file, folder) name = File.basename(src, '.*') ext = File.extname(src) puts "Creating mock for #{name}..." unless @silent - @cm_generator.create_mock(name, @cm_parser.parse(name, File.read(src)), ext, folder) + @cm_generator.create_mock(name, @cm_parser.parse(name, File.read(src), orig_file), ext, folder) end def generate_skeleton(src) @@ -106,6 +106,6 @@ def option_maker(options, key, val) if options[:skeleton] CMock.new(options).setup_skeletons(filelist) else - CMock.new(options).setup_mocks(filelist) + CMock.new(options).setup_mocks(filelist, "") end end diff --git a/lib/cmock_header_parser.rb b/lib/cmock_header_parser.rb index 491e4671..c97b3c17 100644 --- a/lib/cmock_header_parser.rb +++ b/lib/cmock_header_parser.rb @@ -29,7 +29,7 @@ def initialize(cfg) @c_strippables += ['inline'] if @treat_inlines == :include # we'll need to remove the attribute if we're allowing inlines end - def parse(name, source) + def parse(name, source, orig_file) parse_project = { :module_name => name.gsub(/\W/, ''), :typedefs => [], @@ -49,11 +49,17 @@ def parse(name, source) end end - parse_project[:normalized_source] = if @treat_inlines == :include - transform_inline_functions(source) - else - '' - end + if @treat_inlines == :include + if orig_file == nil + raise "Treat inlines is active but not-preprocessed original file is missing for " + name + elsif File.file?(orig_file) + # Run the inline transform on the original and never on the preprocessed source. + parse_project[:normalized_source] = transform_inline_functions(File.read(orig_file)) + else + # CMock.rb directly calls with orig_file="". In this case restore the original behavior. + parse_project[:normalized_source] = transform_inline_functions(source) + end + end { :includes => nil, :functions => parse_project[:functions],