466be76fd8
P1-①: error_init/add 从 malloc/realloc/strdup 改为 arena_alloc/arena_strdup P1-②: impl 方法中 Self 类型在 sema 解析为实际结构体名 P1-③: trait 方法 fallback 增加前缀校验(strncmp),method_name 统一更新 P1-④: codegen args[16] 增加溢出检查,移除 parser 未使用的 mods/uses 数组 新增: 36_self_type.l 集成测试(Self 类型 + trait 方法) Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
50 lines
1.5 KiB
C
50 lines
1.5 KiB
C
#include "error.h"
|
|
#include "arena.h"
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <stdarg.h>
|
|
#include <string.h>
|
|
|
|
void error_init(ErrorList* list, Arena* a) {
|
|
list->capacity = 64;
|
|
list->errors = arena_alloc(a, list->capacity * sizeof(ErrorInfo));
|
|
list->count = 0;
|
|
list->arena = a;
|
|
if (!list->errors) list->capacity = 0;
|
|
}
|
|
|
|
void error_add(ErrorList* list, const char* filename, int line, int col, const char* fmt, ...) {
|
|
if (!list->errors) return;
|
|
if (list->count >= list->capacity) {
|
|
size_t new_cap = list->capacity + 64;
|
|
ErrorInfo* new_errs = arena_alloc(list->arena, new_cap * sizeof(ErrorInfo));
|
|
if (!new_errs) return;
|
|
memcpy(new_errs, list->errors, list->capacity * sizeof(ErrorInfo));
|
|
list->errors = new_errs;
|
|
list->capacity = new_cap;
|
|
}
|
|
char buf[512];
|
|
va_list args;
|
|
va_start(args, fmt);
|
|
int n = vsnprintf(buf, sizeof(buf), fmt, args);
|
|
va_end(args);
|
|
if (n < 0) return;
|
|
char* msg = arena_strdup(list->arena, buf);
|
|
char* fname = arena_strdup(list->arena, filename);
|
|
if (!msg || !fname) return;
|
|
list->errors[list->count++] = (ErrorInfo){
|
|
.message = msg,
|
|
.filename = fname,
|
|
.line = line,
|
|
.col = col,
|
|
};
|
|
}
|
|
|
|
void error_print(const ErrorList* list) {
|
|
for (size_t i = 0; i < list->count; i++) {
|
|
const ErrorInfo* e = &list->errors[i];
|
|
fprintf(stderr, "\033[1;31m错误:\033[0m %s:%d:%d: %s\n",
|
|
e->filename, e->line, e->col, e->message);
|
|
}
|
|
}
|