Gwion coverage report


Directory: src/
File: src/gwion.c
Date: 2023-01-30 18:32:28
Exec Total Coverage
Lines: 171 186 91.9%
Functions: 21 25 84.0%
Branches: 43 58 74.1%

Line Branch Exec Source
1 #include <unistd.h>
2 #include "gwion_util.h"
3 #include "gwion_ast.h"
4 #include "gwion_env.h"
5 #include "vm.h"
6 #include "instr.h"
7 #include "emit.h"
8 #include "gwion.h"
9 #include "plug.h"
10 #include "driver.h"
11 #include "engine.h"
12 #include "arg.h"
13 #include "compile.h"
14 #include "object.h"
15 #include "pass.h"
16 #include "shreduler_private.h"
17 #include "ugen.h"
18
19 707 ANN m_bool gwion_audio(const Gwion gwion) {
20 707 Driver *const di = gwion->vm->bbq;
21
3/4
✓ Branch 0 taken 69 times.
✓ Branch 1 taken 638 times.
✓ Branch 3 taken 69 times.
✗ Branch 4 not taken.
707 if (di->si->arg) CHECK_BB(driver_ini(gwion));
22 638 di->func(di->driver);
23
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 638 times.
638 CHECK_BB(di->driver->ini(gwion->vm, di));
24 638 driver_alloc(di);
25 638 return GW_OK;
26 }
27
28 638 ANN static VM_Shred gwion_cleaner(const Gwion gwion) {
29 const VM_Code code =
30 638 new_vmcode(gwion->mp, NULL, NULL, "in code dtor", 0, true, false);
31 638 const VM_Shred shred = new_vm_shred(gwion->mp, code);
32 638 vm_ini_shred(gwion->vm, shred);
33 638 return shred;
34 }
35
36 5 ANN void gwion_cpy_blackhole(const Gwion gwion) {
37 5 dummy_driver(gwion->vm->bbq->driver);
38 5 const M_Object o = new_M_UGen(gwion);
39 5 const UGen u = UGEN(o);
40 5 ugen_ini(gwion, u, 1, 1);
41 5 ugen_gen(gwion, u, compute_mono, o, 0);
42 5 vector_add(&gwion->vm->ugen, (vtype)u);
43 5 }
44
45 5 ANN VM *gwion_cpy(const VM *src) {
46 5 const Gwion gwion = mp_calloc(src->gwion->mp, Gwion);
47 5 gwion->vm = new_vm(src->gwion->mp, true);
48 5 gwion->vm->gwion = gwion;
49 5 gwion->vm->bbq->si = soundinfo_cpy(src->gwion->mp, src->bbq->si);
50 5 gwion->emit = src->gwion->emit;
51 5 gwion->env = src->gwion->env;
52 5 gwion->data = cpy_gwiondata(src->gwion->mp, src->gwion->data);
53 5 gwion->st = src->gwion->st;
54 5 gwion->mp = src->gwion->mp;
55 5 gwion->type = src->gwion->type;
56 5 gwion_cpy_blackhole(gwion);
57 5 return gwion->vm;
58 }
59
60 707 ANN static void gwion_core(const Gwion gwion) {
61 707 gwion->vm = new_vm(gwion->mp, true);
62 707 gwion->emit = new_emitter(gwion->mp);
63 707 gwion->env = new_env(gwion->mp);
64 707 gwion->emit->env = gwion->env;
65 707 gwion->vm->gwion = gwion->emit->gwion = gwion->env->gwion = gwion;
66 707 }
67
68
69 638 ANN static Func gwion_locale(const Gwion gwion) {
70 638 const Nspc nspc = gwion->env->curr;
71 638 const Symbol sym = insert_symbol(gwion->st, "BasicLocale");
72 638 const Value v = nspc_lookup_value1(nspc, sym);
73 638 return v->d.func_ref;
74 }
75
76 707 ANN static m_bool gwion_ok(const Gwion gwion, CliArg *arg) {
77
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 707 times.
707 CHECK_BB(plug_ini(gwion, &arg->lib));
78 707 shreduler_set_loop(gwion->vm->shreduler, arg->loop);
79
2/2
✓ Branch 1 taken 638 times.
✓ Branch 2 taken 69 times.
707 if (gwion_audio(gwion) > 0) {
80
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 638 times.
638 CHECK_BB(plug_run(gwion, &arg->mod));
81
1/2
✓ Branch 1 taken 638 times.
✗ Branch 2 not taken.
638 if (type_engine_init(gwion)) {
82 638 vector_add(&gwion->data->plugs->vec, (m_uint)gwion->env->global_nspc);
83 638 gwion->vm->cleaner_shred = gwion_cleaner(gwion);
84 638 gwion->emit->locale = gwion_locale(gwion);
85 638 return GW_OK;
86 }
87 }
88 69 return GW_ERROR;
89 }
90
91 #define LOCALE_INFO INSTALL_PREFIX "/share"
92
93 ANN static void doc_mode(const Gwion gwion) {
94 struct Vector_ v;
95 vector_init(&v);
96 vector_add(&v, (m_uint) "scan0");
97 pass_set(gwion, &v);
98 vector_release(&v);
99 }
100
101 707 ANN m_bool gwion_ini(const Gwion gwion, CliArg *arg) {
102 #ifdef USE_GETTEXT
103 setlocale(LC_ALL, NULL);
104 bindtextdomain(GWION_PACKAGE, LOCALE_INFO);
105 bindtextdomain(GWION_PACKAGE "_util", LOCALE_INFO);
106 bindtextdomain(GWION_PACKAGE "_ast", LOCALE_INFO);
107 #endif
108 707 gwion->mp = mempool_ini((sizeof(struct VM_Shred_) + SIZEOF_REG + SIZEOF_MEM));
109 707 gwion->st = new_symbol_table(gwion->mp, 65347);
110 707 gwion->ppa = mp_calloc(gwion->mp, PPArg);
111 707 pparg_ini(gwion->mp, gwion->ppa);
112 707 gwion_core(gwion);
113 707 gwion->data = new_gwiondata(gwion->mp);
114 707 gwion->type = (Type *)xcalloc(MAX_TYPE, sizeof(struct Type_ *));
115 707 arg->si = gwion->vm->bbq->si = new_soundinfo(gwion->mp);
116 707 new_passes(gwion);
117
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 707 times.
707 CHECK_BB(arg_parse(gwion, arg));
118
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 707 times.
707 if (arg->color == COLOR_NEVER)
119 tcol_override_color_checks(0);
120
1/2
✓ Branch 0 taken 707 times.
✗ Branch 1 not taken.
707 else if (arg->color == COLOR_AUTO)
121 707 tcol_override_color_checks(isatty(1));
122 else if (arg->color == COLOR_ALWAYS)
123 tcol_override_color_checks(1);
124
2/2
✓ Branch 1 taken 705 times.
✓ Branch 2 taken 2 times.
707 if(!vector_size(&gwion->data->passes->vec)) {
125
1/2
✓ Branch 0 taken 705 times.
✗ Branch 1 not taken.
705 if (!gwion->data->cdoc)
126 705 pass_default(gwion);
127 else doc_mode(gwion);
128 }
129
1/2
✓ Branch 0 taken 707 times.
✗ Branch 1 not taken.
707 return !arg->quit ? gwion_ok(gwion, arg) : GW_ERROR;
130 }
131
132 638 ANN void gwion_run(const Gwion gwion) {
133 638 VM *vm = gwion->vm;
134 638 vm->bbq->driver->run(vm, vm->bbq);
135 638 }
136
137 1 ANN static inline void free_gwion_cpy(const Gwion gwion, const VM_Shred shred) {
138 1 gwion_end_child(shred, gwion);
139 1 const UGen u = (UGen)vector_front(&gwion->vm->ugen);
140 1 M_Object blackhole = u->module.gen.data;
141 1 release(blackhole, shred);
142 1 free_vm(gwion->vm);
143 1 free_gwiondata_cpy(gwion->mp, gwion->data);
144 1 mp_free(gwion->mp, Gwion, gwion);
145 1 }
146
147 1 ANN static void fork_clean2(const VM_Shred shred, const Vector v) {
148
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
2 for (m_uint i = 0; i < vector_size(v); ++i) {
149 1 const Gwion gwion = (Gwion)vector_at(v, i);
150 1 free_gwion_cpy(gwion, shred);
151 }
152 1 vector_release(v);
153 1 v->ptr = NULL;
154 1 }
155
156 713 ANN void gwion_end_child(const VM_Shred shred, const Gwion gwion) {
157
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 708 times.
713 if (gwion->data->child.ptr) fork_clean(shred, &gwion->data->child);
158
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 712 times.
713 if (gwion->data->child2.ptr) fork_clean2(shred, &gwion->data->child2);
159 713 }
160
161 707 ANN void gwion_end(const Gwion gwion) {
162 707 VM *vm = gwion->vm;
163 707 vm_clean(vm, gwion);
164 707 release_ctx(gwion->env->scope, gwion);
165
1/2
✓ Branch 0 taken 707 times.
✗ Branch 1 not taken.
707 if (gwion->data->plugs) free_plug(gwion);
166 707 free_env(gwion->env);
167 707 free_emitter(gwion->mp, gwion->emit);
168 707 free_vm(vm);
169 707 pparg_end(gwion->ppa);
170 707 mp_free(gwion->mp, PPArg, gwion->ppa);
171 707 free_gwiondata(gwion);
172 707 free_symbols(gwion->st);
173 707 xfree(gwion->type);
174 707 mempool_end(gwion->mp);
175 707 }
176
177 274 ANN void env_error_footer(const Env env) {
178 274 bool ctor = false;
179
3/4
✓ Branch 0 taken 58 times.
✓ Branch 1 taken 216 times.
✓ Branch 2 taken 58 times.
✗ Branch 3 not taken.
274 if (env->func && env->func->def) {
180
2/2
✓ Branch 1 taken 38 times.
✓ Branch 2 taken 20 times.
58 if(!is_ctor(env->func->def))
181 38 gwerr_secondary("in function", env->name, env->func->def->base->pos);
182 else {
183 20 gwerr_secondary("in class pre constructor", env->name, env->class_def->info->cdef->pos);
184 20 ctor = true;
185 }
186 }
187
6/6
✓ Branch 0 taken 254 times.
✓ Branch 1 taken 20 times.
✓ Branch 2 taken 27 times.
✓ Branch 3 taken 227 times.
✓ Branch 5 taken 3 times.
✓ Branch 6 taken 24 times.
274 if (!ctor && env->class_def && tflag(env->class_def, tflag_cdef))
188 3 gwerr_secondary("in class", env->name, env->class_def->info->cdef->pos);
189 274 }
190
191 266 ANN static void env_xxx(const Env env, const loc_t pos, const m_str fmt,
192 266 va_list arg) {
193 #ifndef __FUZZING__
194 va_list tmpa;
195 266 va_copy(tmpa, arg);
196 266 const int size = vsnprintf(NULL, 0, fmt, tmpa);
197 266 va_end(tmpa);
198 266 char c[size + 1];
199 266 vsprintf(c, fmt, arg);
200 266 gwerr_basic(c, NULL, NULL, env->name, pos, 0);
201 266 env_error_footer(env);
202 #endif
203 266 }
204
205 3 ANN static void _env_warn(const Env env, const loc_t pos, const m_str fmt,
206 3 va_list arg) {
207 #ifndef __FUZZING__
208 va_list tmpa;
209 3 va_copy(tmpa, arg);
210 3 const int size = vsnprintf(NULL, 0, fmt, tmpa);
211 3 va_end(tmpa);
212 3 char c[size + 1];
213 3 vsprintf(c, fmt, arg);
214 3 gwerr_warn(c, NULL, NULL, env->name, pos);
215 3 env_error_footer(env);
216 #endif
217 3 }
218
219 3 ANN void env_warn(const Env env, const loc_t pos, const m_str fmt, ...) {
220 #ifndef __FUZZING__
221 va_list arg;
222 3 va_start(arg, fmt);
223 3 _env_warn(env, pos, fmt, arg);
224 3 va_end(arg);
225 #endif
226 3 }
227
228 303 ANN void env_err(const Env env, const loc_t pos, const m_str fmt, ...) {
229
3/4
✓ Branch 0 taken 303 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 37 times.
✓ Branch 3 taken 266 times.
303 if (env->context && env->context->error) return;
230 #ifndef __FUZZING__
231 va_list arg;
232 266 va_start(arg, fmt);
233 266 env_xxx(env, pos, fmt, arg);
234 266 va_end(arg);
235 #endif
236 266 env_set_error(env, true);
237 }
238
239 3008 ANN struct SpecialId_ *specialid_get(const Gwion gwion, const Symbol sym) {
240 3008 const Map map = &gwion->data->id;
241
2/2
✓ Branch 1 taken 26319 times.
✓ Branch 2 taken 2354 times.
28673 for (m_uint i = 0; i < map_size(map); ++i) {
242
2/2
✓ Branch 0 taken 654 times.
✓ Branch 1 taken 25665 times.
26319 if (sym == (Symbol)VKEY(map, i)) return (struct SpecialId_ *)VVAL(map, i);
243 }
244 2354 return NULL;
245 }
246
247 638 ANN void push_global(struct Gwion_ *gwion, const m_str name) {
248 638 const Nspc nspc = new_nspc(gwion->mp, name);
249 638 nspc->parent = gwion->env->global_nspc;
250 638 gwion->env->curr = gwion->env->global_nspc = nspc;
251 638 }
252
253 707 ANN void pop_global(const Gwion gwion) {
254 707 Nspc nspc = gwion->env->global_nspc->parent;
255
2/2
✓ Branch 0 taken 685 times.
✓ Branch 1 taken 707 times.
1392 while (nspc) {
256 685 const Nspc parent = nspc->parent;
257 685 nspc_remref(nspc, gwion);
258 685 nspc = parent;
259 }
260 707 }
261
262 ANN void gwion_set_debug(const Gwion gwion, const bool dbg) {
263 gwion->emit->info->debug = dbg;
264 }
265 ANN void gwion_set_dump(const Gwion gwion, const bool dump) {
266 gwion->emit->info->dump = dump;
267 }
268 ANN void gwion_set_cdoc(const Gwion gwion, const bool cdoc) {
269 gwion->data->cdoc = cdoc;
270 }
271