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

@@ -466,6 +466,8 @@
466466 @nd_type.push("")
467467 @nd_name.push("")
468468 @nd_value.push(0)
469+ @nd_line.push(0) # keep the source-map arrays parallel with the rest, so a
470+ @nd_file.push(0) # dynamically-allocated node can't drift them out of sync
469471 @nd_content.push("")
470472 @nd_flags.push(0)
471473 @nd_operator.push("")
@@ -529,6 +531,7 @@
529531 @nd_name = Array.new(n, "")
530532 @nd_value = Array.new(n, 0)
531533 @nd_line = Array.new(n, 0)
534+ @nd_file = Array.new(n, 0)
532535 @nd_content = Array.new(n, "")
533536 @nd_flags = Array.new(n, 0)
534537 @nd_operator = Array.new(n, "")
@@ -673,6 +676,21 @@
673676 if field == "node_line"
674677 @nd_line[nid] = val
675678 end
679+ # Debug multi-file map: id into the FILE table (0 = toplevel source).
680+ if field == "node_file"
681+ @nd_file[nid] = val
682+ end
683+ end
684+
685+ # Debug multi-file map: record a FILE <id> <path> table entry emitted by
686+ # the parser. Grown on demand so ids can arrive in any order.
687+ def set_file_entry(id, path)
688+ @file_table ||= [] # self-contained: the analyze/codegen consumers init it,
689+ # but don't rely on that from this shared helper
690+ while @file_table.length <= id
691+ @file_table.push("")
692+ end
693+ @file_table[id] = path
676694 end
677695
678696 def set_ref_field(nid, field, ref_id)