]> git.donarmstrong.com Git - lilypond.git/blob - buildscripts/coverage.py
thinkwo.
[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, coverage_count, all_lines, file):
42         assert coverage_count >= 0
43         assert type (range) == type (())
44         
45         self.coverage_count = coverage_count
46         self.range = range
47         self.all_lines = all_lines
48         self.file = file
49
50     def length (self):
51         return self.range[1] - self.range[0]
52
53     def text (self):
54         return ''.join ([l[2] for l in self.lines()])
55         
56     def lines (self):
57         return self.all_lines[self.range[0]:
58                               self.range[1]]
59     def widen (self):
60         self.range = (min (self.range[0] -1, 0),
61                       self.range[0] +1)
62     def write (self):
63         print 'chunk in', self.file
64         for (c, n, l) in self.lines ():
65             cov = '%d' % c
66             if c == 0:
67                 cov = '#######'
68             elif c < 0:
69                 cov = ''
70             sys.stdout.write ('%8s:%8d:%s' % (cov, n, l))
71             
72     def uncovered_score (self):
73         return self.length ()
74     
75 class SchemeChunk (Chunk):
76     def uncovered_score (self):
77         text = self.text ()
78         if (text.startswith  ('(define')
79             and not text.startswith ('(define (')):
80             return 0
81
82         return len ([l for (c,n,l) in self.lines() if (c == 0)]) 
83
84 def read_gcov (f):
85     ls = []
86
87     in_lines = [l for l in open (f).readlines ()]
88     (count_len, line_num_len) = tuple (map (len, in_lines[0].split (':')[:2]))
89     
90     for l in in_lines:
91         c = l[:count_len].strip ()
92         l = l[count_len+1:]
93         n = int (l[:line_num_len].strip ())
94
95         if n == 0:
96             continue
97
98         if '#' in c:
99             c = 0
100         elif c == '-':
101             c = -1
102         else:
103             c = int (c)
104         
105         l = l[line_num_len+1:]
106
107         ls.append ((c,n,l))
108         
109     return ls
110
111 def get_c_chunks (ls, file):
112     chunks = []
113     chunk = []
114
115     last_c = -1
116     for (c, n, l) in ls:
117         if not (c == last_c or c < 0 and l != '}\n'):
118             if chunk and last_c >= 0:
119                 nums = [n-1 for (n, l) in chunk]
120                 chunks.append (Chunk ((min (nums), max (nums)+1),
121                                       last_c, ls, file))
122                 chunk = []
123
124         chunk.append ((n,l))
125         if c >= 0:
126             last_c = c
127             
128     return chunks
129
130 def get_scm_chunks (ls, file):
131     chunks = []
132     chunk = []
133
134     def new_chunk ():
135         if chunk:
136             nums = [n-1 for (n, l) in chunk]
137             chunks.append (SchemeChunk ((min (nums), max (nums)+1),
138                                         max (last_c, 0), ls, file))
139             chunk[:] = []
140         
141     last_c = -1
142     for (cov_count, line_number, line) in ls:
143         if line.startswith ('(define'):
144             new_chunk ()
145             last_c = -1
146         
147         chunk.append ((line_number, line))
148         if cov_count >= 0:
149             last_c = cov_count
150
151     return chunks
152
153 def widen_chunk (ch, ls):
154     a -= 1
155     b += 1
156
157     return [(n, l)  for (c, n, l) in ls[a:b]]
158     
159
160 def extract_chunks (file):
161     try:
162         ls = read_gcov (file)
163     except IOError, s :
164         print s
165         return []
166         
167     cs = []
168     if 'scm' in file:
169         cs = get_scm_chunks (ls, file)
170     else:
171         cs = get_c_chunks (ls, file)
172     return cs
173
174
175 def filter_uncovered (chunks):
176     def interesting (c):
177         if c.coverage_count > 0:
178             return False
179         
180         t = c.text()
181         for stat in  ('warning', 'error', 'print', 'scm_gc_mark'):
182             if stat in t:
183                 return False
184         return True
185    
186     return [c for c in chunks if interesting (c)]
187     
188
189 def main ():
190     p = optparse.OptionParser (usage="usage coverage.py [options] files",
191                                description="")
192     p.add_option ("--summary",
193                   action='store_true',
194                   default=False,
195                   dest="summary")
196     
197     p.add_option ("--hotspots",
198                   default=False,
199                   action='store_true',
200                   dest="hotspots")
201     
202     p.add_option ("--uncovered",
203                   default=False,
204                   action='store_true',
205                   dest="uncovered")
206
207     
208     (options, args) = p.parse_args ()
209     
210
211     if options.summary:
212         summary (['%s.gcov-summary' % s for s in args])
213
214     if options.uncovered or options.hotspots:
215         chunks = []
216         for a in args:
217             name = a
218             if name.endswith ('scm'):
219                 name += '.cov'
220             else:
221                 name += '.gcov'
222             
223             chunks += extract_chunks  (name)
224
225         if options.uncovered:
226             chunks = filter_uncovered (chunks)
227             chunks = [(c.uncovered_score (), c) for c in chunks]
228         elif options.hotspots:
229             chunks = [((c.coverage_count, -c.length()), c) for c in chunks]
230             
231             
232         chunks.sort ()
233         chunks.reverse ()
234         for (score, c) in chunks:
235             c.write ()
236
237             
238         
239 if __name__ == '__main__':
240     main ()