本文整理汇总了Python中pycparser.c_ast.FileAST方法的典型用法代码示例。如果您正苦于以下问题:Python c_ast.FileAST方法的具体用法?Python c_ast.FileAST怎么用?Python c_ast.FileAST使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pycparser.c_ast
的用法示例。
在下文中一共展示了c_ast.FileAST方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: explain_c_declaration
# 需要导入模块: from pycparser import c_ast [as 别名]
# 或者: from pycparser.c_ast import FileAST [as 别名]
def explain_c_declaration(c_decl):
""" Parses the declaration in c_decl and returns a text
explanation as a string.
The last external node of the string is used, to allow
earlier typedefs for used types.
"""
parser = c_parser.CParser()
try:
node = parser.parse(c_decl, filename='<stdin>')
except c_parser.ParseError:
e = sys.exc_info()[1]
return "Parse error:" + str(e)
if (not isinstance(node, c_ast.FileAST) or
not isinstance(node.ext[-1], c_ast.Decl)
):
return "Not a valid declaration"
return _explain_decl_node(node.ext[-1])
示例2: read_ptrace_h
# 需要导入模块: from pycparser import c_ast [as 别名]
# 或者: from pycparser.c_ast import FileAST [as 别名]
def read_ptrace_h(path, include_path):
text = get_header_text(path, include_path)
parser = ext_c_parser.GnuCParser()
fileast = parser.parse(text, path)
if not isinstance(fileast, c_ast.FileAST):
die('could not parse user.h')
output = ['\n']
for decl in fileast.ext:
if (not isinstance(decl, c_ast.Decl) or
not isinstance(decl.type, c_ast.Enum)):
continue
for item in decl.type.values.enumerators:
if item.name.startswith('PTRACE_'):
output.append('{} = {}'.format(
item.name, render_const(item.value)))
typedefs = parse_typedefs(fileast)
structs = {'__ptrace_peeksiginfo_args'}
output.extend(parse_structs(fileast, structs, typedefs))
return output
示例3: parse_c
# 需要导入模块: from pycparser import c_ast [as 别名]
# 或者: from pycparser.c_ast import FileAST [as 别名]
def parse_c(source: str) -> ca.FileAST:
try:
return CParser().parse(source, "<source>")
except ParseError as e:
msg = str(e)
position, msg = msg.split(": ", 1)
parts = position.split(":")
if len(parts) >= 2:
# Adjust the line number by 1 to correct for the added typedefs
lineno = int(parts[1]) - 1
posstr = f" at line {lineno}"
if len(parts) >= 3:
posstr += f", column {parts[2]}"
try:
line = source.split("\n")[lineno].rstrip()
posstr += "\n\n" + line
except IndexError:
posstr += "(out of bounds?)"
else:
posstr = ""
raise DecompFailure(f"Syntax error when parsing C context.\n{msg}{posstr}")
示例4: get_kernel_header
# 需要导入模块: from pycparser import c_ast [as 别名]
# 或者: from pycparser.c_ast import FileAST [as 别名]
def get_kernel_header(self, name='kernel'):
"""
Generate and store kernel.h
:return: tuple of filename of header and file pointer of lockfile
"""
file_name = 'kernel.h'
file_path = self.get_intermediate_location(
file_name, machine_and_compiler_dependent=False)
lock_mode, lock_fp = self.lock_intermediate(file_path)
if lock_mode == fcntl.LOCK_SH:
# use cache
with open(file_path) as f:
code = f.read()
else: # lock_mode == fcntl.LOCK_EX
# needs update
func_decl = self._build_kernel_function_declaration(name=name)
scalar_decls = self.get_scalar_declarations()
code = CGenerator().visit(
c_ast.FileAST(ext=[func_decl]+scalar_decls))
with open(file_path, 'w') as f:
f.write(code)
fcntl.flock(lock_fp, fcntl.LOCK_SH) # degrade to shared lock
return file_name, lock_fp
示例5: test_without_cpp
# 需要导入模块: from pycparser import c_ast [as 别名]
# 或者: from pycparser.c_ast import FileAST [as 别名]
def test_without_cpp(self):
ast = parse_file(self._find_file('example_c_file.c'))
self.assertTrue(isinstance(ast, c_ast.FileAST))
示例6: test_with_cpp
# 需要导入模块: from pycparser import c_ast [as 别名]
# 或者: from pycparser.c_ast import FileAST [as 别名]
def test_with_cpp(self):
memmgr_path = self._find_file('memmgr.c')
c_files_path = os.path.dirname(memmgr_path)
ast = parse_file(memmgr_path, use_cpp=True,
cpp_path=CPPPATH,
cpp_args='-I%s' % c_files_path)
self.assertTrue(isinstance(ast, c_ast.FileAST))
fake_libc = os.path.join(c_files_path, '..', '..',
'utils', 'fake_libc_include')
ast2 = parse_file(self._find_file('year.c'), use_cpp=True,
cpp_path=CPPPATH,
cpp_args=[r'-I%s' % fake_libc])
self.assertTrue(isinstance(ast2, c_ast.FileAST))
示例7: test_cpp_funkydir
# 需要导入模块: from pycparser import c_ast [as 别名]
# 或者: from pycparser.c_ast import FileAST [as 别名]
def test_cpp_funkydir(self):
# This test contains Windows specific path escapes
if sys.platform != 'win32':
return
c_files_path = os.path.join('tests', 'c_files')
ast = parse_file(self._find_file('simplemain.c'), use_cpp=True,
cpp_path=CPPPATH, cpp_args='-I%s' % c_files_path)
self.assertTrue(isinstance(ast, c_ast.FileAST))
示例8: test_no_real_content_after_cpp
# 需要导入模块: from pycparser import c_ast [as 别名]
# 或者: from pycparser.c_ast import FileAST [as 别名]
def test_no_real_content_after_cpp(self):
ast = parse_file(self._find_file('empty.h'), use_cpp=True,
cpp_path=CPPPATH)
self.assertTrue(isinstance(ast, c_ast.FileAST))
示例9: read_user_h
# 需要导入模块: from pycparser import c_ast [as 别名]
# 或者: from pycparser.c_ast import FileAST [as 别名]
def read_user_h(path, include_path):
text = get_header_text(path, include_path)
parser = ext_c_parser.GnuCParser()
fileast = parser.parse(text, path)
if not isinstance(fileast, c_ast.FileAST):
die('could not parse user.h')
typedefs = parse_typedefs(fileast)
structs = {'user_regs_struct', 'user_fpregs_struct'}
return parse_structs(fileast, structs, typedefs)
示例10: read_signal_h
# 需要导入模块: from pycparser import c_ast [as 别名]
# 或者: from pycparser.c_ast import FileAST [as 别名]
def read_signal_h(path, include_path):
text = get_header_text(path, include_path)
parser = ext_c_parser.GnuCParser()
fileast = parser.parse(text, path)
if not isinstance(fileast, c_ast.FileAST):
die('could not parse signal.h')
typedefs = parse_typedefs(fileast)
structs = {'siginfo_t'}
return parse_structs(fileast, structs, typedefs)
示例11: generalize
# 需要导入模块: from pycparser import c_ast [as 别名]
# 或者: from pycparser.c_ast import FileAST [as 别名]
def generalize(self, syntax):
assert isinstance(syntax, c_ast.FileAST)
backend = CAstGeneralizerBackend()
general_ast = backend.visit(syntax)
return general_ast
示例12: __init__
# 需要导入模块: from pycparser import c_ast [as 别名]
# 或者: from pycparser.c_ast import FileAST [as 别名]
def __init__(self, psize=4, bigend=False):
self.psize = psize
self.pclass = vs_prim.v_ptr32
self.cls_parsers = {
c_ast.Decl: self.c_getVsDecl,
c_ast.Struct: self.c_getVsStruct,
c_ast.FileAST: self.c_getFileAst,
c_ast.PtrDecl: self.c_getPointer,
#c_ast.FuncDecl: self.c_getFuncDecl,
c_ast.Constant: self.c_getConstant,
c_ast.TypeDecl: self.c_getVsType,
c_ast.ArrayDecl: self.c_getVsArray,
c_ast.IdentifierType: self.c_getIdentType,
}
self.vs_ctypes = {
('char',): vs_prim.v_int8,
('unsigned','char'): vs_prim.v_uint8,
('short',): vs_prim.v_int16,
('short','int'): vs_prim.v_int16,
('unsigned', 'short',): vs_prim.v_uint16,
('unsigned', 'short','int'):vs_prim.v_uint16,
('int',): vs_prim.v_int32,
('unsigned','int',): vs_prim.v_uint32,
('long',): vs_prim.v_int32,
('long','int'): vs_prim.v_int32,
('unsigned','long',): vs_prim.v_uint32,
('unsigned','long','int'): vs_prim.v_uint32,
}
if psize == 8:
self.pclass = vs_prim.v_ptr64
self.vs_ctypes.update({
('long',): vs_prim.v_int64,
('long','int'): vs_prim.v_int64,
('unsigned','long',): vs_prim.v_uint64,
('unsigned','long','int'): vs_prim.v_uint64,
})
示例13: build_typemap
# 需要导入模块: from pycparser import c_ast [as 别名]
# 或者: from pycparser.c_ast import FileAST [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