Esempio n. 1
0
def process1(f):
    data = exportutils.preprocess(f, include=True)
    pats = {}
    cmds = {}
    res = fcnre.search(data)
    while res is not None:
        if res.group('command'):
            cmds[res.group('name')] = res.group('command')
            if not res.group(0).startswith('static'):
                gcmds[res.group('name')] = res.group('command')
        else:
            pats[res.group('name')] = res.group('pattern')
            if not res.group(0).startswith('static'):
                gpats[res.group('name')] = res.group('pattern')
        res = fcnre.search(data, pos=res.end(0))

    res = patre.search(data)
    while res is not None:
        imp = res.group('imp')
        mod = res.group('mod')
        fcn = res.group('fcn')
        retc = res.group('retc')
        argc = res.group('argc')
        args = res.group('args')
        if res.group('cmdpat') == 'pattern':
            if imp not in pats and imp not in gpats:
                if imp in cmds or imp in gcmds:
                    print('command implementation {} for pattern {}.{}'.format(
                        imp, mod, fcn))
                else:
                    mel.append(('pattern', imp, mod, fcn, retc, argc, args))
        else:
            if imp not in cmds and imp not in gcmds:
                if imp in pats or imp in gpats:
                    print('pattern implementation {} for command {}.{}'.format(
                        imp, mod, fcn))
                else:
                    mel.append(('command', imp, mod, fcn, retc, argc, args))
            else:
                checkcommand(imp, mod, fcn, cmds.get(imp, gcmds.get(imp)),
                             int(retc), int(argc), args)
        malcheck(imp, mod, fcn, int(retc), int(argc), args)
        res = patre.search(data, pos=res.end(0))
Esempio n. 2
0
def process(f):
    data = open(f).read()
    if f.endswith('.mal'):
        data = re.sub(r'[ \t]*#.*', '', data) # remove comments
        for res in comreg.finditer(data):
            malf, args, rets, func = res.groups()
            if malf not in atomfunctypes or args.strip():
                rtypes = []
                atypes = []
                if not rets:
                    rets = ':void'
                for tres in treg.finditer(rets):
                    typ = tres.group(1)
                    if typ.startswith('bat['):
                        typ = 'bat'
                    rtypes.append(mappings.get(typ, typ))
                for tres in treg.finditer(args):
                    typ = tres.group(1)
                    if typ.startswith('bat['):
                        typ = 'bat'
                    atypes.append(mappings.get(typ, typ))
                malfuncs.append((tuple(rtypes), tuple(atypes), malf, func, f))
            elif args.strip():
                print('atom function %s should be declared without arguments in %s' % (malf, f))
            else:
                if rets:
                    print('atom function %s should be declared without return type in %s' % (malf, f))
                atom = None
                base = None
                for ares in atmreg.finditer(data, 0, res.start(0)):
                    atom = ares.group('atom')
                    base = ares.group('base')
                if not atom:
                    print('atom function %s declared without known atom name in %s' % (malf, f))
                    continue
                atomfuncs.append((malf, atom, base, func, f))
        for res in patreg.finditer(data):
            malf, args, rets, func = res.groups()
            malpats.append((malf, func, f))
    elif f.endswith('.h') or f.endswith('.c'):
        data = exportutils.preprocess(data)

        for res in expre.finditer(data):
            pos = res.end(0)
            decl = exportutils.normalize(res.group('decl'))
            res = nmere.search(decl)
            if decl.startswith('char *'):
                decl = 'str ' + decl[6:]
            if '(' in decl:
                res = freg.match(decl)
                if res is not None:
                    rtype, name, args = res.groups()
                    args = [y for y in map(lambda x: x.strip(), args.split(','))]
                    if len(args) == 4 and \
                       args[0].startswith('Client ') and \
                       args[1].startswith('MalBlkPtr ') and \
                       args[2].startswith('MalStkPtr ') and \
                       args[3].startswith('InstrPtr ') and \
                       rtype == 'str':
                        pdecls[name] = f
                    elif rtype == 'str':
                        a = []
                        for arg in args:
                            if '(' in arg:
                                # complicated (function pointer) argument
                                break
                            if creg.search(arg) is not None:
                                rdonly = True
                                arg = creg.sub('', arg)
                            else:
                                rdonly = False
                            arg = arg.strip()
                            if arg.startswith('ptr ') and not '*' in arg:
                                arg = 'void *' + arg[4:]
                            # normalize "char *" to "str"
                            if arg.startswith('char **'):
                                arg = 'str ' + arg[6:]
                            elif arg.startswith('char *'):
                                arg = 'str' + arg[6:]
                            if '*' in arg or ' ' in arg:
                                # remove argument name (just keeping type)
                                arg = argreg.sub('', arg)
                            if '*' not in arg:
                                break
                            typ = areg.match(arg).group(0)
                            a.append((cmappings.get(typ, typ), rdonly))
                        else:
                            decls[name] = (tuple(a), f)
                    else:
                        if rtype == 'ptr':
                            rtype = 'void *'
                        a = []
                        for arg in args:
                            if '(' in arg:
                                # complicated (function pointer) argument
                                break
                            if creg.search(arg) is not None:
                                rdonly = True
                                arg = creg.sub('', arg)
                            else:
                                rdonly = False
                            arg = arg.strip()
                            if '*' in arg or ' ' in arg:
                                # remove argument name (just keeping type)
                                arg = argreg.sub('', arg)
                            if arg == 'str':
                                arg = 'char *'
                            a.append((arg, rdonly))
                        else:
                            odecls[name] = (rtype, tuple(a), f)
