Some time ago we took responsibility for an outdated code base.
One of the quirks of this very poorly structured / written code was that it contained a number of really huge structures, each of which contained hundreds of members. One of the many steps that we have taken is to clear as much code as possible that was not used, therefore, it is necessary to search for unused structs / struct elements.
Regarding structures, I created a combination of python, GNU Global, and ctags to display unused structure elements.
Basically, I use ctags to create a tag file, the python script described below will analyze this file to find the whole structure and then use GNU Global to search the previously generated global database to see if this element is used in the code.
This approach has a number of rather serious drawbacks, but it kind of solved the problem that we encountered, and gave us a good start for further cleaning.
There must be a better way to do this!
The question arises: how to find unused structures and structure elements in the code base?
#!/usr/bin/env python import os import string import sys import operator def printheader(word): """generate a nice header string""" print "\n%s\n%s" % (word, "-" * len(word)) class StructFreqAnalysis: """ add description""" def __init__(self): self.path2hfile='' self.name='' self.id='' self.members=[] def show(self): print 'path2hfile:',self.path2hfile print 'name:',self.name print 'members:',self.members print def sort(self): return sorted(self.members, key=operator.itemgetter(1)) def prettyprint(self): '''display a sorted list''' print 'struct:',self.name print 'path:',self.path2hfile for i in self.sort(): print ' ',i[0],':',i[1] print f=open('tags','r') x={}
Edit
As @ Jens-Gustedt suggested using the compiler, this is a good way to do this. I am following an approach that can do some sort of High Level filtering before using the compiler approach.