From 068d5b3d203f6c3e1536cef6397d31133d143a57 Mon Sep 17 00:00:00 2001 From: Michael Matz Date: Wed, 3 Jun 2020 18:38:11 +0200 Subject: [PATCH] pp: Move errors about stray backslashes they were emitted too early, in particular also in macro substitution which might turn out to not be stray in case it's further stringified. Check in next() instead. Add two testcases that an error is still emitted for obvious top-level baskslashes, and that stringifying such a thing works. --- tccpp.c | 5 ++--- tests/tcctest.c | 3 +++ tests/tests2/60_errors_and_warnings.c | 3 +++ tests/tests2/60_errors_and_warnings.expect | 3 +++ 4 files changed, 11 insertions(+), 3 deletions(-) diff --git a/tccpp.c b/tccpp.c index c4c1ce53..1c2c8bc9 100644 --- a/tccpp.c +++ b/tccpp.c @@ -3511,8 +3511,6 @@ static void macro_subst( if (tok_str->len) spc = is_space(t = tok_str->str[tok_str->lastlen]); } else { - if (t == '\\' && !(parse_flags & PARSE_FLAG_ACCEPT_STRAYS)) - tcc_error("stray '\\' in program"); no_subst: if (!check_space(t, &spc)) tok_str_add2(tok_str, t, &cval); @@ -3569,7 +3567,8 @@ ST_FUNC void next(void) } else if (tok == TOK_PPSTR) { if (parse_flags & PARSE_FLAG_TOK_STR) parse_string((char *)tokc.str.data, tokc.str.size - 1); - } + } else if (tok == '\\' && !(parse_flags & PARSE_FLAG_ACCEPT_STRAYS)) + tcc_error("stray '\\' in program"); } /* push back current token and set current token to 'last_tok'. Only diff --git a/tests/tcctest.c b/tests/tcctest.c index 1c12dc31..a866330e 100644 --- a/tests/tcctest.c +++ b/tests/tcctest.c @@ -420,6 +420,9 @@ comment have_included_42test_h = 1; have_included_42test_h_second = 1; have_included_42test_h_third = 1; + + /* Check that we don't complain about stray \ here */ + printf("print a backslash: %s\n", stringify(\\)); } diff --git a/tests/tests2/60_errors_and_warnings.c b/tests/tests2/60_errors_and_warnings.c index ac331ebb..19b7f0c1 100644 --- a/tests/tests2/60_errors_and_warnings.c +++ b/tests/tests2/60_errors_and_warnings.c @@ -318,5 +318,8 @@ int main() int n = _Generic(*a, double:0, long double:1); } +#elif defined test_stray_backslash +#define x \a +x /******************************************************************/ #endif diff --git a/tests/tests2/60_errors_and_warnings.expect b/tests/tests2/60_errors_and_warnings.expect index 838f6fcc..d693f91a 100644 --- a/tests/tests2/60_errors_and_warnings.expect +++ b/tests/tests2/60_errors_and_warnings.expect @@ -150,3 +150,6 @@ bar : 3 ; 3 [test_long_double_type_for_win32] 60_errors_and_warnings.c:317: warning: assignment from incompatible pointer type + +[test_stray_backslash] +60_errors_and_warnings.c:323: error: stray '\' in program