ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Devel-FindRef/FindRef.xs
(Generate patch)

Comparing Devel-FindRef/FindRef.xs (file contents):
Revision 1.3 by root, Thu Jan 11 23:06:51 2007 UTC vs.
Revision 1.9 by root, Fri Jul 11 15:29:48 2008 UTC

1#include "EXTERN.h" 1#include "EXTERN.h"
2#include "perl.h" 2#include "perl.h"
3#include "XSUB.h" 3#include "XSUB.h"
4 4
5#define res_pair(text) \ 5#define PERL_VERSION_ATLEAST(a,b,c) \
6 { \ 6 (PERL_REVISION > (a) \
7 AV *av = newAV (); \ 7 || (PERL_REVISION == (a) \
8 av_push (av, newSVpv (text, 0)); \ 8 && (PERL_VERSION > (b) \
9 av_push (av, newRV_inc (sv)); \ 9 || (PERL_VERSION == (b) && PERLSUBVERSION >= (c)))))
10 av_push (about, newRV_noinc ((SV *)av)); \
11 }
12 10
13#define res_gv(sigil) \ 11#if !PERL_VERSION_ATLEAST (5,8,9)
12# define SVt_LAST 16
13#endif
14
15#if !PERL_VERSION_ATLEAST (5,10,0)
16# define SvPAD_OUR(dummy) 0
17#endif
18
19#define res_pair(text) \
14 { \ 20 do { \
15 AV *av = newAV (); \ 21 AV *av = newAV (); \
22 av_push (av, newSVpv (text, 0)); \
23 if (rmagical) SvRMAGICAL_on (sv); \
24 av_push (av, sv_rvweaken (newRV_inc (sv))); \
25 if (rmagical) SvRMAGICAL_off (sv); \
26 av_push (about, newRV_noinc ((SV *)av)); \
27 } while (0)
28
29#define res_gv(sigil) \
30 do { \
31 AV *av = newAV (); \
16 av_push (av, newSVpv (form ("in the global %c%s::%.*s", sigil, \ 32 av_push (av, newSVpv (form ("in the global %c%s::%.*s", sigil, \
17 HvNAME (GvSTASH (sv)), \ 33 HvNAME (GvSTASH (sv)), \
18 GvNAMELEN (sv), GvNAME (sv) ? GvNAME (sv) : "<anonymous>"), \ 34 GvNAMELEN (sv), GvNAME (sv) ? GvNAME (sv) : "<anonymous>"), \
19 0)); \ 35 0)); \
20 av_push (about, newRV_noinc ((SV *)av)); \ 36 av_push (about, newRV_noinc ((SV *)av)); \
21 } 37 } while (0)
22 38
23MODULE = Devel::FindRef PACKAGE = Devel::FindRef 39MODULE = Devel::FindRef PACKAGE = Devel::FindRef
24 40
25PROTOTYPES: ENABLE 41PROTOTYPES: ENABLE
26 42
34void 50void
35find_ (SV *target) 51find_ (SV *target)
36 PPCODE: 52 PPCODE:
37{ 53{
38 SV *arena, *targ; 54 SV *arena, *targ;
39 int rmagical, i; 55 U32 rmagical;
56 int i;
40 AV *about = newAV (); 57 AV *about = newAV ();
41 AV *excl = newAV (); 58 AV *excl = newAV ();
42 59
43 if (!SvROK (target)) 60 if (!SvROK (target))
44 croak ("find expects a reference to a perl value"); 61 croak ("find expects a reference to a perl value");
62 if ((rmagical = SvRMAGICAL (sv))) 79 if ((rmagical = SvRMAGICAL (sv)))
63 SvRMAGICAL_off (sv); 80 SvRMAGICAL_off (sv);
64 81
65 if (SvTYPE (sv) >= SVt_PVMG) 82 if (SvTYPE (sv) >= SVt_PVMG)
66 { 83 {
67 MAGIC *mg = SvMAGIC (sv); 84
68 while (mg) 85 if (SvTYPE (sv) != SVt_PVMG || SvPAD_OUR (sv))
69 { 86 {
70 if (mg->mg_obj == targ) 87 MAGIC *mg = SvMAGIC (sv);
71 res_pair (form ("referenced (in mg_obj) by '%c' type magic attached to", mg->mg_type));
72 if ((SV *)mg->mg_ptr == targ && mg->mg_flags & MGf_REFCOUNTED)
73 res_pair (form ("referenced (in mg_ptr) by '%c' type magic attached to", mg->mg_type));
74 88
89 while (mg)
90 {
91 if (mg->mg_obj == targ)
92 res_pair (form ("referenced (in mg_obj) by '%c' type magic attached to", mg->mg_type));
93
94 if ((SV *)mg->mg_ptr == targ && mg->mg_flags & MGf_REFCOUNTED)
95 res_pair (form ("referenced (in mg_ptr) by '%c' type magic attached to", mg->mg_type));
96
75 mg = mg->mg_moremagic; 97 mg = mg->mg_moremagic;
98 }
76 } 99 }
77 } 100 }
78 101
79 switch (SvTYPE (sv)) 102 if (SvROK (sv))
80 { 103 {
81 case SVt_RV:
82 if (sv != target && SvRV (sv) == targ) 104 if (sv != target && SvRV (sv) == targ && !SvWEAKREF (sv))
83 res_pair ("referenced by"); 105 res_pair ("referenced by");
106 }
107 else
108 switch (SvTYPE (sv))
84 break; 109 {
110 case SVt_PVAV:
111 if (AvREAL (sv))
112 for (i = AvFILLp (sv) + 1; i--; )
113 if (AvARRAY (sv)[i] == targ)
114 res_pair (form ("in array element %d of", i));
85 115
86 case SVt_PVAV:
87 if (AvREAL (sv))
88 for (i = AvFILLp (sv) + 1; i--; )
89 if (AvARRAY (sv)[i] == targ)
90 res_pair (form ("in array element %d of", i));
91 break; 116 break;
92 117
93 case SVt_PVHV: 118 case SVt_PVHV:
94 if (hv_iterinit ((HV *)sv)) 119 if (hv_iterinit ((HV *)sv))
120 {
121 HE *he;
122
123 while ((he = hv_iternext ((HV *)sv)))
124 if (HeVAL (he) == targ)
125 res_pair (form ("in the member '%.*s' of", HeKLEN (he), HeKEY (he)));
126 }
127
128 break;
129
130 case SVt_PVCV:
95 { 131 {
132 int depth = CvDEPTH (sv);
133
134 /* Anonymous subs have a padlist but zero depth */
135 if (!depth && CvPADLIST (sv))
136 depth = 1;
137
138 if (depth)
96 HE *he; 139 {
97 while ((he = hv_iternext ((HV *)sv))) 140 AV *padlist = CvPADLIST (sv);
141
142 while (depth)
143 {
144 AV *pad = (AV *)AvARRAY (padlist)[depth];
145
146 av_push (excl, newSVuv (PTR2UV (pad))); /* exclude pads themselves from being found */
147
148 for (i = AvFILLp (pad) + 1; i--; )
98 if (HeVAL (he) == targ) 149 if (AvARRAY (pad)[i] == targ)
99 res_pair (form ("in the member '%.*s' of", HeKLEN (he), HeKEY (he))); 150 {
151 /* Values from constant functions are stored in the pad without any name */
152 SV *name_sv = AvARRAY (AvARRAY (padlist)[0])[i];
153
154 if (name_sv && SvPOK (name_sv))
155 res_pair (form ("in the lexical '%s' in", SvPVX (name_sv)));
156 else
157 res_pair ("in an unnamed lexical in");
158 }
159
160 --depth;
161 }
162 }
163
164 if (CvCONST (sv) && (SV*)CvXSUBANY (sv).any_ptr == targ)
165 res_pair ("the constant value of");
166
167 if (!CvWEAKOUTSIDE (sv) && (SV*)CvOUTSIDE (sv) == targ)
168 res_pair ("the containing scope for");
100 } 169 }
101 break;
102 170
171 break;
172
103 case SVt_PVCV: 173 case SVt_PVGV:
104 {
105 int depth = CvDEPTH (sv);
106 if (depth) 174 if (GvGP (sv))
107 { 175 {
108 AV *padlist = CvPADLIST (sv); 176 if (GvSV (sv) == (SV *)targ) res_gv ('$');
109 while (depth) 177 if (GvAV (sv) == (AV *)targ) res_gv ('@');
178 if (GvHV (sv) == (HV *)targ) res_gv ('%');
179 if (GvCV (sv) == (CV *)targ) res_gv ('&');
110 { 180 }
111 AV *pad = (AV *)AvARRAY (padlist)[depth];
112 av_push (excl, newSVuv (PTR2UV (pad))); /* exclude pads from being found */
113 for (i = AvFILLp (pad); i--; )
114 if (AvARRAY (pad)[i] == targ)
115 res_pair (form ("in the lexical '%s' in", SvPVX (AvARRAY (AvARRAY (padlist)[0])[i])));
116 181
117 --depth;
118 }
119 }
120 }
121 break; 182 break;
122
123 case SVt_PVGV:
124 if (GvGP (sv))
125 {
126 if (GvSV (sv) == targ)
127 res_gv ('$');
128 if (GvAV (sv) == (AV *)targ)
129 res_gv ('@');
130 if (GvHV (sv) == (HV *)targ)
131 res_gv ('%');
132 if (GvCV (sv) == (CV *)targ)
133 res_gv ('&');
134 }
135 break;
136 } 183 }
137 184
138 if (rmagical) 185 if (rmagical)
139 SvRMAGICAL_off (sv); 186 SvRMAGICAL_on (sv);
140 } 187 }
141 } 188 }
142 189
143 EXTEND (SP, 2); 190 EXTEND (SP, 2);
144 PUSHs (sv_2mortal (newRV_noinc ((SV *)about))); 191 PUSHs (sv_2mortal (newRV_noinc ((SV *)about)));

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines