本文整理匯總了Python中pycparser.c_ast.NodeVisitor方法的典型用法代碼示例。如果您正苦於以下問題:Python c_ast.NodeVisitor方法的具體用法?Python c_ast.NodeVisitor怎麽用?Python c_ast.NodeVisitor使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類pycparser.c_ast
的用法示例。
在下文中一共展示了c_ast.NodeVisitor方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: build_typemap
# 需要導入模塊: from pycparser import c_ast [as 別名]
# 或者: from pycparser.c_ast import NodeVisitor [as 別名]
def build_typemap(source: str) -> TypeMap:
source = add_builtin_typedefs(source)
source = strip_comments(source)
ast: ca.FileAST = parse_c(source)
ret = TypeMap()
for item in ast.ext:
if isinstance(item, ca.Typedef):
ret.typedefs[item.name] = item.type
if isinstance(item, ca.FuncDef):
assert item.decl.name is not None, "cannot define anonymous function"
assert isinstance(item.decl.type, FuncDecl)
ret.functions[item.decl.name] = parse_function(item.decl.type)
if isinstance(item, ca.Decl) and isinstance(item.type, FuncDecl):
assert item.name is not None, "cannot define anonymous function"
ret.functions[item.name] = parse_function(item.type)
defined_function_decls: Set[ca.Decl] = set()
class Visitor(ca.NodeVisitor):
def visit_Struct(self, struct: ca.Struct) -> None:
if struct.decls is not None:
parse_struct(struct, ret)
def visit_Union(self, union: ca.Union) -> None:
if union.decls is not None:
parse_struct(union, ret)
def visit_Decl(self, decl: ca.Decl) -> None:
if decl.name is not None:
ret.var_types[decl.name] = decl.type
if not isinstance(decl.type, FuncDecl):
self.visit(decl.type)
def visit_Enum(self, enum: ca.Enum) -> None:
if enum.name is not None:
ret.typedefs[enum.name] = basic_type(["int"])
def visit_FuncDef(self, fn: ca.FuncDef) -> None:
if fn.decl.name is not None:
ret.var_types[fn.decl.name] = fn.decl.type
Visitor().visit(ast)
return ret