Gwion coverage report


Directory: src/
File: src/clean.c
Date: 2023-01-30 18:32:28
Exec Total Coverage
Lines: 254 266 95.5%
Functions: 51 56 91.1%
Branches: 109 122 89.3%

Line Branch Exec Source
1 #include "gwion_util.h"
2 #include "gwion_ast.h"
3 #include "gwion_env.h"
4 #include "vm.h"
5 #include "gwion.h"
6 #define __CLEAN_IMPL
7 #include "clean.h"
8
9 8768 ANN static void clean_array_sub(Clean *a, Array_Sub b) {
10
2/2
✓ Branch 0 taken 87 times.
✓ Branch 1 taken 8681 times.
8768 if (b->exp) clean_exp(a, b->exp);
11 8768 }
12
13 #define clean_id_list(a, b) do {} while(0)
14 #define clean_specialized_list(a, b) do {} while(0)
15
16 18198 ANN static void clean_type_list(Clean *a, Type_List b) {
17
2/2
✓ Branch 0 taken 18228 times.
✓ Branch 1 taken 18198 times.
36426 for(uint32_t i = 0; i < b->len; i++) {
18 18228 Type_Decl *td = *mp_vector_at(b, Type_Decl*, i);
19 18228 clean_type_decl(a, td);
20 }
21 18198 }
22
23 28118 ANN static void clean_tmpl(Clean *a, Tmpl *b) {
24
2/2
✓ Branch 0 taken 1486 times.
✓ Branch 1 taken 26632 times.
28118 if (!b->call) clean_specialized_list(a, b->list);
25 1486 else clean_type_list(a, b->call);
26 28118 }
27
28 12 ANN static void clean_range(Clean *a, Range *b) {
29
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 1 times.
12 if (b->start) clean_exp(a, b->start);
30
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 4 times.
12 if (b->end) clean_exp(a, b->end);
31 12 }
32
33 124252 ANN static void clean_type_decl(Clean *a, Type_Decl *b) {
34
2/2
✓ Branch 0 taken 8711 times.
✓ Branch 1 taken 115541 times.
124252 if (b->array) clean_array_sub(a, b->array);
35
2/2
✓ Branch 0 taken 16712 times.
✓ Branch 1 taken 107540 times.
124252 if (b->types) clean_type_list(a, b->types);
36
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 124252 times.
124252 if (b->fptr) clean_fptr_def(a, b->fptr);
37
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 124236 times.
124252 if (b->next) clean_type_decl(a, b->next);
38 124252 }
39
40 4562 ANN static void clean_prim(Clean *a, Exp_Primary *b) {
41
6/6
✓ Branch 0 taken 3497 times.
✓ Branch 1 taken 1065 times.
✓ Branch 2 taken 3494 times.
✓ Branch 3 taken 3 times.
✓ Branch 4 taken 24 times.
✓ Branch 5 taken 3470 times.
4562 if (b->prim_type == ae_prim_hack || b->prim_type == ae_prim_dict || b->prim_type == ae_prim_interp)
42 1092 clean_exp(a, b->d.exp);
43
2/2
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 3445 times.
3470 else if (b->prim_type == ae_prim_array)
44 25 clean_array_sub(a, b->d.array);
45
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3442 times.
3445 else if (b->prim_type == ae_prim_range)
46 3 clean_range(a, b->d.range);
47 4562 }
48
49 53559 ANN static void clean_var_decl(Clean *a, Var_Decl *b) {
50
4/4
✓ Branch 0 taken 383 times.
✓ Branch 1 taken 53176 times.
✓ Branch 2 taken 358 times.
✓ Branch 3 taken 25 times.
53559 if (a->scope && b->value) value_remref(b->value, a->gwion);
51 53559 }
52
53 1400 ANN static void clean_exp_decl(Clean *a, Exp_Decl *b) {
54
1/2
✓ Branch 0 taken 1400 times.
✗ Branch 1 not taken.
1400 if (b->td) clean_type_decl(a, b->td);
55 1400 clean_var_decl(a, &b->vd);
56 1400 }
57
58 623 ANN static void clean_exp_binary(Clean *a, Exp_Binary *b) {
59 623 clean_exp(a, b->lhs);
60 623 clean_exp(a, b->rhs);
61 623 }
62
63 2 ANN static void clean_captures(Clean *a, Capture_List b) {
64
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 for(uint32_t i = 0; i < b->len; i++) {
65 2 const Capture *cap = mp_vector_at(b, Capture, i);
66
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if(cap->temp) value_remref(cap->temp, a->gwion);
67 }
68 2 }
69
70 124 ANN static void clean_exp_unary(Clean *a, Exp_Unary *b) {
71
3/4
✓ Branch 0 taken 66 times.
✓ Branch 1 taken 30 times.
✓ Branch 2 taken 28 times.
✗ Branch 3 not taken.
124 switch (b->unary_type) {
72 66 case unary_exp:
73 66 clean_exp(a, b->exp);
74 66 break;
75 30 case unary_td:
76 30 clean_type_decl(a, b->ctor.td);
77
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 20 times.
30 if(b->ctor.exp)
78 10 clean_exp(a, b->ctor.exp);
79 30 break;
80 28 case unary_code:
81 28 clean_stmt_list(a, b->code);
82 28 break;
83 }
84
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 122 times.
124 if(b->captures) clean_captures(a, b->captures);
85 124 }
86
87 20 ANN static void clean_exp_cast(Clean *a, Exp_Cast *b) {
88 20 clean_type_decl(a, b->td);
89 20 clean_exp(a, b->exp);
90 20 }
91
92 39 ANN static void clean_exp_post(Clean *a, Exp_Postfix *b) {
93 39 clean_exp(a, b->exp);
94 39 }
95
96 605 ANN static void clean_exp_call(Clean *a, Exp_Call *b) {
97 605 clean_exp(a, b->func);
98
2/2
✓ Branch 0 taken 370 times.
✓ Branch 1 taken 235 times.
605 if (b->args) clean_exp(a, b->args);
99
2/2
✓ Branch 0 taken 80 times.
✓ Branch 1 taken 525 times.
605 if (b->tmpl) clean_tmpl(a, b->tmpl);
100 605 }
101
102 32 ANN static void clean_exp_array(Clean *a, Exp_Array *b) {
103 32 clean_exp(a, b->base);
104 32 clean_array_sub(a, b->array);
105 32 }
106
107 9 ANN static void clean_exp_slice(Clean *a, Exp_Slice *b) {
108 9 clean_exp(a, b->base);
109 9 clean_range(a, b->range);
110 9 }
111
112 9 ANN static void clean_exp_if(Clean *a, Exp_If *b) {
113 9 clean_exp(a, b->cond);
114
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 if (b->if_exp) clean_exp(a, b->if_exp);
115 9 clean_exp(a, b->else_exp);
116 9 }
117
118 636 ANN static void clean_exp_dot(Clean *a, Exp_Dot *b) { clean_exp(a, b->base); }
119
120 28 ANN static void clean_exp_lambda(Clean *a, Exp_Lambda *b) {
121 28 clean_func_def(a, b->def);
122 28 }
123
124 ANN static void clean_exp_td(Clean *a, Type_Decl **b) {
125 clean_type_decl(a, *b);
126 }
127
128 DECL_EXP_FUNC(clean, void, Clean *)
129 8087 ANN static void clean_exp(Clean *a, Exp b) {
130 8087 clean_exp_func[b->exp_type](a, &b->d);
131
2/2
✓ Branch 0 taken 529 times.
✓ Branch 1 taken 7558 times.
8087 if (b->next) clean_exp(a, b->next);
132 8087 }
133
134 3095 ANN static void clean_stmt_exp(Clean *a, Stmt_Exp b) {
135
2/2
✓ Branch 0 taken 3080 times.
✓ Branch 1 taken 15 times.
3095 if (b->val) clean_exp(a, b->val);
136 3095 }
137
138 35 ANN static void clean_stmt_flow(Clean *a, Stmt_Flow b) {
139 35 ++a->scope;
140 35 clean_exp(a, b->cond);
141 35 clean_stmt(a, b->body);
142 35 --a->scope;
143 35 }
144
145 #define clean_stmt_while clean_stmt_flow
146 #define clean_stmt_until clean_stmt_flow
147
148 8 ANN static void clean_stmt_for(Clean *a, Stmt_For b) {
149 8 ++a->scope;
150 8 clean_stmt(a, b->c1);
151
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 if (b->c2) clean_stmt(a, b->c2);
152
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 2 times.
8 if (b->c3) clean_exp(a, b->c3);
153 8 clean_stmt(a, b->body);
154 8 --a->scope;
155 8 }
156
157 1 ANN static void clean_idx(Clean *a, struct EachIdx_ *b) {
158
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (b->v) value_remref(b->v, a->gwion);
159 1 }
160
161 10 ANN static void clean_stmt_each(Clean *a, Stmt_Each b) {
162 10 ++a->scope;
163 10 clean_exp(a, b->exp);
164 10 clean_stmt(a, b->body);
165
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 3 times.
10 if (b->v) mp_free(a->gwion->mp, Value, b->v);
166
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 9 times.
10 if (b->idx) clean_idx(a, b->idx);
167 10 --a->scope;
168 10 }
169
170 10 ANN static void clean_stmt_loop(Clean *a, Stmt_Loop b) {
171 10 ++a->scope;
172 10 clean_exp(a, b->cond);
173 10 clean_stmt(a, b->body);
174 10 --a->scope;
175 10 }
176
177 45 ANN static void clean_stmt_if(Clean *a, Stmt_If b) {
178 45 ++a->scope;
179 45 clean_exp(a, b->cond);
180 45 clean_stmt(a, b->if_body);
181
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 37 times.
45 if (b->else_body) clean_stmt(a, b->else_body);
182 45 --a->scope;
183 45 }
184
185 142 ANN static void clean_stmt_code(Clean *a, Stmt_Code b) {
186 142 ++a->scope;
187
2/2
✓ Branch 0 taken 140 times.
✓ Branch 1 taken 2 times.
142 if (b->stmt_list) clean_stmt_list(a, b->stmt_list);
188 142 --a->scope;
189 142 }
190
191 98 ANN static void clean_stmt_return(Clean *a, Stmt_Exp b) {
192
2/2
✓ Branch 0 taken 94 times.
✓ Branch 1 taken 4 times.
98 if (b->val) clean_exp(a, b->val);
193 98 }
194
195 7 ANN static void clean_case_list(Clean *a, Stmt_List b) {
196
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 7 times.
18 for(m_uint i = 0; i < b->len; i++) {
197 11 const Stmt stmt = mp_vector_at(b, struct Stmt_, i);
198 11 clean_stmt_case(a, &stmt->d.stmt_match);
199 }
200 7 }
201
202 3 ANN static void clean_handler_list(Clean *a, Handler_List b) {
203 3 ++a->scope;
204
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
6 for(uint32_t i = 0; i < b->len; i++) {
205 3 Handler *handler = mp_vector_at(b, Handler, i);
206 3 clean_stmt(a, handler->stmt);
207 }
208 3 --a->scope;
209 3 }
210
211 3 ANN static void clean_stmt_try(Clean *a, Stmt_Try b) {
212 3 ++a->scope;
213 3 clean_stmt(a, b->stmt);
214 3 --a->scope;
215 3 clean_handler_list(a, b->handler);
216 3 }
217
218 7 ANN static void clean_stmt_match(Clean *a, Stmt_Match b) {
219 7 ++a->scope;
220 7 clean_exp(a, b->cond);
221 7 clean_case_list(a, b->list);
222
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 6 times.
7 if (b->where) clean_stmt(a, b->where);
223 7 --a->scope;
224 7 }
225
226 11 ANN static void clean_stmt_case(Clean *a, Stmt_Match b) {
227 11 ++a->scope;
228 11 clean_exp(a, b->cond);
229 11 clean_stmt_list(a, b->list);
230
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 9 times.
11 if (b->when) clean_exp(a, b->when);
231 11 --a->scope;
232 11 }
233
234 ANN static void clean_stmt_defer(Clean *a, Stmt_Defer b) {
235 clean_stmt(a, b->stmt);
236 }
237
238 85 ANN static void clean_dummy(Clean *a NUSED, void *b NUSED) {}
239 #define clean_stmt_jump clean_dummy
240 #define clean_stmt_pp clean_dummy
241 #define clean_stmt_break clean_dummy
242 #define clean_stmt_continue clean_dummy
243 #define clean_stmt_retry clean_dummy
244 #define clean_stmt_spread clean_dummy
245
246 DECL_STMT_FUNC(clean, void, Clean *)
247 3538 ANN static void clean_stmt(Clean *a, Stmt b) {
248 3538 clean_stmt_func[b->stmt_type](a, &b->d);
249 3538 }
250
251 38783 ANN static void clean_arg_list(Clean *a, Arg_List b) {
252
2/2
✓ Branch 0 taken 52151 times.
✓ Branch 1 taken 38783 times.
90934 for(uint32_t i = 0; i < b->len; i++) {
253 52151 Arg *arg = mp_vector_at(b, Arg, i);
254
2/2
✓ Branch 0 taken 52131 times.
✓ Branch 1 taken 20 times.
52151 if (arg->td) clean_type_decl(a, arg->td);
255 52151 clean_var_decl(a, &arg->var_decl);
256 }
257 38783 }
258
259 2040 ANN static void clean_stmt_list(Clean *a, Stmt_List b) {
260
2/2
✓ Branch 0 taken 3399 times.
✓ Branch 1 taken 2040 times.
5439 for(m_uint i = 0; i < b->len; i++) {
261 3399 const Stmt stmt = mp_vector_at(b, struct Stmt_, i);
262 3399 clean_stmt(a, stmt);
263 }
264 2040 }
265
266 49120 ANN static void clean_func_base(Clean *a, Func_Base *b) {
267
2/2
✓ Branch 0 taken 49078 times.
✓ Branch 1 taken 42 times.
49120 if (b->td) clean_type_decl(a, b->td);
268
2/2
✓ Branch 0 taken 38783 times.
✓ Branch 1 taken 10337 times.
49120 if (b->args) clean_arg_list(a, b->args);
269
2/2
✓ Branch 0 taken 24099 times.
✓ Branch 1 taken 25021 times.
49120 if (b->tmpl) clean_tmpl(a, b->tmpl);
270 49120 }
271
272 39140 ANN static void clean_func_def(Clean *a, Func_Def b) {
273 39140 clean_func_base(a, b->base);
274
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 39140 times.
39140 if(b->captures) clean_captures(a, b->captures);
275 39140 ++a->scope;
276
4/4
✓ Branch 0 taken 6021 times.
✓ Branch 1 taken 33119 times.
✓ Branch 2 taken 1968 times.
✓ Branch 3 taken 4053 times.
39140 if (!b->builtin && b->d.code &&
277
4/4
✓ Branch 0 taken 1888 times.
✓ Branch 1 taken 80 times.
✓ Branch 3 taken 541 times.
✓ Branch 4 taken 1347 times.
1968 !(b->base->func && safe_vflag(b->base->func->value_ref, vflag_builtin)))
278 621 clean_stmt_list(a, b->d.code);
279 else
280 38519 b->d.code = NULL;
281 39140 --a->scope;
282 39140 }
283
284 10147 ANN void func_def_cleaner(const Gwion gwion, Func_Def b) {
285 10147 Clean a = {.gwion = gwion};
286 10147 clean_func_def(&a, b);
287 10147 free_func_def(gwion->mp, b);
288 10147 }
289
290 ANN static void clean_extend_def(Clean *a, Extend_Def b) {
291 clean_type_decl(a, b->td);
292 }
293
294 4199 ANN static void clean_class_def(Clean *a, Class_Def b) {
295 4199 clean_type_def(a, &b->base);
296 4199 if (b->traits) clean_id_list(a, b->traits);
297
2/2
✓ Branch 0 taken 3501 times.
✓ Branch 1 taken 698 times.
4199 if (b->body) clean_ast(a, b->body);
298 4199 }
299
300 3914 ANN void class_def_cleaner(const Gwion gwion, Class_Def b) {
301 3914 Clean a = {.gwion = gwion};
302 3914 clean_class_def(&a, b);
303 3914 free_class_def(gwion->mp, b);
304 3914 }
305
306 8 ANN static void clean_enum_def(Clean *a NUSED, Enum_Def b) {
307 clean_id_list(a, b->list);
308
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 if (b->values.ptr) vector_release(&b->values);
309 8 }
310
311 6 ANN static void clean_union_list(Clean *a, Union_List b) {
312
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 6 times.
14 for(uint32_t i = 0; i < b->len; i++) {
313 8 Union_Member *tgt = mp_vector_at(b, Union_Member, i);
314 8 clean_type_decl(a, tgt->td);
315 8 clean_var_decl(a, &tgt->vd);
316 }
317 6 }
318
319 6 ANN static void clean_union_def(Clean *a, Union_Def b) {
320 6 clean_union_list(a, b->l);
321
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 5 times.
6 if (b->tmpl) clean_tmpl(a, b->tmpl);
322 6 }
323
324 9980 ANN static void clean_fptr_def(Clean *a, Fptr_Def b) {
325 9980 clean_func_base(a, b->base);
326 // if (b->type) type_remref(b->type, a->gwion);
327 9980 }
328
329 4220 ANN static void clean_type_def(Clean *a, Type_Def b) {
330
2/2
✓ Branch 0 taken 3341 times.
✓ Branch 1 taken 879 times.
4220 if (b->ext) clean_type_decl(a, b->ext);
331
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4220 times.
4220 if (b->when) {
332 clean_exp(a, b->when);
333 if (b->when_def) clean_func_def(a, b->when_def);
334 }
335
2/2
✓ Branch 0 taken 3938 times.
✓ Branch 1 taken 282 times.
4220 if (b->tmpl) clean_tmpl(a, b->tmpl);
336 4220 }
337
338 ANN static void clean_trait_def(Clean *a, Trait_Def b) {
339 if (b->traits) clean_id_list(a, b->traits);
340 if (b->body) clean_ast(a, b->body);
341 }
342 ANN static void clean_prim_def(Clean *a NUSED, Prim_Def b NUSED) {}
343
344 DECL_SECTION_FUNC(clean, void, Clean *);
345
346 40505 ANN static inline void clean_section(Clean *a, Section *b) {
347 40505 clean_section_func[b->section_type](a, *(void **)&b->d);
348 40505 }
349
350 4142 ANN static void clean_ast(Clean *a, Ast b) {
351
2/2
✓ Branch 0 taken 40505 times.
✓ Branch 1 taken 4142 times.
44647 for(m_uint i = 0; i < b->len; i++) {
352 40505 Section *section = mp_vector_at(b, Section, i);
353 40505 clean_section(a, section);
354 }
355 4142 }
356
357 641 ANN void ast_cleaner(const Gwion gwion, Ast b) {
358 641 Clean a = {.gwion = gwion};
359 641 clean_ast(&a, b);
360 641 free_ast(gwion->mp, b);
361 641 }
362