Debian Codegraph @

lib/cmock_generator.rb

raw symbols SCIP
1 # ==========================================
2 # CMock Project - Automatic Mock Generation for C
3 # Copyright (c) 2007 Mike Karlesky, Mark VanderVoord, Greg Williams
4 # [Released under MIT License. Please refer to license.txt for details]
5 # ==========================================
6
7 class CMockGenerator
8 attr_accessor :config, :file_writer, :module_name, :module_ext, :clean_mock_name, :mock_name, :utils, :plugins, :weak, :ordered
9
10 def initialize(config, file_writer, utils, plugins)
11 @file_writer = file_writer
12 @utils = utils
13 @plugins = plugins
14 @config = config
15 @prefix = @config.mock_prefix
16 @suffix = @config.mock_suffix
17 @weak = @config.weak
18 @include_inline = @config.treat_inlines
19 @ordered = @config.enforce_strict_ordering
20 @framework = @config.framework.to_s
21 @fail_on_unexpected_calls = @config.fail_on_unexpected_calls
22 @exclude_setjmp_h = @config.exclude_setjmp_h
23 @subdir = @config.subdir
24
25 @includes_h_pre_orig_header = ((@config.includes || []) + (@config.includes_h_pre_orig_header || [])).uniq.map { |h| h =~ /</ ? h : "\"#{h}\"" }
26 @includes_h_post_orig_header = (@config.includes_h_post_orig_header || []).map { |h| h =~ /</ ? h : "\"#{h}\"" }
27 @includes_c_pre_header = (@config.includes_c_pre_header || []).map { |h| h =~ /</ ? h : "\"#{h}\"" }
28 @includes_c_post_header = (@config.includes_c_post_header || []).map { |h| h =~ /</ ? h : "\"#{h}\"" }
29
30 here = File.dirname __FILE__
31 unity_path_in_ceedling = "#{here}/../../unity" # path to Unity from within Ceedling
32 unity_path_in_cmock = "#{here}/../vendor/unity" # path to Unity from within CMock
33 # path to Unity as specified by env var
34 unity_path_in_env = ENV.key?('UNITY_DIR') ? File.expand_path(ENV.fetch('UNITY_DIR')) : nil
35
36 if unity_path_in_env && File.exist?(unity_path_in_env)
37 require "#{unity_path_in_env}/auto/type_sanitizer"
38 elsif File.exist? unity_path_in_ceedling
39 require "#{unity_path_in_ceedling}/auto/type_sanitizer"
40 elsif File.exist? unity_path_in_cmock
41 require "#{unity_path_in_cmock}/auto/type_sanitizer"
42 else
43 raise 'Failed to find an instance of Unity to pull in type_sanitizer module!'
44 end
45 end
46
47 def create_mock(module_name, parsed_stuff, module_ext = nil, folder = nil)
48 # determine the name for our new mock
49 mock_name = @prefix + module_name + @suffix
50
51 # determine the folder our mock will reside
52 mock_folder = if folder && @subdir
53 File.join(@subdir, folder)
54 elsif @subdir
55 @subdir
56 else
57 folder
58 end
59
60 # adds a trailing slash to the folder output
61 mock_folder = File.join(mock_folder, '') if mock_folder
62
63 # create out mock project from incoming data
64 mock_project = {
65 :module_name => module_name,
66 :module_ext => (module_ext || '.h'),
67 :mock_name => mock_name,
68 :clean_name => TypeSanitizer.sanitize_c_identifier(mock_name),
69 :folder => mock_folder,
70 :parsed_stuff => parsed_stuff,
71 :skeleton => false
72 }
73
74 create_mock_subdir(mock_project)
75 create_mock_header_file(mock_project)
76 create_mock_source_file(mock_project)
77 end
78
79 def create_skeleton(module_name, parsed_stuff)
80 mock_project = {
81 :module_name => module_name,
82 :module_ext => '.h',
83 :parsed_stuff => parsed_stuff,
84 :skeleton => true
85 }
86
87 create_skeleton_source_file(mock_project)
88 end
89
90 private if $ThisIsOnlyATest.nil? ##############################
91
92 def create_mock_subdir(mock_project)
93 @file_writer.create_subdir(mock_project[:folder])
94 end
95
96 def create_using_statement(file, function)
97 file << "using namespace #{function[:namespace].join('::')};\n" unless function[:namespace].empty?
98 end
99
100 def create_mock_header_file(mock_project)
101 if @include_inline == :include
102 @file_writer.create_file(mock_project[:module_name] + (mock_project[:module_ext]), mock_project[:folder]) do |file, _filename|
103 file << mock_project[:parsed_stuff][:normalized_source]
104 end
105 end
106
107 @file_writer.create_file(mock_project[:mock_name] + mock_project[:module_ext], mock_project[:folder]) do |file, filename|
108 create_mock_header_header(file, filename, mock_project)
109 create_mock_header_service_call_declarations(file, mock_project)
110 create_typedefs(file, mock_project)
111 mock_project[:parsed_stuff][:functions].each do |function|
112 create_using_statement(file, function)
113 file << @plugins.run(:mock_function_declarations, function)
114 end
115 create_mock_header_footer(file)
116 end
117 end
118
119 def create_mock_source_file(mock_project)
120 @file_writer.create_file(mock_project[:mock_name] + '.c', mock_project[:folder]) do |file, filename|
121 create_source_header_section(file, filename, mock_project)
122 create_instance_structure(file, mock_project)
123 create_extern_declarations(file)
124 create_mock_verify_function(file, mock_project)
125 create_mock_init_function(file, mock_project)
126 create_mock_destroy_function(file, mock_project)
127 mock_project[:parsed_stuff][:functions].each do |function|
128 create_mock_implementation(file, function)
129 create_mock_interfaces(file, function)
130 end
131 end
132 end
133
134 def create_skeleton_source_file(mock_project)
135 filename = "#{@config.mock_path}/#{@subdir + '/' if @subdir}#{mock_project[:module_name]}.c"
136 existing = File.exist?(filename) ? File.read(filename) : ''
137 @file_writer.create_file(mock_project[:module_name] + '.c', @subdir) do |file, fullname|
138 blank_project = mock_project.clone
139 blank_project[:parsed_stuff] = { :functions => [] }
140 if existing.empty?
141 create_source_header_section(file, fullname, blank_project)
142 else
143 file << existing << "\n"
144 end
145 mock_project[:parsed_stuff][:functions].each do |function|
146 create_function_skeleton(file, function, existing)
147 end
148 end
149 end
150
151 def create_mock_header_header(file, _filename, mock_project)
152 define_name = mock_project[:clean_name].upcase
153 orig_filename = (mock_project[:folder] || '') + mock_project[:module_name] + mock_project[:module_ext]
154 file << "/* AUTOGENERATED FILE. DO NOT EDIT. */\n"
155 file << "#ifndef _#{define_name}_H\n"
156 file << "#define _#{define_name}_H\n\n"
157 file << "#include \"#{@framework}.h\"\n"
158 @includes_h_pre_orig_header.each { |inc| file << "#include #{inc}\n" }
159 file << @config.orig_header_include_fmt.gsub(/%s/, orig_filename.to_s) + "\n"
160 @includes_h_post_orig_header.each { |inc| file << "#include #{inc}\n" }
161 plugin_includes = @plugins.run(:include_files)
162 file << plugin_includes unless plugin_includes.empty?
163 file << "\n"
164 file << "/* Ignore the following warnings, since we are copying code */\n"
165 file << "#if defined(__GNUC__) && !defined(__ICC) && !defined(__TMS470__)\n"
166 file << "#if __GNUC__ > 4 || (__GNUC__ == 4 && (__GNUC_MINOR__ > 6 || (__GNUC_MINOR__ == 6 && __GNUC_PATCHLEVEL__ > 0)))\n"
167 file << "#pragma GCC diagnostic push\n"
168 file << "#endif\n"
169 file << "#if !defined(__clang__)\n"
170 file << "#pragma GCC diagnostic ignored \"-Wpragmas\"\n"
171 file << "#endif\n"
172 file << "#pragma GCC diagnostic ignored \"-Wunknown-pragmas\"\n"
173 file << "#pragma GCC diagnostic ignored \"-Wduplicate-decl-specifier\"\n"
174 file << "#endif\n"
175 file << "\n"
176 file << "#ifdef __cplusplus\n"
177 file << "extern \"C\" {\n"
178 file << "#endif\n"
179 file << "\n"
180 end
181
182 def create_typedefs(file, mock_project)
183 file << "\n"
184 mock_project[:parsed_stuff][:typedefs].each { |typedef| file << "#{typedef}\n" }
185 file << "\n\n"
186 end
187
188 def create_mock_header_service_call_declarations(file, mock_project)
189 file << "void #{mock_project[:clean_name]}_Init(void);\n"
190 file << "void #{mock_project[:clean_name]}_Destroy(void);\n"
191 file << "void #{mock_project[:clean_name]}_Verify(void);\n\n"
192 end
193
194 def create_mock_header_footer(header)
195 header << "\n"
196 header << "#ifdef __cplusplus\n"
197 header << "}\n"
198 header << "#endif\n"
199 header << "\n"
200 header << "#if defined(__GNUC__) && !defined(__ICC) && !defined(__TMS470__)\n"
201 header << "#if __GNUC__ > 4 || (__GNUC__ == 4 && (__GNUC_MINOR__ > 6 || (__GNUC_MINOR__ == 6 && __GNUC_PATCHLEVEL__ > 0)))\n"
202 header << "#pragma GCC diagnostic pop\n"
203 header << "#endif\n"
204 header << "#endif\n"
205 header << "\n"
206 header << "#endif\n"
207 end
208
209 def create_source_header_section(file, filename, mock_project)
210 header_file = (mock_project[:folder] || '') + filename.gsub('.c', mock_project[:module_ext])
211 file << "/* AUTOGENERATED FILE. DO NOT EDIT. */\n" unless mock_project[:parsed_stuff][:functions].empty?
212 file << "#include <string.h>\n"
213 file << "#include <stdlib.h>\n"
214 unless @exclude_setjmp_h
215 file << "#include <setjmp.h>\n"
216 end
217 file << "#include \"cmock.h\"\n"
218 @includes_c_pre_header.each { |inc| file << "#include #{inc}\n" }
219 file << "#include \"#{header_file}\"\n"
220 @includes_c_post_header.each { |inc| file << "#include #{inc}\n" }
221 file << "\n"
222 strs = []
223 mock_project[:parsed_stuff][:functions].each do |func|
224 strs << func[:name]
225 func[:args].each { |arg| strs << arg[:name] }
226 end
227 strs.uniq.sort.each do |str|
228 file << "static const char* CMockString_#{str} = \"#{str}\";\n"
229 end
230 file << "\n"
231 end
232
233 def create_instance_structure(file, mock_project)
234 functions = mock_project[:parsed_stuff][:functions]
235 functions.each do |function|
236 file << "typedef struct _CMOCK_#{function[:name]}_CALL_INSTANCE\n{\n"
237 file << " UNITY_LINE_TYPE LineNumber;\n"
238 file << @plugins.run(:instance_typedefs, function)
239 file << "\n} CMOCK_#{function[:name]}_CALL_INSTANCE;\n\n"
240 end
241 file << "static struct #{mock_project[:clean_name]}Instance\n{\n"
242 if functions.empty?
243 file << " unsigned char placeHolder;\n"
244 end
245 functions.each do |function|
246 file << @plugins.run(:instance_structure, function)
247 file << " CMOCK_MEM_INDEX_TYPE #{function[:name]}_CallInstance;\n"
248 end
249 file << "} Mock;\n\n"
250 end
251
252 def create_extern_declarations(file)
253 unless @exclude_setjmp_h
254 file << "extern jmp_buf AbortFrame;\n"
255 end
256 if @ordered
257 file << "extern int GlobalExpectCount;\n"
258 file << "extern int GlobalVerifyOrder;\n"
259 end
260 file << "\n"
261 end
262
263 def create_mock_verify_function(file, mock_project)
264 file << "void #{mock_project[:clean_name]}_Verify(void)\n{\n"
265 verifications = mock_project[:parsed_stuff][:functions].collect do |function|
266 v = @plugins.run(:mock_verify, function)
267 v.empty? ? v : [" call_instance = Mock.#{function[:name]}_CallInstance;\n", v]
268 end.join
269 unless verifications.empty?
270 file << " UNITY_LINE_TYPE cmock_line = TEST_LINE_NUM;\n"
271 file << " CMOCK_MEM_INDEX_TYPE call_instance;\n"
272 file << verifications
273 end
274 file << "}\n\n"
275 end
276
277 def create_mock_init_function(file, mock_project)
278 file << "void #{mock_project[:clean_name]}_Init(void)\n{\n"
279 file << " #{mock_project[:clean_name]}_Destroy();\n"
280 file << "}\n\n"
281 end
282
283 def create_mock_destroy_function(file, mock_project)
284 file << "void #{mock_project[:clean_name]}_Destroy(void)\n{\n"
285 file << " CMock_Guts_MemFreeAll();\n"
286 file << " memset(&Mock, 0, sizeof(Mock));\n"
287 file << mock_project[:parsed_stuff][:functions].collect { |function| @plugins.run(:mock_destroy, function) }.join
288
289 unless @fail_on_unexpected_calls
290 file << mock_project[:parsed_stuff][:functions].collect { |function| @plugins.run(:mock_ignore, function) }.join
291 end
292
293 if @ordered
294 file << " GlobalExpectCount = 0;\n"
295 file << " GlobalVerifyOrder = 0;\n"
296 end
297 file << "}\n\n"
298 end
299
300 def create_mock_implementation(file, function)
301 # prepare return value and arguments
302 function_mod_and_rettype = (function[:modifier].empty? ? '' : "#{function[:modifier]} ") +
303 (function[:return][:type]) +
304 (function[:c_calling_convention] ? " #{function[:c_calling_convention]}" : '')
305 args_string = function[:args_string]
306 args_string += (', ' + function[:var_arg]) unless function[:var_arg].nil?
307
308 # Encapsulate in namespace(s) if applicable
309 function[:namespace].each do |ns|
310 file << "namespace #{ns} {\n"
311 end
312
313 # Determine class prefix (if any)
314 cls_pre = ''
315 unless function[:class].nil?
316 cls_pre = "#{function[:class]}::"
317 end
318
319 # Create mock function
320 unless @weak.empty?
321 file << "#if defined (__IAR_SYSTEMS_ICC__)\n"
322 file << "#pragma weak #{function[:unscoped_name]}\n"
323 file << "#else\n"
324 file << "#{function_mod_and_rettype} #{function[:unscoped_name]}(#{args_string}) #{weak};\n"
325 file << "#endif\n\n"
326 end
327 file << "#{function_mod_and_rettype} #{cls_pre}#{function[:unscoped_name]}(#{args_string})\n"
328 file << "{\n"
329 file << " UNITY_LINE_TYPE cmock_line = TEST_LINE_NUM;\n"
330 file << " CMOCK_#{function[:name]}_CALL_INSTANCE* cmock_call_instance;\n"
331 file << " UNITY_SET_DETAIL(CMockString_#{function[:name]});\n"
332 file << " cmock_call_instance = (CMOCK_#{function[:name]}_CALL_INSTANCE*)CMock_Guts_GetAddressFor(Mock.#{function[:name]}_CallInstance);\n"
333 file << " Mock.#{function[:name]}_CallInstance = CMock_Guts_MemNext(Mock.#{function[:name]}_CallInstance);\n"
334 file << @plugins.run(:mock_implementation_precheck, function)
335 file << " UNITY_TEST_ASSERT_NOT_NULL(cmock_call_instance, cmock_line, CMockStringCalledMore);\n"
336 file << " cmock_line = cmock_call_instance->LineNumber;\n"
337 if @ordered
338 file << " if (cmock_call_instance->CallOrder > ++GlobalVerifyOrder)\n"
339 file << " UNITY_TEST_FAIL(cmock_line, CMockStringCalledEarly);\n"
340 file << " if (cmock_call_instance->CallOrder < GlobalVerifyOrder)\n"
341 file << " UNITY_TEST_FAIL(cmock_line, CMockStringCalledLate);\n"
342 end
343 file << @plugins.run(:mock_implementation, function)
344 file << " UNITY_CLR_DETAILS();\n"
345 file << " return cmock_call_instance->ReturnVal;\n" unless function[:return][:void?]
346 file << "}\n"
347
348 # Close any namespace(s) opened above
349 function[:namespace].each do
350 file << "}\n"
351 end
352
353 file << "\n"
354 end
355
356 def create_mock_interfaces(file, function)
357 file << @utils.code_add_argument_loader(function)
358 file << @plugins.run(:mock_interfaces, function)
359 end
360
361 def create_function_skeleton(file, function, existing)
362 # prepare return value and arguments
363 function_mod_and_rettype = (function[:modifier].empty? ? '' : "#{function[:modifier]} ") +
364 (function[:return][:type]) +
365 (function[:c_calling_convention] ? " #{function[:c_calling_convention]}" : '')
366 args_string = function[:args_string]
367 args_string += (', ' + function[:var_arg]) unless function[:var_arg].nil?
368
369 decl = "#{function_mod_and_rettype} #{function[:name]}(#{args_string})"
370
371 return if existing.include?(decl)
372
373 file << "#{decl}\n"
374 file << "{\n"
375 file << " /*TODO: Implement Me!*/\n"
376 function[:args].each { |arg| file << " (void)#{arg[:name]};\n" }
377 file << " return (#{(function[:return][:type])})0;\n" unless function[:return][:void?]
378 file << "}\n\n"
379 end
380 end