| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
import sys |
| 7 |
|
| 8 |
def skip_comments(lines): |
| 9 |
state = 0 |
| 10 |
for line in lines: |
| 11 |
n = len(line) |
| 12 |
l = '' |
| 13 |
p = 0 |
| 14 |
while p < n: |
| 15 |
if state == 0: |
| 16 |
a = line.find('//', p) |
| 17 |
b = line.find('/*', p) |
| 18 |
if a > -1 and (a < b or b == -1): |
| 19 |
l += line[p:a] |
| 20 |
p = n |
| 21 |
elif b > -1 and (b < a or a == -1): |
| 22 |
l += line[p:b] |
| 23 |
p = b+2 |
| 24 |
state = 1 |
| 25 |
else: |
| 26 |
l += line[p:] |
| 27 |
p = n |
| 28 |
elif state == 1: |
| 29 |
a = line.rfind('*/', p) |
| 30 |
if a == -1: |
| 31 |
p = n |
| 32 |
else: |
| 33 |
p = a + 2 |
| 34 |
state = 0 |
| 35 |
yield l |
| 36 |
|
| 37 |
def cond_lines(lines): |
| 38 |
state = 0 |
| 39 |
pcnt = 0 |
| 40 |
for nr, line in enumerate(lines, 1): |
| 41 |
if not line: |
| 42 |
continue |
| 43 |
n = len(line) |
| 44 |
p = 0 |
| 45 |
do_yield = False |
| 46 |
while p < n: |
| 47 |
if state == 0: |
| 48 |
p = line.find('if', p) |
| 49 |
if p == -1: |
| 50 |
p = n |
| 51 |
continue |
| 52 |
if (p == 0 or not line[p-1].isalpha()) \ |
| 53 |
and (p+2 == len(line) or not line[p+2].isalpha()): |
| 54 |
do_yield = True |
| 55 |
state = 1 |
| 56 |
p += 2 |
| 57 |
elif state == 1: |
| 58 |
do_yield = True |
| 59 |
p = line.find('(', p) |
| 60 |
if p == -1: |
| 61 |
p = n |
| 62 |
else: |
| 63 |
p += 1 |
| 64 |
state = 2 |
| 65 |
pcnt = 1 |
| 66 |
elif state == 2: |
| 67 |
do_yield = True |
| 68 |
for p in range(p, n): |
| 69 |
if line[p] == '(': |
| 70 |
pcnt += 1 |
| 71 |
elif line[p] == ')': |
| 72 |
pcnt -= 1 |
| 73 |
if not pcnt: |
| 74 |
state = 0 |
| 75 |
break |
| 76 |
p += 1 |
| 77 |
if do_yield: |
| 78 |
yield nr |
| 79 |
|
| 80 |
def cond_lines_from_file(filename): |
| 81 |
with open(filename) as f: |
| 82 |
yield from cond_lines(skip_comments(f)) |
| 83 |
|
| 84 |
def filter_lcov_trace(lines): |
| 85 |
nrs = set() |
| 86 |
for line in lines: |
| 87 |
if line.startswith('SF:'): |
| 88 |
nrs = set(cond_lines_from_file(line[3:-1])) |
| 89 |
elif line.startswith('BRDA:'): |
| 90 |
xs = line[5:].split(',') |
| 91 |
nr = int(xs[0]) if xs else 0 |
| 92 |
if nr not in nrs: |
| 93 |
continue |
| 94 |
yield line |
| 95 |
|
| 96 |
def filter_lcov_trace_file(s_filename, d_file): |
| 97 |
|
| 98 |
|
| 99 |
with open(s_filename, encoding="utf-8", errors="backslashreplace") as f: |
| 100 |
for l in filter_lcov_trace(f): |
| 101 |
print(l, end='', file=d_file) |
| 102 |
|
| 103 |
if __name__ == '__main__': |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
filter_lcov_trace_file(sys.argv[1], sys.stdout) |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|