Plain.xs

filter: Statementstmt 84.6% 149 / 176 Branchbran 64.0% 73 / 114 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 76.5% 222 / 290 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/Switch/Plain.pm ↑ Summary
1 /*
2 Copyright 2012, 2014, 2016 Lukas Mai.
3  
4 This program is free software; you can redistribute it and/or modify it
5 under the terms of either: the GNU General Public License as published
6 by the Free Software Foundation; or the Artistic License.
7  
8 See http://dev.perl.org/licenses/ for more information.
9 */
10  
11 #ifdef __GNUC__
12 #if __GNUC__ >= 5
13 #define IF_HAVE_GCC_5(X) X
14 #endif
15  
16 #if (__GNUC__ == 4 && __GNUC_MINOR__ >= 6) || __GNUC__ >= 5
17 #define PRAGMA_GCC_(X) _Pragma(#X)
18 #define PRAGMA_GCC(X) PRAGMA_GCC_(GCC X)
19 #endif
20 #endif
21  
22 #ifndef IF_HAVE_GCC_5
23 #define IF_HAVE_GCC_5(X)
24 #endif
25  
26 #ifndef PRAGMA_GCC
27 #define PRAGMA_GCC(X)
28 #endif
29  
30 #ifdef DEVEL
31 #define WARNINGS_RESET PRAGMA_GCC(diagnostic pop)
32 #define WARNINGS_ENABLEW(X) PRAGMA_GCC(diagnostic warning #X)
33 #define WARNINGS_ENABLE \
34 WARNINGS_ENABLEW(-Wall) \
35 WARNINGS_ENABLEW(-Wextra) \
36 WARNINGS_ENABLEW(-Wundef) \
37 WARNINGS_ENABLEW(-Wshadow) \
38 WARNINGS_ENABLEW(-Wbad-function-cast) \
39 WARNINGS_ENABLEW(-Wcast-align) \
40 WARNINGS_ENABLEW(-Wwrite-strings) \
41 WARNINGS_ENABLEW(-Wstrict-prototypes) \
42 WARNINGS_ENABLEW(-Wmissing-prototypes) \
43 WARNINGS_ENABLEW(-Winline) \
44 WARNINGS_ENABLEW(-Wdisabled-optimization) \
45 IF_HAVE_GCC_5(WARNINGS_ENABLEW(-Wnested-externs))
46  
47 #else
48 #define WARNINGS_RESET
49 #define WARNINGS_ENABLE
50 #endif
51  
52  
53 #define PERL_NO_GET_CONTEXT
54 #include "EXTERN.h"
55 #include "perl.h"
56 #include "XSUB.h"
57  
58 #include <string.h>
59 #include <ctype.h>
60 #include <assert.h>
61  
62 #ifdef PERL_MAD
63 #error "MADness is not supported."
64 #endif
65  
66 #define HAVE_PERL_VERSION(R, V, S) \
67 (PERL_REVISION > (R) || (PERL_REVISION == (R) && (PERL_VERSION > (V) || (PERL_VERSION == (V) && (PERL_SUBVERSION >= (S))))))
68  
69 #if HAVE_PERL_VERSION(5, 19, 3)
70 #define IF_HAVE_PERL_5_19_3(YES, NO) YES
71 #else
72 #define IF_HAVE_PERL_5_19_3(YES, NO) NO
73 #endif
74  
75 #ifndef SvREFCNT_dec_NN
76 #define SvREFCNT_dec_NN(SV) SvREFCNT_dec(SV)
77 #endif
78  
79 #define MY_PKG "Switch::Plain"
80  
81  
82 /* 5.22+ shouldn't require any hax */
83 #if !HAVE_PERL_VERSION(5, 22, 0)
84  
85 #include "hax/intro_my.c.inc"
86 #include "hax/block_start.c.inc"
87 #include "hax/block_end.c.inc"
88  
89 #endif
90  
91  
92 WARNINGS_ENABLE
93  
94 #define HINTK_FLAGS MY_PKG "/flags"
95  
96  
97 static int (*next_keyword_plugin)(pTHX_ char *, STRLEN, OP **);
98  
99 15 static void free_ptr_op(pTHX_ void *vp) {
100 15 OP **pp = vp;
101 15 op_free(*pp);
102 15 Safefree(pp);
103 15 }
104  
105 enum {
106 FLAG_SSWITCH = 0x01,
107 FLAG_NSWITCH = 0x02
108 };
109  
110 217 static void my_sv_cat_c(pTHX_ SV *sv, U32 c) {
111 char ds[UTF8_MAXBYTES + 1], *d;
112 217 d = (char *)uvchr_to_utf8((U8 *)ds, c);
113 217 ▶ if (d - ds > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (d - ds > 1) { 0 217 217
114 0 sv_utf8_upgrade(sv);
115 }
116 217 sv_catpvn(sv, ds, d - ds);
117 217 }
118  
119  
120 #define MY_UNI_IDFIRST(C) isIDFIRST_uni(C)
121 #define MY_UNI_IDCONT(C) isALNUM_uni(C)
122  
123 76 static SV *my_scan_word(pTHX) {
124 I32 c;
125 SV *sv;
126  
127 76 c = lex_peek_unichar(0);
128 76 ▶ if (c == -1 || !MY_UNI_IDFIRST(c)) {
Branch: 6 branches
BranchTrueFalseTotal
if (c == -1 || !MY_UNI_IDFIRST(c)) { 76 0 76
if (c == -1 || !MY_UNI_IDFIRST(c)) { 76 0 76
if (c == -1 || !MY_UNI_IDFIRST(c)) { 76 0 76
if (c == -1 || !MY_UNI_IDFIRST(c)) { 32 44 76
if (c == -1 || !MY_UNI_IDFIRST(c)) { 32 44 76
if (c == -1 || !MY_UNI_IDFIRST(c)) { 0 0 0
129 32 return NULL;
130 }
131 44 lex_read_unichar(0);
132  
133 44 sv = sv_2mortal(newSVpvs(""));
134 44 ▶ if (lex_bufutf8()) {
Branch: 1 branch
BranchTrueFalseTotal
if (lex_bufutf8()) { 0 44 44
135 0 SvUTF8_on(sv);
136 }
137  
138 44 my_sv_cat_c(aTHX_ sv, c);
139  
140 217 ▶ while ((c = lex_peek_unichar(0)) != -1 && MY_UNI_IDCONT(c)) {
Branch: 6 branches
BranchTrueFalseTotal
while ((c = lex_peek_unichar(0)) != -1 && MY_UNI_IDCONT(c)) { 217 0 217
while ((c = lex_peek_unichar(0)) != -1 && MY_UNI_IDCONT(c)) { 217 0 217
while ((c = lex_peek_unichar(0)) != -1 && MY_UNI_IDCONT(c)) { 217 0 217
while ((c = lex_peek_unichar(0)) != -1 && MY_UNI_IDCONT(c)) { 173 44 217
while ((c = lex_peek_unichar(0)) != -1 && MY_UNI_IDCONT(c)) { 173 44 217
while ((c = lex_peek_unichar(0)) != -1 && MY_UNI_IDCONT(c)) { 0 0 0
141 173 lex_read_unichar(0);
142 173 my_sv_cat_c(aTHX_ sv, c);
143 }
144  
145 44 return sv;
146 }
147  
148 #define DEFSTRUCT(T) typedef struct T T; struct T
149  
150 DEFSTRUCT(IfThen) {
151 OP *cond;
152 OP *body;
153 };
154  
155 DEFSTRUCT(IfThenVector) {
156 IfThen *data;
157 size_t used, size;
158 };
159  
160 0 static void ifthen_destroy(pTHX_ IfThen *it) {
161 0 op_free(it->cond);
162 0 it->cond = NULL;
163 0 op_free(it->body);
164 0 it->body = NULL;
165 0 }
166  
167 15 static void itv_free(pTHX_ void *vp) {
168 15 IfThenVector *itv = vp;
169 15 ▶ while (itv->used > 0) {
Branch: 1 branch
BranchTrueFalseTotal
while (itv->used > 0) { 0 15 15
170 0 itv->used--;
171 0 ifthen_destroy(aTHX_ &itv->data[itv->used]);
172 }
173 15 Safefree(itv->data);
174 15 Safefree(itv);
175 15 }
176  
177 15 static IfThenVector *itv_alloc_ephemeral(pTHX) {
178 IfThenVector *itv;
179  
180 15 Newx(itv, 1, IfThenVector);
181 15 itv->data = NULL;
182 15 itv->used = itv->size = 0;
183 15 SAVEDESTRUCTOR_X(itv_free, itv);
184  
185 15 itv->size = 42;
186 15 ▶ Newx(itv->data, itv->size, IfThen);
Branch: 1 branch
BranchTrueFalseTotal
Newx(itv->data, itv->size, IfThen); 0 15 15
187  
188 15 return itv;
189 }
190  
191 32 static void itv_push(IfThenVector *itv, OP *cond, OP *body) {
192 IfThen *it;
193  
194 assert(itv->used <= itv->size);
195 32 ▶ if (itv->used == itv->size) {
Branch: 1 branch
BranchTrueFalseTotal
if (itv->used == itv->size) { 0 32 32
196 0 itv->size += itv->size / 2 + 1;
197 0 ▶ Renew(itv->data, itv->size, IfThen);
Branch: 1 branch
BranchTrueFalseTotal
Renew(itv->data, itv->size, IfThen); 0 0 0
198 }
199  
200 32 it = &itv->data[itv->used++];
201 32 it->cond = cond;
202 32 it->body = body;
203 32 }
204  
205 32 static void do_alternative(pTHX_ IfThenVector *itv, int compare_numeric) {
206 OP *cond_acc;
207  
208 32 cond_acc = NULL;
209  
210 do {
211 OP *cond;
212 SV *sv1, *sv2;
213 const char *kw;
214 size_t kw_len;
215  
216 38 sv1 = my_scan_word(aTHX);
217 38 ▶ if (!sv1) {
Branch: 1 branch
BranchTrueFalseTotal
if (!sv1) { 0 38 38
218 0 int n = PL_parser->bufend - PL_parser->bufptr;
219 0 ▶ if (n > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (n > 0) { 0 0 0
220 0 croak("Missing 'case' or 'default' before \"%.*s\"", n, PL_parser->bufptr);
221 }
222 0 croak("Missing 'case' or 'default'");
223 }
224  
225 38 kw = SvPV(sv1, kw_len);
226 53 if (!(
227 38 ▶ (kw_len == 4 && memcmp(kw, "case", 4) == 0) ||
Branch: 2 branches
BranchTrueFalseTotal
(kw_len == 4 && memcmp(kw, "case", 4) == 0) || 23 15 38
(kw_len == 4 && memcmp(kw, "case", 4) == 0) || 0 23 23
228 15 ▶ (kw_len == 7 && memcmp(kw, "default", 7) == 0)
Branch: 2 branches
BranchTrueFalseTotal
(kw_len == 7 && memcmp(kw, "default", 7) == 0) 15 0 15
(kw_len == 7 && memcmp(kw, "default", 7) == 0) 0 15 15
229 )) {
230 0 croak("Missing 'case' or 'default' before \"%"SVf"\"", sv1);
231 }
232  
233 /* default */
234 38 ▶ if (kw_len == 7) {
Branch: 1 branch
BranchTrueFalseTotal
if (kw_len == 7) { 15 23 38
235 15 cond = NULL;
236 } else {
237 23 ▶ if (!(cond = parse_fullexpr(PARSE_OPTIONAL))) {
Branch: 1 branch
BranchTrueFalseTotal
if (!(cond = parse_fullexpr(PARSE_OPTIONAL))) { 0 23 23
238 0 croak("Missing expression after 'case'");
239 }
240 23 ▶ cond = newBINOP(
Branch: 1 branch
BranchTrueFalseTotal
cond = newBINOP( 7 16 23
241 compare_numeric ? OP_EQ : OP_SEQ,
242 0,
243 newSVREF(newGVOP(OP_GV, 0, PL_defgv)),
244 op_contextualize(cond, G_SCALAR)
245 );
246 }
247  
248 38 lex_read_space(0);
249 38 sv2 = my_scan_word(aTHX);
250 38 ▶ if (sv2) {
Branch: 1 branch
BranchTrueFalseTotal
if (sv2) { 6 32 38
251 OP *cond2;
252  
253 6 kw = SvPV(sv2, kw_len);
254 8 if (!(
255 6 ▶ (kw_len == 2 && memcmp(kw, "if", 2) == 0) ||
Branch: 2 branches
BranchTrueFalseTotal
(kw_len == 2 && memcmp(kw, "if", 2) == 0) || 4 2 6
(kw_len == 2 && memcmp(kw, "if", 2) == 0) || 0 4 4
256 2 ▶ (kw_len == 6 && memcmp(kw, "unless", 6) == 0)
Branch: 2 branches
BranchTrueFalseTotal
(kw_len == 6 && memcmp(kw, "unless", 6) == 0) 2 0 2
(kw_len == 6 && memcmp(kw, "unless", 6) == 0) 0 2 2
257 )) {
258 0 croak("Missing ':' after '%"SVf"'", sv1);
259 }
260  
261 6 cond2 = parse_fullexpr(PARSE_OPTIONAL);
262 6 ▶ if (!cond2) {
Branch: 1 branch
BranchTrueFalseTotal
if (!cond2) { 0 6 6
263 0 croak("Missing expression after '%"SVf"'", sv2);
264 }
265 6 lex_read_space(0);
266 6 cond2->op_flags |= OPf_PARENS;
267  
268 /* unless */
269 6 ▶ if (kw_len == 6) {
Branch: 1 branch
BranchTrueFalseTotal
if (kw_len == 6) { 2 4 6
270 2 cond2 = newUNOP(OP_NOT, OPf_SPECIAL, op_contextualize(cond2, G_SCALAR));
271 }
272  
273 6 ▶ cond = !cond ? cond2 : newLOGOP(OP_AND, 0, cond, cond2);
Branch: 1 branch
BranchTrueFalseTotal
cond = !cond ? cond2 : newLOGOP(OP_AND, 0, cond, cond2); 2 4 6
274 }
275  
276 38 ▶ if (lex_peek_unichar(0) != ':') {
Branch: 1 branch
BranchTrueFalseTotal
if (lex_peek_unichar(0) != ':') { 0 38 38
277 0 croak("Missing ':' after '%"SVf"'", sv1);
278 }
279 38 lex_read_unichar(0);
280 38 lex_read_space(0);
281  
282 38 ▶ cond_acc = !cond_acc ? cond : newLOGOP(OP_OR, 0, cond_acc, cond);
Branch: 1 branch
BranchTrueFalseTotal
cond_acc = !cond_acc ? cond : newLOGOP(OP_OR, 0, cond_acc, cond); 6 32 38
283  
284 38 ▶ } while (lex_peek_unichar(0) != '{');
Branch: 1 branch
BranchTrueFalseTotal
} while (lex_peek_unichar(0) != '{'); 6 32 38
285  
286 32 ▶ if (cond_acc) {
Branch: 1 branch
BranchTrueFalseTotal
if (cond_acc) { 21 11 32
287 21 intro_my();
288 }
289  
290 {
291 OP *body;
292 int block_ix;
293  
294 32 block_ix = block_start(FALSE);
295 32 body = parse_block(0);
296 32 body = block_end(block_ix, body);
297 /*
298 body->op_flags |= OPf_PARENS;
299 body = op_scope(aTHX_ body);
300 */
301  
302 32 itv_push(itv, cond_acc, body);
303 }
304  
305 32 lex_read_space(0);
306 32 }
307  
308 15 static void parse_switch(pTHX_ int compare_numeric, OP **op_ptr) {
309 IfThenVector *itv;
310 OP **gen_sentinel;
311 int save_ix;
312 I32 c;
313  
314 15 lex_read_space(0);
315  
316 15 c = lex_peek_unichar(0);
317 15 ▶ if (c != '(') {
Branch: 1 branch
BranchTrueFalseTotal
if (c != '(') { 0 15 15
318 0 ▶ croak("Missing '(' after '%cswitch'", compare_numeric ? 'n' : 's');
Branch: 1 branch
BranchTrueFalseTotal
croak("Missing '(' after '%cswitch'", compare_numeric ? 'n' : 's'); 0 0 0
319 }
320 15 lex_read_unichar(0);
321  
322 15 Newx(gen_sentinel, 1, OP *);
323 15 *gen_sentinel = NULL;
324 15 SAVEDESTRUCTOR_X(free_ptr_op, gen_sentinel);
325  
326 /* create outer block: '{' */
327 15 save_ix = block_start(TRUE);
328  
329 15 ▶ if (!(*gen_sentinel = parse_fullexpr(PARSE_OPTIONAL))) {
Branch: 1 branch
BranchTrueFalseTotal
if (!(*gen_sentinel = parse_fullexpr(PARSE_OPTIONAL))) { 0 15 15
330 0 ▶ croak("Missing expression after '%cswitch ('", compare_numeric ? 'n' : 's');
Branch: 1 branch
BranchTrueFalseTotal
croak("Missing expression after '%cswitch ('", compare_numeric ? 'n' : 's'); 0 0 0
331 }
332 15 lex_read_space(0);
333  
334 15 c = lex_peek_unichar(0);
335 15 ▶ if (c != ')') {
Branch: 1 branch
BranchTrueFalseTotal
if (c != ')') { 0 15 15
336 0 croak("Missing ')'");
337 }
338 15 lex_read_unichar(0);
339 15 lex_read_space(0);
340  
341 {
342 OP *target, *gen;
343  
344 15 gen = *gen_sentinel;
345 15 gen = newUNOP(OP_REFGEN, 0, op_lvalue(op_contextualize(gen, G_SCALAR), OP_REFGEN));
346  
347 15 target = newGVREF(0, newGVOP(OP_GV, 0, PL_defgv));
348 15 target = op_lvalue(target, OP_NULL);
349 15 gen = newASSIGNOP(OPf_STACKED, target, 0, gen);
350  
351 15 *gen_sentinel = gen;
352 }
353  
354 15 c = lex_peek_unichar(0);
355 15 ▶ if (c != '{') {
Branch: 1 branch
BranchTrueFalseTotal
if (c != '{') { 0 15 15
356 0 croak("Missing '{'");
357 }
358 15 lex_read_unichar(0);
359 15 lex_read_space(0);
360  
361 15 itv = itv_alloc_ephemeral(aTHX);
362  
363 47 ▶ while (lex_peek_unichar(0) != '}') {
Branch: 1 branch
BranchTrueFalseTotal
while (lex_peek_unichar(0) != '}') { 32 15 47
364 32 do_alternative(aTHX_ itv, compare_numeric);
365 }
366 15 lex_read_unichar(0);
367  
368 {
369 15 OP *gbody = NULL;
370  
371 47 ▶ while (itv->used) {
Branch: 1 branch
BranchTrueFalseTotal
while (itv->used) { 32 15 47
372 32 const IfThen *cur = &itv->data[--itv->used];
373 32 ▶ gbody = newCONDOP(
Branch: 1 branch
BranchTrueFalseTotal
gbody = newCONDOP( 21 11 32
374 0,
375 /* newSTATEOP(OPf_SPECIAL, NULL, ) XXX? */
376 cur->cond ? cur->cond : newSVOP(OP_CONST, 0, &PL_sv_yes),
377 op_scope(cur->body),
378 gbody
379 );
380 }
381  
382 15 *gen_sentinel = op_append_list(OP_LINESEQ, *gen_sentinel, newSTATEOP(0, NULL, gbody));
383 }
384  
385 /* close outer block: '}' */
386 15 *gen_sentinel = block_end(save_ix, *gen_sentinel);
387 15 *gen_sentinel = op_scope(*gen_sentinel); /* XXX? */
388  
389 15 *op_ptr = *gen_sentinel;
390 15 *gen_sentinel = NULL;
391 15 }
392  
393 15 static IV bc_flags(pTHX) {
394 HV *hints;
395 SV *sv, **psv;
396  
397 15 ▶ if (!(hints = GvHV(PL_hintgv))) {
Branch: 1 branch
BranchTrueFalseTotal
if (!(hints = GvHV(PL_hintgv))) { 0 15 15
398 0 return 0;
399 }
400 15 ▶ if (!(psv = hv_fetch(hints, HINTK_FLAGS, sizeof HINTK_FLAGS - 1, 0))) {
Branch: 1 branch
BranchTrueFalseTotal
if (!(psv = hv_fetch(hints, HINTK_FLAGS, sizeof HINTK_FLAGS - 1, 0))) { 0 15 15
401 0 return 0;
402 }
403 15 sv = *psv;
404 15 return SvIV(sv);
405 }
406  
407 19026 static int my_keyword_plugin(pTHX_ char *keyword_ptr, STRLEN keyword_len, OP **op_ptr) {
408 int ret;
409 char c;
410  
411 19026 c = *keyword_ptr;
412 19026 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ( 1367 17659 19026
413 1367 ▶ keyword_len == 7 &&
Branch: 1 branch
BranchTrueFalseTotal
keyword_len == 7 && 1267 100 1367
414 1267 ▶ (c == 's' || c == 'n') &&
Branch: 1 branch
BranchTrueFalseTotal
(c == 's' || c == 'n') && 5 1262 1267
415 105 ▶ memcmp(keyword_ptr + 1, "switch", 6) == 0 &&
Branch: 1 branch
BranchTrueFalseTotal
memcmp(keyword_ptr + 1, "switch", 6) == 0 && 15 90 105
416 15 ▶ (bc_flags(aTHX) & (c == 'n' ? FLAG_NSWITCH : FLAG_SSWITCH))
Branch: 2 branches
BranchTrueFalseTotal
(bc_flags(aTHX) & (c == 'n' ? FLAG_NSWITCH : FLAG_SSWITCH)) 5 10 15
(bc_flags(aTHX) & (c == 'n' ? FLAG_NSWITCH : FLAG_SSWITCH)) 15 0 15
417 ) {
418 15 ENTER;
419 15 SAVETMPS;
420 15 parse_switch(aTHX_ c == 'n', op_ptr);
421 15 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 15 0 15
422 15 LEAVE;
423 15 ret = KEYWORD_PLUGIN_STMT;
424 } else {
425 19011 ret = next_keyword_plugin(aTHX_ keyword_ptr, keyword_len, op_ptr);
426 }
427  
428  
429 19026 return ret;
430 }
431  
432 4 static void my_boot(pTHX) {
433 4 HV *const stash = gv_stashpvs(MY_PKG, GV_ADD);
434  
435 4 newCONSTSUB(stash, "FLAG_SSWITCH", newSViv(FLAG_SSWITCH));
436 4 newCONSTSUB(stash, "FLAG_NSWITCH", newSViv(FLAG_NSWITCH));
437 4 newCONSTSUB(stash, "HINTK_FLAGS", newSVpvs(HINTK_FLAGS));
438  
439 4 next_keyword_plugin = PL_keyword_plugin;
440 4 PL_keyword_plugin = my_keyword_plugin;
441 4 }
442  
443 WARNINGS_RESET
444  
445 MODULE = Switch::Plain PACKAGE = Switch::Plain
446 PROTOTYPES: ENABLE
447  
448 BOOT:
449 4 my_boot(aTHX);
« blib/lib/Switch/Plain.pm ↑ Summary