From f2297c20b69d942fd1e8fdcdd8f4d142c71de662 Mon Sep 17 00:00:00 2001 From: Matthew Sotoudeh Date: Sun, 30 Jul 2023 18:09:52 -0700 Subject: bad line numbers support --- preprocess.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) (limited to 'preprocess.c') diff --git a/preprocess.c b/preprocess.c index abc095f..e3a3455 100644 --- a/preprocess.c +++ b/preprocess.c @@ -24,6 +24,10 @@ #include "chibicc.h" +static char *CURR_FILE; +static unsigned long LINE_SUB, + LINE_ADD; + typedef struct MacroParam MacroParam; struct MacroParam { MacroParam *next; @@ -807,6 +811,9 @@ static void read_line_marker(Token **rest, Token *tok) { if (tok->kind != TK_STR) error_tok(tok, "filename expected"); start->file->display_name = tok->str; + CURR_FILE = tok->str; + LINE_SUB = tok->line_no; + LINE_ADD = tok->val; } // Visit all tokens in `tok` while evaluating preprocessing @@ -823,7 +830,9 @@ static Token *preprocess2(Token *tok) { // Pass through if it is not a "#". if (!is_hash(tok)) { tok->line_delta = tok->file->line_delta; - tok->filename = tok->file->display_name; + tok->line_no -= LINE_SUB; + tok->line_no += LINE_ADD; + tok->filename = CURR_FILE ? CURR_FILE : tok->file->display_name; cur = cur->next = tok; tok = tok->next; continue; -- cgit v1.2.3