title: Locate unused structures and structure-members
body: <p>Some time ago we took over the responsibility of a legacy code base.</p>
  
  <p>One of the quirks of this very badly structured/written code was that
  it contained a number of really huge structs, each containing
  hundreds of members. One of the many steps that we did was to clean
  out as much of the code as possible that wasn't used, hence the need
  to find unused structs/struct members.</p>
  
  <p>Regarding the structs, I conjured up a combination of python, <a href="http://www.gnu.org/software/global/" rel="nofollow">GNU
  Global</a> and <a href="http://ctags.sourceforge.net/" rel="nofollow">ctags</a> to list the struct members that are unused.</p>
  
  <p>Basically, what I'm doing is to use <code>ctags</code> to generate a tags file,
  the python-script below parses that file to locate all struct
  members and then using <code>GNU Global</code> to do a lookup in the previously
  generated global-database to see if that member is used in the code.</p>
  
  <p>This approach have a number of quite serious flaws, but it sort of
  solved the issue we faced and gave us a good start for further
  cleanup.</p>
  
  <p>There must be a better way to do this!</p>
  
  <p>The question is: How to find unused structures and structure members
  in a code base?</p>
  
  <pre><code>#!/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={} # struct_name -&gt; class
  y={} # internal tags id -&gt; class
  
  for i in f:
      i=i.strip()
      if 'typeref:struct:' in i:
          line=i.split()
          x[line[0]]=StructFreqAnalysis()
          x[line[0]].name=line[0]
          x[line[0]].path2hfile=line[1]
          for j in line:
              if 'typeref' in j:
                  s=j.split(':')
                  x[line[0]].id=s[-1]
                  y[s[-1]]=x[line[0]]
  
  f.seek(0)
  for i in f:
      i=i.strip()
      if 'struct:' in i:
          items=i.split()
          name=items[0]
          id=items[-1].split(':')[-1]
          if id:
              if id in y:
                  key=y[id]
                  key.members.append([name,0])
  f.close()
  
  # do frequency count
  for k,v in x.iteritems():
      for i in v.members:
          cmd='global -a -s %s'%i[0]     # -a absolute path. use global to give src-file for member
          g=os.popen(cmd)
          for gout in g:
              if '.c' in gout:
                  gout=gout.strip()
                  f=open(gout,'r')
                  for line in f:
                      if '-&gt;'+i[0] in line or '.'+i[0] in line:
                          i[1]=i[1]+1
                  f.close()
  
  printheader('All structures')
  for k,v in x.iteritems():
      v.prettyprint()
  
  #show which structs that can be removed
  printheader('These structs could perhaps be removed')
  for k,v in x.iteritems():
      if len(v.members)==0:
          v.show()
  
  printheader('Total number of probably unused members')
  cnt=0
  for k,v in x.iteritems():
      for i in v.members:
          if i[1]==0:
              cnt=cnt+1
  print cnt
  </code></pre>
  
  <p><strong>Edit</strong></p>
  
  <p>As proposed by @Jens-Gustedt using the compiler is a good way to do it. I'm after a approach that can do a sort of "High Level" filtering before using the compiler-approach.</p>