Class: Preprocessinator

Inherits:
Object show all
Defined in:
lib/ceedling/preprocess/preprocessinator.rb

Instance Method Summary collapse

Instance Method Details

#cached_includes_list?(test:, filepath:) ⇒ Boolean

Returns:

  • (Boolean)


159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/ceedling/preprocess/preprocessinator.rb', line 159

def cached_includes_list?(test:, filepath:)
  _filepath = @file_path_utils.form_preprocessed_includes_list_filepath( filepath, test )

  # Get or create a mutex for this specific cache file
  file_lock = @file_locks_mutex.synchronize do
    @file_locks[_filepath] ||= Mutex.new
  end

  file_lock.synchronize do
    # If existing YAML file of includes is newer than the file we're processing, skip preprocessing
    return @file_wrapper.newer?( _filepath, filepath )
  end
end

#generate_directives_only_output(filepath:, test:, flags:, include_paths:, vendor_paths:, defines:) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/ceedling/preprocess/preprocessinator.rb', line 53

def generate_directives_only_output(filepath:, test:, flags:, include_paths:, vendor_paths:, defines:)
  raw_preprocessed_filepath = 
    @file_path_utils.form_preprocessed_file_raw_directives_only_filepath( filepath, test )

  compacted_preprocessed_fileapth =
    @file_path_utils.form_preprocessed_file_compacted_directives_only_filepath( filepath, test )

  # Run GCC with directives-only preprocessor expansion
  command = @tool_executor.build_command_line(
    @configurator.tools_test_file_directives_only_preprocessor,
    # Additional arguments
    flags,
    # Argument replacement
    filepath,
    raw_preprocessed_filepath,
    defines,
    (include_paths + vendor_paths)
  )
  command[:options][:boom] = false
  results = @tool_executor.exec( command )

  # Preprocessor did not succeed
  if results[:exit_code] != 0
    msg = "Failed to generate directive-only preprocessor output (fallback methods will be used) for #{filepath}"
    @loginator.log( msg, Verbosity::OBNOXIOUS, LogLabels::ERROR )
    return nil
  end

  # Remove comments from directives-only file in filesystem.
  # Directives-only output keeps our most essential details (include directives & macros) and handles #ifdefs, etc.
  # However, it does not strip out comments.
  @comment_stripper.strip_file( raw_preprocessed_filepath )

  # Collect all code from between line markers into a clean file
  @reconstructor.compact_file_from_expansion(
    input_filepath: raw_preprocessed_filepath,
    source_filepath: filepath,
    output_filepath: compacted_preprocessed_fileapth
  )

  return raw_preprocessed_filepath
end

#load_includes_list(test:, filepath:) ⇒ Object



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/ceedling/preprocess/preprocessinator.rb', line 173

def load_includes_list(test:, filepath:)
  includes = []

  _filepath = @file_path_utils.form_preprocessed_includes_list_filepath( filepath, test )

  # Get or create a mutex for this specific cache file
  file_lock = @file_locks_mutex.synchronize do
    @file_locks[_filepath] ||= Mutex.new
  end

  file_lock.synchronize do
    # If existing YAML file of includes is newer than the file we're processing, skip preprocessing
    if @file_wrapper.newer?( _filepath, filepath )
      msg = @reportinator.generate_module_progress(
        operation: "Loading #include statement listing file for",
        module_name: test,
        filename: File.basename(filepath)
        )
      @loginator.log( msg, Verbosity::OBNOXIOUS )
    
      includes = @includes_handler.load_includes_list( _filepath )

      header = "Loaded existing #include list from #{_filepath}:"
      @loginator.log_list( includes, header, Verbosity::DEBUG )
    end
  end

  return !includes.empty?, includes
end

#preprocess_bare_includes(filepath:, test:, search_paths:, flags:, defines:) ⇒ Object

Extract bare includes (does not differentiate user/system) from a file. Called externally.



42
43
44
45
46
47
48
49
50
51
# File 'lib/ceedling/preprocess/preprocessinator.rb', line 42

def preprocess_bare_includes(filepath:, test:, search_paths:, flags:, defines:)
  # Pass-through
  return @includes_handler.extract_bare_includes(
    filepath:      filepath,
    test:          test,
    flags:         flags,
    search_paths:  search_paths,
    defines:       defines
    )
end

#preprocess_mockable_header_file(test:, filepath:, directives_only_filepath:, fallback:, flags:, include_paths:, vendor_paths:, defines:, extras: false) ⇒ Object



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'lib/ceedling/preprocess/preprocessinator.rb', line 203

