aboutsummaryrefslogtreecommitdiff
path: root/src/debug.c
blob: 566da42f3b8b38e07c9e075209f0f393e80fef64 (plain) (blame)
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
/* SPDX-License-Identifier: GPL-3.0-or-later */

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>

#include <cu/debug.h>

const char *issue_level_str(enum issue_level level)
{
	switch (level) {
	case SRC_INFO: return "info";
	case SRC_WARN: return "warn";
	case SRC_ERROR: return "error";
	}

	return "unknown";
}

static const char *find_lineno(const char *buf, size_t no)
{
	if (no == 1)
		return buf;

	char c;
	while ((c = *buf)) {
		buf++;

		if (c == '\n')
			no--;

		if (no == 1)
			break;
	}

	return buf;
}

static void _issue(struct src_issue issue, const char *fmt, va_list args)
{
	/* get start and end of current line in buffer */
	const char *line_start = find_lineno(issue.fctx.fbuf,
	                                     issue.loc.first_line);
	const char *line_end = strchr(line_start, '\n');
	if (!line_end)
		line_end = strchr(line_start, 0);

	const int line_len = line_end - line_start;

	fprintf(stderr, "%s:%i:%i: %s: ", issue.fctx.fname,
	        issue.loc.first_line,
	        issue.loc.first_col,
	        issue_level_str(issue.level));

	vfprintf(stderr, fmt, args);
	fputc('\n', stderr);

	int lineno_len = snprintf(NULL, 0, "%i", issue.loc.first_line);
	fputc(' ', stderr);
	fprintf(stderr, "%i | ", issue.loc.first_line);

	fprintf(stderr, "%.*s\n", line_len, line_start);

	for (int i = 0; i < lineno_len + 2; ++i)
		fputc(' ', stderr);

	fprintf(stderr, "| ");

	for (int i = 0; i < issue.loc.first_col - 1; ++i)
		fputc(line_start[i] == '\t' ? '\t' : ' ', stderr);

	for (int i = issue.loc.first_col; i < issue.loc.last_col; ++i) {
		if (i == issue.loc.first_col)
			fputc('^', stderr);
		else
			fputc('~', stderr);
	}

	fputc('\n', stderr);
}

void src_issue(struct src_issue issue, const char *err_msg, ...)
{
	va_list args;
	va_start(args, err_msg);
	_issue(issue, err_msg, args);
	va_end(args);
}

/* TODO: should really implement these better, the bad error messages are
 * starting to play a role in debugging */
void semantic_error(struct file_ctx fctx, struct ast_node *node,
                    const char *fmt, ...)
{
	va_list args;
	va_start(args, fmt);
	struct src_issue issue;
	issue.level = SRC_ERROR;
	issue.loc = node->loc;
	issue.fctx = fctx;
	_issue(issue, fmt, args);
	va_end(args);
}

void semantic_warn(struct file_ctx fctx, struct ast_node *node, const char *fmt,
                   ...)
{
	va_list args;
	va_start(args, fmt);
	struct src_issue issue;
	issue.level = SRC_WARN;
	issue.loc = node->loc;
	issue.fctx = fctx;
	_issue(issue, fmt, args);
	va_end(args);
}

void semantic_info(struct file_ctx fctx, struct ast_node *node, const char *fmt,
                   ...)
{
	va_list args;
	va_start(args, fmt);
	struct src_issue issue;
	issue.level = SRC_INFO;
	issue.loc = node->loc;
	issue.fctx = fctx;
	_issue(issue, fmt, args);
	va_end(args);
}

void internal_error(const char *fmt, ...)
{
	va_list args;
	va_start(args, fmt);
	fprintf(stderr, "internal error: ");
	vfprintf(stderr, fmt, args);
	fprintf(stderr, "\n");
	va_end(args);
}

static void _type_str(FILE *fp, struct ast_node *type)
{
	if (!type)
		return;

	assert(type->node_type == AST_TYPE);

	switch (type->_type.kind) {
	case AST_TYPE_POINTER:
		fputc('\'', fp);
		break;

	case AST_TYPE_ID: {
		struct ast_node *id = type->_type.id;
		fprintf(fp, "%s", id->_id.id);
		break;
	}

	case AST_TYPE_TEMPLATE: {
		struct ast_node *template = type->_type.template.template;
		assert(template);

		struct ast_node *template_id = template->_template.id;
		struct ast_node *template_act = type->_type.template.actual;
		if (template_act) {
			fprintf(fp, "%s as ", template_id->_id.id);
			_type_str(fp, template_act);
		}
		else {
			fprintf(fp, "%s", template_id->_id.id);
		}
		break;
	}

	case AST_TYPE_ALIAS: {
		struct ast_node *alias = type->_type.alias.alias;
		assert(alias);

		struct ast_node *alias_id = alias->_alias.id;
		struct ast_node *alias_act = type->_type.alias.actual;
		if (alias_act) {
			/* deeply nested aliases look pretty funny here */
			fprintf(fp, "%s aka ", alias_id->_id.id);
			_type_str(fp, alias_act);
		}
		else {
			fprintf(fp, "%s", alias_id->_id.id);
		}
		break;
	}

	case AST_TYPE_STRUCT: {
		struct ast_node *struc_id = type->_type.struc.struc;
		fprintf(fp, "%s", struc_id->_id.id);

		struct ast_node *impls = type->_type.struc.impls;
		if (impls) {
			fprintf(fp, " (");
			while (impls) {
				_type_str(fp, impls);
				impls = impls->next;
			}
			fprintf(fp, ")");
		}
	}
	}

	_type_str(fp, type->_type.next);
}

char *type_str(struct ast_node *node)
{
	/* maybe hacky? */
	if (!node)
		return strdup("void");

	char *buf = NULL; size_t size = 0;
	FILE *memstream = open_memstream(&buf, &size);

	/* TODO: improve template detection */
	/* we were given a plain type, pass it directly along to _type_str */
	if (node->node_type == AST_TYPE)
		_type_str(memstream, node);
	else
		/* otherwise, try to fish out the type of the node */
		_type_str(memstream, node->type);

	fclose(memstream);
	return buf;
}

static void _call_str(FILE *f, struct ast_node *call)
{
	struct ast_node *id = call->_call.id;
	const char *id_str = id->_id.id;
	fprintf(f, "%s", id_str);

	struct ast_node *args = call->_call.args;
	fprintf(f, "(");

	while (args) {
		char *type = type_str(args);
		fprintf(f, "%s", type);
		free(type);

		args = args->next;
		if (args)
			fprintf(f, ", ");
		else
			break;
	}

	fprintf(f, ")");
}

char *call_str(struct ast_node *call)
{
	assert(call->node_type == AST_CALL);
	char *buf = NULL; size_t size = 0;
	FILE *memstream = open_memstream(&buf, &size);
	_call_str(memstream, call);
	fclose(memstream);
	return buf;
}