Update log_on_line to take Location* instead of individual fields

This commit is contained in:
2026-04-24 22:13:29 +02:00
parent 26a1d0285e
commit 902e2f0325
4 changed files with 25 additions and 18 deletions
+9 -9
View File
@@ -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';
+5 -6
View File
@@ -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
+10 -2
View File
@@ -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");
}
+1 -1
View File
@@ -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;
}