def preprocess_mockable_header_file(
    test:,
    filepath:,
    directives_only_filepath:,
    fallback:,
    flags:,
    include_paths:,
    vendor_paths:,
    defines:,
    extras: false
)
  msg = @reportinator.generate_module_progress(
    operation: 'Preprocessing header file for follow-on mock handling',
    module_name: test,
    filename: File.basename( filepath )
  )
  @loginator.log( msg )

  preprocessed_filepath = @file_path_utils.form_preprocessed_file_filepath( filepath, test )

  plugin_arg_hash = {
    header_file:              filepath,
    preprocessed_header_file: preprocessed_filepath,
    test:                     test,
    flags:                    flags,
    include_paths:            include_paths,
    defines:                  defines      
  }

  # Trigger pre_mock_preprocessing plugin hook
  @plugin_manager.pre_mock_preprocess( plugin_arg_hash )

  arg_hash = {
    test:                      test,
    filepath:                  filepath,
    directives_only_filepath:  directives_only_filepath,
    fallback:                  fallback,
    flags:                     flags,
    include_paths:             include_paths,
    vendor_paths:              vendor_paths,
    defines:                   defines
  }

  # Extract includes & log progress and details   
  includes = preprocess_file_includes_common( **arg_hash )

  header = "Discovered #includes for mockable header from #{filepath}:"
  @loginator.log_list( includes, header, Verbosity::OBNOXIOUS )

  arg_hash = {
    test:                      test,
    filepath:                  filepath,
    directives_only_filepath:  directives_only_filepath,
    fallback:                  fallback,
    flags:                     flags,
    include_paths:             include_paths,
    defines:                   defines,
    extras:                    extras
  }

  # `contents` & `extras` are arrays of text strings to be assembled in generating a new header file.
  # `extras` are macro definitions, pragmas, etc. needed for the special case of mocking `inline` function declarations.
  # `extras` are empty for any cases other than mocking `inline` function declarations
  # (We don't want to increase our chances of a badly generated file--extracting extras could fail in complex files.)
  contents, extras = @file_assembler.collect_mockable_header_file_contents( **arg_hash )

  arg_hash = {
    filename:              File.basename( filepath ),
    preprocessed_filepath: preprocessed_filepath,
    contents:              contents,
    extras:                extras,
    includes:              includes                       
  }

  # Create a reconstituted header file from preprocessing expansion and preserving any extras
  @file_assembler.assemble_preprocessed_header_file( **arg_hash )

  # Trigger post_mock_preprocessing plugin hook
  @plugin_manager.post_mock_preprocess( plugin_arg_hash )

  return preprocessed_filepath
end

#preprocess_partial_header_expand_macros(filepath:, test:, flags:, include_paths:, vendor_paths:, defines:) ⇒ Object



473
474
475
476
477
478
479
480
481
482
# File 'lib/ceedling/preprocess/preprocessinator.rb', line 473

def preprocess_partial_header_expand_macros(filepath:, test:, flags:, include_paths:, vendor_paths:, defines:)
  _preprocess_partial_expand_macros(
    filepath:      filepath,
    test:          test,
    flags:         flags,
    include_paths: include_paths,
    vendor_paths:  vendor_paths,
    defines:       defines
  )
end

#preprocess_partial_header_file_preserve_macros(test:, filepath:, directives_only_filepath:, fallback:, flags:, include_paths:, vendor_paths:, defines:) ⇒ Object



286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
# File 'lib/ceedling/preprocess/preprocessinator.rb', line 286

