meta: add ivy-diag library

ivy-diag is used for generating and emitting diagnostic messages during compilation.
This commit is contained in:
2025-05-08 10:48:23 +01:00
parent 8e8637882d
commit c459f50e67
13 changed files with 1151 additions and 0 deletions

166
diag/include/ivy/diag.h Normal file
View File

@@ -0,0 +1,166 @@
#ifndef IVY_DIAG_H_
#define IVY_DIAG_H_
#include <ivy/status.h>
#include <stddef.h>
#include <stdio.h>
#define IVY_DIAG_HL(type, row0, col0, row1, col1) \
{ \
.h_type = IVY_DIAG_HIGHLIGHT_##type, .h_start_row = row0, \
.h_start_col = col0, .h_end_row = row1, .h_end_col = col1 \
}
#define IVY_DIAG_ADD(row, col, str) \
{ \
.a_type = IVY_DIAG_AMENDMENT_ADD, .__x = strlen(str), \
.a_add = { \
.a_row = (row), \
.a_col = (col), \
.a_str = (str), \
}, \
}
#define IVY_DIAG_REMOVE(row, col, len) \
{ \
.a_type = IVY_DIAG_AMENDMENT_REMOVE, \
.a_remove = { \
.a_row = (row), \
.a_col = (col), \
.a_length = (len), \
}, \
}
#define IVY_DIAG_REPLACE(row, col, len, str) \
{ \
.a_type = IVY_DIAG_AMENDMENT_REPLACE, .__x = strlen(str), \
.a_replace = { \
.a_row = (row), \
.a_col = (col), \
.a_length = (len), \
.a_str = (str), \
}, \
}
struct ivy_line_source;
struct ivy_diag_ctx;
struct ivy_diag;
struct b_tty;
enum ivy_diag_stream_flags {
IVY_DIAG_STREAM_F_NONE = 0x00u,
IVY_DIAG_STREAM_F_COLOUR = 0x01u,
};
enum ivy_diag_stream_type {
IVY_DIAG_STREAM_NONE = 0,
IVY_DIAG_STREAM_FILE,
IVY_DIAG_STREAM_TTY,
};
struct ivy_diag_stream {
enum ivy_diag_stream_type s_type;
enum ivy_diag_stream_flags s_flags;
size_t s_row, s_col;
int s_esc;
union {
struct b_tty *s_tty;
FILE *s_fp;
};
};
enum ivy_diag_type {
IVY_DIAG_NONE = 0,
IVY_DIAG_ERROR,
IVY_DIAG_WARNING,
IVY_DIAG_HINT,
};
enum ivy_diag_highlight_type {
IVY_DIAG_HIGHLIGHT_NONE = 0,
IVY_DIAG_HIGHLIGHT_ERROR,
IVY_DIAG_HIGHLIGHT_WARNING,
IVY_DIAG_HIGHLIGHT_HINT,
};
struct ivy_diag_highlight {
enum ivy_diag_highlight_type h_type;
unsigned long h_start_row, h_start_col;
unsigned long h_end_row, h_end_col;
};
enum ivy_diag_amendment_type {
IVY_DIAG_AMENDMENT_NONE = 0,
IVY_DIAG_AMENDMENT_ADD,
IVY_DIAG_AMENDMENT_REMOVE,
IVY_DIAG_AMENDMENT_REPLACE,
};
enum ivy_diag_format {
IVY_DIAG_FORMAT_NONE = 0,
IVY_DIAG_FORMAT_PRETTY,
IVY_DIAG_FORMAT_JSON,
};
struct ivy_diag_amendment {
enum ivy_diag_amendment_type a_type;
unsigned long __x;
union {
struct {
unsigned long a_row, a_col;
char *a_str;
} a_add;
struct {
unsigned long a_row, a_col;
unsigned long a_length;
} a_remove;
struct {
unsigned long a_row, a_col;
unsigned long a_length;
char *a_str;
} a_replace;
};
};
struct ivy_diag_class {
enum ivy_diag_type c_type;
const char *c_title;
};
struct ivy_diag_msg {
const char *msg_content;
};
extern void ivy_diag_stream_init_file(struct ivy_diag_stream *stream, FILE *fp);
extern void ivy_diag_stream_init_tty(
struct ivy_diag_stream *stream, struct b_tty *tty);
extern enum ivy_status ivy_diag_ctx_create(struct ivy_diag_ctx **out);
extern void ivy_diag_ctx_destroy(struct ivy_diag_ctx *ctx);
extern enum ivy_status ivy_diag_set_line_source(
struct ivy_diag_ctx *ctx, struct ivy_line_source *src);
extern enum ivy_status ivy_diag_ctx_set_class_definitions(
struct ivy_diag_ctx *ctx, const struct ivy_diag_class *classes,
size_t nr_classes);
extern enum ivy_status ivy_diag_ctx_set_msg_definitions(
struct ivy_diag_ctx *ctx, const struct ivy_diag_msg *msgs, size_t nr_msgs);
extern void ivy_diag_ctx_write(
struct ivy_diag_ctx *ctx, enum ivy_diag_format format,
struct ivy_diag_stream *stream);
extern struct ivy_diag *ivy_diag_ctx_create_diag(
struct ivy_diag_ctx *ctx, unsigned long diag_class);
extern void ivy_diag_set_location(
struct ivy_diag *diag, unsigned long row, unsigned long col);
extern void ivy_diag_push_msg(struct ivy_diag *diag, unsigned long msg, ...);
extern void ivy_diag_push_snippet(
struct ivy_diag *diag, unsigned long first_line, unsigned long last_line,
const struct ivy_diag_amendment *amendmends, size_t nr_amendments,
const struct ivy_diag_highlight *highlights, size_t nr_highlights);
#endif