Skip to content

Commit

Permalink
Show line direcitve name in messages
Browse files Browse the repository at this point in the history
  • Loading branch information
fuhsnn committed Dec 22, 2024
1 parent ed427e1 commit 56136c9
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 12 deletions.
4 changes: 1 addition & 3 deletions parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -2613,10 +2613,8 @@ static int64_t eval_error(Token *tok, char *fmt, ...) {
}
va_list ap;
va_start(ap, fmt);
verror_at(tok->file->name, tok->file->contents, tok->line_no, tok->loc, fmt, ap);
verror_at_tok(tok, fmt, ap);
va_end(ap);
if (tok->origin)
warn_tok(tok->origin, "in expansion of macro");
exit(1);
}

Expand Down
3 changes: 1 addition & 2 deletions preprocess.c
Original file line number Diff line number Diff line change
Expand Up @@ -1162,8 +1162,7 @@ static Token *preprocess2(Token *tok) {
continue;
}

if (opt_g)
add_loc_info(tok);
add_loc_info(tok);

cur = cur->next = tok;
tok = tok->next;
Expand Down
2 changes: 1 addition & 1 deletion slimcc.h
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ void error(char *fmt, ...) FMTCHK(1,2) NORETURN;
void error_at(char *loc, char *fmt, ...) FMTCHK(2,3) NORETURN;
void error_tok(Token *tok, char *fmt, ...) FMTCHK(2,3) NORETURN;
void warn_tok(Token *tok, char *fmt, ...) FMTCHK(2,3);
void verror_at(char *filename, char *input, int line_no, char *loc, char *fmt, va_list ap);
void verror_at_tok(Token *tok, char *fmt, va_list ap);
bool equal(Token *tok, char *op);
bool equal_ext(Token *tok, char *op);
Token *skip(Token *tok, char *op);
Expand Down
24 changes: 18 additions & 6 deletions tokenize.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ void error(char *fmt, ...) {
//
// foo.c:10: x = y + 1;
// ^ <error message here>
void verror_at(char *filename, char *input, int line_no,
static void verror_at(char *filename, char *input, int line_no,
char *loc, char *fmt, va_list ap) {
// Find a line containing `loc`.
char *line = loc;
Expand All @@ -53,6 +53,21 @@ void verror_at(char *filename, char *input, int line_no,
fprintf(stderr, "\n");
}

void verror_at_tok(Token *tok, char *fmt, va_list ap) {
if (tok->file->file_no != tok->display_file_no) {
char *name = NULL;
File **files = get_input_files();
for (int i = 0; files[i]; i++)
if (tok->display_file_no == files[i]->file_no)
name = files[i]->name;
if (name)
fprintf(stderr, "#line %d \"%s\"\n", tok->display_line_no, name);
}
verror_at(tok->file->name, tok->file->contents, tok->line_no, tok->loc, fmt, ap);
if (tok->origin)
warn_tok(tok->origin, "in expansion of macro");
}

void error_at(char *loc, char *fmt, ...) {
int line_no = 1;
for (char *p = current_file->contents; p < loc; p++)
Expand All @@ -69,18 +84,15 @@ void error_at(char *loc, char *fmt, ...) {
void error_tok(Token *tok, char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
verror_at(tok->file->name, tok->file->contents, tok->line_no, tok->loc, fmt, ap);
verror_at_tok(tok, fmt, ap);
va_end(ap);

if (tok->origin)
warn_tok(tok->origin, "in expansion of macro");
exit(1);
}

void warn_tok(Token *tok, char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
verror_at(tok->file->name, tok->file->contents, tok->line_no, tok->loc, fmt, ap);
verror_at_tok(tok, fmt, ap);
va_end(ap);
}

Expand Down

0 comments on commit 56136c9

Please sign in to comment.