我找到以下示例来搜索诸如malloc
之类的特定函数名称,但是我想在C源文件的函数声明中找到所有函数名称。因此,在ReturnCode HashCreate(Hash** hash, unsigned int table_size)
的情况下,我正在寻找HashCreate和行号。由于我不喜欢Python,而且看起来很复杂,所以我问我该怎么做?
from __future__ import print_function
import sys
sys.path.extend(['.', '..'])
from pycparser import c_parser, c_ast, parse_file
class FuncCallVisitor(c_ast.NodeVisitor):
def __init__(self, funcname):
self.funcname = funcname
def visit_FuncCall(self, node):
if node.name.name == self.funcname:
print('%s called at %s' % (self.funcname, node.name.coord))
def show_func_calls(filename, funcname):
ast = parse_file(filename, use_cpp=True,
cpp_path='clang',
cpp_args=['-E'])
v = FuncCallVisitor(funcname)
v.visit(ast)
if __name__ == "__main__":
if len(sys.argv) > 2:
filename = sys.argv[1]
func = sys.argv[2]
else:
filename = 'hash.c'
func = 'malloc'
show_func_calls(filename, func)
最佳答案
func_defs示例完全符合您的期望:
# Using pycparser for printing out all the functions defined in a
# C file.