def preprocess_partial_header_file_preserve_macros(
    test:,
    filepath:,
    directives_only_filepath:,
    fallback:,
    flags:,
    include_paths:,
    vendor_paths:,
    defines:
)
  msg = @reportinator.generate_module_progress(
    operation: 'Preprocessing header file for follow-on Partials handling',
    module_name: test,
    filename: File.basename( filepath )
  )
  @loginator.log( msg )

  preprocessed_filepath = @file_path_utils.form_preprocessed_file_filepath( filepath, test )

  arg_hash = {
    test:                      test,
    filepath:                  filepath,
    directives_only_filepath:  directives_only_filepath,
    fallback:                  fallback,
    flags:                     flags,
    include_paths:             include_paths,
    vendor_paths:              vendor_paths,
    defines:                   defines
  }

  # Extract includes & log progress and details
  includes = preprocess_file_includes_common( **arg_hash )

  header = "Discovered #includes for Partial header from #{filepath}:"
  @loginator.log_list( includes, header, Verbosity::OBNOXIOUS )

  contents =
    if fallback
      @file_assembler.collect_file_contents_fallback( source_filepath: filepath, defines: defines )
    else
      @file_assembler.collect_file_contents_from_directives_only_preprocessing( source_filepath: filepath, test: test )
    end

  # In fallback mode, #define macros are stripped from contents (all '#' lines skipped).
  # Re-extract them so the CExtractor can see them when parsing the reconstituted file.
  extras = fallback ? @file_assembler.collect_macros_and_pragmas_fallback( source_filepath: filepath, defines: defines ) : []

  arg_hash = {
    filename:              File.basename( filepath ),
    preprocessed_filepath: preprocessed_filepath,
    contents:              contents,
    extras:                extras,
    includes:              includes
  }

  # Create a reconstituted header file
  @file_assembler.assemble_preprocessed_header_file( **arg_hash )

  return preprocessed_filepath, includes
end

#preprocess_partial_source_expand_macros(filepath:, test:, flags:, include_paths:, vendor_paths:, defines:) ⇒ Object



484
485
486
487
488
489
490
491
492
493
# File 'lib/ceedling/preprocess/preprocessinator.rb', line 484

def preprocess_partial_source_expand_macros(filepath:, test:, flags:, include_paths:, vendor_paths:, defines:)
  _preprocess_partial_expand_macros(
    filepath:      filepath,
    test:          test,
    flags:         flags,
    include_paths: include_paths,
    vendor_paths:  vendor_paths,
    defines:       defines
  )
end

#preprocess_partial_source_file_preserve_macros(test:, filepath:, directives_only_filepath:, fallback:, flags:, include_paths:, vendor_paths:, defines:) ⇒ Object



347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
# File 'lib/ceedling/preprocess/preprocessinator.rb', line 347

def preprocess_partial_source_file_preserve_macros(
    test:,
    filepath:,
    directives_only_filepath:,
    fallback:,
    flags:,
    include_paths:,
    vendor_paths:,
    defines:
)
  msg = @reportinator.generate_module_progress(
    operation: 'Preprocessing source file for follow-on Partials handling',
    module_name: test,
    filename: File.basename( filepath )
  )
  @loginator.log( msg )

  preprocessed_filepath = @file_path_utils.form_preprocessed_file_filepath( filepath, test )

  arg_hash = {
    test:                      test,
    filepath:                  filepath,
    directives_only_filepath:  directives_only_filepath,
    fallback:                  fallback,
    flags:                     flags,
    include_paths:             include_paths,
    vendor_paths:              vendor_paths,
    defines:                   defines
  }

  # Extract includes & log progress and info
  includes = preprocess_file_includes_common( **arg_hash )

  header = "Discovered #includes for Partial source from #{filepath}:"
  @loginator.log_list( includes, header, Verbosity::OBNOXIOUS )

  contents =
    if fallback
      @file_assembler.collect_file_contents_fallback( source_filepath: filepath, defines: defines )
    else
      @file_assembler.collect_file_contents_from_directives_only_preprocessing( source_filepath: filepath, test: test )
    end

  # In fallback mode, #define macros are stripped from contents (all '#' lines skipped).
  # Re-extract them so the CExtractor can see them when parsing the reconstituted file.
  extras = fallback ? @file_assembler.collect_macros_and_pragmas_fallback( source_filepath: filepath, defines: defines ) : []

  arg_hash = {
    filename:              File.basename( filepath ),
    preprocessed_filepath: preprocessed_filepath,
    contents:              contents,
    extras:                extras,
    includes:              includes
  }

  # Create a reconstituted source file
  @file_assembler.assemble_preprocessed_code_file( **arg_hash )

  return preprocessed_filepath, includes
end

#preprocess_system_includes(name:, filepath:, directives_only_filepath:, fallback: false, defines: []) ⇒ Object

Extract system includes from a file using directives-only output (or text-only fallback). Called externally and internally by preprocess_common.



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/ceedling/preprocess/preprocessinator.rb', line 123

