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.4 by root, Wed Feb 7 21:34:02 2007 UTC vs.
Revision 1.11 by root, Fri Jul 11 17:59:19 2008 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines