Ejemplo n.º 1
0
def merge_gcovs(gcovs):
    output_line_info = OrderedDict()

    for line in gcovs[0].line_info.iterkeys():
        lines = [g.line_info[line] for g in gcovs]

        Uncov_norm = '#####'
        Uncov_throw = '====='
        Uncov = [Uncov_norm, Uncov_throw]
        Nocode = '-'

        output_count = None

        lines_count = []
        for lc in lines:
            line_count = 0
            try:
                line_count = int(lc.count)
            except:
                pass
            lines_count.append(line_count)

        any_lines_covered = reduce(lambda x, y: x or y,
                                   [a > 0 for a in lines_count])
        any_lines_uncovered = reduce(lambda x, y: x or y,
                                     [a.count in Uncov for a in lines])
        all_lines_nocode = reduce(lambda x, y: x and y,
                                  [a.count == Nocode for a in lines])

        if any_lines_covered:
            output_count = sum(lines_count)
        elif any_lines_uncovered:
            output_count = Uncov_norm
        elif all_lines_nocode:
            output_count = Nocode

        #if line1_count > 0 or line2_count > 0:

        #  output_count = line1_count + line2_count
        #elif line1.count == Uncov_norm or line2.count == Uncov_norm:
        #  output_count = Uncov_norm
        #elif line1.count == Nocode and line2.count == Nocode:
        #  output_count = Nocode

        if output_count is None:
            print 'Unhandled situation:'
            for idx, line in enumerate(lines):
                print '  line %d: ' % idx, line

        output_line_info[line] = read_gcov.LineInfo(str(output_count), line,
                                                    lines[0].src)

    return read_gcov.GcovFile(gcovs[0].fname, gcovs[0].tags, output_line_info,
                              None, None, None, gcovs[0].func_ranges)
Ejemplo n.º 2
0
def mark_as_uncovered(gcov_base):
    diff_line_info = OrderedDict()
    for line in gcov_base.line_info.keys():
        base_line = gcov_base.line_info[line]
        Uncov_norm = '#####'
        Nocode = '-'

        diff_count = base_line.count
        try:
            base_count = int(base_line.count)
            diff_count = Uncov_norm
        except ValueError:
            if base_line.count == Uncov_norm:
                diff_count = Nocode

        diff_line_info[line] = read_gcov.LineInfo(diff_count, line,
                                                  base_line.src)

    return read_gcov.GcovFile(gcov_base.fname, gcov_base.tags, diff_line_info,
                              None, None, None, gcov_base.func_ranges)
Ejemplo n.º 3
0
def merge_two_gcovs(gcov1, gcov2):
    output_line_info = OrderedDict()

    for line in gcov1.line_info.iterkeys():
        line1 = gcov1.line_info[line]
        line2 = gcov2.line_info[line]

        Uncov_norm = '#####'
        Nocode = '-'

        output_count = None

        line1_count = 0
        try:
            line1_count = int(line1.count)
        except:
            pass

        line2_count = 0
        try:
            line2_count = int(line2.count)
        except:
            pass

        if line1_count > 0 or line2_count > 0:
            output_count = line1_count + line2_count
        elif line1.count == Uncov_norm or line2.count == Uncov_norm:
            output_count = Uncov_norm
        elif line1.count == Nocode and line2.count == Nocode:
            output_count = Nocode

        if output_count is None:
            print 'Unhandled situation:'
            print '  line1: ', line1
            print '  line2: ', line2

        output_line_info[line] = read_gcov.LineInfo(str(output_count), line,
                                                    line1.src)

    return read_gcov.GcovFile(gcov1.fname, gcov1.tags, output_line_info, None,
                              None, None, gcov1.func_ranges)
