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.
This commit is contained in:
2026-10-16 18:59:49 +03:00
parent 974657bc6b
commit 3ce76c81f5
2 changed files with 14 additions and 5 deletions

View File

@@ -45,8 +45,9 @@
(define (line-directive src) (define (line-directive src)
;; `%line' is fmt-c's #line directive. cpp-line concatenates its ;; `%line' is fmt-c's #line directive. cpp-line concatenates its
;; second argument verbatim, so the file name arrives already quoted. ;; second argument verbatim, so the file name arrives as the string
`(%line ,(cadr src) ,(fmt #f #\" (car src) #\"))) ;; 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) (define (walk-body stmts)
"Walk a statement list, re-anchoring each statement that has a known "Walk a statement list, re-anchoring each statement that has a known

View File

@@ -97,7 +97,7 @@
;;; ------------------------------------------------------------------ ;;; ------------------------------------------------------------------
;;; Pipeline and #line accounting ;;; Pipeline and #line accounting
(define (compile-to-c source) (define (compile-to-c source #!optional (file "fixture.sex"))
"Run reader -> semen -> writer on SOURCE, returning the generated C. "Run reader -> semen -> writer on SOURCE, returning the generated C.
parse-all is called directly rather than through read-raw-forms so the parse-all is called directly rather than through read-raw-forms so the
fixture can be given a file name: a location carries one, and the fixture can be given a file name: a location carries one, and the
@@ -105,7 +105,7 @@ file is what makes an imported module report itself rather than the unit
that imported it." that imported it."
(let ((forms (with-input-from-string source (let ((forms (with-input-from-string source
(lambda () (lambda ()
(parameterize ((current-source-file "fixture.sex")) (parameterize ((current-source-file file))
(parse-all (current-input-port))))))) (parse-all (current-input-port)))))))
(with-output-to-string (with-output-to-string
(lambda () (emit-c (semen-process forms)))))) (lambda () (emit-c (semen-process forms))))))
@@ -249,4 +249,12 @@ after that is one more. Lines before the first directive get #f."
(test "every directive names the fixture" (test "every directive names the fixture"
(list "\"fixture.sex\"") (list "\"fixture.sex\"")
(delete-duplicates (delete-duplicates
(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 "and is written the way C writes a string"
"\"we\\\"ird\\\\dir/a?\\?=.sex\""
(directive-file
(find directive-line
(split-lines (compile-to-c "(fn f () int (return 1))"
"we\"ird\\dir/a??=.sex")))))))