From c8a39b7e62788673b498b773cae07c41b8e297df Mon Sep 17 00:00:00 2001 From: alex-eg Date: Fri, 16 Oct 2026 18:59:49 +0300 Subject: [PATCH] write a #line file name as a C string literal It was pasted between quotes as it came, so a quote in the path ended it, a backslash began an escape, and ?? read as a trigraph. --- fmt-c-writer.scm | 5 +++-- tests/line-directives.scm | 16 ++++++++++++---- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/fmt-c-writer.scm b/fmt-c-writer.scm index 2b84f11..44ea4da 100644 --- a/fmt-c-writer.scm +++ b/fmt-c-writer.scm @@ -41,8 +41,9 @@ (define (line-directive src) ;; `%line' is fmt-c's #line directive. cpp-line concatenates its - ;; second argument verbatim, so the file name arrives already quoted. - `(%line ,(cadr src) ,(fmt #f #\" (car src) #\"))) + ;; second argument verbatim, so the file name arrives as the string + ;; literal C reads it as: a quote, a backslash or a ?? in it escaped. + `(%line ,(cadr src) ,(fmt #f (c-in-expr (c-literal (car src)))))) (define (walk-body stmts) "Walk a statement list, re-anchoring each statement that has a known diff --git a/tests/line-directives.scm b/tests/line-directives.scm index b27286e..877b56b 100644 --- a/tests/line-directives.scm +++ b/tests/line-directives.scm @@ -96,10 +96,10 @@ ;;; ------------------------------------------------------------------ ;;; Pipeline and #line accounting -(define (compile-to-c source) - "Run reader -> semen -> writer on SOURCE, read from fixture.sex, +(define (compile-to-c source #!optional (file "fixture.sex")) + "Run reader -> semen -> writer on SOURCE, read from FILE, returning the generated C." - (let ((forms (read-source source "fixture.sex"))) + (let ((forms (read-source source file))) (with-output-to-string (lambda () (emit-c (semen-process forms)))))) @@ -244,4 +244,12 @@ after that is one more. Lines before the first directive get #f." (delete-duplicates (map (lambda (quoted) (pathname-strip-directory (string-trim-both quoted #\"))) - (filter values (map directive-file (split-lines c-out)))))))) + (filter values (map directive-file (split-lines c-out)))))) + ;; it was pasted between quotes as it came: a quote ended it, a + ;; backslash began an escape, and ??= was a trigraph + (test-assert "and is written the way C writes a string" + (string-suffix? "/we\\\"ird\\\\a?\\?=.sex\"" + (directive-file + (find directive-line + (split-lines (compile-to-c "(fn f () int (return 1))" + "we\"ird\\a??=.sex"))))))))