shithub: scc

Download patch

ref: 607e768887f2b705e4d855754ffd41009b16b078
parent: db565b93a3e8db0e72be663d3cb6560ab96fc64f
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Sun Oct 31 13:46:25 EDT 2021

cc1: Improve escape handling in string()

The code in string() was a bit convoluted due to the semantic
of disescape. This patch improves the code reducing the number
of conditions and adding a comment explaining the logic
between esc and disescape.

--- a/src/cmd/cc/cc1/lex.c
+++ b/src/cmd/cc/cc1/lex.c
@@ -539,6 +539,14 @@
 	return CONSTANT;
 }
 
+/*
+ * string() parses a constant string, and convert all the
+ * escape sequences into single characters. This behaviour
+ * is correct except when we parse a #define, where we want
+ * to preserve the literal content of the string. In that
+ * case cpp.c:/^define( sets the variable disescape to
+ * disable converting escape sequences into characters.
+ */
 static int
 string(void)
 {
@@ -557,14 +565,15 @@
 			errorp("missing terminating '\"' character");
 			break;
 		}
-		if (c != '\\') {
+
+		if (c == '\\' && !esc && disescape)
+			esc = 1;
+		else
 			esc = 0;
-		} else {
-			if (!disescape)
-				c = escape();
-			else
-				esc = 1;
-		}
+
+		if (c == '\\' && !esc)
+			c = escape();
+
 		if (bp == &yytext[STRINGSIZ+1]) {
 			for (++input->p; *input->p != '"'; ++input->p) {
 				if (*input->p == '\\')