aboutsummaryrefslogtreecommitdiff
path: root/trim-lcov.py
blob: 767ab3716608d9b5b26feb3ce8b24d0b996e469a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

# Copyright 2013 Canonical Ltd.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# version 3.0 as published by the Free Software Foundation.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License version 3.0 for more details.
#
# You should have received a copy of the GNU General Public
# License along with this library. If not, see
# <http://www.gnu.org/licenses/>.
#
# Author: Ryan Lortie <desrt@desrt.ca>


# This script removes branch and/or line coverage data for lines that
# contain a particular substring.
#
# In the interest of "fairness" it removes all branch or coverage data
# when a match is found -- not just negative data.  It is therefore
# likely that running this script will actually reduce the total number
# of lines and branches that are marked as covered (in absolute terms).
#
# This script intentionally avoids checking for errors.  Any exceptions
# will trigger make to fail.

import sys

line_suppress = ['g_assert_not_reached']
branch_suppress = ['g_assert', 'g_return_if_fail', 'g_return_val_if_fail', 'G_DEFINE_TYPE']

def check_suppress(suppressions, source, data):
    line, _, rest = data.partition(',')
    line = int(line) - 1

    assert line < len(source)

    for suppression in suppressions:
        if suppression in source[line]:
            return True

    return False

source = []
for line in sys.stdin:
    line = line[:-1]

    keyword, _, rest = line.partition(':')

    # Source file
    if keyword == 'SF':

        with open(rest, 'r') as pFile:

            source = pFile.readlines()

    # Branch coverage data
    elif keyword == 'BRDA':
        if check_suppress(branch_suppress, source, rest):
            continue

    # Line coverage data
    elif keyword == 'DA':
        if check_suppress(line_suppress, source, rest):
            continue

    print(line)