示例#1
0
def which_fix_goes_first(program, fix1, fix2):
    try:
        fix1_location = extract_line_number(' '.join(fix1.split()[1:]))
        fix2_location = extract_line_number(' '.join(fix2.split()[1:]))
    except Exception:
        #print fix1
        #print fix2
        raise
    
    if not fix_ids_are_in_program(recompose_program(get_lines(program)[fix2_location:]), fix2) and fix_ids_are_in_program(recompose_program(get_lines(program)[fix1_location:]), fix1):
        return fix1
    
    if not fix_ids_are_in_program(recompose_program(get_lines(program)[fix1_location:]), fix1) and fix_ids_are_in_program(recompose_program(get_lines(program)[fix2_location:]), fix2):
        return fix2
    
    if not fix_ids_are_in_program(recompose_program(get_lines(program)[fix1_location:]), fix1) and not fix_ids_are_in_program(recompose_program(get_lines(program)[fix2_location:]), fix2):
        raise CouldNotFindUsesForEitherException
    
    if fix1_location < fix2_location:
        return fix1
    elif fix2_location < fix1_location:
        return fix2
    
    prog_lines = get_lines(program)
    id_in_fix1 = None
    id_in_fix2 = None
    
    for token in fix1.split():
        if '_<id>_' in token:
            assert id_in_fix1 == None, fix1
            id_in_fix1 = token
        elif token == '_<op>_[':
            break
    
    for token in fix2.split():
        if '_<id>_' in token:
            assert id_in_fix2 == None, fix2
            id_in_fix2 = token
        elif token == '_<op>_[':
            break
    
    assert id_in_fix1 != id_in_fix2, fix1 + ' & ' + fix2
    assert fix1_location == fix2_location
    
    for i in range(fix1_location, len(prog_lines)):
        for token in prog_lines[i].split():
            if token == id_in_fix1:
                return fix1
            elif token == id_in_fix2:
                return fix2
    
    assert False, 'unreachable code'
    raise CouldNotFindUsesForEitherException
示例#2
0
    def _sanitize_brackets(self, tokens_string):
        lines = get_lines(tokens_string)

        if len(lines) == 1:
            # Should be lines == ['']???
            raise EmptyProgramException(tokens_string)

        for i in range(len(lines) - 1, -1, -1):
            line = lines[i]

            if line.strip() == '_<op>_}' or line.strip() == '_<op>_} _<op>_}' \
               or line.strip() == '_<op>_} _<op>_} _<op>_}' or line.strip() == '_<op>_} _<op>_;' \
               or line.strip() == '_<op>_} _<op>_} _<op>_} _<op>_}' \
               or line.strip() == '_<op>_{' \
               or line.strip() == '_<op>_{ _<op>_{':
                if i > 0:
                    lines[i - 1] += ' ' + line.strip()
                    lines[i] = ''
                else:
                    # can't handle this case!
                    return ''

        # Remove empty lines
        for i in range(len(lines) - 1, -1, -1):
            if lines[i] == '':
                del lines[i]

        for line in lines:
            assert (lines[i].strip() != '')
            # Should be line instead of lines[i]???

        return recompose_program(lines)
示例#3
0
def add_fix_number(corrupted_prog, fix_number):
    try:
        lines = get_lines(corrupted_prog)
    except Exception:
        print corrupted_prog
        raise
    
    last_line = '_<directive>_#include _<include>_<FixNumber_%d>' % fix_number
    lines.append(last_line)
        
    return recompose_program(lines)
示例#4
0
def do_fix_at_line(corrupted_prog, line, fix):
    try:
        lines = get_lines(corrupted_prog)
    except Exception:
        print corrupted_prog
        raise
    
    try:
        lines[line] = fix
    except IndexError:
        raise
        
    return recompose_program(lines)
def apply_fix(program, fix, kind='replace', check_literals=False):
    # Break up program string into lines
    lines = get_lines(program)

    # Truncate the fix
    fix = _truncate_fix(fix)

    # Make sure there are two parts
    if len(fix.split('~')) != 2:
        raise InvalidFixLocationException

    # Retrieve insertion location
    try:
        if kind == 'replace':
            fix_location = extract_line_number(fix)
        else:
            assert kind == 'insert'

            if fix.split()[0] != '_<insertion>_':
                print "Warning: First token did not suggest insertion (should not happen)"

            fix_location = extract_line_number(' '.join(fix.split()[1]))
    except FailedToGetLineNumberException:
        raise InvalidFixLocationException

    # Remove line number
    fix = _remove_line_number(fix)

    # Insert the fix
    if kind == 'replace':
        try:
            if lines[fix_location].count('_<id>_') != fix.count('_<id>_'):
                raise SubstitutionFailedException
            if check_literals:
                for lit in ['string', 'char', 'number']:
                    if lines[fix_location].count('_<%s>' % lit) != fix.count('_<%s>_' % lit):
                        raise SubstitutionFailedException

            lines[fix_location] = replace_ids(fix, lines[fix_location])
        except IndexError:
            raise InvalidFixLocationException
    else:
        assert kind == 'insert'
        lines.insert(fix_location+1, fix)

    return recompose_program(lines)
示例#6
0
def undeclare_variable(rng, program_string):
    # Lines
    orig_lines = get_lines(program_string)
    # Variables
    variables = []
    for token in program_string.split():
        if '_<id>_' in token and token not in variables:
            variables.append(token)
    # Look for a declaration
    declaration, declaration_pos = find_declaration(
        rng, variables, list(range(len(orig_lines))), orig_lines)
    # Find the function signature
    fix_line = insert_fix(declaration_pos, orig_lines)
    fix = '_<insertion>_ {} ~ {}'.format(' '.join(str(fix_line)), declaration)
    # ...
    if orig_lines[declaration_pos].strip() == '':
        del orig_lines[declaration_pos]
    return recompose_program(orig_lines), fix, fix_line
示例#7
0
def do_fix_at_line(corrupted_prog, line, fix):
    try:
        lines = get_lines(corrupted_prog)
    except Exception:
        print corrupted_prog
        raise
    if '~' in fix:
        try:
            fix = fix.split(' ~ ')[1]
            fix = fix.strip()
        except:
            print fix, fix.split(' ~ ')
            raise
    try:
        lines[line] = fix
    except IndexError:
        raise
    return recompose_program(lines)
def apply_fix(program, fix, kind='replace', check_literals=False):
    print "apply_fix passed"
    # Break up program string into lines
    lines = get_lines(program)

    print "*******************"
    print "lines ="
    print lines
    print "*******************"
    print "lines length :", len(lines)
    # Truncate the fix
    fix = _truncate_fix(fix)
    print "*******************"
    print "fix ="
    print fix
    print "*******************"
    print "fix.split('~') :", fix.split('~')
    print "len(fix.split('~')) :", len(fix.split('~'))
    # Make sure there are two parts
    if len(fix.split('~')) != 2:
        print "InvalidFixLocationExeption"
        print "can not split 2 part"
        raise InvalidFixLocationException
    print "Retrieve insertion location"
    # Retrieve insertion location
    try:
        print "if replace 1"
        if kind == 'replace':
            fix_location = extract_line_number(fix)
            print "kind == replace"
            print "*******************"
            print "fix_location ="
            print fix_location
            print "*******************"
        else:
            assert kind == 'insert'

            if fix.split()[0] != '_<insertion>_':
                print "Warning: First token did not suggest insertion (should not happen)"

            fix_location = extract_line_number(' '.join(fix.split()[1]))
            print "*******************"
            print "fix_location =="
            print fix_location
            print "*******************"
    except FailedToGetLineNumberException:
        raise InvalidFixLocationException
    print "Remove line number"
    # Remove line number
    fix = _remove_line_number(fix)

    print "*******************"
    print "fix ="
    print fix
    print "*******************"
    # Insert the fix
    if kind == 'replace':
        print "if replace 2"
        try:
            check_literals = False  #debug
            if lines[fix_location].count('_<id>_') != fix.count('_<id>_'):
                print "not include original id"
                raise SubstitutionFailedException
            if check_literals:
                print "check literals"
                for lit in ['string', 'char', 'number']:
                    if lines[fix_location].count('_<%s>' % lit) != fix.count(
                            '_<%s>_' % lit):
                        print "not include original literal"
                        raise SubstitutionFailedException

            lines[fix_location] = replace_ids(fix, lines[fix_location])
        except IndexError:
            print "InvalidFixLocationException"
            raise InvalidFixLocationException
    else:
        assert kind == 'insert'
        lines.insert(fix_location + 1, fix)
    print "apply_fix end"
    return recompose_program(lines)