Skip to content

Commit

Permalink
Allow macro in #line directive
Browse files Browse the repository at this point in the history
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
  • Loading branch information
hermantb committed Jan 5, 2025
1 parent 68000c0 commit 999ec46
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 11 deletions.
25 changes: 14 additions & 11 deletions tccpp.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */

Expand Down
19 changes: 19 additions & 0 deletions tests/pp/23.S
Original file line number Diff line number Diff line change
@@ -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 changes: 8 additions & 0 deletions tests/pp/23.expect
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
1
3
20
22
30
40 "23.S"
50 "file1"
60 "file2"

0 comments on commit 999ec46

Please sign in to comment.