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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines