コード例 #1
0
ファイル: malcheck.py プロジェクト: MonetDB/MonetDB
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)
コード例 #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)