]> git.vpit.fr Git - perl/modules/indirect.git/blob - indirect.xs
Hide a very unlikely failure from coverage
[perl/modules/indirect.git] / indirect.xs
1 /* This file is part of the indirect Perl module.
2  * See http://search.cpan.org/dist/indirect/ */
3
4 #define PERL_NO_GET_CONTEXT
5 #include "EXTERN.h"
6 #include "perl.h"
7 #include "XSUB.h"
8
9 #ifndef SvPV_const
10 # define SvPV_const SvPV
11 #endif
12
13 #ifndef SvPV_nolen_const
14 # define SvPV_nolen_const SvPV_nolen
15 #endif
16
17 #ifndef SvPVX_const
18 # define SvPVX_const SvPVX
19 #endif
20
21 #ifndef sv_catpvn_nomg
22 # define sv_catpvn_nomg sv_catpvn
23 #endif
24
25 /* ... Hints ............................................................... */
26
27 STATIC U32 indirect_hash = 0;
28
29 STATIC IV indirect_hint(pTHX) {
30 #define indirect_hint() indirect_hint(aTHX)
31  SV *id = Perl_refcounted_he_fetch(aTHX_ PL_curcop->cop_hints_hash,
32                                          NULL,
33                                          "indirect", 8,
34                                          0,
35                                          indirect_hash);
36  return (id && SvOK(id) && SvIOK(id)) ? SvIV(id) : 0;
37 }
38
39 /* ... op -> source position ............................................... */
40
41 STATIC HV *indirect_map = NULL;
42 STATIC const char *indirect_linestr = NULL;
43
44 #define OP2STR(O) (sprintf(buf, "%u", PTR2UV(o)))
45
46 STATIC void indirect_map_store(pTHX_ const OP *o, const char *src, SV *sv) {
47 #define indirect_map_store(O, S, N) indirect_map_store(aTHX_ (O), (S), (N))
48  char buf[32];
49  const char *pl_linestr;
50  SV *val;
51
52  /* When lex_inwhat is set, we're in a quotelike environment (qq, qr, but not q)
53   * In this case the linestr has temporarly changed, but the old buffer should
54   * still be alive somewhere. */
55
56  if (!PL_parser->lex_inwhat) {
57   pl_linestr = SvPVX_const(PL_parser->linestr);
58   if (indirect_linestr != pl_linestr) {
59    hv_clear(indirect_map);
60    indirect_linestr = pl_linestr;
61   }
62  }
63
64  val = newSVsv(sv);
65  SvUPGRADE(val, SVt_PVIV);
66  SvUVX(val) = PTR2UV(src);
67  SvIOK_on(val);
68  SvIsUV_on(val);
69  if (!hv_store(indirect_map, buf, OP2STR(o), val, 0)) SvREFCNT_dec(val);
70 }
71
72 STATIC const char *indirect_map_fetch(pTHX_ const OP *o, SV ** const name) {
73 #define indirect_map_fetch(O, S) indirect_map_fetch(aTHX_ (O), (S))
74  char buf[32];
75  SV **val;
76
77  if (indirect_linestr != SvPVX_const(PL_parser->linestr))
78   return NULL;
79
80  val = hv_fetch(indirect_map, buf, OP2STR(o), 0);
81  if (!val) {
82   *name = NULL;
83   return NULL;
84  }
85
86  *name = *val;
87  return INT2PTR(const char *, SvUVX(*val));
88 }
89
90 STATIC void indirect_map_delete(pTHX_ const OP *o) {
91 #define indirect_map_delete(O) indirect_map_delete(aTHX_ (O))
92  char buf[32];
93
94  hv_delete(indirect_map, buf, OP2STR(o), G_DISCARD);
95 }
96
97 STATIC void indirect_map_clean(pTHX_ const OP *o) {
98 #define indirect_map_clean(O) indirect_map_clean(aTHX_ (O))
99  if (o->op_flags & OPf_KIDS) {
100   const OP *kid = cUNOPo->op_first;
101   for (; kid; kid = kid->op_sibling) {
102    indirect_map_delete(kid);
103    indirect_map_clean(kid);
104   }
105  } else {
106   indirect_map_delete(o);
107  }
108 }
109
110 STATIC const char *indirect_find(pTHX_ SV *sv, const char *s) {
111 #define indirect_find(N, S) indirect_find(aTHX_ (N), (S))
112  STRLEN len;
113  const char *p = NULL, *r = SvPV_const(sv, len);
114
115  p = strstr(s, r);
116  while (p) {
117   p += len;
118   if (!isALNUM(*p))
119    break;
120   p = strstr(p + 1, r);
121  }
122
123  return p;
124 }
125
126 /* ... ck_const ............................................................ */
127
128 STATIC OP *(*indirect_old_ck_const)(pTHX_ OP *) = 0;
129
130 STATIC OP *indirect_ck_const(pTHX_ OP *o) {
131  o = CALL_FPTR(indirect_old_ck_const)(aTHX_ o);
132
133  if (indirect_hint()) {
134   SV *sv = cSVOPo_sv;
135   if (SvPOK(sv) && (SvTYPE(sv) >= SVt_PV))
136    indirect_map_store(o, indirect_find(sv, PL_parser->oldbufptr), sv);
137  }
138
139  return o;
140 }
141
142 /* ... ck_rv2sv ............................................................ */
143
144 STATIC OP *(*indirect_old_ck_rv2sv)(pTHX_ OP *) = 0;
145
146 STATIC OP *indirect_ck_rv2sv(pTHX_ OP *o) {
147  if (indirect_hint()) {
148   OP *op = cUNOPo->op_first;
149   const char *name = NULL;
150   STRLEN len;
151   switch (op->op_type) {
152    case OP_GV:
153    case OP_GVSV: {
154     GV *gv = cGVOPx_gv(op);
155     name = GvNAME(gv);
156     len  = GvNAMELEN(gv);
157     break;
158    }
159    default: {
160     SV *sv = cSVOPx_sv(op);
161     if (SvPOK(sv) && (SvTYPE(sv) >= SVt_PV))
162      name = SvPV_const(sv, len);
163     break;
164    }
165   }
166   if (name) {
167    SV *sv = sv_2mortal(newSVpvn("$", 1));
168    sv_catpvn_nomg(sv, name, len);
169    const char *s;
170    s = indirect_find(sv, PL_parser->oldbufptr);
171    if (!s) { /* If it failed, retry without the current stash */
172     const char *stash = HvNAME_get(PL_curstash);
173     STRLEN stashlen = HvNAMELEN_get(PL_curstash);
174     if ((len < stashlen + 2) || name != strstr(name, stash)) {
175      /* Failed again ? Try to remove main */
176      stash = "main";
177      stashlen = 4;
178      if ((len < stashlen + 2) || name != strstr(name, stash))
179       goto done;
180     }
181     sv_setpvn(sv, "$", 1);
182     if (name[stashlen] != ':' || name[stashlen+1] != ':')
183      goto done;
184     stashlen += 2;
185     sv_catpvn_nomg(sv, name + stashlen, len - stashlen);
186     s = indirect_find(sv, PL_parser->oldbufptr);
187     if (!s)
188      goto done;
189    }
190    o = CALL_FPTR(indirect_old_ck_rv2sv)(aTHX_ o);
191    indirect_map_store(o, s, sv);
192    return o;
193   }
194  }
195
196 done:
197  return CALL_FPTR(indirect_old_ck_rv2sv)(aTHX_ o);
198 }
199
200 /* ... ck_padany ........................................................... */
201
202 STATIC OP *(*indirect_old_ck_padany)(pTHX_ OP *) = 0;
203
204 STATIC OP *indirect_ck_padany(pTHX_ OP *o) {
205  o = CALL_FPTR(indirect_old_ck_padany)(aTHX_ o);
206
207  if (indirect_hint()) {
208   SV *sv;
209   const char *s = PL_parser->oldbufptr, *t = PL_parser->bufptr - 1;
210
211   while (s < t && isSPACE(*s)) ++s;
212   while (t > s && isSPACE(*t)) --t;
213   sv = sv_2mortal(newSVpvn(s, t - s + 1));
214
215   indirect_map_store(o, s, sv);
216  }
217
218  return o;
219 }
220
221 /* ... ck_method ........................................................... */
222
223 STATIC OP *(*indirect_old_ck_method)(pTHX_ OP *) = 0;
224
225 STATIC OP *indirect_ck_method(pTHX_ OP *o) {
226  if (indirect_hint()) {
227   OP *op = cUNOPo->op_first;
228   SV *sv;
229   const char *s = indirect_map_fetch(op, &sv);
230   if (!s) {
231    sv = cSVOPx_sv(op);
232    if (!SvPOK(sv) || (SvTYPE(sv) < SVt_PV))
233     goto done;
234    sv = sv_mortalcopy(sv);
235    s  = indirect_find(sv, PL_parser->oldbufptr);
236   }
237   o = CALL_FPTR(indirect_old_ck_method)(aTHX_ o);
238   /* o may now be a method_named */
239   indirect_map_store(o, s, sv);
240   return o;
241  }
242
243 done:
244  return CALL_FPTR(indirect_old_ck_method)(aTHX_ o);
245 }
246
247 /* ... ck_entersub ......................................................... */
248
249 STATIC const char indirect_msg[] = "Indirect call of method \"%s\" on object \"%s\"";
250
251 STATIC OP *(*indirect_old_ck_entersub)(pTHX_ OP *) = 0;
252
253 STATIC OP *indirect_ck_entersub(pTHX_ OP *o) {
254  LISTOP *op;
255  OP *om, *oo;
256  IV hint = indirect_hint();
257
258  o = CALL_FPTR(indirect_old_ck_entersub)(aTHX_ o);
259
260  if (hint) {
261   const char *pm, *po;
262   SV *svm, *svo;
263   oo = o;
264   do {
265    op = (LISTOP *) oo;
266    if (!op->op_flags & OPf_KIDS)
267     goto done;
268    oo = op->op_first;
269   } while (oo->op_type != OP_PUSHMARK);
270   oo = oo->op_sibling;
271   om = op->op_last;
272   if (om->op_type == OP_METHOD)
273    om = cUNOPx(om)->op_first;
274   else if (om->op_type != OP_METHOD_NAMED)
275    goto done;
276   pm = indirect_map_fetch(om, &svm);
277   po = indirect_map_fetch(oo, &svo);
278   if (pm && po && pm < po) {
279    const char *psvm = SvPV_nolen_const(svm), *psvo = SvPV_nolen_const(svo);
280    if (hint == 2)
281     croak(indirect_msg, psvm, psvo);
282    else
283     warn(indirect_msg, psvm, psvo);
284   }
285 done:
286   indirect_map_clean(o);
287  }
288
289  return o;
290 }
291
292 STATIC U32 indirect_initialized = 0;
293
294 /* --- XS ------------------------------------------------------------------ */
295
296 MODULE = indirect      PACKAGE = indirect
297
298 PROTOTYPES: DISABLE
299
300 BOOT:
301 {
302  if (!indirect_initialized++) {
303   PERL_HASH(indirect_hash, "indirect", 8);
304   indirect_map = newHV();
305   indirect_old_ck_const    = PL_check[OP_CONST];
306   PL_check[OP_CONST]       = MEMBER_TO_FPTR(indirect_ck_const);
307   indirect_old_ck_rv2sv    = PL_check[OP_RV2SV];
308   PL_check[OP_RV2SV]       = MEMBER_TO_FPTR(indirect_ck_rv2sv);
309   indirect_old_ck_padany   = PL_check[OP_PADANY];
310   PL_check[OP_PADANY]      = MEMBER_TO_FPTR(indirect_ck_padany);
311   indirect_old_ck_method   = PL_check[OP_METHOD];
312   PL_check[OP_METHOD]      = MEMBER_TO_FPTR(indirect_ck_method);
313   indirect_old_ck_entersub = PL_check[OP_ENTERSUB];
314   PL_check[OP_ENTERSUB]    = MEMBER_TO_FPTR(indirect_ck_entersub);
315  }
316 }