Xref.pm revision 7c478bd95313f5f23a4c958a745db2134aa03244
package B::Xref;
=head1 NAME
B::Xref - Generates cross reference reports for Perl programs
=head1 SYNOPSIS
perl -MO=Xref[,OPTIONS] foo.pl
=head1 DESCRIPTION
The B::Xref module is used to generate a cross reference listing of all
definitions and uses of variables, subroutines and formats in a Perl program.
It is implemented as a backend for the Perl compiler.
The report generated is in the following format:
File filename1
Subroutine subname1
Package package1
object1 C<line numbers>
object2 C<line numbers>
...
Package package2
...
Each B<File> section reports on a single file. Each B<Subroutine> section
reports on a single subroutine apart from the special cases
"(definitions)" and "(main)". These report, respectively, on subroutine
definitions found by the initial symbol table walk and on the main part of
the program or module external to all subroutines.
The report is then grouped by the B<Package> of each variable,
subroutine or format with the special case "(lexicals)" meaning
lexical variables. Each B<object> name (implicitly qualified by its
containing B<Package>) includes its type character(s) at the beginning
where possible. Lexical variables are easier to track and even
included dereferencing information where possible.
The C<line numbers> are a comma separated list of line numbers (some
preceded by code letters) where that object is used in some way.
Simple uses aren't preceded by a code letter. Introductions (such as
where a lexical is first defined with C<my>) are indicated with the
letter "i". Subroutine and method calls are indicated by the character
"&". Subroutine definitions are indicated by "s" and format
definitions by "f".
=head1 OPTIONS
Option words are separated by commas (not whitespace) and follow the
usual conventions of compiler backend options.
=over 8
=item C<-oFILENAME>
Directs output to C<FILENAME> instead of standard output.
=item C<-r>
Raw output. Instead of producing a human-readable report, outputs a line
in machine-readable form for each definition/use of a variable/sub/format.
=item C<-D[tO]>
(Internal) debug options, probably only useful if C<-r> included.
The C<t> option prints the object on the top of the stack as it's
being tracked. The C<O> option prints each operator as it's being
processed in the execution order of the program.
=back
=head1 BUGS
Non-lexical variables are quite difficult to track through a program.
Sometimes the type of a non-lexical variable's use is impossible to
determine. Introductions of non-lexical non-scalars don't seem to be
reported properly.
=head1 AUTHOR
Malcolm Beattie, mbeattie@sable.ox.ac.uk.
=cut
use strict;
use Config;
);
my @pad; # lexicals in current pad
# as ["(lexical)", type, name]
my %done; # keyed by $$op: set when each $op is done
# [pack, type, name] (pack can be "(lexical)")
my $file; # shadows current filename
my $line; # shadows current line number
my $subname; # shadows current sub name
my %table; # Multi-level hash to record all uses etc.
my @todo = (); # List of CVs that need processing
# Options
sub process {
if ($type eq "*") {
if ($event eq "used") {
return;
} elsif ($event eq "subused") {
$type = "&";
}
}
$type =~ s/(.)\*$/$1/g;
if ($raw) {
printf "%-16s %-12s %5d %-12s %4s %-16s %s\n",
} else {
# Wheee
$line);
}
}
sub load_pad {
my $padlist = shift;
@pad = ();
}
if ($Config{useithreads}) {
my (@vallist);
# these pad GVs don't have corresponding names, so same @pad
# array can be used without collisions
}
}
}
sub xref {
my $start = shift;
my $op;
} elsif ($opname eq "substcont") {
redo;
} elsif ($opname eq "enterloop") {
} elsif ($opname eq "subst") {
} else {
no strict 'refs';
my $ppname = "pp_$opname";
}
}
}
sub xref_cv {
my $cv = shift;
$subname = "(main)";
}
sub xref_object {
my $cvref = shift;
}
sub xref_main {
$subname = "(main)";
while (@todo) {
}
}
sub pp_nextstate {
my $op = shift;
}
sub pp_padsv {
my $op = shift;
}
sub deref {
}
sub pp_gvsv {
my $op = shift;
my $gv;
if ($Config{useithreads}) {
}
else {
}
}
sub pp_gv {
my $op = shift;
my $gv;
if ($Config{useithreads}) {
}
else {
}
}
sub pp_const {
my $op = shift;
# constant could be in the pad (under useithreads)
if ($$sv) {
}
else {
}
}
sub pp_method {
my $op = shift;
}
sub pp_entersub {
my $op = shift;
} else {
}
}
#
# Stuff for cross referencing definitions of variables and subs
#
my $gv = shift;
if ($$cv) {
#return if $done{$$cv}++;
}
if ($$form) {
}
}
sub xref_definitions {
return if $nodefs;
$subname = "(definitions)";
}
no strict qw(vars refs);
}
sub output {
return if $raw;
print "File $file\n";
print " Subroutine $subname\n";
foreach $pack (sort(keys(%$persubname))) {
print " Package $pack\n";
my @lines;
}
}
}
}
}
}
}
sub compile {
my @options = @_;
if ($option =~ /^-(.)(.*)/) {
$opt = $1;
$arg = $2;
} else {
last OPTION;
}
shift @options;
last OPTION;
} elsif ($opt eq "o") {
} elsif ($opt eq "d") {
$nodefs = 1;
} elsif ($opt eq "r") {
$raw = 1;
} elsif ($opt eq "D") {
if ($arg eq "o") {
B->debug(1);
} elsif ($arg eq "O") {
$debug_op = 1;
} elsif ($arg eq "t") {
$debug_top = 1;
}
}
}
}
if (@options) {
return sub {
my $objname;
eval "xref_object(\\&$objname)";
die "xref_object(\\&$objname) failed: $@" if $@;
}
output();
}
} else {
return sub {
xref_main();
output();
}
}
}
1;