]> 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 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_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 widen_chunk (ch, ls):
119     a -= 1
120     b += 1
121
122     return [(n, l)  for (c, n, l) in ls[a:b]]
123     
124
125 def extract_chunks (file):
126     try:
127         ls = read_gcov (file)
128     except IOError, s :
129         print s
130         return []
131         
132     return get_chunks (ls, file)
133
134 def filter_uncovered (chunks):
135     def interesting (c):
136         if c.coverage_count > 0:
137             return False
138         
139         t = c.text()
140         for stat in  ('warning', 'error', 'print', 'scm_gc_mark'):
141             if stat in t:
142                 return False
143         return True
144    
145     return [c for c in chunks if interesting (c)]
146     
147
148 def main ():
149     p = optparse.OptionParser (usage="usage coverage.py [options] files",
150                                description="")
151     p.add_option ("--summary",
152                   action='store_true',
153                   default=False,
154                   dest="summary")
155     
156     p.add_option ("--hotspots",
157                   default=False,
158                   action='store_true',
159                   dest="hotspots")
160     
161     p.add_option ("--uncovered",
162                   default=False,
163                   action='store_true',
164                   dest="uncovered")
165
166     
167     (options, args) = p.parse_args ()
168     
169
170     if options.summary:
171         summary (['%s.gcov-summary' % s for s in args])
172
173     if options.uncovered or options.hotspots:
174         chunks = []
175         for a in args:
176             chunks += extract_chunks  ('%s.gcov' % a)
177
178         if options.uncovered:
179             chunks = filter_uncovered (chunks)
180             chunks = [(c.length (), c) for c in chunks]
181         elif options.hotspots:
182             chunks = [((c.coverage_count, -c.length()), c) for c in chunks]
183             
184             
185         chunks.sort ()
186         chunks.reverse ()
187         for (score, c) in chunks:
188             c.write ()
189
190             
191         
192 if __name__ == '__main__':
193     main ()