Commit 164ce350

164ce35025dbMerged

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

Description

Map C compile errors back to Ruby source lines via #line, on by default

When the generated C was ill-typed, the C compiler's error referenced the generated out.c (e.g. out.c:72: invalid operands to binary <<) with no path back to the user's Ruby. Emit #line N "src.rb" directives in normal optimized builds so the toolchain attributes any compile error to the Ruby source line instead:

before: out.c:72:19: error: invalid operands to binary << (have 'sp_Set' ...) after: app.rb:6:19: error: invalid operands to binary << (have 'sp_Set' ...)

This reuses the same #line machinery --debug already emits, decoupled from --debug's -O0 / non-inlined / -g cost: the directives are a front-end-only diagnostic, so the optimizer never sees them and the binary is identical modulo line attribution. On by default (no runtime cost, only larger generated C); opt out with --no-line-map.

Coverage: #line reaches the statement stream, so it maps expression-level errors. Errors emitted in the hoisted struct/typedef region (unknown stdlib type, builtin struct redefinition) still report against the generated C and want emit-time guards instead. The line is exact; the column is the C column relabeled onto the Ruby line (approximate in Ruby terms).

Validated: make check (844 pass), optcarrot (checksum 59662), make bench (57 pass); generated C is byte-identical modulo #line lines.

Refs #1338

Co-Authored-By: Claude Opus 4.8 [email protected]

Details

Commit
164ce35025db
Parent
1f054fd8169c

spinelmodifiedView file

@@ -50,6 +50,11 @@
5050 EMIT_TYPES=0
5151 STDOUT_MODE=0
5252 RUN_MODE=0
53+# #line directives are on by default: they cost nothing at runtime (purely a
54+# diagnostic directive; the binary is identical modulo line attribution) and
55+# let the C toolchain report any compile error on the generated C against the
56+# Ruby source line. Opt out with --no-line-map.
57+LINE_MAP=1
5358 OPT_LEVEL="2"
5459 CC_CMD="cc"
5560 EXTRA_FLAGS=""
@@ -111,6 +116,14 @@
111116 # the default -O2 gives lossy DWARF; use --debug for
112117 # faithful stepping).
113118 DEBUG=1; shift ;;
119+ --line-map) # Emit `#line` directives so a compile error on the
120+ # generated C is reported against the Ruby source line,
121+ # without --debug's -O0 / non-inlined / -g cost. On by
122+ # default; this flag is kept for explicitness.
123+ LINE_MAP=1; shift ;;
124+ --no-line-map) # Suppress #line directives (smaller generated C; compile
125+ # errors report against out.c instead of the Ruby source).
126+ LINE_MAP=0; shift ;;
114127 -e) shift
115128 if [ "$EVAL_USED" -eq 0 ]; then
116129 EVAL_SCRIPT="$1"
@@ -234,6 +247,8 @@
234247 echo " --debug Debug build: step through the .rb in gdb/lldb" >&2
235248 echo " (emits #line, keeps methods non-inlined, -g -O0)" >&2
236249 echo " -g Add debug info + #line without forcing -O0" >&2
250+ echo " --no-line-map Suppress #line directives (smaller C; compile errors" >&2
251+ echo " report against generated C, not the Ruby source)" >&2
237252 echo " --cc=CMD C compiler (default: cc)" >&2
238253 echo " -e STR Inline Ruby source (repeatable; joined with newlines)" >&2
239254 echo " --rbs DIR Seed analyzer with RBS signatures from DIR (advisory)" >&2
@@ -261,6 +276,12 @@
261276 # Debug mode signal, read by the parser (per-node line stamps) and the
262277 # codegen (#line emission + non-inlined methods). Must be exported
263278 # before the parse step below since spinel_parse reads it at init.
279+# --line-map: stamp #line directives (parser + codegen) for Ruby-mapped
280+# compile errors, but keep the normal optimized build (no -O0/-g/-rdynamic).
281+# --debug implies it; SPINEL_DEBUG already turns on the same #line path.
282+if [ "$LINE_MAP" -eq 1 ] && [ "$DEBUG" -eq 0 ]; then
283+ export SPINEL_LINE_MAP=1
284+fi
264285 if [ "$DEBUG" -eq 1 ]; then
265286 export SPINEL_DEBUG=1
266287 EXTRA_FLAGS="$EXTRA_FLAGS -g"