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