pycparser支持用户定义的类型吗?我想从*.C文件中获取返回类型为用户定义类型的函数列表。
发布于 2017-11-05 21:28:37
当然是这样。您只想为FuncDef节点编写一个访问器。
FuncDef包含一个Decl,它的子类型是FuncDecl。此FuncDecl将返回类型作为其子类型。
返回的类型可以是一个TypeDecl,在这种情况下,类型标识符是它的类型子项;或者它是一个PtrDecl,在这种情况下,它的类型子项是TypeDecl,它的类型子项是类型标识符。
明白了吗?下面是一个示例FuncDef访问器,它打印每个函数的名称和返回类型:
class FuncDefVisitor(c_ast.NodeVisitor):
"""
A simple visitor for FuncDef nodes that prints the names and
return types of definitions.
"""
def visit_FuncDef(self, node):
return_type = node.decl.type.type
if type(return_type) == c_ast.TypeDecl:
identifier = return_type.type
else: # type(return_type) == c_ast.PtrDecl
identifier = return_type.type.type
print("{}: {}".format(node.decl.name, identifier.names))以下是解析cparser分发版中的hash.c示例文件时的输出:
hash_func: ['unsigned', 'int']
HashCreate: ['ReturnCode']
HashInsert: ['ReturnCode']
HashFind: ['Entry']
HashRemove: ['ReturnCode']
HashPrint: ['void']
HashDestroy: ['void']现在您只需要过滤掉内置类型,或者过滤出您感兴趣的UDT。
https://stackoverflow.com/questions/47117781
复制相似问题