How to use the pycparser.c_ast.NodeVisitor function in pycparser

To help you get started, we’ve selected a few pycparser examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github unbit / uwsgi-docs / _options / parse_c.py View on Github external
# Good luck!

from pycparser import CParser
from pycparser.c_ast import NodeVisitor, Constant
import re
import sys

DIRECTIVE_RE = re.compile("^#(.+)$", re.MULTILINE)

class DictWrapper(object):
	def __init__(self, dict):
		self.dict = dict
	def __getattr__(self, key):
		return self.dict[key]

class OptionParser(NodeVisitor):
	def __init__(self):
		self.commands = []

	def visit_ExprList(self, node):
		bits = [n[1] for n in node.children()]
		if isinstance(bits[0], Constant) and bits[0].type == "string":
			self.add_option(node, bits)
		
		self.generic_visit(node)

	def parse_file(self, filename):
		parser = CParser()
		buf = file(filename).read()
		buf = DIRECTIVE_RE.sub("", buf)#r"/* directive \1 elided */", buf)
		t = parser.parse(buf, filename)
		self.visit(t)
github eventh / kpro9 / CSjark / csjark / cparser.py View on Github external
def parse(text, filename='', parser=c_parser.CParser()):
    """Parse C code and return an AST."""
    return parser.parse(text, filename)


def find_structs(ast, platform=None):
    """Walks the AST nodes to find structs."""
    if platform is None:
        platform = Platform.mappings['default']

    visitor = StructVisitor(platform)
    visitor.visit(ast)
    return list(StructVisitor.all_protocols.values())


class StructVisitor(c_ast.NodeVisitor):
    """A class which visit struct nodes in the AST.

    The Visitor traverse the Tree, and when it finds Struct, Enum, Union,
    Typedef or TypeDecl nodes it calls the respective methods in this class.

    It will populate all_protocols class member with Dissector-instances
    representing all the relevant C data structures it found.

    The alll_know_types class member is used to discover which C file
    should be included if we fail parsing because of unknown types.
    """

    all_protocols = {} # Map struct name to Protocol instances
    all_known_types = {} # Map type name to source filename

    _last_visitor = None
github simonlindholm / decomp-permuter / src / ast_util.py View on Github external
def is_effectful(expr: Expression) -> bool:
    found = False

    class Visitor(ca.NodeVisitor):
        def visit_UnaryOp(self, node: ca.UnaryOp) -> None:
            nonlocal found
            if node.op in ["p++", "p--", "++", "--"]:
                found = True
            else:
                self.generic_visit(node.expr)

        def visit_FuncCall(self, _: ca.Node) -> None:
            nonlocal found
            found = True

        def visit_Assignment(self, _: ca.Node) -> None:
            nonlocal found
            found = True

    Visitor().visit(expr)
github randombit / botan / src / scripts / ffi_decls.py View on Github external
elif typ == 'size_t':
            return 'POINTER(c_size_t)'
        #elif typ == 'uint8_t':
        #    return 'POINTER(c_uint8)'
        elif typ == 'uint32_t':
            return 'POINTER(c_uint32)'
        elif typ == 'uint64_t':
            return 'POINTER(c_uint64)'
        elif typ == 'int':
            return 'POINTER(c_int)'

    raise Exception("Unknown type %s/%d" % (typ, is_ptr))

GROUP = None

class FuncDefVisitor(c_ast.NodeVisitor):
    def visit_FuncDecl(self, node):

        if not isinstance(node.type, c_ast.TypeDecl):
            #print("ignoring", node.type)
            return

        if node.type.type.names != ['int']:
            #print("ignoring", node.type)
            return

        # all functions returning ints:
        fn_name = node.type.declname

        fn_group = fn_name.split('_')[1]
        if fn_group == 'privkey':
            fn_group = 'pubkey' # hack
github Kozea / cairocffi / utils / mkconstants.py View on Github external
import sys
import textwrap

import pycparser.c_generator


def parse_constant(node):
    if isinstance(node, pycparser.c_ast.Constant):
        return node.value
    elif isinstance(node, pycparser.c_ast.UnaryOp) and node.op == '-':
        return '-' + parse_constant(node.expr)
    else:
        raise TypeError(node)


