PDA

View Full Version : C++ dead symbol finder (in perlm lol!)


gernblan
09-13-2007, 07:59 PM
Hope this helps anyone looking to do some cleanup on the code base!

#!/usr/bin/perl

# Courtesy of Keelyeh, ServerOp of Jest3 eqemu server. September 14, 2007 Version 1.0
# This script takes a set of object files as input (just list them as args!). ANY symbols in
# the files that are defined as external but that are not used in another one of the object files
# will be printed.

my %symbols;

# first, open an input pipe to the output of the nm command
open IN_FILE, "nm @ARGV|" or die ("Could not connect to nm command");

# Next, process each line in the input stream. Check for a filename line first (these lines
# all end with a colon (:) and are the ONLY lines that do. If you find one, set it as the
# current filename and continue.
my $cur_file; #This is the current file we're looking at

while (<IN_FILE>) {
if (/(.*):$/) {
$cur_file = $1;
next;
}

# Next, check for blank lines (or any other type of short line. If found, ignore them.
if (length($_) < 12) {
next ; # it must be a blank line or some other junk
}

# By now, you have a line that has the symbol information. The first eight characters of the line
# are symbol information (if any). A type character is located in character position 10 (which is 9
# under perl because perl starts counting at 0) and the symbol name begins in column number 12
# (actual position in perl is 11, again because you must count 0 as the first position in perl).
my $type = substr($_, 9, 1);
my $name = substr($_, 11);
chomp($name); # Get rid any linefeeds or carriage returns at end of line. We do not need them.

# Now, if the symbol type is "U" then the symbol is undefined in the current file. That means that
# it is used. Any OTHER symbol type code indicates a definition. The use or symbol of The
# definition is now thus recorded...
if ($type eq "U") {
$symbols{$name}->{'undefined'} = $cur_file;
} else {
$symbols{$name}->{'defined'} = $cur_file;
}
}

# Once all of the information has been processed, all we have to do now is identify the dead code
# and print out the results. A dead symbol is one that is defined but NOT used. In other words, one
# for which there is NO "undefined" entry.
foreach my $cur_symbol (sort keys %symbols) {
if (not defined($symbols{$cur_symbol}->{undefined})) {
print "Not used.\n";
print " Symbol: $cur_symbol\n";
print " Defined in: $symbols{$cur_symbol}->{'defined'}\n";
}
}

# The final result is a list of symbols that are NOT used and are candidates for potential elimination!

Granted, to actually FIND dead symbols it will take some serious arguments to this script, but it can handle it if your shell can, OR you can just put the list of args into a text file and pipe it to the perl script. Those who would care about this at all know what I am talking about already!

Also, I didn't write the output to disk or something else hard coded because, again, one who wants to can just pipe the script output themselves.

Point of this is to be simple, readable, with overkill comments explaining it, and flexible.

Peace.