<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">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&lt;line numbers&gt;
	  object2        C&lt;line numbers&gt;
	  ...
	Package package2
	...

Each B&lt;File&gt; section reports on a single file. Each B&lt;Subroutine&gt; 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&lt;Package&gt; of each variable,
subroutine or format with the special case "(lexicals)" meaning
lexical variables. Each B&lt;object&gt; name (implicitly qualified by its
containing B&lt;Package&gt;) 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&lt;line numbers&gt; 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&lt;my&gt;) are indicated with the
letter "i". Subroutine and method calls are indicated by the character
"&amp;".  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&lt;-oFILENAME&gt;

Directs output to C&lt;FILENAME&gt; instead of standard output.

=item C&lt;-r&gt;

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&lt;-D[tO]&gt;

(Internal) debug options, probably only useful if C&lt;-r&gt; included.
The C&lt;t&gt; option prints the object on the top of the stack as it's
being tracked. The C&lt;O&gt; 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;
use B qw(peekop class comppadlist main_start svref_2object walksymtable
         OPpLVAL_INTRO SVf_POK
        );

sub UNKNOWN { ["?", "?", "?"] }

my @pad;			# lexicals in current pad
				# as ["(lexical)", type, name]
my %done;			# keyed by $$op: set when each $op is done
my $top = UNKNOWN;		# shadows top element of stack as
				# [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

my %code = (intro =&gt; "i", used =&gt; "",
	    subdef =&gt; "s", subused =&gt; "&amp;",
	    formdef =&gt; "f", meth =&gt; "-&gt;");


# Options
my ($debug_op, $debug_top, $nodefs, $raw);

sub process {
    my ($var, $event) = @_;
    my ($pack, $type, $name) = @$var;
    if ($type eq "*") {
	if ($event eq "used") {
	    return;
	} elsif ($event eq "subused") {
	    $type = "&amp;";
	}
    }
    $type =~ s/(.)\*$/$1/g;
    if ($raw) {
	printf "%-16s %-12s %5d %-12s %4s %-16s %s\n",
	    $file, $subname, $line, $pack, $type, $name, $event;
    } else {
	# Wheee
	push(@{$table{$file}-&gt;{$subname}-&gt;{$pack}-&gt;{$type.$name}-&gt;{$event}},
	    $line);
    }
}

sub load_pad {
    my $padlist = shift;
    my ($namelistav, $vallistav, @namelist, $ix);
    @pad = ();
    return if class($padlist) eq "SPECIAL";
    ($namelistav,$vallistav) = $padlist-&gt;ARRAY;
    @namelist = $namelistav-&gt;ARRAY;
    for ($ix = 1; $ix &lt; @namelist; $ix++) {
	my $namesv = $namelist[$ix];
	next if class($namesv) eq "SPECIAL";
	my ($type, $name) = $namesv-&gt;PV =~ /^(.)([^\0]*)(\0.*)?$/;
	$pad[$ix] = ["(lexical)", $type, $name];
    }
    if ($Config{useithreads}) {
	my (@vallist);
	@vallist = $vallistav-&gt;ARRAY;
	for ($ix = 1; $ix &lt; @vallist; $ix++) {
	    my $valsv = $vallist[$ix];
	    next unless class($valsv) eq "GV";
	    # these pad GVs don't have corresponding names, so same @pad
	    # array can be used without collisions
	    $pad[$ix] = [$valsv-&gt;STASH-&gt;NAME, "*", $valsv-&gt;NAME];
	}
    }
}

