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, const char *filename) {
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("{+}%s{0} [internal]\n", filename);
80 perr_print_line_number(printer, err, color);
81 gw_err(main);
82 gw_err("\n");
83 if (explain) {
84 gw_err(explain);
85 gw_err("\n");
86 }
87}
88
89static void _gwlog_error(const char *main, const char *explain,
90 const char *filename, const loc_t loc, const uint error_code,
91 const enum libprettyerr_errtype errtype) {
92#ifdef __FUZZING__
93 return;
94#endif
95 char *line = get_src(filename, loc);
96
97 perr_printer_t printer;
98 perr_printer_init(&printer, stderr, line,
99 true, // use utf8,
100 perr_runner_basic_style);
101
102 printer.rounded = true;
103
104 const size_t sz = line ? (loc.last.line == loc.first.line
105 ? (size_t)(loc.last.column - loc.first.column)
106 : strlen(line) - loc.first.column) : 0;
107 const perr_t err = PERR_Error(
108 errtype, PERR_Str(loc.first.line, line),
109 PERR_Pos(loc.first.column - 1, sz), main,
110 explain, error_code, get_filename(filename));
111
112 if (line) {
113 perr_print_error(&printer, &err);
114 xfree(line);
115 } else
116 nosrc(&printer, &err, main, explain, filename);
117}
118
119void gwlog_error(const char *main, const char *explain,
120 const char *filename, const loc_t loc, const uint error_code) {
121 _error(main, explain, filename, loc, error_code, PERR_ERROR);
122}
123
124ANN static void _gwlog_secondary(const char *main, const char *filename,
125 const enum libprettyerr_errtype type,
126 const loc_t loc) {
127 perr_printer_t printer;
128 char * line = get_src(filename, loc);
129
130 perr_printer_init(&printer, stderr, line,
131 true, // use utf8,
132 perr_runner_secondary_style);
133 printer.rounded = true;
134
135 const perr_t err = PERR_Secondary(
136 type, PERR_Str(loc.first.line, line),
137 PERR_Pos(loc.first.column - 1, loc.last.column - loc.first.column), main,
138 get_filename(filename));
139 if (line) {
140 perr_print_error(&printer, &err);
141 xfree(line);
142 } else
143 nosrc(&printer, &err, main, NULL, filename);
144}
145
146ANN void _gwlog_warning(const char *main, const char *filename,
147 const loc_t loc) {
148 _gwlog_secondary(main, filename, PERR_WARNING, loc);
149}
150ANN void gwlog_warning(const char *main, const char *filename,
151 const loc_t loc) {
152 _warning(main, filename, loc);
153}
154
155ANN void _gwlog_related(const char *main, const char *filename,
156 const loc_t loc) {
157 _gwlog_secondary(main, filename, PERR_INFO, loc);
158}
159ANN void gwlog_related(const char *main, const char *filename,
160 const loc_t loc) {
161 _related(main, filename, loc);
162}
163
164ANN void gwlog_hint(const char *main, const char *filename,
165 const loc_t loc) {
166 _hint(main, filename, loc);
167}
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:159
static ANN void _gwlog_secondary(const char *main, const char *filename, const enum libprettyerr_errtype type, const loc_t loc)
Definition gwlog.c:124
static pos_t default_pos
Definition gwlog.c:5
static gwlog_warning_function_t _hint
Definition gwlog.c:28
static void nosrc(const perr_printer_t *printer, const perr_t *err, const char *main, const char *explain, const char *filename)
Definition gwlog.c:68
void gwlog_error(const char *main, const char *explain, const char *filename, const loc_t loc, const uint error_code)
Definition gwlog.c:119
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:146
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:89
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:155
ANN void gwlog_warning(const char *main, const char *filename, const loc_t loc)
Definition gwlog.c:150
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
ANN void gwlog_hint(const char *main, const char *filename, const loc_t loc)
Definition gwlog.c:164
struct loc_t_ loc_t
Definition absyn.h:348
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
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