mirror of
https://github.com/ThrowTheSwitch/CMock.git
synced 2026-09-12 21:29:57 +00:00
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:
+15
-1
@@ -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?)
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user