preprocessinator_includes_handler.rb 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. class PreprocessinatorIncludesHandler
  2. constructor :configurator, :tool_executor, :task_invoker, :file_path_utils, :yaml_wrapper, :file_wrapper
  3. # shallow includes: only those headers a source file explicitly includes
  4. def invoke_shallow_includes_list(filepath)
  5. @task_invoker.invoke_test_shallow_include_lists( [@file_path_utils.form_preprocessed_includes_list_filepath(filepath)] )
  6. end
  7. # ask the preprocessor for a make-style dependency rule of only the headers the source file immediately includes
  8. def form_shallow_dependencies_rule(filepath)
  9. # change filename (prefix of '_') to prevent preprocessor from finding include files in temp directory containing file it's scanning
  10. temp_filepath = @file_path_utils.form_temp_path(filepath, '_')
  11. # read the file and replace all include statements with a decorated version
  12. # (decorating the names creates file names that don't exist, thus preventing the preprocessor
  13. # from snaking out and discovering the entire include path that winds through the code)
  14. contents = @file_wrapper.read(filepath)
  15. contents.gsub!( /#include\s+\"\s*(\S+)\s*\"/, "#include \"\\1\"\n#include \"@@@@\\1\"" )
  16. @file_wrapper.write( temp_filepath, contents )
  17. # extract the make-style dependency rule telling the preprocessor to
  18. # ignore the fact that it can't find the included files
  19. command = @tool_executor.build_command_line(@configurator.tools_test_includes_preprocessor, temp_filepath)
  20. shell_result = @tool_executor.exec(command[:line], command[:options])
  21. return shell_result[:output]
  22. end
  23. # headers only; ignore any crazy .c includes
  24. def extract_shallow_includes(make_rule)
  25. list = []
  26. header_extension = @configurator.extension_header
  27. headers = make_rule.scan(/(\S+#{'\\'+header_extension})/).flatten # escape slashes before dot file extension
  28. headers.uniq!
  29. headers.map! { |header| header.sub(/(@@@@)|(.+\/)/, '') }
  30. headers.sort!
  31. headers.each_with_index do |header, index|
  32. break if (headers.size == (index-1))
  33. list << header if (header == headers[index + 1])
  34. end
  35. return list
  36. end
  37. def write_shallow_includes_list(filepath, list)
  38. @yaml_wrapper.dump(filepath, list)
  39. end
  40. end