From 999ec460a617998a1ccfbf544c6f982e79fa0a1f Mon Sep 17 00:00:00 2001 From: herman ten brugge Date: Sun, 5 Jan 2025 20:10:06 +0100 Subject: [PATCH] Allow macro in #line directive Using: #define LINE1 10 #line LINE1 #define LINE2 20 #define FILE "file" #line LINE2 FILE Should now work. Add new testcase tests/pp/23.S --- tccpp.c | 25 ++++++++++++++----------- tests/pp/23.S | 19 +++++++++++++++++++ tests/pp/23.expect | 8 ++++++++ 3 files changed, 41 insertions(+), 11 deletions(-) create mode 100644 tests/pp/23.S create mode 100644 tests/pp/23.expect diff --git a/tccpp.c b/tccpp.c index b2ae48af1..077f7dfe6 100644 --- a/tccpp.c +++ b/tccpp.c @@ -1929,31 +1929,34 @@ ST_FUNC void preprocess(int is_bof) break; case TOK_LINE: - next_nomacro(); - if (tok != TOK_PPNUM) { + next(); + if (tok != TOK_CINT) { _line_err: tcc_error("wrong #line format"); } + n = tokc.i; goto _line_num; case TOK_PPNUM: if (parse_flags & PARSE_FLAG_ASM_FILE) goto ignore; - _line_num: for (n = 0, q = tokc.str.data; *q; ++q) { if (!isnum(*q)) goto _line_err; n = n * 10 + *q - '0'; } - next_nomacro(); - if (tok != TOK_LINEFEED) { - if (tok == TOK_PPSTR && tokc.str.data[0] == '"') { - tokc.str.data[tokc.str.size - 2] = 0; - tccpp_putfile(tokc.str.data + 1); - } else - goto _line_err; - } + _line_num: + next(); + if (tok == TOK_STR) { + tccpp_putfile(tokc.str.data); + n--; + } + else if (tok != TOK_LINEFEED) + goto _line_err; + if (macro_ptr && *macro_ptr == 0) + macro_stack->save_line_num = n; if (file->fd > 0) total_lines += file->line_num - n; + file->line_ref += file->line_num - n; file->line_num = n; goto ignore; /* skip optional level number */ diff --git a/tests/pp/23.S b/tests/pp/23.S new file mode 100644 index 000000000..bfacc935d --- /dev/null +++ b/tests/pp/23.S @@ -0,0 +1,19 @@ +__LINE__ +# 10 +__LINE__ +# line 20 +__LINE__ +# 64mb +__LINE__ +# line 30 +__LINE__ +#define LINE1 40 +# line LINE1 +__LINE__ __FILE__ +#define LINE2 50 +# line LINE2 "file1" +__LINE__ __FILE__ +#define LINE3 60 +#define FILE "file2" +# line LINE3 FILE +__LINE__ __FILE__ diff --git a/tests/pp/23.expect b/tests/pp/23.expect new file mode 100644 index 000000000..ac1f0c7a8 --- /dev/null +++ b/tests/pp/23.expect @@ -0,0 +1,8 @@ +1 +3 +20 +22 +30 +40 "23.S" +50 "file1" +60 "file2"