moria.org.uk

Sun, 06 Aug 2006

Finding Unused Externs in C

gcc -Wunused-function -Wmissing-declarations does a good job at identifying functions that are not declared in any header file, and once you mark them static then tells you if they are unused. However, with programs that have been developed over many years, and have a lot of cruft in the source code (such as Doom/PrBoom), there may be many functions that are prototyped in header files but are not actually used from other files; gcc cannot tell you about these.

I had an idea for locating these unnecessary externs: just run the link stage of gcc with one .o file taken out, and record all the 'undefined reference to' errors you get. That gives you the list of used externs; so any others are unused. So I wrote a couple of scripts to assist with this. First, find-used-externs:

#!/usr/bin/ruby -w

def filter_gcc(cmdline)
    IO.popen(cmdline.join(" ") + " 2>&1 ") do |p|
        p.each_line do |l|
            print $1,"\n" if l =~ /undefined reference to \`(.*)'/;
        end
    end
end

ARGV.each do | a |
    next unless a =~ /\.o$/;
    cmdline = ARGV.reject { |b| b == a }
    filter_gcc(cmdline)
end

Which tells you all the symbols in .o files referenced by other files — cut and paste the normal link step for the project, and prefix the command with ./find-used-externs. It runs the link command once for each .o file, omitting only that .o file from the command line, and parsing the errors.

Secondly, you have to find all the externs provided by each .h file, and see if any are not on the used list. list-externs is a very crude script to try and read extern variables and function prototypes from header files:

#!/usr/bin/ruby -w

ARGF.each_line do |l|
    l.sub!(/\/\/.*/,'')
    l.sub!(/\/\*.*/,'')
    print $1,"\n" if l =~ /^\s*extern.*\s([a-zA-Z_][a-zA-Z_0-9]*)\s*;/
    print $1,"\n" if l =~ /^\s*[^#].*\s([a-zA-Z_][a-zA-Z_0-9]*)\s*\(/
end

So sort the output of that, and use comm(1) to find the entries that are not in the (sorted) output of find-used-externs. And that tells you what header file declarations are not currently needed.

[13:38] | [/computers/code] | #

Colin Phipps.
Archives
January 2007
November 2006
October 2006
September 2006
August 2006
July 2006
June 2006
May 2006
April 2006
March 2006
February 2006
January 2006
December 2005
November 2005
October 2005
July 2005
June 2005
May 2005
April 2005
March 2005
February 2005
January 2005
December 2004
November 2004
October 2004
Web Sites
zsync
PrBoom
About Kye
Credits
Blosxom
Powered by
Blogs that link here
[Valid Atom]