Commit f14172a2

f14172a22bcfMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 6 files · ~6 modified

Description

spinelc: emit #line directives so cc errors map to the Ruby source

The C spinel driver lost the legacy backend's source-mapping: a cc error on the generated C reported a line in out.c, not the original .rb. The parser already stamps per-node node_line / node_file (gated on SPINEL_DEBUG / SPINEL_LINE_MAP) and emits a FILE <id> <path> table for multi-file (require-spliced) programs, but spinelc's NodeTable loader dropped the FILE table and codegen never emitted #line.

Parse the FILE table into NodeTable (with an nt_file_path accessor) and have codegen emit a deduped #line N "path" at each statement boundary when SPINEL_LINE_MAP / SPINEL_DEBUG is set. Off by default, so spinelc's direct output (make test/bench/optcarrot/bootstrap) is byte-unchanged.

The spinel driver now defaults line-map on (parity with spinel-legacy), adds --line-map / --no-line-map, and routes -g / --debug through SPINEL_DEBUG (+ -rdynamic on ELF for Exception#backtrace), restoring gdb/lldb stepping through the .rb.

make check: 846 pass, 0 fail.

Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]

Details

Commit
f14172a22bcf
Parent
a6538996d3e2

spinelmodifiedView file

@@ -23,8 +23,12 @@
2323 # first .rb file ends spinel-flag parsing, and the rest
2424 # of the command line becomes the binary's ARGV.
2525 # -O LEVEL Optimization level for cc (default: 2)
26-# -g Add debug info (-g) to the cc step
27-# --debug Debug build: -g -O0
26+# -g Add debug info (-g) + #line, leaving -O as-is
27+# --debug Debug build: #line directives + -g -O0, so gdb/lldb step
28+# through the original .rb source
29+# --line-map Emit #line directives (on by default)
30+# --no-line-map Suppress #line directives (compile errors map to the
31+# generated C, not the Ruby source)
2832 # --cc=CMD C compiler command (default: cc)
2933 # -e STR Inline Ruby source (repeatable; multiple -e join with \n)
3034 # --int-overflow=MODE Int +/-/* overflow handling (default: raise)
@@ -57,6 +61,12 @@
5761 EVAL_USED=0
5862 EVAL_TMP=""
5963 INT_OVERFLOW="raise"
64+DEBUG=0
65+# #line directives are on by default: they cost nothing at runtime (the binary
66+# is identical modulo line attribution) and let the C toolchain report a
67+# compile error on the generated C against the Ruby source line. Opt out with
68+# --no-line-map.
69+LINE_MAP=1
6070
6171 # Parse spinel-side flags. In `-E` (run) mode, the first positional `.rb`
6272 # arg marks the boundary between spinel flags and the script's ARGV:
@@ -85,8 +95,16 @@
8595 ;;
8696 -o) shift; OUTPUT="$1"; shift ;;
8797 -O) shift; OPT_LEVEL="$1"; shift ;;
88- --debug) OPT_LEVEL="0"; EXTRA_FLAGS="$EXTRA_FLAGS -g"; shift ;;
89- -g) EXTRA_FLAGS="$EXTRA_FLAGS -g"; shift ;;
98+ --debug) # Debug build: native-debugger stepping through the .rb. Emits
99+ # #line directives, forces -O0, and adds -g.
100+ DEBUG=1; OPT_LEVEL="0"; EXTRA_FLAGS="$EXTRA_FLAGS -g"; shift ;;
101+ -g) # Debug info + #line, but leave -O as-is.
102+ DEBUG=1; EXTRA_FLAGS="$EXTRA_FLAGS -g"; shift ;;
103+ --line-map) # Emit #line directives (on by default; kept for explicitness).
104+ LINE_MAP=1; shift ;;
105+ --no-line-map) # Suppress #line directives (compile errors report against
106+ # the generated C instead of the Ruby source).
107+ LINE_MAP=0; shift ;;
90108 -e) shift
91109 if [ "$EVAL_USED" -eq 0 ]; then
92110 EVAL_SCRIPT="$1"
@@ -160,8 +178,10 @@
160178 echo " -S Print C to stdout" >&2
161179 echo " -E Run the compiled binary; leftover args become its ARGV" >&2
162180 echo " -O LEVEL Optimization level (default: 2)" >&2
163- echo " -g Add debug info (-g)" >&2
164- echo " --debug Debug build (-g -O0)" >&2
181+ echo " -g Add debug info (-g) + #line, leaving -O as-is" >&2
182+ echo " --debug Debug build: step through the .rb in gdb/lldb (#line, -g -O0)" >&2
183+ echo " --no-line-map Suppress #line directives (compile errors report" >&2
184+ echo " against generated C, not the Ruby source)" >&2
165185 echo " --cc=CMD C compiler (default: cc)" >&2
166186 echo " -e STR Inline Ruby source (repeatable; joined with newlines)" >&2
167187 echo " --int-overflow=MODE Int +/-/* overflow handling (default: raise)" >&2
@@ -185,6 +205,24 @@
185205 fi
186206 trap 'rm -f "$EVAL_TMP"; [ -n "$C_TMP_DIR" ] && rm -rf "$C_TMP_DIR"; [ -n "$RUN_TMP_DIR" ] && rm -rf "$RUN_TMP_DIR"' EXIT
187207
208+# Source mapping: spinelc's parser stamps per-node source lines and codegen
209+# emits `#line N "file"` when SPINEL_DEBUG / SPINEL_LINE_MAP is set, so a C
210+# compile error is reported against the original Ruby line. --debug / -g imply
211+# the fuller SPINEL_DEBUG path (plus -g, set above); plain --line-map keeps the
212+# optimized build and just adds #line.
213+if [ "$DEBUG" -eq 1 ]; then
214+ export SPINEL_DEBUG=1
215+ # Native Exception#backtrace on Linux: glibc's backtrace_symbols only names
216+ # frames whose symbols are in the dynamic symbol table; -rdynamic exports
217+ # them. ELF-only -- skip on macOS (Mach-O resolves already) and MinGW/Cygwin.
218+ case "$(uname -s 2>/dev/null)" in
219+ Darwin|MINGW*|MSYS*|CYGWIN*) ;;
220+ *) EXTRA_FLAGS="$EXTRA_FLAGS -rdynamic" ;;
221+ esac
222+elif [ "$LINE_MAP" -eq 1 ]; then
223+ export SPINEL_LINE_MAP=1
224+fi
225+
188226 # Step 1: Compile (parse + analyze + codegen) — spinelc emits C.
189227 if [ "$STDOUT_MODE" -eq 1 ]; then
190228 "$SPINELC" "$SOURCE"