Code.xs

filter: Statementstmt 83.0% 319 / 384 Branchbran 68.0% 181 / 266 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 76.9% 500 / 650 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/Quote/Code.pm ↑ Summary
1 /*
2 Copyright 2012, 2013, 2023 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 https://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 error #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 #ifdef __has_attribute
53 #if __has_attribute(noreturn)
54 #define ATTR_NORETURN __attribute__((noreturn))
55 #endif
56 #endif
57  
58 #ifndef ATTR_NORETURN
59 #define ATTR_NORETURN
60 #endif
61  
62  
63 #define PERL_NO_GET_CONTEXT
64 #include "EXTERN.h"
65 #include "perl.h"
66 #include "XSUB.h"
67  
68 #include <string.h>
69 #include <ctype.h>
70 #include <assert.h>
71  
72  
73 WARNINGS_ENABLE
74  
75  
76 #define HAVE_PERL_VERSION(R, V, S) \
77 (PERL_REVISION > (R) || (PERL_REVISION == (R) && (PERL_VERSION > (V) || (PERL_VERSION == (V) && (PERL_SUBVERSION >= (S))))))
78  
79 #if HAVE_PERL_VERSION(5, 16, 0)
80 #define IF_HAVE_PERL_5_16(YES, NO) YES
81 #else
82 #define IF_HAVE_PERL_5_16(YES, NO) NO
83 #endif
84  
85  
86 #define MY_PKG "Quote::Code"
87  
88 #define HINTK_QC MY_PKG "/qc"
89 #define HINTK_QC_TO MY_PKG "/qc_to"
90 #define HINTK_QCW MY_PKG "/qcw"
91  
92 static int (*next_keyword_plugin)(pTHX_ char *, STRLEN, OP **);
93  
94 enum ChunkType {
95 CHUNK_SV,
96 CHUNK_OP
97 };
98  
99 union ptr_sv_op {
100 SV *sv;
101 OP *op;
102 };
103  
104 typedef struct {
105 enum ChunkType type;
106 union ptr_sv_op ptr;
107 } Chunk;
108  
109 typedef struct {
110 size_t size, len;
111 Chunk *data;
112 } ChunkVec;
113  
114 15 static void free_ptr_op(pTHX_ void *vp) {
115 15 OP **pp = vp;
116 15 op_free(*pp);
117 15 Safefree(pp);
118 15 }
119  
120 80 static void free_ptr_chunks(pTHX_ void *vp) {
121 80 ChunkVec **pp = vp;
122 80 ChunkVec *p = *pp;
123 80 ▶ if (p) {
Branch: 1 branch
BranchTrueFalseTotal
if (p) { 80 0 80
124 size_t i;
125 6140 ▶ for (i = 0; i < p->len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < p->len; i++) { 6060 80 6140
126 6060 switch (p->data[i].type) {
127 2022 case CHUNK_SV: /* nop, already mortal */ break;
128 4038 case CHUNK_OP: op_free(p->data[i].ptr.op); break;
129 }
130 }
131 80 Safefree(p->data);
132 80 Safefree(p);
133 }
134 80 Safefree(pp);
135 80 }
136  
137 enum {
138 FLAG_BACKSLASH_ESCAPE = 0x1,
139 FLAG_HASH_INTERPOLATE = 0x2,
140 FLAG_STOP_AT_SPACE = 0x4
141 };
142  
143 typedef struct {
144 I32 delim_start, delim_stop;
145 SV *delim_str, *leftover;
146 int flags;
147 } QCSpec;
148  
149 ATTR_NORETURN
150 1 static void missing_terminator(pTHX_ const QCSpec *spec, line_t line) {
151 1 I32 c = spec->delim_stop;
152 1 SV *sv = spec->delim_str;
153  
154 1 ▶ if (!sv) {
Branch: 1 branch
BranchTrueFalseTotal
if (!sv) { 0 1 1
155 0 sv = sv_2mortal(newSVpvs("'\"'"));
156 0 ▶ if (c != '"') {
Branch: 1 branch
BranchTrueFalseTotal
if (c != '"') { 0 0 0
157 U8 utf8_tmp[UTF8_MAXBYTES + 1], *d;
158 0 d = uvchr_to_utf8(utf8_tmp, c);
159 0 pv_uni_display(sv, utf8_tmp, d - utf8_tmp, 100, UNI_DISPLAY_QQ);
160 }
161 }
162  
163 1 ▶ if (c != '"') {
Branch: 1 branch
BranchTrueFalseTotal
if (c != '"') { 1 0 1
164 1 sv_insert(sv, 0, 0, "\"", 1);
165 1 sv_catpvs(sv, "\"");
166 }
167  
168 1 ▶ if (line) {
Branch: 1 branch
BranchTrueFalseTotal
if (line) { 1 0 1
169 1 CopLINE_set(PL_curcop, line);
170 }
171 1 croak("Can't find string terminator %"SVf" anywhere before EOF", SVfARG(sv));
172 }
173  
174 7382 static void my_sv_cat_c(pTHX_ SV *sv, U32 c) {
175 U8 ds[UTF8_MAXBYTES + 1], *d;
176 7382 d = uvchr_to_utf8(ds, c);
177 7382 ▶ if (d - ds > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (d - ds > 1) { 7 7375 7382
178 7 sv_utf8_upgrade(sv);
179 }
180 7382 sv_catpvn(sv, (char *)ds, d - ds);
181 7382 }
182  
183 16 static U32 hex2int(unsigned char c) {
184 static char xdigits[] = "0123456789abcdef";
185 16 char *p = strchr(xdigits, tolower(c));
186 16 ▶ if (!c || !p) {
Branch: 2 branches
BranchTrueFalseTotal
if (!c || !p) { 16 0 16
if (!c || !p) { 0 16 16
187 0 return 0;
188 }
189 16 return p - xdigits;
190 }
191  
192 2086 static void my_op_cat_sv(pTHX_ OP **pop, SV *sv) {
193 assert(sv != NULL);
194 2086 OP *const str = newSVOP(OP_CONST, 0, SvREFCNT_inc_simple_NN(sv));
195 2086 ▶ *pop = !*pop ? str : newBINOP(OP_CONCAT, 0, *pop, str);
Branch: 1 branch
BranchTrueFalseTotal
*pop = !*pop ? str : newBINOP(OP_CONCAT, 0, *pop, str); 2022 64 2086
196 2086 }
197  
198 6060 static void chunk_push(pTHX_ ChunkVec *vec, enum ChunkType type, union ptr_sv_op ptr) {
199 6060 ▶ if (vec->len == vec->size) {
Branch: 1 branch
BranchTrueFalseTotal
if (vec->len == vec->size) { 15 6045 6060
200 15 ▶ if (vec->size >= ~(size_t)0 / 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (vec->size >= ~(size_t)0 / 3) { 0 15 15
201 0 croak("Out of memory while parsing " MY_PKG " qc (vec->size: %zu)", vec->size);
202 }
203 15 vec->size *= 3;
204 15 vec->size /= 2;
205 15 ▶ Renew(vec->data, vec->size, Chunk);
Branch: 1 branch
BranchTrueFalseTotal
Renew(vec->data, vec->size, Chunk); 0 15 15
206 }
207 6060 vec->data[vec->len].type = type;
208 6060 vec->data[vec->len].ptr = ptr;
209 6060 vec->len++;
210 6060 }
211  
212 80 static OP *parse_qctail(pTHX_ const QCSpec *spec, int *pnesting) {
213 I32 c;
214 ChunkVec **gen_sentinel, *vec;
215 SV *sv;
216 line_t start;
217 80 SV *const delim_str = spec->delim_str;
218 const int
219 80 have_delim_stop = spec->delim_stop != -1;
220  
221 assert(have_delim_stop == !delim_str);
222 assert(!delim_str || spec->leftover);
223  
224 80 start = CopLINE(PL_curcop);
225  
226 80 Newx(gen_sentinel, 1, ChunkVec *);
227 80 *gen_sentinel = NULL;
228 80 SAVEDESTRUCTOR_X(free_ptr_chunks, gen_sentinel);
229  
230 80 Newx(*gen_sentinel, 1, ChunkVec);
231 80 vec = *gen_sentinel;
232 80 vec->size = 16;
233 80 ▶ Newx(vec->data, vec->size, Chunk);
Branch: 1 branch
BranchTrueFalseTotal
Newx(vec->data, vec->size, Chunk); 0 80 80
234 80 vec->len = 0;
235  
236 80 sv = sv_2mortal(newSVpvs(""));
237 80 ▶ if (lex_bufutf8()) {
Branch: 1 branch
BranchTrueFalseTotal
if (lex_bufutf8()) { 2 78 80
238 2 SvUTF8_on(sv);
239 }
240 80 c = '\n';
241  
242 11273 for (;;) {
243 char *elim;
244 11353 I32 b = c;
245  
246 11353 c = lex_peek_unichar(0);
247 11353 ▶ if (c == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (c == -1) { 1 11352 11353
248 1 missing_terminator(aTHX_ spec, start);
249 }
250  
251 assert(PL_parser->bufend >= PL_parser->bufptr);
252  
253 11352 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ( 114 11238 11352
254 114 ▶ b == '\n' &&
Branch: 1 branch
BranchTrueFalseTotal
b == '\n' && 53 61 114
255 53 delim_str &&
256 /* c == spec->delim_start && */
257 53 ▶ (STRLEN)(PL_parser->bufend - PL_parser->bufptr) >= SvCUR(delim_str) &&
Branch: 1 branch
BranchTrueFalseTotal
(STRLEN)(PL_parser->bufend - PL_parser->bufptr) >= SvCUR(delim_str) && 49 4 53
258 49 (elim = PL_parser->bufptr + SvCUR(delim_str),
259 49 ▶ memcmp(PL_parser->bufptr, SvPVX(delim_str), SvCUR(delim_str)) == 0) && (
Branch: 1 branch
BranchTrueFalseTotal
memcmp(PL_parser->bufptr, SvPVX(delim_str), SvCUR(delim_str)) == 0) && ( 20 29 49
260 0 !(
261 20 (STRLEN)(PL_parser->bufend - PL_parser->bufptr) > SvCUR(delim_str) ||
262 0 lex_next_chunk(0)
263 20 ) ||
264 20 ▶ (elim++, PL_parser->bufptr[SvCUR(delim_str)] == '\n') || (
Branch: 2 branches
BranchTrueFalseTotal
(elim++, PL_parser->bufptr[SvCUR(delim_str)] == '\n') || ( 0 20 20
(elim++, PL_parser->bufptr[SvCUR(delim_str)] == '\n') || ( 0 0 0
265 0 elim++,
266 0 ▶ PL_parser->bufptr[SvCUR(delim_str)] == '\r' &&
Branch: 1 branch
BranchTrueFalseTotal
PL_parser->bufptr[SvCUR(delim_str)] == '\r' && 0 0 0
267 0 ▶ PL_parser->bufptr[SvCUR(delim_str) + 1] == '\n'
Branch: 1 branch
BranchTrueFalseTotal
PL_parser->bufptr[SvCUR(delim_str) + 1] == '\n' 0 0 0
268 )
269 )
270 ) {
271 20 lex_read_to(elim);
272 20 lex_stuff_sv(spec->leftover, 0);
273 20 break;
274 }
275  
276 11332 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ( 4038 7294 11332
277 11540 ▶ !(spec->flags & FLAG_HASH_INTERPOLATE)
Branch: 1 branch
BranchTrueFalseTotal
!(spec->flags & FLAG_HASH_INTERPOLATE) 11124 208 11332
278 ? c == '{'
279 13 : c == '#' &&
280 13 ▶ spec->delim_stop != '#' &&
Branch: 1 branch
BranchTrueFalseTotal
spec->delim_stop != '#' && 13 0 13
281 232 ▶ PL_parser->bufptr[1] == '{' &&
Branch: 2 branches
BranchTrueFalseTotal
PL_parser->bufptr[1] == '{' && 13 195 208
PL_parser->bufptr[1] == '{' && 11 2 13
282 11 (lex_read_unichar(0), 1)
283 4038 ) {
284 OP *op;
285  
286 4038 op = parse_block(0);
287 4038 op = newUNOP(OP_NULL, OPf_SPECIAL, op_scope(op));
288  
289 4038 ▶ if (SvCUR(sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvCUR(sv)) { 2022 2016 4038
290 union ptr_sv_op ptr;
291 2022 ptr.sv = sv;
292 2022 chunk_push(aTHX_ vec, CHUNK_SV, ptr);
293  
294 2022 sv = sv_2mortal(newSVpvs(""));
295 2022 ▶ if (lex_bufutf8()) {
Branch: 1 branch
BranchTrueFalseTotal
if (lex_bufutf8()) { 0 2022 2022
296 0 SvUTF8_on(sv);
297 }
298 }
299  
300 {
301 union ptr_sv_op ptr;
302 4038 ptr.op = op;
303 4038 chunk_push(aTHX_ vec, CHUNK_OP, ptr);
304 }
305  
306 4038 c = -1;
307 4038 continue;
308 }
309  
310 7294 lex_read_unichar(0);
311  
312 7294 ▶ if (pnesting && c == spec->delim_start) {
Branch: 2 branches
BranchTrueFalseTotal
if (pnesting && c == spec->delim_start) { 151 7143 7294
if (pnesting && c == spec->delim_start) { 5 146 151
313 5 (*pnesting)++;
314 7289 ▶ } else if (have_delim_stop && c == spec->delim_stop) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (have_delim_stop && c == spec->delim_stop) { 7092 197 7289
} else if (have_delim_stop && c == spec->delim_stop) { 40 7052 7092
315 40 ▶ if (!pnesting || *pnesting == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (!pnesting || *pnesting == 0) { 22 18 40
if (!pnesting || *pnesting == 0) { 17 5 22
316 35 ▶ if (spec->flags & FLAG_STOP_AT_SPACE) {
Branch: 1 branch
BranchTrueFalseTotal
if (spec->flags & FLAG_STOP_AT_SPACE) { 8 27 35
317 /* terrible hack to let qcw() know to stop parsing */
318 8 char tmp = c;
319 8 lex_stuff_pvn(&tmp, 1, 0);
320 }
321 35 break;
322 }
323 5 (*pnesting)--;
324 7249 ▶ } else if ((spec->flags & FLAG_STOP_AT_SPACE) && isSPACE(c)) {
Branch: 3 branches
BranchTrueFalseTotal
} else if ((spec->flags & FLAG_STOP_AT_SPACE) && isSPACE(c)) { 99 7150 7249
} else if ((spec->flags & FLAG_STOP_AT_SPACE) && isSPACE(c)) { 99 0 99
} else if ((spec->flags & FLAG_STOP_AT_SPACE) && isSPACE(c)) { 76 23 99
325 break;
326 7226 ▶ } else if (c == '\\' && (spec->flags & FLAG_BACKSLASH_ESCAPE)) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (c == '\\' && (spec->flags & FLAG_BACKSLASH_ESCAPE)) { 41 7185 7226
} else if (c == '\\' && (spec->flags & FLAG_BACKSLASH_ESCAPE)) { 34 7 41
327 U32 u;
328  
329 34 c = lex_read_unichar(0);
330 34 switch (c) {
331 0 case -1:
332 0 missing_terminator(aTHX_ spec, start);
333  
334 0 case 'a': c = '\a'; break;
335 0 case 'b': c = '\b'; break;
336 0 case 'e': c = '\033'; break;
337 0 case 'f': c = '\f'; break;
338 3 case 'n': c = '\n'; break;
339 0 case 'r': c = '\r'; break;
340 4 case 't': c = '\t'; break;
341  
342 0 case 'c':
343 0 c = lex_read_unichar(0);
344 0 ▶ if (c == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (c == -1) { 0 0 0
345 0 missing_terminator(aTHX_ spec, start);
346 }
347 0 ▶ c = toUPPER(c) ^ 64;
Branch: 1 branch
BranchTrueFalseTotal
c = toUPPER(c) ^ 64; 0 0 0
348 0 break;
349  
350 0 case 'o':
351 0 c = lex_read_unichar(0);
352 0 ▶ if (c != '{') {
Branch: 1 branch
BranchTrueFalseTotal
if (c != '{') { 0 0 0
353 0 croak("Missing braces on \\o{}");
354 }
355 0 u = 0;
356 0 ▶ while (c = lex_peek_unichar(0), c >= '0' && c <= '7') {
Branch: 3 branches
BranchTrueFalseTotal
while (c = lex_peek_unichar(0), c >= '0' && c <= '7') { 0 0 0
while (c = lex_peek_unichar(0), c >= '0' && c <= '7') { 0 0 0
while (c = lex_peek_unichar(0), c >= '0' && c <= '7') { 0 0 0
357 0 u = u * 8 + (c - '0');
358 0 lex_read_unichar(0);
359 }
360 0 ▶ if (c != '}') {
Branch: 1 branch
BranchTrueFalseTotal
if (c != '}') { 0 0 0
361 0 croak("Missing right brace on \\o{}");
362 }
363 0 lex_read_unichar(0);
364 0 c = u;
365 0 break;
366  
367 6 case 'x':
368 6 c = lex_read_unichar(0);
369 6 ▶ if (c == '{') {
Branch: 1 branch
BranchTrueFalseTotal
if (c == '{') { 2 4 6
370 2 u = 0;
371 10 ▶ while (c = lex_peek_unichar(0), isXDIGIT(c)) {
Branch: 3 branches
BranchTrueFalseTotal
while (c = lex_peek_unichar(0), isXDIGIT(c)) { 10 0 10
while (c = lex_peek_unichar(0), isXDIGIT(c)) { 8 2 10
while (c = lex_peek_unichar(0), isXDIGIT(c)) { 8 2 10
372 8 u = u * 16 + hex2int(c);
373 8 lex_read_unichar(0);
374 }
375 2 ▶ if (c != '}') {
Branch: 1 branch
BranchTrueFalseTotal
if (c != '}') { 0 2 2
376 0 croak("Missing right brace on \\x{}");
377 }
378 2 lex_read_unichar(0);
379 2 c = u;
380 4 ▶ } else if (isXDIGIT(c)) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (isXDIGIT(c)) { 4 0 4
} else if (isXDIGIT(c)) { 4 0 4
381 4 u = hex2int(c);
382 4 c = lex_peek_unichar(0);
383 4 ▶ if (isXDIGIT(c)) {
Branch: 2 branches
BranchTrueFalseTotal
if (isXDIGIT(c)) { 4 0 4
if (isXDIGIT(c)) { 4 0 4
384 4 u = u * 16 + hex2int(c);
385 4 lex_read_unichar(0);
386 }
387 4 c = u;
388 } else {
389 0 c = 0;
390 }
391 6 break;
392  
393 6 case 'N': {
394 SV *name;
395 char *n_ptr;
396 STRLEN n_len;
397  
398 6 c = lex_read_unichar(0);
399 6 ▶ if (c != '{') {
Branch: 1 branch
BranchTrueFalseTotal
if (c != '{') { 0 6 6
400 0 croak("Missing braces on \\N{}");
401 }
402  
403 6 name = sv_2mortal(newSVpvs(""));
404 6 ▶ if (lex_bufutf8()) {
Branch: 1 branch
BranchTrueFalseTotal
if (lex_bufutf8()) { 0 6 6
405 0 SvUTF8_on(name);
406 }
407  
408 87 ▶ while ((c = lex_read_unichar(0)) != '}') {
Branch: 1 branch
BranchTrueFalseTotal
while ((c = lex_read_unichar(0)) != '}') { 81 6 87
409 81 ▶ if (c == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (c == -1) { 0 81 81
410 0 croak("Missing right brace on \\N{}");
411 }
412 81 my_sv_cat_c(aTHX_ name, c);
413 }
414  
415 6 n_ptr = SvPV(name, n_len);
416  
417 6 ▶ if (n_len >= 2 && n_ptr[0] == 'U' && n_ptr[1] == '+') {
Branch: 3 branches
BranchTrueFalseTotal
if (n_len >= 2 && n_ptr[0] == 'U' && n_ptr[1] == '+') { 6 0 6
if (n_len >= 2 && n_ptr[0] == 'U' && n_ptr[1] == '+') { 2 4 6
if (n_len >= 2 && n_ptr[0] == 'U' && n_ptr[1] == '+') { 2 0 2
418 2 I32 flags = PERL_SCAN_ALLOW_UNDERSCORES | PERL_SCAN_DISALLOW_PREFIX;
419 STRLEN x_len;
420  
421 2 n_ptr += 2;
422 2 n_len -= 2;
423  
424 2 x_len = n_len;
425 2 c = grok_hex(n_ptr, &x_len, &flags, NULL);
426 2 ▶ if (x_len == 0 || x_len != n_len) {
Branch: 2 branches
BranchTrueFalseTotal
if (x_len == 0 || x_len != n_len) { 2 0 2
if (x_len == 0 || x_len != n_len) { 0 2 2
427 0 croak("Invalid hexadecimal number in \\N{U+...}");
428 }
429  
430 2 break;
431 }
432  
433 {
434 HV *table;
435 SV **cvp;
436  
437 #if HAVE_PERL_VERSION(5, 15, 7)
438 4 if (
439 4 ▶ !(table = GvHV(PL_hintgv)) ||
Branch: 1 branch
BranchTrueFalseTotal
!(table = GvHV(PL_hintgv)) || 4 0 4
440 8 !(PL_hints & HINT_LOCALIZE_HH) ||
441 4 !(cvp = hv_fetchs(table, "charnames", FALSE)) ||
442 3 ▶ !SvOK(*cvp)
Branch: 1 branch
BranchTrueFalseTotal
!SvOK(*cvp) 0 3 3
443 ) {
444 1 load_module(
445 0, newSVpvs("charnames"), NULL,
446 newSVpvs(":full"), newSVpvs(":short"), (SV *)NULL
447 );
448 }
449 #endif
450  
451 4 if (
452 4 ▶ !(table = GvHV(PL_hintgv)) ||
Branch: 1 branch
BranchTrueFalseTotal
!(table = GvHV(PL_hintgv)) || 4 0 4
453 4 ▶ !(PL_hints & HINT_LOCALIZE_HH)
Branch: 1 branch
BranchTrueFalseTotal
!(PL_hints & HINT_LOCALIZE_HH) 0 4 4
454 ) {
455 /* ??? */
456 0 croak("Constant(\\N{%"SVf"} unknown", SVfARG(name));
457 }
458  
459 4 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ( 4 0 4
460 4 !(cvp = hv_fetchs(table, "charnames", FALSE)) ||
461 4 ▶ !SvOK(*cvp)
Branch: 1 branch
BranchTrueFalseTotal
!SvOK(*cvp) 0 4 4
462 ) {
463 0 croak("Unknown charname '%"SVf"'", SVfARG(name));
464 }
465  
466 3 {
467 4 SV *r, *cv = *cvp;
468 {
469 4 dSP;
470  
471 4 PUSHSTACKi(PERLSI_OVERLOAD);
472 4 ENTER;
473 4 SAVETMPS;
474  
475 4 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 4 4
476 4 ▶ EXTEND(SP, 1);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 1); 0 4 4
477 4 PUSHs(name);
478 4 PUTBACK;
479  
480 4 call_sv(cv, G_SCALAR);
481 4 SPAGAIN;
482  
483 4 r = POPs;
484 4 SvREFCNT_inc_simple_void_NN(r);
485  
486 4 PUTBACK;
487 4 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 4 0 4
488 4 LEAVE;
489 }
490 4 POPSTACK;
491  
492 4 ▶ if (!SvOK(r)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(r)) { 1 3 4
493 1 SvREFCNT_dec(r);
494 1 croak("Unknown charname '%"SVf"'", SVfARG(name));
495 }
496  
497 3 sv_catsv(sv, r);
498 3 SvREFCNT_dec(r);
499 3 continue;
500 }
501 }
502  
503 break;
504 }
505  
506 15 default:
507 15 ▶ if (c >= '0' && c <= '7') {
Branch: 2 branches
BranchTrueFalseTotal
if (c >= '0' && c <= '7') { 8 7 15
if (c >= '0' && c <= '7') { 0 8 8
508 0 u = c - '0';
509 0 c = lex_peek_unichar(0);
510 0 ▶ if (c >= '0' && c <= '7') {
Branch: 2 branches
BranchTrueFalseTotal
if (c >= '0' && c <= '7') { 0 0 0
if (c >= '0' && c <= '7') { 0 0 0
511 0 u = u * 8 + (c - '0');
512 0 lex_read_unichar(0);
513 0 c = lex_peek_unichar(0);
514 0 ▶ if (c >= '0' && c <= '7') {
Branch: 2 branches
BranchTrueFalseTotal
if (c >= '0' && c <= '7') { 0 0 0
if (c >= '0' && c <= '7') { 0 0 0
515 0 u = u * 8 + (c - '0');
516 0 lex_read_unichar(0);
517 }
518 }
519 0 c = u;
520 }
521 15 break;
522 }
523 }
524  
525 7232 my_sv_cat_c(aTHX_ sv, c);
526 }
527  
528 {
529 78 OP *op = NULL;
530 size_t i;
531 6138 ▶ for (i = 0; i < vec->len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < vec->len; i++) { 6060 78 6138
532 6060 switch (vec->data[i].type) {
533 2022 case CHUNK_SV:
534 2022 my_op_cat_sv(aTHX_ &op, vec->data[i].ptr.sv);
535 2022 break;
536 4038 case CHUNK_OP:
537 4038 ▶ op = !op ? vec->data[i].ptr.op : newBINOP(OP_CONCAT, 0, op, vec->data[i].ptr.op);
Branch: 1 branch
BranchTrueFalseTotal
op = !op ? vec->data[i].ptr.op : newBINOP(OP_CONCAT, 0, op, vec->data[i].ptr.op); 13 4025 4038
538 4038 vec->data[i].ptr.op = NULL;
539 4038 break;
540 }
541 }
542  
543 78 ▶ if (SvCUR(sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvCUR(sv)) { 64 14 78
544 64 my_op_cat_sv(aTHX_ &op, sv);
545 }
546  
547 78 ▶ if (!op) {
Branch: 1 branch
BranchTrueFalseTotal
if (!op) { 1 77 78
548 1 op = newSVOP(OP_CONST, 0, newSVpvs(""));
549 }
550  
551 78 ▶ if (op->op_type == OP_CONST) {
Branch: 1 branch
BranchTrueFalseTotal
if (op->op_type == OP_CONST) { 48 30 78
552 48 SvPOK_only_UTF8(((SVOP *)op)->op_sv);
553 30 ▶ } else if (op->op_type != OP_CONCAT) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (op->op_type != OP_CONCAT) { 6 24 30
554 /* can't do this because B::Deparse dies on it:
555 * op = newUNOP(OP_STRINGIFY, 0, op);
556 */
557 6 op = newBINOP(OP_CONCAT, 0, op, newSVOP(OP_CONST, 0, newSVpvs("")));
558 }
559  
560 78 return op;
561 }
562 }
563  
564 28 static void parse_qc(pTHX_ OP **op_ptr) {
565 I32 c;
566  
567 28 c = lex_peek_unichar(0);
568  
569 28 ▶ if (c != '#') {
Branch: 1 branch
BranchTrueFalseTotal
if (c != '#') { 28 0 28
570 28 lex_read_space(0);
571 28 c = lex_peek_unichar(0);
572 28 ▶ if (c == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (c == -1) { 0 28 28
573 0 croak("Unexpected EOF after qc");
574 }
575 }
576 28 lex_read_unichar(0);
577  
578 {
579 28 I32 delim_start = c;
580 28 I32 delim_stop =
581 55 ▶ c == '(' ? ')' :
Branch: 1 branch
BranchTrueFalseTotal
c == '(' ? ')' : 27 1 28
582 53 ▶ c == '[' ? ']' :
Branch: 1 branch
BranchTrueFalseTotal
c == '[' ? ']' : 26 1 27
583 46 ▶ c == '{' ? '}' :
Branch: 1 branch
BranchTrueFalseTotal
c == '{' ? '}' : 20 6 26
584 20 ▶ c == '<' ? '>' :
Branch: 1 branch
BranchTrueFalseTotal
c == '<' ? '>' : 18 2 20
585 c
586 ;
587 28 int nesting = 0;
588 28 const QCSpec spec = {
589 delim_start, delim_stop,
590 NULL, NULL,
591 FLAG_BACKSLASH_ESCAPE
592 };
593  
594 28 ▶ *op_ptr = parse_qctail(aTHX_ &spec, delim_start == delim_stop ? NULL : &nesting);
Branch: 1 branch
BranchTrueFalseTotal
*op_ptr = parse_qctail(aTHX_ &spec, delim_start == delim_stop ? NULL : &nesting); 10 18 28
595 }
596 27 }
597  
598 15 static void parse_qcw(pTHX_ OP **op_ptr) {
599 I32 c;
600  
601 15 c = lex_peek_unichar(0);
602  
603 15 ▶ if (c != '#') {
Branch: 1 branch
BranchTrueFalseTotal
if (c != '#') { 15 0 15
604 15 lex_read_space(0);
605 15 c = lex_peek_unichar(0);
606 15 ▶ if (c == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (c == -1) { 0 15 15
607 0 croak("Unexpected EOF after qcw");
608 }
609 }
610 15 lex_read_unichar(0);
611  
612 {
613 15 I32 delim_start = c;
614 15 I32 delim_stop =
615 23 ▶ c == '(' ? ')' :
Branch: 1 branch
BranchTrueFalseTotal
c == '(' ? ')' : 8 7 15
616 15 ▶ c == '[' ? ']' :
Branch: 1 branch
BranchTrueFalseTotal
c == '[' ? ']' : 7 1 8
617 11 ▶ c == '{' ? '}' :
Branch: 1 branch
BranchTrueFalseTotal
c == '{' ? '}' : 4 3 7
618 4 ▶ c == '<' ? '>' :
Branch: 1 branch
BranchTrueFalseTotal
c == '<' ? '>' : 4 0 4
619 c
620 ;
621 15 int nesting = 0;
622 15 const QCSpec spec = {
623 delim_start, delim_stop,
624 NULL, NULL,
625 FLAG_BACKSLASH_ESCAPE | FLAG_STOP_AT_SPACE
626 };
627 OP **gen_sentinel;
628  
629 15 Newx(gen_sentinel, 1, OP *);
630 15 *gen_sentinel = NULL;
631 15 SAVEDESTRUCTOR_X(free_ptr_op, gen_sentinel);
632  
633 31 for (;;) {
634 OP *cur;
635 63 ▶ while (c = lex_peek_unichar(0), c != -1 && isSPACE(c)) {
Branch: 4 branches
BranchTrueFalseTotal
while (c = lex_peek_unichar(0), c != -1 && isSPACE(c)) { 63 0 63
while (c = lex_peek_unichar(0), c != -1 && isSPACE(c)) { 63 0 63
while (c = lex_peek_unichar(0), c != -1 && isSPACE(c)) { 17 46 63
while (c = lex_peek_unichar(0), c != -1 && isSPACE(c)) { 17 46 63
636 17 lex_read_unichar(0);
637 }
638 46 ▶ if (c == delim_stop && nesting == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (c == delim_stop && nesting == 0) { 15 31 46
if (c == delim_stop && nesting == 0) { 15 0 15
639 15 lex_read_unichar(0);
640 15 break;
641 }
642 31 ▶ cur = parse_qctail(aTHX_ &spec, delim_start == delim_stop ? NULL : &nesting);
Branch: 1 branch
BranchTrueFalseTotal
cur = parse_qctail(aTHX_ &spec, delim_start == delim_stop ? NULL : &nesting); 27 4 31
643 31 *gen_sentinel = op_append_elem(OP_LIST, *gen_sentinel, cur);
644 }
645  
646 15 ▶ if (*gen_sentinel) {
Branch: 1 branch
BranchTrueFalseTotal
if (*gen_sentinel) { 13 2 15
647 13 *op_ptr = *gen_sentinel;
648 13 *gen_sentinel = NULL;
649 } else {
650 2 *op_ptr = newNULLLIST();
651 }
652 15 (*op_ptr)->op_flags |= OPf_PARENS;
653 }
654 15 }
655  
656 21 static void parse_qc_to(pTHX_ OP **op_ptr) {
657 I32 c, qdelim;
658 SV *delim, *leftover;
659 line_t start;
660  
661 21 lex_read_space(0);
662 21 ▶ if (!strnEQ(PL_parser->bufptr, "<<", 2)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!strnEQ(PL_parser->bufptr, "<<", 2)) { 0 21 21
663 0 croak("Missing \"<<\" after qc_to");
664 }
665 21 lex_read_to(PL_parser->bufptr + 2);
666  
667 21 lex_read_space(0);
668 21 start = CopLINE(PL_curcop);
669 21 c = lex_peek_unichar(0);
670 21 ▶ if (!(c == '\'' || c == '"')) {
Branch: 2 branches
BranchTrueFalseTotal
if (!(c == '\'' || c == '"')) { 10 11 21
if (!(c == '\'' || c == '"')) { 0 10 10
671 0 croak("Missing \"'\" or '\"' after qc_to <<");
672 }
673 21 qdelim = c;
674 21 lex_read_unichar(0);
675  
676 21 delim = sv_2mortal(newSVpvs(""));
677 21 ▶ if (lex_bufutf8()) {
Branch: 1 branch
BranchTrueFalseTotal
if (lex_bufutf8()) { 0 21 21
678 0 SvUTF8_on(delim);
679 }
680  
681 for (;;) {
682 90 c = lex_read_unichar(0);
683 90 ▶ if (c == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (c == -1) { 0 90 90
684 0 CopLINE_set(PL_curcop, start);
685 0 ▶ croak("Can't find string terminator %s anywhere before EOF", qdelim == '"' ? "'\"'" : "\"'\"");
Branch: 1 branch
BranchTrueFalseTotal
croak("Can't find string terminator %s anywhere before EOF", qdelim == '"' ? "'\"'" : "\"'\""); 0 0 0
686 }
687 90 ▶ if (c == qdelim) {
Branch: 1 branch
BranchTrueFalseTotal
if (c == qdelim) { 21 69 90
688 21 break;
689 }
690 69 my_sv_cat_c(aTHX_ delim, c);
691 }
692  
693 {
694 21 char *fin = memchr(PL_parser->bufptr, '\n', PL_parser->bufend - PL_parser->bufptr);
695 21 ▶ if (fin) {
Branch: 1 branch
BranchTrueFalseTotal
if (fin) { 21 0 21
696 21 fin++;
697 } else {
698 0 fin = PL_parser->bufend;
699 }
700  
701 21 ▶ leftover = sv_2mortal(newSVpvn_utf8(PL_parser->bufptr, fin - PL_parser->bufptr, lex_bufutf8()));
Branch: 1 branch
BranchTrueFalseTotal
leftover = sv_2mortal(newSVpvn_utf8(PL_parser->bufptr, fin - PL_parser->bufptr, lex_bufutf8())); 0 21 21
702 21 lex_unstuff(fin);
703 }
704  
705 {
706 42 const QCSpec spec = {
707 -1, -1,
708 delim, leftover,
709 21 ▶ FLAG_HASH_INTERPOLATE | (qdelim == '"' ? FLAG_BACKSLASH_ESCAPE : 0)
Branch: 1 branch
BranchTrueFalseTotal
FLAG_HASH_INTERPOLATE | (qdelim == '"' ? FLAG_BACKSLASH_ESCAPE : 0) 10 11 21
710 };
711 21 *op_ptr = parse_qctail(aTHX_ &spec, NULL);
712 }
713 20 }
714  
715 67 static int qc_enabled(pTHX_ const char *hk_ptr, size_t hk_len) {
716 HV *hints;
717 SV *sv, **psv;
718  
719 67 ▶ if (!(hints = GvHV(PL_hintgv))) {
Branch: 1 branch
BranchTrueFalseTotal
if (!(hints = GvHV(PL_hintgv))) { 0 67 67
720 0 return FALSE;
721 }
722 67 ▶ if (!(psv = hv_fetch(hints, hk_ptr, hk_len, 0))) {
Branch: 1 branch
BranchTrueFalseTotal
if (!(psv = hv_fetch(hints, hk_ptr, hk_len, 0))) { 3 64 67
723 3 return FALSE;
724 }
725 64 sv = *psv;
726 64 return SvTRUE(sv);
727 }
728 #define qc_enableds(S) qc_enabled(aTHX_ STR_WITH_LEN(S))
729  
730 48626 static int my_keyword_plugin(pTHX_ char *keyword_ptr, STRLEN keyword_len, OP **op_ptr) {
731 int ret;
732  
733 48626 ▶ if (keyword_len == 2 && keyword_ptr[0] == 'q' && keyword_ptr[1] == 'c' && qc_enableds(HINTK_QC)) {
Branch: 4 branches
BranchTrueFalseTotal
if (keyword_len == 2 && keyword_ptr[0] == 'q' && keyword_ptr[1] == 'c' && qc_enableds(HINTK_QC)) { 15930 32696 48626
if (keyword_len == 2 && keyword_ptr[0] == 'q' && keyword_ptr[1] == 'c' && qc_enableds(HINTK_QC)) { 1476 14454 15930
if (keyword_len == 2 && keyword_ptr[0] == 'q' && keyword_ptr[1] == 'c' && qc_enableds(HINTK_QC)) { 31 1445 1476
if (keyword_len == 2 && keyword_ptr[0] == 'q' && keyword_ptr[1] == 'c' && qc_enableds(HINTK_QC)) { 28 3 31
734 28 ENTER;
735 28 parse_qc(aTHX_ op_ptr);
736 27 LEAVE;
737 27 ret = KEYWORD_PLUGIN_EXPR;
738 48598 ▶ } else if (keyword_len == 5 && memcmp(keyword_ptr, "qc_to", 5) == 0 && qc_enableds(HINTK_QC_TO)) {
Branch: 3 branches
BranchTrueFalseTotal
} else if (keyword_len == 5 && memcmp(keyword_ptr, "qc_to", 5) == 0 && qc_enableds(HINTK_QC_TO)) { 3907 44691 48598
} else if (keyword_len == 5 && memcmp(keyword_ptr, "qc_to", 5) == 0 && qc_enableds(HINTK_QC_TO)) { 21 3886 3907
} else if (keyword_len == 5 && memcmp(keyword_ptr, "qc_to", 5) == 0 && qc_enableds(HINTK_QC_TO)) { 21 0 21
739 21 ENTER;
740 21 parse_qc_to(aTHX_ op_ptr);
741 20 LEAVE;
742 20 ret = KEYWORD_PLUGIN_EXPR;
743 48577 ▶ } else if (keyword_len == 3 && memcmp(keyword_ptr, "qcw", 3) == 0 && qc_enableds(HINTK_QCW)) {
Branch: 3 branches
BranchTrueFalseTotal
} else if (keyword_len == 3 && memcmp(keyword_ptr, "qcw", 3) == 0 && qc_enableds(HINTK_QCW)) { 9647 38930 48577
} else if (keyword_len == 3 && memcmp(keyword_ptr, "qcw", 3) == 0 && qc_enableds(HINTK_QCW)) { 15 9632 9647
} else if (keyword_len == 3 && memcmp(keyword_ptr, "qcw", 3) == 0 && qc_enableds(HINTK_QCW)) { 15 0 15
744 15 ENTER;
745 15 parse_qcw(aTHX_ op_ptr);
746 15 LEAVE;
747 15 ret = KEYWORD_PLUGIN_EXPR;
748 } else {
749 48562 ret = next_keyword_plugin(aTHX_ keyword_ptr, keyword_len, op_ptr);
750 }
751  
752 48624 return ret;
753 }
754  
755 /* https://github.com/Perl/perl5/issues/16229 */
756 #ifndef wrap_keyword_plugin
757 #define wrap_keyword_plugin(A, B) S_wrap_keyword_plugin(aTHX_ A, B)
758 static void S_wrap_keyword_plugin(pTHX_ Perl_keyword_plugin_t new_plugin, Perl_keyword_plugin_t *old_plugin_p) {
759 PERL_UNUSED_CONTEXT;
760 if (*old_plugin_p) {
761 return;
762 }
763 MUTEX_LOCK(&PL_op_mutex);
764 if (!*old_plugin_p) {
765 *old_plugin_p = PL_keyword_plugin;
766 PL_keyword_plugin = new_plugin;
767 }
768 MUTEX_UNLOCK(&PL_op_mutex);
769 }
770 #endif
771  
772 10 static void my_boot(pTHX) {
773 10 HV *const stash = gv_stashpvs(MY_PKG, GV_ADD);
774  
775 10 newCONSTSUB(stash, "HINTK_QC", newSVpvs(HINTK_QC));
776 10 newCONSTSUB(stash, "HINTK_QC_TO", newSVpvs(HINTK_QC_TO));
777 10 newCONSTSUB(stash, "HINTK_QCW", newSVpvs(HINTK_QCW));
778  
779 10 wrap_keyword_plugin(my_keyword_plugin, &next_keyword_plugin);
780 10 }
781  
782 WARNINGS_RESET
783  
784 MODULE = Quote::Code PACKAGE = Quote::Code
785 PROTOTYPES: ENABLE
786  
787 BOOT:
788 10 my_boot(aTHX);
« blib/lib/Quote/Code.pm ↑ Summary