summaryrefslogtreecommitdiff
path: root/err.c
blob: 9fada7ba2e8ca7e531668bd87b0cf1e526620e3f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/*
  Copyright (C) 2019, 2020 Leo Tenenbaum.
  This file is part of toc. toc is distributed under version 3 of the GNU General Public License, without any warranty whatsoever.
  You should have received a copy of the GNU General Public License along with toc. If not, see <https://www.gnu.org/licenses/>.
*/

#define TEXT_ERR_START "\x1b[91m"
#define TEXT_ERR_END "\x1b[0m"

#define TEXT_INFO_START "\x1b[94m"
#define TEXT_INFO_END "\x1b[0m"
#define TEXT_WARN_START "\x1b[93m"
#define TEXT_WARN_END "\x1b[0m"
#define TEXT_IMPORTANT_START "\x1b[1m"
#define TEXT_IMPORTANT_END "\x1b[0m"

#define TEXT_INDICATOR_START "\x1b[96m"
#define TEXT_INDICATOR_END "\x1b[0m"

#if defined(TOC_DEBUG) && __STDC_VERSION__ >= 199901
#define ERR_SHOW_SOURCE_LOCATION 1
#endif


static void print_pos_highlight(FILE *out, ErrCtx *ctx, File *file, U32 start_pos, U32 end_pos) {
	char *str = file->contents;
	if (!str) {
		return;
	}
	
	char *start = str + start_pos;
	char *line_start = start;
	char *end = str + end_pos;

	/* go back to last newline / 0 byte */
	while (*line_start != '\0' && *line_start != '\n') --line_start;
	if (line_start < start) ++line_start;

	/* skip space at start of line */
	while (isspace(*line_start) && line_start < end)
		++line_start;
		
	char *line_end = strchr(start, '\n');
	int has_newline = line_end != NULL;
	if (!has_newline)
		line_end = strchr(start, '\0');
	assert(line_end);
	if (!line_start[0])
		fprintf(out, "<end of file>");
	else {
		/* write up to start of error */
		fwrite(line_start, 1, (size_t)(start - line_start), out);
		if (ctx->color_enabled)
			fprintf(out, TEXT_INDICATOR_START);
		if (line_end < end) {
			/* write error part (only go to end of line) */
			fwrite(start, 1, (size_t)(line_end - start), out);
			if (ctx->color_enabled)
				fprintf(out, TEXT_INDICATOR_END);
		} else {
			/* write error part */
			fwrite(start, 1, (size_t)(end - start), out);
			if (ctx->color_enabled)
				fprintf(out, TEXT_INDICATOR_END);
			/* write rest of line */
			fwrite(end, 1, (size_t)(line_end - end), out);
		}
	}
	fprintf(out, "\n");
}

static FILE *err_ctx_file(ErrCtx *ctx) {
	(void)ctx;
	return stderr;
}


static void err_fprint(ErrCtx *ctx, const char *fmt, ...) {
	va_list args;
	va_start(args, fmt);
	vfprintf(err_ctx_file(ctx), fmt, args);
	va_end(args);
}

static void print_location_highlight(FILE *out, Location where) {
	File *f = where.file;
	ErrCtx *ctx = f->ctx;
	if (where.start == 0 && where.end == 0) { err_fprint(ctx, "\n"); return; } /* null location */
	Token *first = &f->tokens[where.start];
	Token *last = &f->tokens[where.end-1];
	
	print_pos_highlight(out, ctx, f, first->pos.start, last->pos.end);
}

/* for debugging */
static void fprint_location(FILE *out, Location location) {
	File *f = location.file;
	fprintf(out, "Line %ld of %s: ", (long)f->tokens[location.start].pos.line, f->filename);
	print_location_highlight(out, location);
}

static void print_location(Location location) {
	fprint_location(stdout, location);
}


static inline const char *ordinals(size_t x) {
	switch (x % 10) {
	case 1: return "st";
	case 2: return "nd";
	case 3: return "rd";
	default: return "th";
	}
}

static void err_text_err(ErrCtx *ctx, const char *s) {
	if (ctx->color_enabled)
		err_fprint(ctx, TEXT_ERR_START "%s" TEXT_ERR_END, s);
	else
		err_fprint(ctx, "%s", s);
}
static void err_text_warn(ErrCtx *ctx, const char *s) {
	if (ctx->color_enabled)
		err_fprint(ctx, TEXT_WARN_START "%s" TEXT_WARN_END, s);
	else
		err_fprint(ctx, "%s", s);
}
static void err_text_info(ErrCtx *ctx, const char *s) {
	if (ctx->color_enabled)
		err_fprint(ctx, TEXT_INFO_START "%s" TEXT_INFO_END, s);
	else
		err_fprint(ctx, "%s", s);
}
static void err_text_important(ErrCtx *ctx, const char *s) {
	if (ctx->color_enabled)
		err_fprint(ctx, TEXT_IMPORTANT_START "%s" TEXT_IMPORTANT_END, s);
	else
		err_fprint(ctx, "%s", s);
}



