From 902e2f03257cea44ca6cf5476e2a54e1d7233831 Mon Sep 17 00:00:00 2001 From: Sebastiaan de Schaetzen Date: Fri, 24 Apr 2026 22:13:29 +0200 Subject: [PATCH] Update log_on_line to take Location* instead of individual fields --- v0/log.c | 18 +++++++++--------- v0/log.h | 11 +++++------ v0/test_log.c | 12 ++++++++++-- v0/token.c | 2 +- 4 files changed, 25 insertions(+), 18 deletions(-) diff --git a/v0/log.c b/v0/log.c index 76fa65c..e002e73 100644 --- a/v0/log.c +++ b/v0/log.c @@ -18,11 +18,11 @@ void log_error(const char* msg) { } } -void log_on_line(const char* filename, const char* line_text, int line, int from, int to, const char* msg, ...) { +void log_on_line(Location* loc, int to_column, const char* msg, ...) { char line_prefix[32]; - int prefix_len = snprintf(line_prefix, sizeof(line_prefix), "%d| ", line); + int prefix_len = snprintf(line_prefix, sizeof(line_prefix), "%d| ", loc->line); - int caret_len = to - from + 1; + int caret_len = to_column - loc->column + 1; if (caret_len < 1) caret_len = 1; // Format the message @@ -32,9 +32,9 @@ void log_on_line(const char* filename, const char* line_text, int line, int from vsnprintf(formatted_msg, sizeof(formatted_msg), msg, args); va_end(args); - size_t total_size = strlen(filename) + 16 + // --- filename --- - prefix_len + strlen(line_text) + 2 + // line| text\n - prefix_len + from - 1 + caret_len + 2 + // indent + ^^\n + size_t total_size = strlen(loc->filename) + 16 + // --- filename --- + prefix_len + strlen(loc->line_text) + 2 + // line| text\n + prefix_len + loc->column - 1 + caret_len + 2 + // indent + ^^\n prefix_len + strlen(formatted_msg) + 2 + // indent + msg\n 1; @@ -42,11 +42,11 @@ void log_on_line(const char* filename, const char* line_text, int line, int from if (!buffer) return; char* p = buffer; - p += sprintf(p, "--- %s ---\n", filename); - p += sprintf(p, "%s%s\n", line_prefix, line_text); + p += sprintf(p, "--- %s ---\n", loc->filename); + p += sprintf(p, "%s%s\n", line_prefix, loc->line_text); // Caret line - for (int i = 0; i < prefix_len + from - 1; i++) *p++ = ' '; + for (int i = 0; i < prefix_len + loc->column - 1; i++) *p++ = ' '; for (int i = 0; i < caret_len; i++) *p++ = '^'; *p++ = '\n'; diff --git a/v0/log.h b/v0/log.h index da81afe..8492a71 100644 --- a/v0/log.h +++ b/v0/log.h @@ -4,6 +4,8 @@ #ifndef LOG_H #define LOG_H +#include "location.h" + /** * A method that can log an error. */ @@ -22,14 +24,11 @@ void log_error(const char* msg); /** * Logs a pretty error with additional information about the line where the error occurred. * - * @param filename The name of the file where the error occurred. - * @param line_text The entire line of text where the error occurred. - * @param line The line number where the error occurred. - * @param from The column number where the error starts. - * @param to The column number where the error ends. + * @param loc The location where the error occurred. + * @param to_column The column number where the error ends. * @param msg The error message to log. This can contain format specifiers like printf, and the additional arguments will be formatted into the message. * @param ... Additional arguments to format into the error message. */ -void log_on_line(const char* filename, const char* line_text, int line, int from, int to, const char* msg, ...); +void log_on_line(Location* loc, int to_column, const char* msg, ...); #endif diff --git a/v0/test_log.c b/v0/test_log.c index f5a9d46..509d1b4 100644 --- a/v0/test_log.c +++ b/v0/test_log.c @@ -21,10 +21,18 @@ static void test_log_error(void) { } static void test_log_on_line(void) { - log_on_line("test.c", "int main() []", 1, 12, 13, "unexpected token"); + Location loc = { + .filename = "test.c", + .line_text = "int main() []", + .line_text_length = 13, + .line = 1, + .column = 12 + }; + + log_on_line(&loc, 13, "unexpected token"); assert_log_file("v0/tests/log_on_line.txt", "expected formatted error message"); log_clear(); - log_on_line("test.c", "int main() []", 1, 12, 13, "unexpected token '%c'", 'x'); + log_on_line(&loc, 13, "unexpected token '%c'", 'x'); assert_log_file("v0/tests/log_on_line_variadic.txt", "expected formatted error message with variadic args"); } diff --git a/v0/token.c b/v0/token.c index 19bbeb8..36a6a09 100644 --- a/v0/token.c +++ b/v0/token.c @@ -192,6 +192,6 @@ Token tokenstream_next(TokenStream* ts) { /* Unknown character */ Token t = create_token(ts, TOKEN_UNKNOWN, start_text, 1, start_line, start_column, line_start); - log_on_line(ts->filename, t.location.line_text, t.location.line, t.location.column, t.location.column, "unexpected token '%c'", c); + log_on_line(&t.location, t.location.column, "unexpected token '%c'", c); return t; }