My Project
Loading...
Searching...
No Matches
gwlog.c
Go to the documentation of this file.
1#include "gwion_util.h"
2#include "gwion_ast.h"
3#include "parser.h"
4
5static pos_t default_pos = { .line = 1, .column = 1 };
6
8 default_pos = pos;
9}
10
11ANN void pos_ini(pos_t *pos) { *pos = default_pos; }
12
13static void _gwlog_error(const char *main, const char *explain,
14 const char *filename, const loc_t loc, const uint error_code,
15 const enum libprettyerr_errtype errtype);
16ANN static void _gwlog_warning(const char *main, const char *filename,
17 const loc_t loc);
18ANN static void _gwlog_related(const char *main, const char *filename,
19 const loc_t loc);
20ANN static void _gwlog_hint(const char *main, const char *filename NUSED,
21 const loc_t loc NUSED) {
22 gw_err("{-}hint:{0} %s\n", main);
23}
24
29
34 _error = error;
35 _warning = warning;
36 _related = related;
37 _hint = hint;
38}
39
40ANN static char *get_src(const char *filename, const loc_t loc) {
41 char * line = NULL;
42 size_t len = 0;
43 uint i = 0;
44 FILE * f = fopen(filename, "r");
45 if (!f) return NULL;
46 fseek(f, 0, SEEK_SET);
47 ssize_t ret;
48 while ((ret = getline(&line, &len, f)) != -1 && ++i < loc.first.line);
49 fclose(f);
50 if (ret != -1) return line;
51 if(line) xfree(line);
52 return NULL;
53}
54
55static inline const char *get_filename(const char *filename) {
56#ifndef BUILD_ON_WINDOWS
57 const char *pwd = getenv("PWD");
58 if(!pwd) return filename;
59#else
60 TCHAR pwd[MAX_PATH];
61 GetCurrentDirectory(MAX_PATH, pwd);
62#endif
63 size_t sz = strlen(pwd);
64 return !strncmp(pwd, filename, sz - 1) ? filename + sz + 1 : filename;
65}
66
67ANN2(1, 2, 3)
68static void nosrc(const perr_printer_t *printer, const perr_t *err,
69 const char *main, const char *explain) {
70 size_t len;
71 char base[16];
72 char color[16];
73 strcpy(base, "+R");
74 const int status = tcol_color_parse(color, 16, base, 2, &len);
75 if (status != TermColorErrorNone)
76 color[0] = 0;
77 else
78 color[len] = 0;
79 gw_err("[internal]\n");
80 perr_print_line_number(printer, err, color);
81 gw_err("%s\n", main);
82 if (explain) gw_err("%s\n", explain);
83}
84
85static void _gwlog_error(const char *main, const char *explain,
86 const char *filename, const loc_t loc, const uint error_code,
87 const enum libprettyerr_errtype errtype) {
88#ifdef __FUZZING__
89 return;
90#endif
91 char *line = get_src(filename, loc);
92
93 perr_printer_t printer;
94 perr_printer_init(&printer, stderr, line,
95 true, // use utf8,
96 perr_runner_basic_style);
97
98 printer.rounded = true;
99
100 const size_t sz = line ? (loc.last.line == loc.first.line
101 ? (size_t)(loc.last.column - loc.first.column)
102 : strlen(line) - loc.first.column) : 0;
103 const perr_t err = PERR_Error(
104 errtype, PERR_Str(loc.first.line, line),
105 PERR_Pos(loc.first.column - 1, sz), main,
106 explain, error_code, get_filename(filename));
107
108 if (line) {
109 perr_print_error(&printer, &err);
110 xfree(line);
111 } else
112 nosrc(&printer, &err, main, explain);
113}
114
115void gwlog_error(const char *main, const char *explain,
116 const char *filename, const loc_t loc, const uint error_code) {
117 _error(main, explain, filename, loc, error_code, PERR_ERROR);
118}
119
120ANN static void _gwlog_secondary(const char *main, const char *filename,
121 const enum libprettyerr_errtype type,
122 const loc_t loc) {
123 perr_printer_t printer;
124 char * line = get_src(filename, loc);
125
126 perr_printer_init(&printer, stderr, line,
127 true, // use utf8,
128 perr_runner_secondary_style);
129 printer.rounded = true;
130
131 const perr_t err = PERR_Secondary(
132 type, PERR_Str(loc.first.line, line),
133 PERR_Pos(loc.first.column - 1, loc.last.column - loc.first.column), main,
134 get_filename(filename));
135 if (line) {
136 perr_print_error(&printer, &err);
137 xfree(line);
138 } else
139 nosrc(&printer, &err, main, NULL);
140}
141
142ANN void _gwlog_warning(const char *main, const char *filename,
143 const loc_t loc) {
144 _gwlog_secondary(main, filename, PERR_WARNING, loc);
145}
146ANN void gwlog_warning(const char *main, const char *filename,
147 const loc_t loc) {
148 _warning(main, filename, loc);
149}
150
151ANN void _gwlog_related(const char *main, const char *filename,
152 const loc_t loc) {
153 _gwlog_secondary(main, filename, PERR_INFO, loc);
154}
155ANN void gwlog_related(const char *main, const char *filename,
156 const loc_t loc) {
157 _related(main, filename, loc);
158}
159
160ANN void gwlog_hint(const char *main, const char *filename,
161 const loc_t loc) {
162 _hint(main, filename, loc);
163}
include this file to use gwion-ast library
static gwlog_warning_function_t _related
Definition gwlog.c:27
ANN void gwlog_related(const char *main, const char *filename, const loc_t loc)
Definition gwlog.c:155
static ANN void _gwlog_secondary(const char *main, const char *filename, const enum libprettyerr_errtype type, const loc_t loc)
Definition gwlog.c:120
static pos_t default_pos
Definition gwlog.c:5
static gwlog_warning_function_t _hint
Definition gwlog.c:28
void gwlog_error(const char *main, const char *explain, const char *filename, const loc_t loc, const uint error_code)
Definition gwlog.c:115
static ANN char * get_src(const char *filename, const loc_t loc)
Definition gwlog.c:40
ANN void pos_ini(pos_t *pos)
Definition gwlog.c:11
static ANN void _gwlog_warning(const char *main, const char *filename, const loc_t loc)
Definition gwlog.c:142
static gwlog_error_function_t _error
Definition gwlog.c:25
static void _gwlog_error(const char *main, const char *explain, const char *filename, const loc_t loc, const uint error_code, const enum libprettyerr_errtype errtype)
Definition gwlog.c:85
ANN void gwlog_set_func(gwlog_error_function_t error, gwlog_warning_function_t warning, gwlog_warning_function_t related, gwlog_warning_function_t hint)
Definition gwlog.c:30
static gwlog_warning_function_t _warning
Definition gwlog.c:26
static const char * get_filename(const char *filename)
Definition gwlog.c:55
static ANN void _gwlog_related(const char *main, const char *filename, const loc_t loc)
Definition gwlog.c:151
ANN void gwlog_warning(const char *main, const char *filename, const loc_t loc)
Definition gwlog.c:146
static ANN void _gwlog_hint(const char *main, const char *filename NUSED, const loc_t loc NUSED)
Definition gwlog.c:20
void gwion_parser_set_default_pos(const pos_t pos)
Definition gwlog.c:7
static void nosrc(const perr_printer_t *printer, const perr_t *err, const char *main, const char *explain)
Definition gwlog.c:68
ANN void gwlog_hint(const char *main, const char *filename, const loc_t loc)
Definition gwlog.c:160
void(* gwlog_warning_function_t)(const char *main, const char *filename, const loc_t loc)
Definition gwlog.h:30
void(* gwlog_error_function_t)(const char *, const char *, const char *, const loc_t, const uint, const enum libprettyerr_errtype)
Definition gwlog.h:28
return NULL
Definition macro.c:41
int main(int argc, char **argv)
Definition main.c:4
ANN2(1)
Definition pparg.c:46
Definition gwlog.h:15
struct pos_t last
Definition gwlog.h:17
struct pos_t first
Definition gwlog.h:16
Definition gwlog.h:7
short unsigned int column
Definition gwlog.h:9
short unsigned int line
Definition gwlog.h:8