My Project
Loading...
Searching...
No Matches
gwlog.c File Reference
#include "gwion_util.h"
#include "gwion_ast.h"
#include "parser.h"

Go to the source code of this file.

Functions

void gwion_parser_set_default_pos (const pos_t pos)
 
ANN void pos_ini (pos_t *pos)
 
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)
 
static ANN void _gwlog_warning (const char *main, const char *filename, const loc_t loc)
 
static ANN void _gwlog_related (const char *main, const char *filename, const loc_t loc)
 
static ANN void _gwlog_hint (const char *main, const char *filename NUSED, const loc_t loc NUSED)
 
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)
 
static ANN char * get_src (const char *filename, const loc_t loc)
 
static const char * get_filename (const char *filename)
 
static void nosrc (const perr_printer_t *printer, const perr_t *err, const char *main, const char *explain, const char *filename)
 
void gwlog_error (const char *main, const char *explain, const char *filename, const loc_t loc, const uint error_code)
 
static ANN void _gwlog_secondary (const char *main, const char *filename, const enum libprettyerr_errtype type, const loc_t loc)
 
ANN void gwlog_warning (const char *main, const char *filename, const loc_t loc)
 
ANN void gwlog_related (const char *main, const char *filename, const loc_t loc)
 
ANN void gwlog_hint (const char *main, const char *filename, const loc_t loc)
 

Variables

static pos_t default_pos = { .line = 1, .column = 1 }
 
static gwlog_error_function_t _error = _gwlog_error
 
static gwlog_warning_function_t _warning = _gwlog_warning
 
static gwlog_warning_function_t _related = _gwlog_related
 
static gwlog_warning_function_t _hint = _gwlog_hint
 

Function Documentation

◆ _gwlog_error()

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 )
static

Definition at line 89 of file gwlog.c.

91 {
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}
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
static ANN char * get_src(const char *filename, const loc_t loc)
Definition gwlog.c:40
static const char * get_filename(const char *filename)
Definition gwlog.c:55
int main(int argc, char **argv)
Definition main.c:4
struct pos_t last
Definition gwlog.h:17
struct pos_t first
Definition gwlog.h:16
short unsigned int column
Definition gwlog.h:9
short unsigned int line
Definition gwlog.h:8

◆ _gwlog_hint()

static ANN void _gwlog_hint ( const char * main,
const char *filename NUSED,
const loc_t loc NUSED )
static

Definition at line 20 of file gwlog.c.

21 {
22 gw_err("{-}hint:{0} %s\n", main);
23}

◆ _gwlog_related()

ANN void _gwlog_related ( const char * main,
const char * filename,
const loc_t loc )
static

Definition at line 155 of file gwlog.c.

156 {
157 _gwlog_secondary(main, filename, PERR_INFO, loc);
158}
static ANN void _gwlog_secondary(const char *main, const char *filename, const enum libprettyerr_errtype type, const loc_t loc)
Definition gwlog.c:124

◆ _gwlog_secondary()

static ANN void _gwlog_secondary ( const char * main,
const char * filename,
const enum libprettyerr_errtype type,
const loc_t loc )
static

Definition at line 124 of file gwlog.c.

126 {
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}
return NULL
Definition macro.c:41

◆ _gwlog_warning()

ANN void _gwlog_warning ( const char * main,
const char * filename,
const loc_t loc )
static

Definition at line 146 of file gwlog.c.

147 {
148 _gwlog_secondary(main, filename, PERR_WARNING, loc);
149}

◆ get_filename()

static const char * get_filename ( const char * filename)
inlinestatic

Definition at line 55 of file gwlog.c.

55 {
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}

◆ get_src()

static ANN char * get_src ( const char * filename,
const loc_t loc )
static

Definition at line 40 of file gwlog.c.

40 {
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}

◆ gwion_parser_set_default_pos()

void gwion_parser_set_default_pos ( const pos_t pos)

Definition at line 7 of file gwlog.c.

7 {
8 default_pos = pos;
9}
static pos_t default_pos
Definition gwlog.c:5

◆ gwlog_error()

void gwlog_error ( const char * main,
const char * explain,
const char * filename,
const loc_t loc,
const uint error_code )

Definition at line 119 of file gwlog.c.

120 {
121 _error(main, explain, filename, loc, error_code, PERR_ERROR);
122}
static gwlog_error_function_t _error
Definition gwlog.c:25

◆ gwlog_hint()

ANN void gwlog_hint ( const char * main,
const char * filename,
const loc_t loc )

Definition at line 164 of file gwlog.c.

165 {
166 _hint(main, filename, loc);
167}
static gwlog_warning_function_t _hint
Definition gwlog.c:28

◆ gwlog_related()

ANN void gwlog_related ( const char * main,
const char * filename,
const loc_t loc )

Definition at line 159 of file gwlog.c.

160 {
161 _related(main, filename, loc);
162}
static gwlog_warning_function_t _related
Definition gwlog.c:27

◆ gwlog_set_func()

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 at line 30 of file gwlog.c.

33 {
34 _error = error;
35 _warning = warning;
36 _related = related;
37 _hint = hint;
38}
static gwlog_warning_function_t _warning
Definition gwlog.c:26

◆ gwlog_warning()

ANN void gwlog_warning ( const char * main,
const char * filename,
const loc_t loc )

Definition at line 150 of file gwlog.c.

151 {
152 _warning(main, filename, loc);
153}

◆ nosrc()

static void nosrc ( const perr_printer_t * printer,
const perr_t * err,
const char * main,
const char * explain,
const char * filename )
static

Definition at line 68 of file gwlog.c.

69 {
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}

◆ pos_ini()

ANN void pos_ini ( pos_t * pos)

Definition at line 11 of file gwlog.c.

11{ *pos = default_pos; }

Variable Documentation

◆ _error

Definition at line 25 of file gwlog.c.

◆ _hint

Definition at line 28 of file gwlog.c.

◆ _related

Definition at line 27 of file gwlog.c.

◆ _warning

Definition at line 26 of file gwlog.c.

◆ default_pos

pos_t default_pos = { .line = 1, .column = 1 }
static

Definition at line 5 of file gwlog.c.

5{ .line = 1, .column = 1 };