Convert inline functions in header-mock to 'normal' functions

- normalize_source() will convert the header-to-mock.
  It will look for the inline function definitions and transform them
  into function declarations for non-inline functions.
This commit is contained in:
laurens
2019-11-10 17:34:52 +01:00
parent 79ee5b8419
commit 5ee470d7e4
7 changed files with 81 additions and 2 deletions
+15 -1
View File
@@ -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?)
+40 -1
View File
@@ -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
+1
View File
@@ -4,6 +4,7 @@
:includes: []
:mock_path: ./system/generated/
:mock_prefix: mock_
:treat_inline: :include
:treat_as_void:
- OSEK_TASK
- VOID_TYPE_CRAZINESS
+18
View File
@@ -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
}
+3
View File
@@ -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
+1
View File
@@ -3,3 +3,4 @@
- 'soda'
- 'pizza'
:treat_externs: :include
:treat_inline: :include
+3
View File
@@ -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