def processClones(self, indx, clone, fd): clone1 = clone[0] clone2 = clone[1] op1 = clone1[3] op2 = clone2[3] clones = [] if config.DEBUG: print "==========================================" print "%d,%d,%d" % (clone1[0], int(clone1[1]), int(clone1[2])) print "operation 1: %s,%d" % (op1, len(op1)) print "operation 2: %s,%d" % (op2, len(op2)) if len(op1) == 0 or len(op2) == 0: return filter = opFilter(op1, op2) #First partition the clone regions in contiguous hunk hunk1 = self.getAdjHunk(op1) hunk2 = self.getAdjHunk(op2) if len(hunk1) == len(hunk2): for i in range(len(hunk1)): start1, end1 = hunk1[i].split('-') start2, end2 = hunk2[i].split('-') cl1 = (clone1[0], int(start1), int(end1)) cl2 = (clone2[0], int(start2), int(end2)) clones.append((indx, cl1, cl2)) else: #uncommon case: high = 1 hunk_max = hunk1 hunk_min = hunk2 start = clone2[1] op = op2 if len(hunk1) < len(hunk2): high = 2 hunk_max = hunk2 hunk_min = hunk1 start = clone1[1] op = op1 start2 = start for i in range(len(hunk_max)): start1, end1 = hunk_max[i].split('-') hunk_len = int(end1) - int(start1) end2 = start2 + hunk_len if high is 1: cl1 = (clone1[0], int(start1), int(end1)) cl2 = (clone2[0], int(start2), int(end2)) else: cl1 = (clone1[0], int(start2), int(end2)) cl2 = (clone2[0], int(start1), int(end1)) clones.append((indx, cl1, cl2)) index = end2 - start + 1 while index < len(op) and 'X' in op[index]: index += 1 start2 = start + index #second filter the hunks w.r.t their options for i in range(len(clones)): clone = clones[i] index = clone[0] cl1 = clone[1] cl2 = clone[2] metric = filter.filterByOp(clone) if metric is None: #only no-change operation continue fd.write("{0}\t{1}.{2}-{3}\t{4}.{5}-{6}\t{7}".format( index, cl1[0], cl1[1], cl1[2], cl2[0], cl2[1], cl2[2], metric)) fd.write(os.linesep)
def split_clone_into_hunks(clone_pair, debug = False): clone1 = clone_pair.clone1 clone2 = clone_pair.clone2 clones = [] #First partition the clone regions in contiguous hunk #print "operation 1:" hunks1 = get_adj_hunk(clone1.ops, debug) #print "operation 2:" hunks2 = get_adj_hunk(clone2.ops, debug) if debug and not (hunks2 and hunks1): print 'empty hunks? ' + str(clone_pair) if len(hunks1) == len(hunks2): for hunk1, hunk2 in zip(hunks1, hunks2): start1,end1 = hunk1 start2,end2 = hunk2 cl1 = Clone(clone_pair.clone1.fidx, start1, end1, []) cl2 = Clone(clone_pair.clone2.fidx, start2, end2, []) clones.append(ClonePair(cl1, cl2, clone_pair.metric)) else: #uncommon case: # or at least you had better hope its rare, because this logic is # not convincing at all high = 1 big_hunks = hunks1 small_hunks = hunks2 start = clone2.start small_ops = clone2.ops if len(hunks1) < len(hunks2): high = 2 big_hunks = hunks2 small_hunks = hunks1 start = clone1.start small_ops = clone1.ops small_start = start for big_hunk in big_hunks: big_start, big_end = big_hunk big_hunk_len = big_end - big_start small_end = small_start + big_hunk_len if high == 1: cl1 = Clone(clone1.fidx, big_start, big_end, []) cl2 = Clone(clone2.fidx, small_start, small_end, []) else: cl1 = Clone(clone1.fidx, small_start, small_end, []) cl2 = Clone(clone2.fidx, big_start, big_end, []) clones.append(ClonePair(cl1, cl2, clone_pair.metric)) index = small_end - start + 1 while index < len(small_ops) and 'X' == small_ops[index].op: index += 1 small_start = start + index ret = [] # second filter the hunks w.r.t their operations op_filter = opFilter(clone1.ops, clone2.ops) for clone_pair in clones: metric = op_filter.filterByOp(clone_pair) if not metric is None: ret.append(ClonePair(clone_pair.clone1, clone_pair.clone2, metric)) return ret
def processClones(self, indx, clone, fd): clone1 = clone[0] clone2 = clone[1] op1 = clone1[3] op2 = clone2[3] clones = [] # print "==========================================" # print op1 # print op2 filter = opFilter(op1, op2) # First partition the clone regions in contiguous hunk # print "operation 1:" hunk1 = self.getAdjHunk(op1) # print "operation 2:" hunk2 = self.getAdjHunk(op2) if len(hunk1) == len(hunk2): for i in range(len(hunk1)): start1, end1 = hunk1[i].split("-") start2, end2 = hunk2[i].split("-") cl1 = (clone1[0], int(start1), int(end1)) cl2 = (clone2[0], int(start2), int(end2)) clones.append((indx, cl1, cl2)) else: # uncommon case: high = 1 hunk_max = hunk1 hunk_min = hunk2 start = clone2[1] op = op2 if len(hunk1) < len(hunk2): high = 2 hunk_max = hunk2 hunk_min = hunk1 start = clone1[1] op = op1 start2 = start for i in range(len(hunk_max)): start1, end1 = hunk_max[i].split("-") hunk_len = int(end1) - int(start1) end2 = start2 + hunk_len if high is 1: cl1 = (clone1[0], int(start1), int(end1)) cl2 = (clone2[0], int(start2), int(end2)) else: cl1 = (clone1[0], int(start2), int(end2)) cl2 = (clone2[0], int(start1), int(end1)) clones.append((indx, cl1, cl2)) index = end2 - start + 1 while index < len(op) and "X" in op[index]: index += 1 start2 = start + index # second filter the hunks w.r.t their options for i in range(len(clones)): clone = clones[i] index = clone[0] cl1 = clone[1] cl2 = clone[2] metric = filter.filterByOp(clone) if metric is None: # only no-change operation continue fd.write( "{0}\t{1}.{2}-{3}\t{4}.{5}-{6}\t{7}".format( index, cl1[0], cl1[1], cl1[2], cl2[0], cl2[1], cl2[2], metric ) ) fd.write(os.linesep)