Skip to content

Instantly share code, notes, and snippets.

@adamsanderson
Created August 13, 2008 18:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamsanderson/5280 to your computer and use it in GitHub Desktop.
Save adamsanderson/5280 to your computer and use it in GitHub Desktop.
#!/usr/local/bin/ruby -ws
# Quick Hack to show which methods are not explicitly called within a class.
# This is only with regard to the class itself.
require 'pp'
begin require 'rubygems' rescue LoadError end
require 'parse_tree'
require 'sexp_processor'
require 'set'
class CallAnalyzer < SexpProcessor
attr_reader :definitions
attr_reader :calls
attr_accessor :create_definitions
def initialize
super
self.auto_shift_type = true
@create_definitions = true
@definitions = Set.new
@calls = Set.new
end
def process_defn(exp)
name = exp.shift
self.definitions << name if create_definitions
return s(:defn, name, process(exp.shift), process(exp.shift))
end
def process_call(exp)
target = exp.shift
name = exp.shift
self.calls << name
if exp.empty?
return s(:call, process(target), name)
else
args = []
args << (process(exp.shift)) while !exp.empty?
return s(:call, process(target), name, *args)
end
end
def process_fcall(exp)
name = exp.shift
self.calls << name
if exp.empty?
return s(:call, name)
else
args = []
args << (process(exp.shift)) while !exp.empty?
return s(:call, name, *args)
end
end
def process_vcall(exp)
name = exp.shift
self.calls << name
if exp.empty?
return s(:call, name)
else
args = []
args << (process(exp.shift)) while !exp.empty?
return s(:call, name, *args)
end
end
end
if __FILE__ == $0 then
path = ARGV.shift
code = IO.read(path)
sexp = ParseTree.new.parse_tree_for_string(code,path)
#pp sexp
processor = CallAnalyzer.new
processor.process(*sexp)
if ARGV.shift == '--'
processor.create_definitions = false
while pattern = ARGV.shift
Dir[pattern].each do |path|
code = IO.read(path)
sexp = ParseTree.new.parse_tree_for_string(code,path)
processor.process(*sexp)
end
end
end
puts "Methods not explicitly called:"
puts((processor.definitions - processor.calls).to_a)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment