Exemplo n.º 1
0
    def visit_If(self, node):
        label_visitor = LabelVisitor()
        label_visitor.visit(node.test)

        test = self.append_node(
            Node(label_visitor.result,
                 node,
                 line_number=node.lineno,
                 path=self.filenames[-1]))

        self.add_if_label(test)

        body_connect_stmts = self.stmt_star_handler(node.body)
        test.connect(body_connect_stmts.first_statement)

        if node.orelse:
            orelse_last_nodes = self.handle_or_else(node.orelse, test)
            body_connect_stmts.last_statements.extend(orelse_last_nodes)
        else:
            body_connect_stmts.last_statements.append(
                test
            )  # if there is no orelse, test needs an edge to the next_node

        last_statements = self.remove_breaks(
            body_connect_stmts.last_statements)

        return ControlFlowNode(
            test,
            last_statements,
            break_statements=body_connect_stmts.break_statements)
Exemplo n.º 2
0
    def assign_tuple_target(self, node, right_hand_side_variables):
        new_assignment_nodes = list()
        for i, target in enumerate(node.targets[0].elts):
            value = node.value.elts[i]

            label = LabelVisitor()
            label.visit(target)

            if isinstance(value, ast.Call):
                new_ast_node = ast.Assign(target, value)
                new_ast_node.lineno = node.lineno

                new_assignment_nodes.append(
                    self.assignment_call_node(label.result, new_ast_node))

            else:
                label.result += ' = '
                label.visit(value)

                new_assignment_nodes.append(
                    self.append_node(
                        AssignmentNode(label.result,
                                       self.extract_left_hand_side(target),
                                       ast.Assign(target, value),
                                       right_hand_side_variables,
                                       line_number=node.lineno,
                                       path=self.filenames[-1])))

        self.connect_nodes(new_assignment_nodes)
        return ControlFlowNode(new_assignment_nodes[0],
                               [new_assignment_nodes[-1]],
                               [])  # return the last added node
Exemplo n.º 3
0
    def visit_Raise(self, node):
        label = LabelVisitor()
        label.visit(node)

        return self.append_node(
            RaiseNode(label.result,
                      line_number=node.lineno,
                      path=self.filenames[-1]))
Exemplo n.º 4
0
    def visit_NameConstant(self, node):
        label_visitor = LabelVisitor()
        label_visitor.visit(node)

        return self.append_node(
            Node(label_visitor.result,
                 node.__class__.__name__,
                 node,
                 line_number=node.lineno,
                 path=self.filenames[-1]))
Exemplo n.º 5
0
    def add_builtin(self, node):
        label = LabelVisitor()
        label.visit(node)
        builtin_call = Node(label.result,
                            node,
                            line_number=node.lineno,
                            path=self.filenames[-1])

        if not self.undecided:
            self.nodes.append(builtin_call)
        self.undecided = False
        return builtin_call
Exemplo n.º 6
0
    def visit_With(self, node):
        label_visitor = LabelVisitor()
        label_visitor.visit(node.items[0])

        with_node = self.append_node(
            Node(label_visitor.result,
                 node,
                 line_number=node.lineno,
                 path=self.filenames[-1]))
        connect_statements = self.stmt_star_handler(node.body)
        with_node.connect(connect_statements.first_statement)
        return ControlFlowNode(with_node, connect_statements.last_statements,
                               connect_statements.break_statements)
Exemplo n.º 7
0
    def visit_While(self, node):
        label_visitor = LabelVisitor()
        label_visitor.visit(node.test)

        test = self.append_node(
            Node(label_visitor.result,
                 node,
                 line_number=node.lineno,
                 path=self.filenames[-1]))

        self.add_while_label(test)

        return self.loop_node_skeleton(test, node)
Exemplo n.º 8
0
    def visit_Call(self, node):

        vars = VarsVisitor()
        vars.visit(node)

        label = LabelVisitor()
        label.visit(node)
        
        n = Node(label.result, variables = vars.result)
        CFG.append(n)

        print_CFG(CFG)
                
        return n
Exemplo n.º 9
0
    def visit_AugAssign(self, node):
        label = LabelVisitor()
        label.visit(node)

        rhs_visitor = RHSVisitor()
        rhs_visitor.visit(node.value)

        return self.append_node(
            AssignmentNode(label.result,
                           self.extract_left_hand_side(node.target),
                           node,
                           rhs_visitor.result,
                           line_number=node.lineno,
                           path=self.filenames[-1]))
Exemplo n.º 10
0
    def visit_Call(self, node):

        vars = VarsVisitor()
        vars.visit(node)

        label = LabelVisitor()
        label.visit(node)

        n = Node(label.result, variables=vars.result)
        CFG.append(n)

        print_CFG(CFG)

        return n
Exemplo n.º 11
0
    def visit_Compare(self, node):
        
        vars = VarsVisitor()
        for i in node.comparators:
            vars.visit(i)
        vars.visit(node.left)

        label = LabelVisitor()
        label.visit(node)

        n = Node(label.result, variables = vars.result)
        CFG.append(n)

        print_CFG(CFG)

        return n
