scc

simple c99 compiler
git clone git://git.simple-cc.org/scc
Log | Files | Refs | Submodules | README | LICENSE

commit 607e768887f2b705e4d855754ffd41009b16b078
parent db565b93a3e8db0e72be663d3cb6560ab96fc64f
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Sun, 31 Oct 2021 17:46:25 +0100

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.

Diffstat:
Msrc/cmd/cc/cc1/lex.c | 23++++++++++++++++-------
1 file changed, 16 insertions(+), 7 deletions(-)

diff --git a/src/cmd/cc/cc1/lex.c b/src/cmd/cc/cc1/lex.c @@ -539,6 +539,14 @@ character(void) 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 @@ string(void) 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 == '\\')