前段时间我们接管了遗留代码库的责任。
这个结构非常糟糕/编写的代码的一个怪癖是 它包含许多非常庞大的结构,每个结构都包含 数百名成员。我们所做的许多步骤之一是清洁 尽可能多地排除未使用的代码,因此需要 查找未使用的结构/结构成员。
关于结构,我想到了 python 的组合,GNU Global和 ctags列出未使用的结构成员。
基本上,我正在做的是使用 ctags
生成一个标签文件,
下面的 python 脚本解析该文件以找到所有结构
成员,然后使用 GNU Global
在先前的
生成全局数据库以查看代码中是否使用了该成员。
这种方法有一些非常严重的缺陷,但它有点 解决了我们面临的问题,为我们的进一步发展提供了良好的开端 清理。
必须有更好的方法来做到这一点!
问题是:如何找到未使用的结构和结构成员 在代码库中?
#!/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 -> class
y={} # internal tags id -> 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 '->'+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
编辑
正如@Jens-Gustedt 所建议的那样,使用编译器是一个很好的方法。我正在寻找一种可以在使用编译器方法之前进行某种“高级”过滤的方法。
最佳答案
如果这些只是一些 struct
并且如果代码没有通过另一种类型访问 struct
的坏黑客......那么你可以注释掉所有你的第一个 struct
的字段,让编译器告诉你。
取消对一个使用字段的注释,直到编译器满意为止。然后一旦编译,进行良好的测试以确保没有黑客攻击的前提。
遍历所有 struct
。
绝对不漂亮,但最后你至少会有一个人知道一点代码。
关于c - 找到未使用的结构和结构成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6071560/