Commit da8b3bca
da8b3bca7078Merged
Ori Pekelman <[email protected]> · 5 months ago · 5 files · ~5 modified
Description
Multi-file source map for --debug (#line across require_relative)
Extends --debug so a native debugger steps through the correct original file, not just the toplevel script. Previously require/require_relative were textually spliced into one buffer, so every node's line was a position in that concatenation and all #line directives named the toplevel file.
Approach (debug-gated; non-debug output is byte-for-byte unchanged):
- spinel_parse.c: when SPINEL_DEBUG=1, each spliced file's content is wrapped in PUSH/POP marker comments (Prism ignores comments, so the AST is unchanged). A single stack-based pass over the final buffer (sp_build_line_map) reconstructs, per buffer line, the original file id and line — needing no original-line bookkeeping at splice time: a PUSH consumes the parent's replaced require line, content lines count in the child's own coordinates until POP. Per node it now emits node_file alongside node_line, and a FILE table. The map is built before syntax-sugar rewriting and disabled (with a warning) if that ever changes the line count.
- compiler_helpers.rb / node_table_loader.rb: load the FILE table and a dedicated @nd_file slot.
- spinel_codegen.rb: emit
#line N "<original file>", resolving each node's file via the table; dedup on (line, file) so a file boundary re-emits.
Verified with lldb: breaking in a require_relative'd helper resolves to helper.rb:N, locals print, and the backtrace spans both files (sp_double_it at helper.rb:2 <- main at main.rb:3). Non-debug multi-file compiles unchanged (no markers/FILE/node_file in the AST) and a sample of the test suite passes. Single-file --debug unaffected.
Limitations: plain require of lib shims is marker-wrapped for correct line
accounting but attributed to its lib path; the value-bisect harness is still
single-file. On the ori fork; no PR against matz/spinel.
Co-Authored-By: Claude Opus 4.8 [email protected]
Details
- Commit
- da8b3bca7078
- Parent
- 392f33b212f9
compiler_helpers.rbmodifiedView file
@nd_type.push("") @nd_name.push("") @nd_value.push(0) @nd_line.push(0) # keep the source-map arrays parallel with the rest, so a @nd_file.push(0) # dynamically-allocated node can't drift them out of sync @nd_content.push("") @nd_flags.push(0) @nd_operator.push("") @nd_name = Array.new(n, "") @nd_value = Array.new(n, 0) @nd_line = Array.new(n, 0) @nd_file = Array.new(n, 0) @nd_content = Array.new(n, "") @nd_flags = Array.new(n, 0) @nd_operator = Array.new(n, "") if field == "node_line" @nd_line[nid] = val end # Debug multi-file map: id into the FILE table (0 = toplevel source). if field == "node_file" @nd_file[nid] = val end end # Debug multi-file map: record a FILE <id> <path> table entry emitted by # the parser. Grown on demand so ids can arrive in any order. def set_file_entry(id, path) @file_table ||= [] # self-contained: the analyze/codegen consumers init it, # but don't rely on that from this shared helper while @file_table.length <= id @file_table.push("") end @file_table[id] = path end def set_ref_field(nid, field, ref_id)node_table_loader.rbmodifiedView file
Loading diff…
spinel_analyze.rbmodifiedView file
Loading diff…
spinel_codegen.rbmodifiedView file
Loading diff…
spinel_parse.cmodifiedView file
Loading diff…