unused.xs

filter: Statementstmt 97.2% 106 / 109 Branchbran 90.7% 49 / 54 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 95.0% 155 / 163 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/warnings/unused.pm ↑ Summary
1 /* warnings-unused/unused.xs */
2  
3 /*
4 See also:
5 op.h
6 op.c
7 pad.h
8 pad.c
9 pp.h
10 pp.c
11 toke.c
12 perlguts.pod
13 perlhack.pod
14 perlapi.pod
15 */
16  
17 #define PERL_NO_GET_CONTEXT
18 #include <EXTERN.h>
19 #include <perl.h>
20 #include <XSUB.h>
21 #include <keywords.h> /* KEY_my, KEY_our */
22  
23 #include "ppport.h"
24  
25  
26 /* Since these APIs are not public, the definitions are a little complicated */
27  
28 #if PERL_BCDVERSION >= 0x5010000
29 # undef PL_tokenbuf /* defined in ppport.h */
30 # undef PL_in_my /* defined in ppport.h */
31 # define PL_tokenbuf (PL_parser->tokenbuf)
32 # define PL_in_my (PL_parser->in_my)
33 #else
34 # ifndef PL_tokenbuf
35 # define PL_tokenbuf PL_Itokenbuf
36 # define PL_in_my PL_Iin_my
37 # endif
38 #endif
39  
40 #define HINT_KEY "warnings::unused"
41  
42 #define SCOPE_KEY ((UV)PL_savestack_ix)
43  
44 #define MY_CXT_KEY "warnings::unused::_guts" XS_VERSION /* for backward compatibility */
45  
46 #define WARN_UNUSED WARN_ONCE
47  
48  
49 #define MESSAGE "Unused variable %s %s at %s line %"IVdf".\n"
50  
51 typedef struct{
52 AV* vars;
53 SV* scope_depth;
54 U32 scope_depth_hash;
55  
56 bool global;
57 } my_cxt_t;
58 START_MY_CXT;
59  
60 #ifdef WU_DEBUGGING
61 #define dump_vars() wl_dump_vars(aTHX)
62 static void
63 wl_dump_vars(pTHX){
64 dMY_CXT;
65 I32 const len = av_len(MY_CXT.vars)+1;
66 I32 i;
67  
68 PerlIO_printf(PerlIO_stderr(), "vars at %s line %"IVdf".\n", OutCopFILE(PL_curcop), (IV)CopLINE(PL_curcop));
69 for(i = 1; i < len; i++){
70 HV* const hv = (HV*)SvRV(*av_fetch(MY_CXT.vars, i, FALSE));
71 HE* he;
72  
73 PerlIO_printf(PerlIO_stderr(), " var table:\n");
74 hv_iterinit(hv);
75 while((he = hv_iternext(hv))){
76 if(SvOK(HeVAL(he))){
77 if(SvPOK(HeVAL(he))){
78 PerlIO_printf(PerlIO_stderr(), " %s unused\n", HeKEY(he));
79 }
80 else{
81 PerlIO_printf(PerlIO_stderr(), " %s=%"UVuf"\n", HeKEY(he), SvUVX(HeVAL(he)));
82 }
83 }
84 else{
85 PerlIO_printf(PerlIO_stderr(), " %s\n", HeKEY(he));
86 }
87 }
88 }
89 }
90 #else /* !WU_DEBUGGING */
91 #define dump_vars() NOOP
92 #endif
93  
94 #define wl_enabled() (ckWARN(WARN_UNUSED) && wl_scope_enabled(aTHX_ aMY_CXT))
95  
96 static int
97 8877 wl_scope_enabled(pTHX_ pMY_CXT){
98 8877 ▶ if(MY_CXT.global){
Branch: 1 branch
BranchTrueFalseTotal
if(MY_CXT.global){ 1033 7844 8877
99 1033 return TRUE;
100 }
101  
102 #if PERL_BCDVERSION >= 0x5014000
103 7844 ▶ if(PL_curcop->cop_hints_hash){
Branch: 1 branch
BranchTrueFalseTotal
if(PL_curcop->cop_hints_hash){ 50 7794 7844
104 50 SV* const sv = Perl_refcounted_he_fetch_pvn(aTHX_
105 50 PL_curcop->cop_hints_hash,
106 HINT_KEY, sizeof(HINT_KEY)-1, 0U, 0);
107 50 ▶ return sv && SvTRUE(sv);
Branch: 2 branches
BranchTrueFalseTotal
return sv && SvTRUE(sv); 50 0 50
return sv && SvTRUE(sv); 50 0 50
108 }
109 #elif PERL_BCDVERSION >= 0x5010000
110 if(PL_curcop->cop_hints_hash){
111 SV* const sv = Perl_refcounted_he_fetch(aTHX_
112 PL_curcop->cop_hints_hash, Nullsv,
113 HINT_KEY, sizeof(HINT_KEY)-1, FALSE, 0);
114 return sv && SvTRUE(sv);
115 }
116 #else
117 /* XXX: It may not work with other modules that use HINT_LOCALIZE_HH */
118 if(PL_hints & HINT_LOCALIZE_HH){
119 //SV** svp = hv_fetchs(GvHV(PL_hintgv), HINT_KEY, FALSE);
120  
121 return TRUE; //svp && SvTRUE(*svp);
122 }
123 #endif
124 7794 return FALSE;
125 }
126  
127 static UV
128 56542 wl_fetch_scope_depth(pTHX_ pMY_CXT_ HV* const tab){
129 56542 HE* const he = hv_fetch_ent(tab, MY_CXT.scope_depth, FALSE, MY_CXT.scope_depth_hash);
130 assert(he);
131  
132 56542 return SvUVX(HeVAL(he));
133 }
134  
135 static HV*
136 10918 wl_push_scope(pTHX_ pMY_CXT_ UV const key){
137 10918 HV* const hv = newHV();
138  
139 10918 (void)hv_store_ent(hv, MY_CXT.scope_depth, newSVuv(key), MY_CXT.scope_depth_hash);
140 10918 av_push(MY_CXT.vars, newRV_noinc((SV*)hv));
141  
142 10918 return hv;
143 }
144  
145 static void
146 7254 wl_flush(pTHX_ UV const key){
147 dMY_CXT;
148 7254 IV i = av_len(MY_CXT.vars) + 1;
149 12904 ▶ while(--i > 0){
Branch: 1 branch
BranchTrueFalseTotal
while(--i > 0){ 11362 1542 12904
150 11362 SV* const hvref = *av_fetch(MY_CXT.vars, i, FALSE);
151 11362 HV* const tab = (HV*)SvRV(hvref);
152 HE* he;
153  
154 assert(SvTYPE(tab) == SVt_PVHV);
155  
156 11362 ▶ if(wl_fetch_scope_depth(aTHX_ aMY_CXT_ tab) <= key){
Branch: 1 branch
BranchTrueFalseTotal
if(wl_fetch_scope_depth(aTHX_ aMY_CXT_ tab) <= key){ 5712 5650 11362
157 5712 break;
158 }
159  
160 /* each and warn */
161 5650 hv_iterinit(tab);
162  
163 25516 ▶ while( (he = hv_iternext(tab)) ){
Branch: 1 branch
BranchTrueFalseTotal
while( (he = hv_iternext(tab)) ){ 14216 5650 19866
164 14216 ▶ if(SvPOK(HeVAL(he))){
Branch: 1 branch
BranchTrueFalseTotal
if(SvPOK(HeVAL(he))){ 14154 62 14216
165 62 Perl_warner(aTHX_ WARN_UNUSED, "%" SVf, HeVAL(he));
166 }
167 }
168  
169 5650 av_pop(MY_CXT.vars);
170  
171 5650 SvREFCNT_dec(hvref);
172 }
173 7254 }
174  
175 static HV*
176 39920 wl_fetch_tab(pTHX_ pMY_CXT_ UV const key){
177 39920 SV* top = *av_fetch(MY_CXT.vars, -1, FALSE);
178 HV* tab;
179 UV top_depth;
180  
181 assert(SvROK(top));
182 assert(SvTYPE(SvRV(top)) == SVt_PVHV);
183  
184 39920 tab = (HV*)SvRV(top);
185  
186 39920 top_depth = wl_fetch_scope_depth(aTHX_ aMY_CXT_ tab);
187  
188 39920 ▶ if(top_depth < key){
Branch: 1 branch
BranchTrueFalseTotal
if(top_depth < key){ 10910 29010 39920
189 10910 tab = wl_push_scope(aTHX_ aMY_CXT_ key);
190 }
191 else{ /*top_depth >= key */
192  
193 /* pop scope if needed */
194 34270 ▶ while(top_depth > key){
Branch: 1 branch
BranchTrueFalseTotal
while(top_depth > key){ 5260 29010 34270
195 HE* he;
196  
197 5260 hv_iterinit(tab);
198  
199 16826 ▶ while( (he = hv_iternext(tab)) ){
Branch: 1 branch
BranchTrueFalseTotal
while( (he = hv_iternext(tab)) ){ 6306 5260 11566
200 6306 ▶ if(SvPOK(HeVAL(he))){ /* skip the SCOPE_DEPTH meta data */
Branch: 1 branch
BranchTrueFalseTotal
if(SvPOK(HeVAL(he))){ /* skip the SCOPE_DEPTH meta data */ 6300 6 6306
201 6 Perl_warner(aTHX_ WARN_UNUSED, "%" SVf, HeVAL(he));
202 }
203 }
204  
205 5260 av_pop(MY_CXT.vars);
206 5260 SvREFCNT_dec(top);
207  
208 5260 top = *av_fetch(MY_CXT.vars, -1, FALSE);
209 5260 tab = (HV*)SvRV(top);
210 5260 top_depth = wl_fetch_scope_depth(aTHX_ aMY_CXT_ tab);
211 }
212 }
213  
214 assert(SvTYPE(tab) == SVt_PVHV);
215  
216 39920 return tab;
217 }
218  
219 static Perl_check_t old_ck_padsv = NULL;
220 static Perl_check_t old_ck_padany = NULL;
221 static OP*
222 39920 wl_ck_padany(pTHX_ OP* const o){
223 39920 ▶ if(PL_in_my != KEY_our){
Branch: 1 branch
BranchTrueFalseTotal
if(PL_in_my != KEY_our){ 39920 0 39920
224 dMY_CXT;
225 39920 const char* const name = PL_tokenbuf;
226 39920 STRLEN const namelen = strlen(name);
227 39920 HV* hv = wl_fetch_tab(aTHX_ aMY_CXT_ SCOPE_KEY);
228  
229 #if 0
230 /* NOTE: The last resort is to consider PL_hints */
231 PerlIO_printf(PerlIO_stderr(),
232 "[%d] $^H=0x%x at %s line %"IVdf".\n",
233 (int)SCOPE_KEY, (unsigned)PL_hints, OutCopFILE(PL_curcop), (IV)CopLINE(PL_curcop));
234 #endif
235 /* declaration */
236 39920 ▶ if(PL_in_my){
Branch: 1 branch
BranchTrueFalseTotal
if(PL_in_my){ 9640 30280 39920
237 9640 SV** const svp = hv_fetch(hv, name, namelen, FALSE);
238 SV* msg;
239  
240 9640 ▶ if(svp && SvOK(*svp)){ /* shadowing */
Branch: 2 branches
BranchTrueFalseTotal
if(svp && SvOK(*svp)){ /* shadowing */ 28 9612 9640
if(svp && SvOK(*svp)){ /* shadowing */ 4 24 28
241 4 Perl_warner(aTHX_ WARN_UNUSED, "%" SVf, *svp);
242 }
243  
244 9640 ▶ if(wl_enabled()){
Branch: 2 branches
BranchTrueFalseTotal
if(wl_enabled()){ 8877 763 9640
if(wl_enabled()){ 1083 7794 8877
245  
246 1083 msg = Perl_newSVpvf(aTHX_
247 MESSAGE,
248 1083 ▶ PL_in_my == KEY_my ? "my" : "state",
Branch: 1 branch
BranchTrueFalseTotal
PL_in_my == KEY_my ? "my" : "state", 1082 1 1083
249 name,
250 1083 ▶ OutCopFILE(PL_curcop), (IV)CopLINE(PL_curcop));
Branch: 1 branch
BranchTrueFalseTotal
OutCopFILE(PL_curcop), (IV)CopLINE(PL_curcop)); 1083 0 1083
251 }
252 else{
253 8557 msg = &PL_sv_undef; /* marks but doesn't complain about it */
254 }
255  
256 9640 (void)hv_store(hv, name, namelen, msg, 0U);
257 }
258 /* use */
259 else{
260 SV** svp;
261 30280 I32 i = av_len(MY_CXT.vars)+1;
262  
263 /* search for the variable until it's found */
264 104623 ▶ while(!(svp = hv_fetch(hv, name, namelen, FALSE))){
Branch: 1 branch
BranchTrueFalseTotal
while(!(svp = hv_fetch(hv, name, namelen, FALSE))){ 74345 30278 104623
265 74345 ▶ if(i <= 0) break;
Branch: 1 branch
BranchTrueFalseTotal
if(i <= 0) break; 2 74343 74345
266  
267 74343 --i;
268 74343 hv = (HV*)SvRV( AvARRAY(MY_CXT.vars)[i] );
269 }
270  
271 30280 ▶ if(svp && SvOK(*svp)){ /* found */
Branch: 2 branches
BranchTrueFalseTotal
if(svp && SvOK(*svp)){ /* found */ 30278 2 30280
if(svp && SvOK(*svp)){ /* found */ 1011 29267 30278
272 1011 SvREFCNT_dec(*svp);
273 1011 *svp = &PL_sv_undef; /* mark as used */
274 }
275  
276 }
277  
278 //dump_vars();
279 }
280 39920 return o->op_type == OP_PADSV
281 10 ? old_ck_padsv (aTHX_ o)
282 39930 ▶ : old_ck_padany(aTHX_ o);
Branch: 1 branch
BranchTrueFalseTotal
: old_ck_padany(aTHX_ o); 10 39910 39920
283 }
284  
285 static Perl_check_t old_ck_leaveloop = NULL;
286 static Perl_check_t old_ck_leavesub = NULL;
287 static Perl_check_t old_ck_leaveeval = NULL;
288  
289 /* flush on the end of the scope */
290 static OP*
291 7246 wl_ck_leave(pTHX_ OP* const o){
292 Perl_check_t ck_proc;
293  
294 7246 wl_flush(aTHX_ SCOPE_KEY);
295  
296 7246 switch(o->op_type){
297 926 case OP_LEAVELOOP:
298 926 ck_proc = old_ck_leaveloop;
299 926 break;
300 5713 case OP_LEAVESUB:
301 5713 ck_proc = old_ck_leavesub;
302 5713 break;
303 607 case OP_LEAVEEVAL:
304 607 ck_proc = old_ck_leaveeval;
305 607 break;
306 0 default:
307 0 ck_proc = NULL;
308 }
309 assert(ck_proc);
310  
311 //PerlIO_printf(PerlIO_stderr(), "[%s] at %s line %"IVdf".\n", OP_NAME(o), OutCopFILE(PL_curcop), (IV)CopLINE(PL_curcop));
312  
313 7246 return ck_proc(aTHX_ o);
314 }
315  
316 MODULE = warnings::unused PACKAGE = warnings::unused
317  
318 PROTOTYPES: DISABLE
319  
320 BOOT:
321 {
322 MY_CXT_INIT;
323 8 MY_CXT.vars = newAV();
324 8 MY_CXT.scope_depth = newSVpvs("scope_depth");
325 8 PERL_HASH(MY_CXT.scope_depth_hash, "scope_depth", sizeof("scope_depth")-1);
326 8 MY_CXT.global = FALSE;
327 /* the stack of varible tables */
328 /* the root variable table */
329 8 wl_push_scope(aTHX_ aMY_CXT_ (UV)1);
330 /* install check hooks */
331 {
332 8 old_ck_padany = PL_check[OP_PADANY];
333 8 PL_check[OP_PADANY] = wl_ck_padany;
334  
335 8 old_ck_padsv = PL_check[OP_PADSV];
336 8 PL_check[OP_PADSV] = wl_ck_padany;
337  
338 /* leave scope */
339 8 old_ck_leavesub = PL_check[OP_LEAVESUB];
340 8 PL_check[OP_LEAVESUB] = wl_ck_leave;
341  
342 8 old_ck_leaveeval = PL_check[OP_LEAVEEVAL];
343 8 PL_check[OP_LEAVEEVAL] = wl_ck_leave;
344  
345 8 old_ck_leaveloop = PL_check[OP_LEAVELOOP];
346 8 PL_check[OP_LEAVELOOP] = wl_ck_leave;
347 }
348 }
349  
350 void
351 _set_mode(mode)
352 const char* mode
353 PREINIT:
354 dMY_CXT;
355 CODE:
356 2 ▶ if(strEQ(mode, "-global")){
Branch: 1 branch
BranchTrueFalseTotal
if(strEQ(mode, "-global")){ 1 1 2
357 1 MY_CXT.global = TRUE;
358 }
359 1 ▶ else if(strEQ(mode, "-lexical")){
Branch: 1 branch
BranchTrueFalseTotal
else if(strEQ(mode, "-lexical")){ 1 0 1
360 1 MY_CXT.global = FALSE;
361 }
362 else{
363 0 Perl_croak(aTHX_ "Unknown mode %s", mode);
364 }
365  
366  
367 void
368 END(...)
369 CODE:
370 PERL_UNUSED_VAR(items);
371 8 wl_flush(aTHX_ 0);
« blib/lib/warnings/unused.pm ↑ Summary