如何在C ++代码中搜索所有构造函数?

how to search for all constructors in c++ code?

本文关键字:搜索 构造函数 代码      更新时间:2023-10-16

我必须找出我的代码库中的所有构造函数(这是巨大的(,有没有简单的方法可以做到这一点(无需打开每个文件,读取它并找到所有类(?我可以在 grep 中使用任何特定于语言的功能?

要找到析构函数很容易,我可以搜索"~"。 我可以写一些代码来查找"::"并匹配左右单词,如果它们相等,那么我可以打印该行。 但是,如果构造函数在类内(在 H/HPP 文件中(,则缺少上述逻辑。

既然你正在考虑使用 grep,我假设你想以编程方式完成它,而不是在 IDE 中。 这也取决于您是解析标头还是代码,我再次假设您要解析标头。

我用python做到了:

inClass=False
className=""
motifClass=re.compile("class [a-zA-Z][a-zA-Z1-9_]*)")#to get the class name
motifEndClass=re.compile("};")#Not sure that'll work for every file
motifConstructor=re.compile("~?"+className+"(.*)")
res=[]
#assuming you already got the file loaded
for line in lines:
if not inClass:#we're searching to be in one
temp=line.match(class)
if temp:
className=res.group(1)
inClass=True
else:
temp=line.match(motifEndClass)
if temp:#doesn't end at the end of the class, since multiple class can be in a file
inClass=False
continue
temp=line.match(motifConstructor)
if temp:
res.append(line)#we're adding the line that matched
#do whatever you want with res here!

我没有测试它,我做得相当快,并试图简化一段旧代码,所以很多东西都不支持,比如嵌套类。 由此,您可以编写脚本来查找目录中的每个标头,并按照自己的喜好使用结果!

搜索所有类名,然后找到与类名相同的函数名称。第二种选择是,我们知道构造函数始终是公共的,因此搜索单词 public 并找到构造函数。

查找构造函数相当简单(正如其他人所说(....查找对构造函数和析构函数的所有调用并非易事,到目前为止,我还没有找到......