Exemplo n.º 12
0
    def visit_Return(self, node):
        label = LabelVisitor()
        label.visit(node)

        this_function_name = self.function_return_stack[-1]

        rhs_visitor = RHSVisitor()
        rhs_visitor.visit(node.value)
        LHS = 'ret_' + this_function_name
        return self.append_node(
            ReturnNode(LHS + ' = ' + label.result,
                       LHS,
                       rhs_visitor.result,
                       node,
                       line_number=node.lineno,
                       path=self.filenames[-1]))
Exemplo n.º 13
0
    def visit_Compare(self, node):

        vars = VarsVisitor()
        for i in node.comparators:
            vars.visit(i)
        vars.visit(node.left)

        label = LabelVisitor()
        label.visit(node)

        n = Node(label.result, variables=vars.result)
        CFG.append(n)

        print_CFG(CFG)

        return n
Exemplo n.º 14
0
    def save_actual_parameters_in_temp(self, args, arguments, line_number):
        """Save the actual parameters of a function call."""
        parameters = dict()
        for i, parameter in enumerate(args):
            temp_name = 'temp_' + str(self.function_index) + '_' + arguments[i]

            label_visitor = LabelVisitor()
            label_visitor.visit(parameter)
            n = RestoreNode(temp_name + ' = ' + label_visitor.result,
                            temp_name, [label_visitor.result],
                            line_number=line_number,
                            path=self.filenames[-1])

            self.nodes[-1].connect(n)
            self.nodes.append(n)

            parameters[label_visitor.result] = arguments[i]
        return parameters
Exemplo n.º 15
0
    def visit_Assign(self, node):
        rhs_visitor = RHSVisitor()
        rhs_visitor.visit(node.value)
        if isinstance(node.targets[0], ast.Tuple):  #  x,y = [1,2]
            if isinstance(node.value, ast.Tuple):
                return self.assign_tuple_target(node, rhs_visitor.result)
            elif isinstance(node.value, ast.Call):
                call = None
                for element in node.targets[0].elts:
                    label = LabelVisitor()
                    label.visit(element)
                    call = self.assignment_call_node(label.result, node)
                return call
        elif len(node.targets) > 1:  #  x = y = 3
            return self.assign_multi_target(node, rhs_visitor.result)
        else:
            if isinstance(node.value, ast.Call):  #  x = call()

                label = LabelVisitor()
                label.visit(node.targets[0])
                return self.assignment_call_node(label.result, node)
            else:  #  x = 4
                label = LabelVisitor()
                label.visit(node)
                return self.append_node(
                    AssignmentNode(label.result,
                                   self.extract_left_hand_side(
                                       node.targets[0]),
                                   node,
                                   rhs_visitor.result,
                                   line_number=node.lineno,
                                   path=self.filenames[-1]))
Exemplo n.º 16
0
    def assign_multi_target(self, node, right_hand_side_variables):
        new_assignment_nodes = list()

        for target in node.targets:
            label = LabelVisitor()
            label.visit(target)
            left_hand_side = label.result
            label.result += ' = '
            label.visit(node.value)

            new_assignment_nodes.append(
                self.append_node(
                    AssignmentNode(label.result,
                                   left_hand_side,
                                   ast.Assign(target, node.value),
                                   right_hand_side_variables,
                                   line_number=node.lineno,
                                   path=self.filenames[-1])))

        self.connect_nodes(new_assignment_nodes)
        return ControlFlowNode(new_assignment_nodes[0],
                               [new_assignment_nodes[-1]],
                               [])  # return the last added node
Exemplo n.º 17
0
    def add_class(self, call_node, def_node):
        label_visitor = LabelVisitor()
        label_visitor.visit(call_node)

        previous_node = self.nodes[-1]

        entry_node = self.append_node(EntryExitNode("Entry " + def_node.name))

        previous_node.connect(entry_node)

        function_body_connect_statements = self.stmt_star_handler(
            def_node.body)

        entry_node.connect(function_body_connect_statements.first_statement)

        exit_node = self.append_node(EntryExitNode("Exit " + def_node.name))
        exit_node.connect_predecessors(
            function_body_connect_statements.last_statements)

        return Node(label_visitor.result,
                    call_node,
                    line_number=call_node.lineno,
                    path=self.filenames[-1])
Exemplo n.º 18
0
    def visit_For(self, node):
        self.undecided = True  # Used for handling functions in for loops

        #issue23
        iterator_label = LabelVisitor()
        iterator = iterator_label.visit(node.iter)
        self.undecided = False

        target_label = LabelVisitor()
        target = target_label.visit(node.target)

        for_node = self.append_node(
            Node("for " + target_label.result + " in " +
                 iterator_label.result + ':',
                 node,
                 line_number=node.lineno,
                 path=self.filenames[-1]))

        if isinstance(node.iter, ast.Call) and get_call_names_as_string(
                node.iter.func) in self.function_names:
            last_node = self.visit(node.iter)
            last_node.connect(for_node)

        return self.loop_node_skeleton(for_node, node)