Esempio n. 3
0
def process(f):
    data = open(f).read()
    if f.endswith('.mal'):
        data = re.sub(r'[ \t]*#.*', '', data)  # remove comments
        for res in comreg.finditer(data):
            malf, args, rets, func = res.groups()
            if malf not in atomfunctypes or args.strip():
                rtypes = []
                atypes = []
                if not rets:
                    rets = ':void'
                for tres in treg.finditer(rets):
                    typ = tres.group(1)
                    if typ.startswith('bat['):
                        typ = 'bat'
                    rtypes.append(mappings.get(typ, typ))
                for tres in treg.finditer(args):
                    typ = tres.group(1)
                    if typ.startswith('bat['):
                        typ = 'bat'
                    atypes.append(mappings.get(typ, typ))
                malfuncs.append((tuple(rtypes), tuple(atypes), malf, func, f))
            elif args.strip():
                print(
                    'atom function %s should be declared without arguments in %s'
                    % (malf, f))
            else:
                if rets:
                    print(
                        'atom function %s should be declared without return type in %s'
                        % (malf, f))
                atom = None
                base = None
                for ares in atmreg.finditer(data, 0, res.start(0)):
                    atom = ares.group('atom')
                    base = ares.group('base')
                if not atom:
                    print(
                        'atom function %s declared without known atom name in %s'
                        % (malf, f))
                    continue
                atomfuncs.append((malf, atom, base, func, f))
        for res in patreg.finditer(data):
            malf, args, rets, func = res.groups()
            malpats.append((malf, func, f))
    elif f.endswith('.h') or f.endswith('.c'):
        data = exportutils.preprocess(data)

        for res in expre.finditer(data):
            pos = res.end(0)
            decl = exportutils.normalize(res.group('decl'))
            res = nmere.search(decl)
            if decl.startswith('char *'):
                decl = 'str ' + decl[6:]
            if '(' in decl:
                res = freg.match(decl)
                if res is not None:
                    rtype, name, args = res.groups()
                    args = [
                        y for y in map(lambda x: x.strip(), args.split(','))
                    ]
                    if len(args) == 4 and \
                       args[0].startswith('Client ') and \
                       args[1].startswith('MalBlkPtr ') and \
                       args[2].startswith('MalStkPtr ') and \
                       args[3].startswith('InstrPtr ') and \
                       rtype == 'str':
                        pdecls[name] = f
                    elif rtype == 'str':
                        a = []
                        for arg in args:
                            if '(' in arg:
                                # complicated (function pointer) argument
                                break
                            if creg.search(arg) is not None:
                                rdonly = True
                                arg = creg.sub('', arg)
                            else:
                                rdonly = False
                            arg = arg.strip()
                            if arg.startswith('ptr ') and not '*' in arg:
                                arg = 'void *' + arg[4:]
                            # normalize "char *" to "str"
                            if arg.startswith('char **'):
                                arg = 'str ' + arg[6:]
                            elif arg.startswith('char *'):
                                arg = 'str' + arg[6:]
                            if '*' in arg or ' ' in arg:
                                # remove argument name (just keeping type)
                                arg = argreg.sub('', arg)
                            if '*' not in arg:
                                break
                            typ = areg.match(arg).group(0)
                            a.append((cmappings.get(typ, typ), rdonly))
                        else:
                            decls[name] = (tuple(a), f)
                    else:
                        if rtype == 'ptr':
                            rtype = 'void *'
                        a = []
                        for arg in args:
                            if '(' in arg:
                                # complicated (function pointer) argument
                                break
                            if creg.search(arg) is not None:
                                rdonly = True
                                arg = creg.sub('', arg)
                            else:
                                rdonly = False
                            arg = arg.strip()
                            if '*' in arg or ' ' in arg:
                                # remove argument name (just keeping type)
                                arg = argreg.sub('', arg)
                            if arg == 'str':
                                arg = 'char *'
                            a.append((arg, rdonly))
                        else:
                            odecls[name] = (rtype, tuple(a), f)