Ejemplo n.º 4
0
def compute_gcov_diff(gcov_base,
                      gcov_unit,
                      print_diff=False,
                      print_diff_summary=False,
                      coverage_stats=None):
    diff_line_info = OrderedDict()
    if print_diff or print_diff_summary:
        print('file ', gcov_base.tags['Source'])

    total_base_count = 0

    base_totals_total = 0
    base_totals_covered = 0
    base_totals_uncovered = 0

    unit_totals_total = 0
    unit_totals_covered = 0
    unit_totals_uncovered = 0
    unit_totals_rel_covered = 0
    unit_totals_rel_uncovered = 0

    for line in gcov_base.line_info.keys():
        if line not in gcov_unit.line_info:
            print('error, line not present: %d ,line=%s' %
                  (line, gcov_base.line_info[line]))
        base_line = gcov_base.line_info[line]
        unit_line = gcov_unit.line_info[line]

        Uncov_norm = '#####'
        Uncov_exp = '====='
        Uncov = [Uncov_norm, Uncov_exp]
        Nocode = '-'
        diff_count = None
        base_count = 0
        try:
            base_count = int(base_line.count)
        except ValueError:
            pass

        unit_count = 0
        try:
            unit_count = int(unit_line.count)
        except ValueError:
            pass

        # Skip some compiler-added functions
        #  Assuming base and unit are the same
        if gcov_base.func_ranges:
            funcs = gcov_base.func_ranges.find_func(line)
            for func_name in funcs:
                if func_name:
                    if func_name.startswith("_GLOBAL__"):
                        base_count = 0
                        unit_count = 0
                    if 'static_initialization_and_destruction' in func_name:
                        base_count = 0
                        unit_count = 0

        total_base_count += base_count

        if base_line.count != Nocode:
            base_totals_total += 1
        if base_line.count in Uncov:
            base_totals_uncovered += 1
        if base_count > 0:
            base_totals_covered += 1

        if unit_line.count != Nocode:
            unit_totals_total += 1
        if unit_line.count in Uncov:
            unit_totals_uncovered += 1
        if unit_count > 0:
            unit_totals_covered += 1

        line_has_diff = False

        #  base_line.count is the string value for the count
        #  base_count is the converted integer value
        #     will be 0 for No code or uncovered

        if base_line.count == Nocode and unit_line.count == Nocode:
            diff_count = Nocode

        # doesn't work well if one side is nocode and the other has count 0
        #elif base_line.count == Nocode and unit_line.count != Nocode:
        #  diff_count = Nocode
        #  line_has_diff = True
        #elif base_line.count != Nocode and unit_line.count == Nocode:
        #  diff_count = Nocode
        #  line_has_diff = True

        elif base_line.count == Nocode and unit_count == 0:
            diff_count = Nocode
        elif base_count == 0 and unit_line.count == Nocode:
            diff_count = Nocode

        elif base_line.count in Uncov and unit_line.count in Uncov:
            #diff_count = 9999   # special count to indicate uncovered in base?
            diff_count = Nocode  # Not sure of the right solution

        elif base_count != 0 and unit_count == 0:
            diff_count = Uncov_norm
            unit_totals_rel_uncovered += 1
            line_has_diff = True
        elif base_count == 0 and unit_count != 0:
            diff_count = Nocode
            line_has_diff = True
        elif base_count != 0 and unit_count != 0:
            diff_count = unit_count
            unit_totals_rel_covered += 1
        elif base_count == 0 and unit_count == 0:
            diff_count = 0

        if print_diff and line_has_diff:
            if gcov_base.line_info[line].src.strip(
            ) != gcov_unit.line_info[line].src.strip():
                print('line diff, base: ', gcov_base.line_info[line])
                print('     unit: ', gcov_unit.line_info[line])
            print('%9s  %9s %6d : %s' % (base_line.count, unit_line.count,
                                         line, gcov_unit.line_info[line].src))
        if diff_count is None:
            print('Unhandled case: ', line, diff_count, base_line.count,
                  unit_line.count)

        diff_line_info[line] = read_gcov.LineInfo(diff_count, line,
                                                  base_line.src)

    if print_diff_summary:
        print('Base        Unit')
        print('%4d/%-4d   %4d/%-4d  covered lines' %
              (base_totals_covered, base_totals_total, unit_totals_covered,
               unit_totals_total))
        print('%4d/%-4d   %4d/%-4d  uncovered lines' %
              (base_totals_uncovered, base_totals_total, unit_totals_uncovered,
               unit_totals_total))
        print('            %4d/%-4d  uncovered lines relative to base' %
              (unit_totals_rel_uncovered,
               (unit_totals_rel_uncovered + unit_totals_rel_covered)))

    if coverage_stats is not None:
        base_coverage = FileCoverage(base_totals_covered,
                                     base_totals_uncovered, base_totals_total)
        unit_coverage = FileCoverage(unit_totals_covered,
                                     unit_totals_uncovered, unit_totals_total)
        rel_coverage = FileCoverage(
            unit_totals_rel_covered, unit_totals_rel_uncovered,
            unit_totals_rel_covered + unit_totals_rel_uncovered)

        cc = CompareCoverage(base_coverage, unit_coverage, rel_coverage)
        coverage_stats[gcov_base.tags['Source']] = cc

    if total_base_count == 0:
        return None

    return read_gcov.GcovFile(gcov_base.fname, gcov_base.tags, diff_line_info,
                              None, None, None, gcov_base.func_ranges)