aboutsummaryrefslogtreecommitdiff
path: root/examples/dte/compiler.c
diff options
context:
space:
mode:
Diffstat (limited to 'examples/dte/compiler.c')
-rw-r--r--examples/dte/compiler.c151
1 files changed, 0 insertions, 151 deletions
diff --git a/examples/dte/compiler.c b/examples/dte/compiler.c
deleted file mode 100644
index b1a0eaa..0000000
--- a/examples/dte/compiler.c
+++ /dev/null
@@ -1,151 +0,0 @@
1#include <stdlib.h>
2#include <string.h>
3#include "compiler.h"
4#include "command/serialize.h"
5#include "error.h"
6#include "regexp.h"
7#include "util/array.h"
8#include "util/debug.h"
9#include "util/intern.h"
10#include "util/str-util.h"
11#include "util/xmalloc.h"
12
13static const char capture_names[][8] = {
14 [ERRFMT_FILE] = "file",
15 [ERRFMT_LINE] = "line",
16 [ERRFMT_COLUMN] = "column",
17 [ERRFMT_MESSAGE] = "message"
18};
19
20UNITTEST {
21 CHECK_STRING_ARRAY(capture_names);
22}
23
24static Compiler *find_or_add_compiler(HashMap *compilers, const char *name)
25{
26 Compiler *c = find_compiler(compilers, name);
27 return c ? c : hashmap_insert(compilers, xstrdup(name), xnew0(Compiler, 1));
28}
29
30Compiler *find_compiler(const HashMap *compilers, const char *name)
31{
32 return hashmap_get(compilers, name);
33}
34
35bool add_error_fmt (
36 HashMap *compilers,
37 const char *name,
38 bool ignore,
39 const char *format,
40 char **desc
41) {
42 int8_t idx[] = {
43 [ERRFMT_FILE] = -1,
44 [ERRFMT_LINE] = -1,
45 [ERRFMT_COLUMN] = -1,
46 [ERRFMT_MESSAGE] = 0,
47 };
48
49 size_t max_idx = 0;
50 for (size_t i = 0, j = 0, n = ARRAYLEN(capture_names); desc[i]; i++) {
51 BUG_ON(i >= ERRORFMT_CAPTURE_MAX);
52 if (streq(desc[i], "_")) {
53 continue;
54 }
55 for (j = 0; j < n; j++) {
56 if (streq(desc[i], capture_names[j])) {
57 max_idx = i + 1;
58 idx[j] = max_idx;
59 break;
60 }
61 }
62 if (unlikely(j == n)) {
63 return error_msg("unknown substring name %s", desc[i]);
64 }
65 }
66
67 ErrorFormat *f = xnew(ErrorFormat, 1);
68 f->ignore = ignore;
69 static_assert_compatible_types(f->capture_index, idx);
70 memcpy(f->capture_index, idx, sizeof(idx));
71
72 if (unlikely(!regexp_compile(&f->re, format, 0))) {
73 free(f);
74 return false;
75 }
76
77 if (unlikely(max_idx > f->re.re_nsub)) {
78 regfree(&f->re);
79 free(f);
80 return error_msg("invalid substring count");
81 }
82
83 Compiler *compiler = find_or_add_compiler(compilers, name);
84 f->pattern = str_intern(format);
85 ptr_array_append(&compiler->error_formats, f);
86 return true;
87}
88
89static void free_error_format(ErrorFormat *f)
90{
91 regfree(&f->re);
92 free(f);
93}
94
95void free_compiler(Compiler *c)
96{
97 ptr_array_free_cb(&c->error_formats, FREE_FUNC(free_error_format));
98 free(c);
99}
100
101void remove_compiler(HashMap *compilers, const char *name)
102{
103 Compiler *c = hashmap_remove(compilers, name);
104 if (c) {
105 free_compiler(c);
106 }
107}
108
109void collect_errorfmt_capture_names(PointerArray *a, const char *prefix)
110{
111 COLLECT_STRINGS(capture_names, a, prefix);
112 if (str_has_prefix("_", prefix)) {
113 ptr_array_append(a, xstrdup("_"));
114 }
115}
116
117void dump_compiler(const Compiler *c, const char *name, String *s)
118{
119 for (size_t i = 0, n = c->error_formats.count; i < n; i++) {
120 ErrorFormat *e = c->error_formats.ptrs[i];
121 string_append_literal(s, "errorfmt ");
122 if (e->ignore) {
123 string_append_literal(s, "-i ");
124 }
125 if (unlikely(name[0] == '-' || e->pattern[0] == '-')) {
126 string_append_literal(s, "-- ");
127 }
128 string_append_escaped_arg(s, name, true);
129 string_append_byte(s, ' ');
130 string_append_escaped_arg(s, e->pattern, true);
131
132 static_assert(ARRAYLEN(e->capture_index) == 4);
133 const int8_t *a = e->capture_index;
134 int max_idx = MAX4(a[0], a[1], a[2], a[3]);
135 BUG_ON(max_idx > ERRORFMT_CAPTURE_MAX);
136
137 for (int j = 1; j <= max_idx; j++) {
138 const char *capname = "_";
139 for (size_t k = 0; k < ARRAYLEN(capture_names); k++) {
140 if (j == a[k]) {
141 capname = capture_names[k];
142 break;
143 }
144 }
145 string_append_byte(s, ' ');
146 string_append_cstring(s, capname);
147 }
148
149 string_append_byte(s, '\n');
150 }
151}