diff --git a/lib/cmock_generator.rb b/lib/cmock_generator.rb index 42725a6..d239446 100644 --- a/lib/cmock_generator.rb +++ b/lib/cmock_generator.rb @@ -16,6 +16,7 @@ class CMockGenerator @prefix = @config.mock_prefix @suffix = @config.mock_suffix @weak = @config.weak + @include_inline = @config.treat_inline @ordered = @config.enforce_strict_ordering @framework = @config.framework.to_s @fail_on_unexpected_calls = @config.fail_on_unexpected_calls @@ -63,6 +64,19 @@ class CMockGenerator def create_mock_header_file(parsed_stuff) @file_writer.create_file(@mock_name + ".h", @subdir) do |file, filename| create_mock_header_header(file, filename) + + unless @include_inline + file << @config.orig_header_include_fmt.gsub(/%s/, "#{orig_filename}") + "\n" + else + file << "\n" + file << "/* BEGIN OF ORIGINAL HEADER */" + file << "\n" + file << parsed_stuff[:normalized_source] + file << "\n" + file << "/* END OF ORIGINAL HEADER */" + file << "\n" + end + create_mock_header_service_call_declarations(file) create_typedefs(file, parsed_stuff[:typedefs]) parsed_stuff[:functions].each do |function| @@ -95,7 +109,7 @@ class CMockGenerator file << "#define _#{define_name}_H\n\n" file << "#include \"#{@framework}.h\"\n" @includes_h_pre_orig_header.each {|inc| file << "#include #{inc}\n"} - file << @config.orig_header_include_fmt.gsub(/%s/, "#{orig_filename}") + "\n" + # file << @config.orig_header_include_fmt.gsub(/%s/, "#{orig_filename}") + "\n" @includes_h_post_orig_header.each {|inc| file << "#include #{inc}\n"} plugin_includes = @plugins.run(:include_files) file << plugin_includes if (!plugin_includes.empty?) diff --git a/lib/cmock_header_parser.rb b/lib/cmock_header_parser.rb index e10d0d3..d18330e 100644 --- a/lib/cmock_header_parser.rb +++ b/lib/cmock_header_parser.rb @@ -45,12 +45,51 @@ class CMockHeaderParser { :includes => nil, :functions => @funcs, - :typedefs => @typedefs + :typedefs => @typedefs, + :normalized_source => normalize_source(source), } end private if $ThisIsOnlyATest.nil? ################ + def normalize_source(source) + # let's clean up the encoding in case they've done anything weird with the characters we might find + source = source.force_encoding("ISO-8859-1").encode("utf-8", :replace => nil) + + # # smush multiline macros into single line (checking for continuation character at end of line '\') + source.gsub!(/\s*\\\s*/m, ' ') + + # #remove comments (block and line, in three steps to ensure correct precedence) + source.gsub!(/\/\/(?:.+\/\*|\*(?:$|[^\/])).*$/, '') # remove line comments that comment out the start of blocks + source.gsub!(/\/\*.*?\*\//m, '') # remove block comments + source.gsub!(/\/\/.*$/, '') # remove line comments (all that remain) + + source.gsub!(/(static|inline)+.*\{.*\w*\}/m) do |m| + m.gsub!(/(static|inline)/, '') # remove static and inline + # remove nested pairs of braces because no function declarations will be inside of them (leave outer pair for function definition detection) + if (RUBY_VERSION.split('.')[0].to_i > 1) + #we assign a string first because (no joke) if Ruby 1.9.3 sees this line as a regex, it will crash. + r = "\\{([^\\{\\}]*|\\g<0>)*\\}" + m.gsub!(/#{r}/m, '{ }') + else + while m.gsub!(/\{[^\{\}]*\{[^\{\}]*\}[^\{\}]*\}/m, '{ }') + end + end + + # Functions having "{ }" at this point are/were inline functions, + # Disguise them as normal functions with the ";" + m.gsub!(/\s*\{\s\}/, ";") + + # TODO: fix these cleanup actions... + m.gsub!(/^\s+/, '') # remove extra white space from beginning of line + m.gsub!(/\s+/, ' ') # remove remaining extra white space + m.gsub!(";",";\n") + m.gsub!(/^\s+/, '') # remove extra white space from beginning of line + end + + source.gsub!(/\s+$/, '') # remove extra white space from end of line + end + def import_source(source) # let's clean up the encoding in case they've done anything weird with the characters we might find diff --git a/test/system/test_compilation/config.yml b/test/system/test_compilation/config.yml index d87c578..23cd75b 100644 --- a/test/system/test_compilation/config.yml +++ b/test/system/test_compilation/config.yml @@ -4,6 +4,7 @@ :includes: [] :mock_path: ./system/generated/ :mock_prefix: mock_ + :treat_inline: :include :treat_as_void: - OSEK_TASK - VOID_TYPE_CRAZINESS diff --git a/test/system/test_compilation/inline.h b/test/system/test_compilation/inline.h new file mode 100644 index 0000000..d605306 --- /dev/null +++ b/test/system/test_compilation/inline.h @@ -0,0 +1,18 @@ + +static inline void dummy_func_0(void) { + //NOP +} + +inline static void dummy_func_1(void) { + //NOP +} + +void inline static dummy_func_2(void) { + //NOP +} + +void dummy_normal_func(int a); + +inline void dummy_func_3(void) { + //NOP +} diff --git a/test/unit/cmock_config_test.rb b/test/unit/cmock_config_test.rb index 291c2cc..1812985 100644 --- a/test/unit/cmock_config_test.rb +++ b/test/unit/cmock_config_test.rb @@ -19,6 +19,7 @@ describe CMockConfig, "Verify CMockConfig Module" do assert_equal(CMockConfig::CMockDefaultOptions[:attributes], config.attributes) assert_equal(CMockConfig::CMockDefaultOptions[:plugins], config.plugins) assert_equal(CMockConfig::CMockDefaultOptions[:treat_externs], config.treat_externs) + assert_equal(CMockConfig::CMockDefaultOptions[:treat_inline], config.treat_inline) end it "replace only options specified in a hash" do @@ -30,6 +31,7 @@ describe CMockConfig, "Verify CMockConfig Module" do assert_equal(test_attributes, config.attributes) assert_equal(CMockConfig::CMockDefaultOptions[:plugins], config.plugins) assert_equal(CMockConfig::CMockDefaultOptions[:treat_externs], config.treat_externs) + assert_equal(CMockConfig::CMockDefaultOptions[:treat_inline], config.treat_inline) end it "replace only options specified in a yaml file" do @@ -40,6 +42,7 @@ describe CMockConfig, "Verify CMockConfig Module" do assert_nil(config.includes) assert_equal(test_plugins, config.plugins) assert_equal(:include, config.treat_externs) + assert_equal(:include, config.treat_inline) end it "populate treat_as map with internal standard_treat_as_map defaults, redefine defaults, and add custom values" do diff --git a/test/unit/cmock_config_test.yml b/test/unit/cmock_config_test.yml index b2444f8..6958b0f 100644 --- a/test/unit/cmock_config_test.yml +++ b/test/unit/cmock_config_test.yml @@ -3,3 +3,4 @@ - 'soda' - 'pizza' :treat_externs: :include + :treat_inline: :include diff --git a/test/unit/cmock_generator_main_test.rb b/test/unit/cmock_generator_main_test.rb index 548ddb9..e864faf 100644 --- a/test/unit/cmock_generator_main_test.rb +++ b/test/unit/cmock_generator_main_test.rb @@ -43,6 +43,7 @@ describe CMockGenerator, "Verify CMockGenerator Module" do #no strict handling @config.expect :mock_prefix, "Mock" + @config.expect :treat_inline, false @config.expect :mock_suffix, "" @config.expect :weak, "" @config.expect :enforce_strict_ordering, nil @@ -61,6 +62,7 @@ describe CMockGenerator, "Verify CMockGenerator Module" do #strict handling @config.expect :mock_prefix, "Mock" + @config.expect :treat_inline, false @config.expect :mock_suffix, "" @config.expect :weak, "" @config.expect :enforce_strict_ordering, true @@ -124,6 +126,7 @@ describe CMockGenerator, "Verify CMockGenerator Module" do #no strict handling @config.expect :mock_prefix, "Mock" @config.expect :mock_suffix, "" + @config.expect :treat_inline, ":exclude" @config.expect :weak, "" @config.expect :enforce_strict_ordering, nil @config.expect :framework, :unity