Class: PreprocessinatorLineMarkerIncludesExtractor
- Defined in:
- lib/ceedling/preprocess/preprocessinator_line_marker_includes_extractor.rb
Overview
Parse GCC preprocessor output (from -fdirectives-only) to extract system include directives
Constant Summary collapse
- LINE_MARKER_REGEX =
Leading whitespace is tolerated (
^\s*, not just^): GCC's -fdirectives-only output replaces a top-level #include with a line marker that inherits that #include's own original indentation (GH #1268) -- an indented#include "x.h"produces an indented# 1 "x.h" 1, not the flush-left marker every OTHER marker GCC generates uses. Without this, such an include is silently missing from the extracted list entirely. /^\s*#\s+(\d+)\s+"([^"]+)"(?:\s+(\d+(?:\s+\d+)*))?$/- SYSTEM =
:system- USER =
:user
Instance Method Summary collapse
-
#extract_includes_from_file(filepath, type, max_depth = nil) ⇒ Array<UserInclude, SystemInclude>
Parse preprocessor output from a file (production use).
-
#extract_includes_from_string(content, filepath, type, max_depth = nil) ⇒ Array<UserInclude, SystemInclude>
Parse preprocessor output from a string (testing use).
Instance Method Details
#extract_includes_from_file(filepath, type, max_depth = nil) ⇒ Array<UserInclude, SystemInclude>
Parse preprocessor output from a file (production use)
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/ceedling/preprocess/preprocessinator_line_marker_includes_extractor.rb', line 100 def extract_includes_from_file(filepath, type, max_depth=nil) validate_type_argument( type ) includes = [] begin # Open in binary mode: GCC output under a non-C locale contains non-ASCII bytes # (e.g. <組み込み> for <built-in> under ja_JP). Text mode would interpret those # bytes using the locale-dependent external encoding (e.g. Windows-31J on ja_JP # Windows), raising an encoding error on read. Binary mode bypasses that translation. # LINE_MARKER_REGEX uses only ASCII delimiters and is safe in binary mode. # The filepath.start_with?('<') guard correctly skips localized markers because # their first byte is 0x3C — ASCII '<' — regardless of the surrounding encoding. # NOTE: binary mode means \r\n line endings are NOT translated on Windows; the # extract_includes method calls line.chomp! before regex matching to handle this. File.open(filepath, 'rb') do |file| includes = extract_includes(io: file, filepath: filepath, type: type, max_depth: max_depth) end rescue StandardError => e raise CeedlingException.new("Failed to extract #{type} includes from preprocessor output file '#{filepath}' ⏩️ #{e.}") end return includes end |
#extract_includes_from_string(content, filepath, type, max_depth = nil) ⇒ Array<UserInclude, SystemInclude>
Parse preprocessor output from a string (testing use)
125 126 127 128 129 130 |
# File 'lib/ceedling/preprocess/preprocessinator_line_marker_includes_extractor.rb', line 125 def extract_includes_from_string(content, filepath, type, max_depth=nil) validate_type_argument( type ) require 'stringio' io = StringIO.new(content) return extract_includes(io: io, filepath: filepath, type: type, max_depth: max_depth) end |