preprocessinator_extractor.rb 1.1 KB

123456789101112131415161718192021222324252627282930
  1. class PreprocessinatorExtractor
  2. def extract_base_file_from_preprocessed_expansion(filepath)
  3. # preprocessing by way of toolchain preprocessor expands macros, eliminates
  4. # comments, strips out #ifdef code, etc. however, it also expands in place
  5. # each #include'd file. so, we must extract only the lines of the file
  6. # that belong to the file originally preprocessed
  7. # iterate through all lines and alternate between extract and ignore modes
  8. # all lines between a '#'line containing file name of our filepath and the
  9. # next '#'line should be extracted
  10. base_name = File.basename(filepath)
  11. not_pragma = /^#(?!pragma\b)/ # preprocessor directive that's not a #pragma
  12. pattern = /^#.*(\s|\/|\\|\")#{Regexp.escape(base_name)}/
  13. found_file = false # have we found the file we care about?
  14. lines = []
  15. File.readlines(filepath).each do |line|
  16. if found_file and not line.match(not_pragma)
  17. lines << line
  18. else
  19. found_file = false
  20. end
  21. found_file = true if line.match(pattern)
  22. end
  23. return lines
  24. end
  25. end