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