sub xref {
    my $start = shift;
    my $op;
    for ($op = $start; $$op; $op = $op-&gt;next) {
	last if $done{$$op}++;
	warn sprintf("top = [%s, %s, %s]\n", @$top) if $debug_top;
	warn peekop($op), "\n" if $debug_op;
	my $opname = $op-&gt;name;
	if ($opname =~ /^(or|and|mapwhile|grepwhile|range|cond_expr)$/) {
	    xref($op-&gt;other);
	} elsif ($opname eq "match" || $opname eq "subst") {
	    xref($op-&gt;pmreplstart);
	} elsif ($opname eq "substcont") {
	    xref($op-&gt;other-&gt;pmreplstart);
	    $op = $op-&gt;other;
	    redo;
	} elsif ($opname eq "enterloop") {
	    xref($op-&gt;redoop);
	    xref($op-&gt;nextop);
	    xref($op-&gt;lastop);
	} elsif ($opname eq "subst") {
	    xref($op-&gt;pmreplstart);
	} else {
	    no strict 'refs';
	    my $ppname = "pp_$opname";
	    &amp;$ppname($op) if defined(&amp;$ppname);
	}
    }
}

sub xref_cv {
    my $cv = shift;
    my $pack = $cv-&gt;GV-&gt;STASH-&gt;NAME;
    $subname = ($pack eq "main" ? "" : "$pack\::") . $cv-&gt;GV-&gt;NAME;
    load_pad($cv-&gt;PADLIST);
    xref($cv-&gt;START);
    $subname = "(main)";
}

sub xref_object {
    my $cvref = shift;
    xref_cv(svref_2object($cvref));
}

sub xref_main {
    $subname = "(main)";
    load_pad(comppadlist);
    xref(main_start);
    while (@todo) {
	xref_cv(shift @todo);
    }
}

sub pp_nextstate {
    my $op = shift;
    $file = $op-&gt;file;
    $line = $op-&gt;line;
    $top = UNKNOWN;
}

sub pp_padsv {
    my $op = shift;
    $top = $pad[$op-&gt;targ];
    process($top, $op-&gt;private &amp; OPpLVAL_INTRO ? "intro" : "used");
}

sub pp_padav { pp_padsv(@_) }
sub pp_padhv { pp_padsv(@_) }

sub deref {
    my ($var, $as) = @_;
    $var-&gt;[1] = $as . $var-&gt;[1];
    process($var, "used");
}

sub pp_rv2cv { deref($top, "&amp;"); }
sub pp_rv2hv { deref($top, "%"); }
sub pp_rv2sv { deref($top, "\$"); }
sub pp_rv2av { deref($top, "\@"); }
sub pp_rv2gv { deref($top, "*"); }

sub pp_gvsv {
    my $op = shift;
    my $gv;
    if ($Config{useithreads}) {
	$top = $pad[$op-&gt;padix];
	$top = UNKNOWN unless $top;
	$top-&gt;[1] = '$';
    }
    else {
	$gv = $op-&gt;gv;
	$top = [$gv-&gt;STASH-&gt;NAME, '$', $gv-&gt;NAME];
    }
    process($top, $op-&gt;private &amp; OPpLVAL_INTRO ? "intro" : "used");
}

sub pp_gv {
    my $op = shift;
    my $gv;
    if ($Config{useithreads}) {
	$top = $pad[$op-&gt;padix];
	$top = UNKNOWN unless $top;
	$top-&gt;[1] = '*';
    }
    else {
	$gv = $op-&gt;gv;
	$top = [$gv-&gt;STASH-&gt;NAME, "*", $gv-&gt;NAME];
    }
    process($top, $op-&gt;private &amp; OPpLVAL_INTRO ? "intro" : "used");
}

sub pp_const {
    my $op = shift;
    my $sv = $op-&gt;sv;
    # constant could be in the pad (under useithreads)
    if ($$sv) {
	$top = ["?", "",
		(class($sv) ne "SPECIAL" &amp;&amp; $sv-&gt;FLAGS &amp; SVf_POK) ? $sv-&gt;PV : "?"];
    }
    else {
	$top = $pad[$op-&gt;targ];
    }
}

sub pp_method {
    my $op = shift;
    $top = ["(method)", "-&gt;".$top-&gt;[1], $top-&gt;[2]];
}

sub pp_entersub {
    my $op = shift;
    if ($top-&gt;[1] eq "m") {
	process($top, "meth");
    } else {
	process($top, "subused");
    }
    $top = UNKNOWN;
}