Exemplo n.º 19
0
    def perform_labeling_on_expression(self, expr):
        obj = parse(expr)
        label = LabelVisitor()
        label.visit(obj)

        return label
Exemplo n.º 20
0
    def visit_Assign(self, node):
        rhs_visitor = RHSVisitor()
        rhs_visitor.visit(node.value)
        if isinstance(node.targets[0], ast.Tuple):  # x,y = [1,2]
            if isinstance(node.value, ast.Tuple):
                for i, target in enumerate(node.targets[0].elts):
                    if isinstance(target, ast.Name) & (target.id
                                                       in traceParams):
                        value = node.value.elts[i]
                        label = LabelVisitor()
                        label.visit(target)
                        if isinstance(value, ast.Call):
                            Tracker.paramsStatements.append(label.result)
                        else:
                            label.result += ' = '
                            label.visit(value)
                            Tracker.paramsStatements.append(label.result)

            elif isinstance(node.value, ast.Call):
                call = None
                # print(astor.to_source(node, indent_with=' ' * 4, add_line_information=False))
                for element in node.targets[0].elts:
                    if isinstance(element, ast.Tuple):
                        for tup in element.elts:
                            # print(tup)
                            if tup.id in traceParams:
                                label = LabelVisitor()
                                label.visit(tup)
                                left_hand_label = label.result
                                # print(left_hand_label)
                                label = LabelVisitor()
                                label.visit(node.value)
                                Tracker.paramsStatements.append(
                                    left_hand_label + '=' + label.result)

                    # if isinstance(element,ast.Name) & (element.id in traceParams):
                    #     label = LabelVisitor()
                    #     label.visit(element)
                    #     left_hand_label = label.result
                    #     label = LabelVisitor()
                    #     label.visit(node.value)
                    #     Tracker.paramsStatements.append(left_hand_label + '=' + label.result)

        elif len(node.targets) > 1:  # x = y = 3
            for target in node.targets:
                label = LabelVisitor()
                label.visit(target)
                if label.result in traceParams:
                    label.result += ' = '
                    label.visit(node.value)
                    Tracker.paramsStatements.append(label.result)

        else:
            label = LabelVisitor()
            label.visit(node.targets[0])
            left_hand_label = label.result
            if left_hand_label in traceParams:
                if isinstance(node.value, ast.Call):  # x = call()
                    label = LabelVisitor()
                    label.visit(node.value)
                    Tracker.paramsStatements.append(left_hand_label + '=' +
                                                    label.result)

                else:  # x = 4
                    label = LabelVisitor()
                    label.visit(node)
                    Tracker.paramsStatements.append(label.result)
Exemplo n.º 21
0
    def visit_Assign(self, node):
        rhs_visitor = RHSVisitor()
        rhs_visitor.visit(node.value)
        if isinstance(node.targets[0], ast.Tuple):  # x,y = [1,2]
            if isinstance(node.value, ast.Tuple):
                for i, target in enumerate(node.targets[0].elts):
                    if isinstance(target, ast.Name) & (target.id
                                                       in traceParams):
                        value = node.value.elts[i]
                        label = LabelVisitor()
                        label.visit(target)
                        if isinstance(value, ast.Call):
                            Tracker.paramsStatements.append(label.result)
                        else:
                            label.result += ' = '
                            label.visit(value)
                            Tracker.paramsStatements.append(label.result)

            elif isinstance(node.value, ast.Call):
                call = None
                for element in node.targets[0].elts:
                    if element.id in traceParams:
                        label = LabelVisitor()
                        label.visit(element)
                        left_hand_label = label.result
                        label = LabelVisitor()
                        label.visit(node.value)
                        Tracker.paramsStatements.append(left_hand_label + '=' +
                                                        label.result)

        elif len(node.targets) > 1:  # x = y = 3
            for target in node.targets:
                label = LabelVisitor()
                label.visit(target)
                if label.result in traceParams:
                    label.result += ' = '
                    label.visit(node.value)
                    Tracker.paramsStatements.append(label.result)

        else:
            label = LabelVisitor()
            label.visit(node.targets[0])
            left_hand_label = label.result
            if left_hand_label in traceParams:
                if isinstance(node.value, ast.Call):  # x = call()
                    label = LabelVisitor()
                    label.visit(node.value)
                    Tracker.paramsStatements.append(left_hand_label + '=' +
                                                    label.result)

                else:  # x = 4
                    label = LabelVisitor()
                    label.visit(node)
                    Tracker.paramsStatements.append(label.result)
Exemplo n.º 22
0
    def perform_labeling_on_expression(self, expr):
        obj = parse(expr)
        label = LabelVisitor()
        label.visit(obj)

        return label