当前位置: 首页>>代码示例>>Python>>正文


Python c_ast.FileAST方法代码示例

本文整理汇总了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]) 
开发者ID:johncsnyder,项目名称:SwiftKitten,代码行数:23,代码来源:cdecl.py

示例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 
开发者ID:pinterest,项目名称:ptracer,代码行数:27,代码来源:extract_ptrace_constants.py

示例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}") 
开发者ID:matt-kempster,项目名称:mips_to_c,代码行数:23,代码来源:c_types.py

示例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 
开发者ID:RRZE-HPC,项目名称:kerncraft,代码行数:27,代码来源:kernel.py

示例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)) 
开发者ID:johncsnyder,项目名称:SwiftKitten,代码行数:5,代码来源:test_general.py

示例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)) 
开发者ID:johncsnyder,项目名称:SwiftKitten,代码行数:17,代码来源:test_general.py

示例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)) 
开发者ID:johncsnyder,项目名称:SwiftKitten,代码行数:11,代码来源:test_general.py

示例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)) 
开发者ID:johncsnyder,项目名称:SwiftKitten,代码行数:6,代码来源:test_general.py

示例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) 
开发者ID:pinterest,项目名称:ptracer,代码行数:13,代码来源:extract_ptrace_constants.py

示例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) 
开发者ID:pinterest,项目名称:ptracer,代码行数:13,代码来源:extract_ptrace_constants.py

示例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 
开发者ID:mbdevpl,项目名称:transpyle,代码行数:7,代码来源:ast_generalizer.py

示例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,
            }) 
开发者ID:joxeankoret,项目名称:nightmare,代码行数:48,代码来源:cparse.py

示例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 
开发者ID:matt-kempster,项目名称:mips_to_c,代码行数:46,代码来源:c_types.py


注:本文中的pycparser.c_ast.FileAST方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。