#
# Stuff for cross referencing definitions of variables and subs
#

sub B::GV::xref {
    my $gv = shift;
    my $cv = $gv-&gt;CV;
    if ($$cv) {
	#return if $done{$$cv}++;
	$file = $gv-&gt;FILE;
	$line = $gv-&gt;LINE;
	process([$gv-&gt;STASH-&gt;NAME, "&amp;", $gv-&gt;NAME], "subdef");
	push(@todo, $cv);
    }
    my $form = $gv-&gt;FORM;
    if ($$form) {
	return if $done{$$form}++;
	$file = $gv-&gt;FILE;
	$line = $gv-&gt;LINE;
	process([$gv-&gt;STASH-&gt;NAME, "", $gv-&gt;NAME], "formdef");
    }
}

sub xref_definitions {
    my ($pack, %exclude);
    return if $nodefs;
    $subname = "(definitions)";
    foreach $pack (qw(B O AutoLoader DynaLoader XSLoader Config DB VMS
		      strict vars FileHandle Exporter Carp)) {
        $exclude{$pack."::"} = 1;
    }
    no strict qw(vars refs);
    walksymtable(\%{"main::"}, "xref", sub { !defined($exclude{$_[0]}) });
}

sub output {
    return if $raw;
    my ($file, $subname, $pack, $name, $ev, $perfile, $persubname,
	$perpack, $pername, $perev);
    foreach $file (sort(keys(%table))) {
	$perfile = $table{$file};
	print "File $file\n";
	foreach $subname (sort(keys(%$perfile))) {
	    $persubname = $perfile-&gt;{$subname};
	    print "  Subroutine $subname\n";
	    foreach $pack (sort(keys(%$persubname))) {
		$perpack = $persubname-&gt;{$pack};
		print "    Package $pack\n";
		foreach $name (sort(keys(%$perpack))) {
		    $pername = $perpack-&gt;{$name};
		    my @lines;
		    foreach $ev (qw(intro formdef subdef meth subused used)) {
			$perev = $pername-&gt;{$ev};
			if (defined($perev) &amp;&amp; @$perev) {
			    my $code = $code{$ev};
			    push(@lines, map("$code$_", @$perev));
			}
		    }
		    printf "      %-16s  %s\n", $name, join(", ", @lines);
		}
	    }
	}
    }
}

sub compile {
    my @options = @_;
    my ($option, $opt, $arg);
  OPTION:
    while ($option = shift @options) {
	if ($option =~ /^-(.)(.*)/) {
	    $opt = $1;
	    $arg = $2;
	} else {
	    unshift @options, $option;
	    last OPTION;
	}
	if ($opt eq "-" &amp;&amp; $arg eq "-") {
	    shift @options;
	    last OPTION;
	} elsif ($opt eq "o") {
	    $arg ||= shift @options;
	    open(STDOUT, "&gt;$arg") or return "$arg: $!\n";
	} elsif ($opt eq "d") {
	    $nodefs = 1;
	} elsif ($opt eq "r") {
	    $raw = 1;
	} elsif ($opt eq "D") {
            $arg ||= shift @options;
	    foreach $arg (split(//, $arg)) {
		if ($arg eq "o") {
		    B-&gt;debug(1);
		} elsif ($arg eq "O") {
		    $debug_op = 1;
		} elsif ($arg eq "t") {
		    $debug_top = 1;
		}
	    }
	}
    }
    if (@options) {
	return sub {
	    my $objname;
	    xref_definitions();
	    foreach $objname (@options) {
		$objname = "main::$objname" unless $objname =~ /::/;
		eval "xref_object(\\&amp;$objname)";
		die "xref_object(\\&amp;$objname) failed: $@" if $@;
	    }
	    output();
	}
    } else {
	return sub {
	    xref_definitions();
	    xref_main();
	    output();
	}
    }
}

1;
</pre></body></html>