commit b66a3d7d212b4e0612709887f92a2568aa53e130
parent 5dbea4132c4faacb1c5a1056b67a75ce698c36bf
Author: Ryan Sepassi <rsepassi@gmail.com>
Date: Fri, 1 May 2026 17:19:54 -0700
cc/pp: stringize escapes control bytes in char/string literals
%pp-quote-bytes was emitting raw \n, \t, \r bytes inside the
synthesized literal text. Per C11 6.10.3.2 #x must produce a
*spelling* of the operand (a valid string-literal); embedding a raw
newline yielded an unterminated/invalid literal. Map 0x0a/0x09/0x0d
back to their two-character escape sequences.
Test: tests/cc-pp/50-stringize-char-special.c stringizes '\n' and
expects "'\\n'"; was producing a literal newline.
Diffstat:
3 files changed, 7 insertions(+), 0 deletions(-)
diff --git a/cc/cc.scm b/cc/cc.scm
@@ -2168,6 +2168,9 @@
(cond
((or (= b delim) (= b 92))
(loop (+ i 1) (cons (bv-of-byte b) (cons "\\" acc))))
+ ((= b 10) (loop (+ i 1) (cons "\\n" acc)))
+ ((= b 9) (loop (+ i 1) (cons "\\t" acc)))
+ ((= b 13) (loop (+ i 1) (cons "\\r" acc)))
(else
(loop (+ i 1) (cons (bv-of-byte b) acc))))))))))
diff --git a/tests/cc-pp/50-stringize-char-special.c b/tests/cc-pp/50-stringize-char-special.c
@@ -0,0 +1,2 @@
+#define S(x) #x
+S('\n')
diff --git a/tests/cc-pp/50-stringize-char-special.expected-toks b/tests/cc-pp/50-stringize-char-special.expected-toks
@@ -0,0 +1,2 @@
+(STR "'\\n'" "50-stringize-char-special.c" 1 14)
+(EOF #f "50-stringize-char-special.c" 3 1)