pycparser 是否支持用户定义的类型?我想从 *.C 文件中获取用户定义类型作为返回类型的函数列表。
问问题
262 次
1 回答
2
当然可以。您只想为FuncDef
节点编写访问者。
FuncDef
包含 a ,其Decl
子类型是 a FuncDecl
。这FuncDecl
将返回类型作为其子类型。
返回类型要么是 a TypeDecl
,在这种情况下,类型标识符是它的子类型,要么是 a PtrDecl
,在这种情况下,它的子类型是 the 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。
于 2017-11-05T13:28:37.440 回答