Class: PreprocessinatorExtractor

Inherits:
Object
  • Object
show all
Defined in:
lib/ceedling/preprocessinator_extractor.rb

Instance Method Summary collapse

Instance Method Details

#extract_base_file_from_preprocessed_directives(filepath) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/ceedling/preprocessinator_extractor.rb', line 31

def extract_base_file_from_preprocessed_directives(filepath)
  # preprocessing by way of toolchain preprocessor eliminates directives only
  # like #ifdef's and leave other code

  # iterate through all lines and only get last chunk of file after a last
  # '#'line containing file name of our filepath

  base_name  = File.basename(filepath)
  pattern    = /^#.*(\s|\/|\\|\")#{Regexp.escape(base_name)}/
  found_file = false # have we found the file we care about?

  lines = []
  File.readlines(filepath).each do |line|
    lines << line

    if line =~ pattern
      lines = []
    end
  end

  return lines
end

#extract_base_file_from_preprocessed_expansion(filepath) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/ceedling/preprocessinator_extractor.rb', line 2

def extract_base_file_from_preprocessed_expansion(filepath)
  # preprocessing by way of toolchain preprocessor expands macros, eliminates
  # comments, strips out #ifdef code, etc.  however, it also expands in place
  # each #include'd file.  so, we must extract only the lines of the file
  # that belong to the file originally preprocessed

  # iterate through all lines and alternate between extract and ignore modes
  # all lines between a '#'line containing file name of our filepath and the
  # next '#'line should be extracted

  base_name  = File.basename(filepath)
  not_pragma = /^#(?!pragma\b)/ # preprocessor directive that's not a #pragma
  pattern    = /^#.*(\s|\/|\\|\")#{Regexp.escape(base_name)}/
  found_file = false # have we found the file we care about?

  lines = []
  File.readlines(filepath).each do |line|
    if found_file and not line =~ not_pragma
      lines << line
    else
      found_file = false
    end

    found_file = true if line =~ pattern
  end

  return lines
end