static void err_vfprint(ErrCtx *ctx, const char *fmt, va_list args) {
	vfprintf(err_ctx_file(ctx), fmt, args);
}

static void err_print_line_file(Location where) {
	File *f = where.file;
	ErrCtx *ctx = f->ctx;
	err_fprint(ctx, " at line %lu of %s:\n", (unsigned long)f->tokens[where.start].pos.line, f->filename);
}

static void err_print_header_(Location where) {
	ErrCtx *ctx = where.file->ctx;
	err_text_err(ctx, "error");
	if (where.start == 0 && where.end == 0) { err_fprint(ctx, ": "); return; }
	err_print_line_file(where);
}

static void info_print_header_(Location where) {
	ErrCtx *ctx = where.file->ctx;
	err_text_info(ctx, "info");
	if (where.start == 0 && where.end == 0) { err_fprint(ctx, ": "); return; }
	err_print_line_file(where);
}

static void warn_print_header_(Location where) {
	ErrCtx *ctx = where.file->ctx;
	err_text_warn(ctx, "warning");
	if (where.start == 0 && where.end == 0) { err_fprint(ctx, ": "); return; }
	err_print_line_file(where);
}


static void err_print_footer_(Location where, bool show_ctx_stack) {
	ErrCtx *ctx = where.file->ctx;
	err_fprint(ctx, "\n\t");
	print_location_highlight(err_ctx_file(ctx), where);
	if (ctx && show_ctx_stack) {
		arr_foreach(ctx->instance_stack, Location, inst) {
			err_fprint(ctx, "While generating this instance of a function or struct:\n\t");
			print_location_highlight(err_ctx_file(ctx), *inst);
		}
	}
}

/* Write nicely-formatted errors to the error file */

static void err_vprint(Location where, const char *fmt, va_list args) {
	ErrCtx *ctx = where.file->ctx;
	if (!ctx->enabled) return;
	ctx->have_errored = true;
	err_print_header_(where);
	err_vfprint(ctx, fmt, args);
	err_print_footer_(where, true);
}


static void err_print_(
#if ERR_SHOW_SOURCE_LOCATION
					   int line, const char *file,
#endif
					   Location where, const char *fmt, ...) {
	va_list args;
#if ERR_SHOW_SOURCE_LOCATION
	ErrCtx *ctx = where.file->ctx;
	if (!ctx->enabled) return;
	if (file)
		err_fprint(ctx, "Generated by line %d of %s:\n", line, file);
#endif
	va_start(args, fmt);
	err_vprint(where, fmt, args);
	va_end(args);
}

#if ERR_SHOW_SOURCE_LOCATION
#define err_print(...) err_print_(__LINE__, __FILE__, __VA_ARGS__)
#else
#define err_print err_print_
#endif
	
static void info_print(Location where, const char *fmt, ...) {
	va_list args;
	ErrCtx *ctx = where.file->ctx;
	if (!ctx->enabled) return;
	va_start(args, fmt);
	info_print_header_(where);
	err_vfprint(ctx, fmt, args);
	err_print_footer_(where, false);
	va_end(args);
}

static void warn_print_(
#if ERR_SHOW_SOURCE_LOCATION
						int line, const char *file,
#endif
						Location where, const char *fmt, ...) {
	va_list args;
	ErrCtx *ctx = where.file->ctx;
	if (!ctx->enabled) return;
#if ERR_SHOW_SOURCE_LOCATION
	if (file)
		err_fprint(ctx, "Generated by line %d of %s:\n", line, file);
#endif
	va_start(args, fmt);
	warn_print_header_(where);
	err_vfprint(ctx, fmt, args);
	err_print_footer_(where, true);
	va_end(args);
}

#if ERR_SHOW_SOURCE_LOCATION
#define warn_print(...) warn_print_(__LINE__, __FILE__, __VA_ARGS__)
#else
#define warn_print warn_print_
#endif


static void *err_malloc(size_t size) {
	if (size == 0) return NULL;
	void *ret = malloc(size);
	if (!ret) {
		fprintf(stderr, "Error: Out of memory.\n");
		exit(EXIT_FAILURE);
	}
	
#ifdef MALLOC_FILL
	memset(ret, MALLOC_FILL, size);
#endif
	return ret;
}

static void *err_calloc(size_t n, size_t size) {
	if (n == 0 || size == 0) return NULL;
	void *ret = calloc(n, size);
	if (!ret) {
		fprintf(stderr, "Error: Out of memory.\n");
		exit(EXIT_FAILURE);
	}
	return ret;
}

static void *err_realloc(void *data, size_t new_size) {
	if (new_size == 0) {
		free(data);
		return NULL;
	}
	void *ret = realloc(data, new_size);
	if (!ret) {
		fprintf(stderr, "Error: Out of memory.\n");
		exit(EXIT_FAILURE);
	}
	return ret;
}