Exemple #1
0
    def find_duplicates_without_smart_filter(all_blocks):
        duplicates = []

        seen_blocks = {}
        for _, block in all_blocks:
            tokens = tokens_info(block)
            if tokens in seen_blocks:
                duplicates.append((seen_blocks[tokens], block, ""))
            else:
                seen_blocks[tokens] = block

        return duplicates
Exemple #2
0
    def has_same_tests(expr):
        """
        For an if-statement or an if-expression, checks whether any
        combination of its tests are syntactically equivalent. If duplicate
        operands are found, return them.

        :rtype: lal.Expr|None
        """
        tests = {}
        all_tests = list_tests(expr)
        if len(all_tests) > 1:
            for test in all_tests:
                tokens = tokens_info(test)
                if tokens in tests:
                    return (tests[tokens], test)
                tests[tokens] = test
Exemple #3
0
    def has_same_operands(expr):
        """
        For a logic relation, checks whether any combination of its
        sub-operands are syntactically equivalent. If duplicate operands are
        found, return them.

        :rtype: lal.Expr|None
        """
        ops = {}
        all_ops = list_operands(expr)
        if len(all_ops) > 1:
            for op in all_ops:
                tokens = tokens_info(op)
                if tokens in ops:
                    return (ops[tokens], op)
                ops[tokens] = op
Exemple #4
0
    def has_same_operands(expr):
        """
        For a logic relation, checks whether any combination of its
        sub-operands are syntactically equivalent. If a duplicate operand is
        found, return it.

        :rtype: lal.Expr|None
        """
        ops = {}
        all_ops = list_left_unequal_operands(expr)
        if len(all_ops) > 1:
            for op in all_ops:
                (op_left, op_right) = op
                tokens = tokens_info(op_left)
                if tokens in ops:
                    return (op_left, ops[tokens], op_right)
                ops[tokens] = op_right
Exemple #5
0
    def find_duplicates_with_smart_filter(all_blocks):
        duplicates = []

        seen_blocks = {}
        for cond, block in all_blocks:
            # Find interesting elements in the condition that leads to this
            # block.
            cond_interests = interesting_elements_in(cond)

            block_tokens = tokens_info(block)
            block_info = seen_blocks.get(block_tokens)

            if block_info is not None:  # If we have seen this block already
                # Retrieve the interesting elements in the condition that lead
                # to the original block.
                orig_cond_interests, block_interests, orig_block = block_info

                # Compute the symmetric difference between the two sets of
                # interesting elements (get those that are in one of them but
                # not in both).
                interests_diff = orig_cond_interests.symmetric_difference(
                    cond_interests)

                # Try to find in the block itself occurrences of the
                # interesting elements that appear only in one of the
                # conditions.
                block_occurrences = frozenset(e for e in block_interests
                                              if e in interests_diff)

                # This may mean that one of the two blocks was not updated
                # properly, so register it.
                if len(block_occurrences) > 0:
                    # Compute the set of interesting elements that appear
                    # in one of the condition but not in the duplicated block.
                    unused_elems = interests_diff - block_occurrences
                    if len(unused_elems) == 1:
                        # If there is only one, maybe the user forgot to use
                        # that variable in the block where this element appears
                        # in the condition.

                        # Retrieve the sole element and create the message.
                        unused_elem = next(iter(unused_elems))
                        msg = _additional_msg_format.format(unused_elem)

                        if unused_elem in orig_cond_interests:
                            # If the unused element appears in the condition
                            # of the first block, that means the first block
                            # is probably the duplicate.
                            duplicates.append((block, orig_block, msg))
                        else:
                            # If the unused element appears in the condition
                            # of the second block, that means the second block
                            # is probably the duplicate.
                            duplicates.append((orig_block, block, msg))
                    else:
                        duplicates.append((orig_block, block, ""))
            else:
                seen_blocks[block_tokens] = (cond_interests,
                                             interesting_elements_in(block),
                                             block)

        return duplicates