def preprocess_system_includes(name:, filepath:, directives_only_filepath:, fallback: false, defines: [])
  includes = []

  if !fallback
    includes = @includes_handler.extract_system_includes_preprocess(
      name:                   name,
      filepath:               filepath,
      preprocessed_filepath:  directives_only_filepath
    )
  else
    includes = @includes_handler.extract_system_includes_from_text(
      name:     name,
      filepath: filepath,
      defines:  defines
    )
  end

  header = "Extracted system #includes from #{filepath}:"
  @loginator.log_list( includes, header, Verbosity::DEBUG )

  return includes
end

#preprocess_test_file(test:, filepath:, directives_only_filepath:, fallback:, includes:, flags:, include_paths:, vendor_paths:, defines:) ⇒ Object



408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
# File 'lib/ceedling/preprocess/preprocessinator.rb', line 408

def preprocess_test_file(
    test:,
    filepath:,
    directives_only_filepath:,
    fallback:,
    includes:,
    flags:,
    include_paths:,
    vendor_paths:,
    defines:
  )
  msg = @reportinator.generate_module_progress(
    operation: 'Preprocessing test file',
    module_name: test,
    filename: File.basename( filepath )
  )
  @loginator.log( msg )

  preprocessed_filepath = @file_path_utils.form_preprocessed_file_filepath( filepath, test )

  plugin_arg_hash = {
    test_file:              filepath,
    preprocessed_test_file: preprocessed_filepath,
    test:                   test,
    flags:                  flags,
    include_paths:          include_paths,
    defines:                defines      
  }

  # Trigger pre_test_preprocess plugin hook
  @plugin_manager.pre_test_preprocess( plugin_arg_hash )

  # NOTE: No call to `preprocess_file_includes_common()` because we already have includes

  arg_hash = {
    test:                      test,
    filepath:                  filepath,
    directives_only_filepath:  directives_only_filepath,
    fallback:                  fallback,
    flags:                     flags,
    include_paths:             include_paths,
    defines:                   defines      
  }

  # `contents` & `extras` are arrays of text strings to be assembled in generating a new test file.
  # `extras` are test build directives TEST_SOURCE_FILE() and TEST_INCLUDE_PATH().
  contents, extras = @file_assembler.collect_test_file_contents( **arg_hash )

  arg_hash = {
    filename:              File.basename( filepath ),
    preprocessed_filepath: preprocessed_filepath,
    contents:              contents,
    extras:                extras,
    includes:              includes                       
  }

  # Create a reconstituted test file from preprocessing expansion and preserving any extras
  @file_assembler.assemble_preprocessed_code_file( **arg_hash )

  # Trigger post_test_preprocess plugin hook
  @plugin_manager.post_test_preprocess( plugin_arg_hash )

  return preprocessed_filepath
end

#preprocess_user_includes(name:, filepath:, directives_only_filepath:, fallback: false, defines: []) ⇒ Object

Extract user includes from a file using directives-only output (or text-only fallback). Called externally and internally by preprocess_common.



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/ceedling/preprocess/preprocessinator.rb', line 98

def preprocess_user_includes(name:, filepath:, directives_only_filepath:, fallback: false, defines: [])
  includes = []

  if !fallback
    includes = @includes_handler.extract_user_includes_preprocess(
      name:                   name,
      filepath:               filepath,
      preprocessed_filepath:  directives_only_filepath
    )
  else
    includes = @includes_handler.extract_user_includes_from_text(
      name:     name,
      filepath: filepath,
      defines:  defines
    )
  end

  header = "Extracted user #includes from #{filepath}:"
  @loginator.log_list( includes, header, Verbosity::DEBUG )

  return includes
end

#setupObject



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/ceedling/preprocess/preprocessinator.rb', line 27

def setup
  # Aliases
  @includes_handler = @preprocessinator_includes_handler
  @file_assembler = @preprocessinator_file_assembler
  @comment_stripper = @preprocessinator_comment_stripper
  @reconstructor = @preprocessinator_reconstructor

  # Thread-safe per-file locking for YAML cache operations
  # Key: includes list filepath (String), Value: Mutex
  @file_locks = {}
  @file_locks_mutex = Mutex.new
end

#store_includes_list(test:, filepath:, includes:) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/ceedling/preprocess/preprocessinator.rb', line 146

def store_includes_list(test:, filepath:, includes:)
  _filepath = @file_path_utils.form_preprocessed_includes_list_filepath( filepath, test )

  # Get or create a mutex for this specific cache file
  file_lock = @file_locks_mutex.synchronize do
    @file_locks[_filepath] ||= Mutex.new
  end

  file_lock.synchronize do
    @includes_handler.write_includes_list( _filepath, includes )
  end
end