Go Back   EQEmulator Home > EQEmulator Forums > Development > Development::Tools

Development::Tools 3rd Party Tools for EQEMu (DB management tools, front ends, etc...)

Reply
 
Thread Tools Display Modes
  #1  
Old 09-13-2007, 07:59 PM
gernblan
Discordant
 
Join Date: Aug 2006
Posts: 394
Default C++ dead symbol finder (in perlm lol!)

Hope this helps anyone looking to do some cleanup on the code base!

Code:
#!/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.
__________________
--
Keelyeh
Owner, ServerOp and Developer
Jest 4 Server
Linux (Jest3 runs on Fedora, our Dev servers usually run on Ubuntu and/or Gentoo), OC-12 Connection = Hella Fast

Last edited by gernblan; 09-14-2007 at 04:03 AM..
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

   

All times are GMT -4. The time now is 01:14 AM.


 

Everquest is a registered trademark of Daybreak Game Company LLC.
EQEmulator is not associated or affiliated in any way with Daybreak Game Company LLC.
Except where otherwise noted, this site is licensed under a Creative Commons License.
       
Powered by vBulletin®, Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Template by Bluepearl Design and vBulletin Templates - Ver3.3