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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines