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
This commit is contained in:
herman ten brugge 2025-01-05 20:10:06 +01:00
parent 68000c01ae
commit 999ec460a6
3 changed files with 40 additions and 10 deletions

23
tccpp.c
View file

@ -1929,31 +1929,34 @@ ST_FUNC void preprocess(int is_bof)
break; break;
case TOK_LINE: case TOK_LINE:
next_nomacro(); next();
if (tok != TOK_PPNUM) { if (tok != TOK_CINT) {
_line_err: _line_err:
tcc_error("wrong #line format"); tcc_error("wrong #line format");
} }
n = tokc.i;
goto _line_num; goto _line_num;
case TOK_PPNUM: case TOK_PPNUM:
if (parse_flags & PARSE_FLAG_ASM_FILE) if (parse_flags & PARSE_FLAG_ASM_FILE)
goto ignore; goto ignore;
_line_num:
for (n = 0, q = tokc.str.data; *q; ++q) { for (n = 0, q = tokc.str.data; *q; ++q) {
if (!isnum(*q)) if (!isnum(*q))
goto _line_err; goto _line_err;
n = n * 10 + *q - '0'; n = n * 10 + *q - '0';
} }
next_nomacro(); _line_num:
if (tok != TOK_LINEFEED) { next();
if (tok == TOK_PPSTR && tokc.str.data[0] == '"') { if (tok == TOK_STR) {
tokc.str.data[tokc.str.size - 2] = 0; tccpp_putfile(tokc.str.data);
tccpp_putfile(tokc.str.data + 1); n--;
} else
goto _line_err;
} }
else if (tok != TOK_LINEFEED)
goto _line_err;
if (macro_ptr && *macro_ptr == 0)
macro_stack->save_line_num = n;
if (file->fd > 0) if (file->fd > 0)
total_lines += file->line_num - n; total_lines += file->line_num - n;
file->line_ref += file->line_num - n;
file->line_num = n; file->line_num = n;
goto ignore; /* skip optional level number */ goto ignore; /* skip optional level number */

19
tests/pp/23.S Normal file
View file

@ -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__

8
tests/pp/23.expect Normal file
View file

@ -0,0 +1,8 @@
1
3
20
22
30
40 "23.S"
50 "file1"
60 "file2"