]> git.donarmstrong.com Git - lilypond.git/blob - buildscripts/coverage.py
Merge branch 'master' of git://git.sv.gnu.org/lilypond
[lilypond.git] / buildscripts / coverage.py
1 #!/bin/sh
2 import os
3 import glob
4 import re
5 import sys
6 import optparse
7
8 #File 'accidental-engraver.cc'
9 #Lines executed:87.70% of 252
10
11 def summary (args):
12     results = []
13     for f in args:
14         str = open (f).read ()
15         m = re.search ("File '([^']+.cc)'\s*Lines executed:([0-9.]+)% of ([0-9]+)", str)
16
17         if m and '/usr/lib' in m.group (1):
18             continue
19
20         if m:
21             cov = float (m.group (2))
22             lines = int (m.group (3))
23             pain = lines * (100.0 - cov)
24             file = m.group (1)
25             tup = (pain, locals ().copy())
26
27             results.append(tup)
28
29     results.sort ()
30     results.reverse()
31
32     print 'files sorted by number of untested lines (decreasing)'
33     print
34     print '%5s (%6s): %s' % ('cov %', 'lines', 'file')
35     print '----------------------------------------------'
36
37     for (pain, d) in results:
38         print '%(cov)5.2f (%(lines)6d): %(file)s' % d
39
40 class Chunk:
41     def __init__ (self, range, all_lines, file):
42         self.range = range
43         self.all_lines = all_lines
44         self.file = file
45
46     def length (self):
47         return self.range[1] - self.range[0]
48
49     def text (self):
50         return ''.join ([l[2] for l in self.lines()])
51         
52     def lines (self):
53         return self.all_lines[self.range[0]:
54                               self.range[1]]
55     def widen (self):
56         self.range = (min (self.range[0] -1, 0),
57                       self.range[0] +1)
58     def write (self):
59         print 'uncovered chunk in', self.file
60         for (c, n, l) in self.lines ():
61             sys.stdout.write ('%8s:%8d:%s' % (c,n,l))
62             
63 def read_gcov (f):
64     ls = []
65
66     in_lines = [l for l in open (f).readlines ()]
67     (count_len, line_num_len) = tuple (map (len, in_lines[0].split( ':')[:2]))
68     
69     for l in in_lines:
70         c = l[:count_len].strip ()
71         l = l[count_len+1:]
72         n = int (l[:line_num_len].strip ())
73
74         if n == 0:
75             continue
76         
77         l = l[line_num_len+1:]
78
79         ls.append ((c,n,l))
80         
81     return ls
82
83 def get_chunks (ls, file):
84     chunks = []
85     chunk = []
86     for (c,n,l) in ls:
87         if '#' in c:
88             chunk.append ((n,l))
89         elif c.strip () != '-' or l == '}\n':
90             if chunk:
91                 nums = [n-1 for (n, l) in chunk]
92                 chunks.append (Chunk ((min (nums), max (nums)+1),
93                                       ls, file))
94                 chunk = []
95             
96     return chunks
97
98
99 def widen_chunk (ch, ls):
100     a -= 1
101     b += 1
102
103     return [(n, l)  for (c, n, l) in ls[a:b]]
104     
105
106 def extract_uncovered (file):
107     try:
108         ls = read_gcov (file)
109     except IOError, s :
110         print s
111         return []
112         
113     cs = get_chunks (ls, file)
114     def interesting (c):
115         t = c.text()
116         for stat in  ('warning', 'error', 'print', 'scm_gc_mark'):
117             if stat in t:
118                 return False
119         return True
120    
121     return [c for c in cs if interesting (c)]
122     
123
124 def main ():
125     p = optparse.OptionParser (usage="usage coverage.py [options] files",
126                                description="")
127     p.add_option ("--summary",
128                   action='store_true',
129                   default=False,
130                   dest="summary")
131     
132     p.add_option ("--uncovered",
133                   default=False,
134                   action='store_true',
135                   dest="uncovered")
136
137     
138     (options, args) = p.parse_args ()
139     
140
141     if options.summary:
142         summary (['%s.gcov-summary' % s for s in args])
143
144     if options.uncovered:
145         uncovered = []
146         for a in args:
147             uncovered += extract_uncovered ('%s.gcov' % a)
148             
149         uncovered = [(c.length (), c) for c in uncovered]
150         uncovered.sort ()
151         uncovered.reverse ()
152         for (score, c) in uncovered:
153             c.write ()
154
155             
156         
157 if __name__ == '__main__':
158     main ()