class PrintEnumsVisitor(pycparser.c_ast.NodeVisitor):
    def visit_Decl(self, node):
        if node.name and node.name.startswith('CAIRO_'):  # len('CAIRO_') == 6
            if node.init.type == 'string':
                print('%s = b%s' % (node.name[6:], node.init.value))
            else:
                print('%s = %s' % (node.name[6:], node.init.value))
            print('')

    def visit_Enum(self, node):
        value = 0
        for enumerator in node.values.enumerators:
            if enumerator.value is not None:
                value_string = parse_constant(enumerator.value)
                value = int(value_string, 0)
            else:
                value_string = str(value)
github thomersch / grandine / lib / spatial / vendor / github.com / pmezard / gogeos / geos / geoscapi.py View on Github external
import sys
from string import Template
from collections import namedtuple
from pycparser import c_parser, c_ast, parse_file

Func = namedtuple('Func', ('name', 'type', 'args'))
Arg = namedtuple('Arg', ('name', 'type'))
Type = namedtuple('Type', ('ptr', 'name', 'array'))

class FuncDeclVisitor(c_ast.NodeVisitor):
    def __init__(self):
        self.funcs = []
        self.reset()

    def reset(self):
        self.name = None
        self.ptr = ''
        self.type = None
        self.inargs = False
        self.args = []
        self.argname = None
        self.array = False

    def visit_Typedef(self, node):
        # Prevent func decls in typedefs from being visited
        pass
github paulsmith / gogeos / geos / geoscapi.py View on Github external
import sys
from string import Template
from collections import namedtuple
from pycparser import c_parser, c_ast, parse_file

Func = namedtuple('Func', ('name', 'type', 'args'))
Arg = namedtuple('Arg', ('name', 'type'))
Type = namedtuple('Type', ('ptr', 'name', 'array'))

class FuncDeclVisitor(c_ast.NodeVisitor):
    def __init__(self):
        self.funcs = []
        self.reset()

    def reset(self):
        self.name = None
        self.ptr = ''
        self.type = None
        self.inargs = False
        self.args = []
        self.argname = None
        self.array = False

    def visit_Typedef(self, node):
        # Prevent func decls in typedefs from being visited
        pass
github networkupstools / nut / scripts / DMF / jsonify-mib.py View on Github external
else:
        r = list()
        r.extend (f2f (node.left))
        r.extend (f2f (node.right))
    return r

def widen_tuples(iter, width, default=None):
    for item in iter:
        if len(item) < width:
            item = list(item)
            while len(item) < width:
                item.append(default)
            item = tuple(item)
        yield item

class Visitor(c_ast.NodeVisitor):

    def __init__(self, *args, **kwargs):
        super(Visitor, self).__init__(*args, **kwargs)
        self._mappings = {
            "INFO" : dict (),
            "MIB2NUT" : dict (),
            "SNMP-INFO" : dict (),
            "ALARMS-INFO" : dict ()}

    def _visit_snmp_info_t (self, node):
        ret = list ()
        for _, ilist in node.init.children ():
            ditem = dict ()

            kids = ilist.children ()
github pjsip / pjproject / pjsip-apps / src / swig / importsym.py View on Github external
#define jmp_buf			int
#define __attribute__(x)
"""

# CPP (C preprocessor) settings
CPP_CFLAGS   = [
	'-I' + PYCPARSER_DIR + '/utils/fake_libc_include',
	"-I" + PJ_ROOT_PATH + "pjlib/include",
	"-I" + PJ_ROOT_PATH + "pjlib-util/include",
	"-I" + PJ_ROOT_PATH + "pjnath/include",
	"-I" + PJ_ROOT_PATH + "pjmedia/include",
	"-I" + PJ_ROOT_PATH + "pjsip/include"
	]


class SymbolVisitor(pycparser.c_ast.NodeVisitor):
	def __init__(self, names):
		self.nodeDict = {}
		for name in names:
			self.nodeDict[name] = None
		
	def _add(self, node):
		if self.nodeDict.has_key(node.name):
			self.nodeDict[node.name] = node
		
	def visit_Struct(self, node):
		self._add(node)
		
	def visit_Enum(self, node):
		self._add(node)

	def visit_Typename(self, node):