• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python
2#
3# Merge gcov graph from several test cases. This can be used to check
4# the coverage of several test cases.
5#
6# Copyright (C) 2008, Intel Corp.
7#   Author: Huang Ying <ying.huang@intel.com>
8#
9# This file is released under the GPLv2.
10
11import sys
12
13def die(str):
14    print str
15    sys.exit(-1)
16
17def die_on(cond, str):
18    if cond:
19        die(str)
20
21class GCovLine(object):
22    def __init__(self, l):
23        object.__init__(self)
24        self.parse(l)
25    def parse(self, l):
26        sep = l.find(':')
27        die_on(sep <= 0, 'input error format error')
28        remain = l[sep:]
29        scnt = l[:sep]
30        if scnt[-1] == '-':
31            count = -1
32        elif scnt[-1] == '#':
33            count = 0
34        else:
35            count = int(scnt)
36        self.sep = sep
37        self.count = count
38        self.remain = remain
39    def merge(self, gcl):
40        self.count = self.count + gcl.count
41    def write(self, of):
42        if self.count < 0:
43            of.write("%*s" % (self.sep, '-'))
44        elif self.count == 0:
45            of.write("%*s" % (self.sep, '#####'))
46        else:
47            of.write("%*d" % (self.sep, self.count))
48        of.write(self.remain)
49
50def parse(f):
51    return [GCovLine(l) for l in f]
52
53def merge(gcls1, gcls2):
54    for gcl1, gcl2 in zip(gcls1, gcls2):
55        gcl1.merge(gcl2)
56
57def gcov_merge(fns, of):
58    f = file(fns[0])
59    gcls_base = parse(f)
60
61    for fn in fns[1:]:
62        f = file(fn)
63        gcls = parse(f)
64        merge(gcls_base, gcls)
65
66    for gcl in gcls_base:
67        gcl.write(of)
68
69def usage():
70    print 'Usage: %s <gcov graph files>' % (sys.argv[0])
71
72if __name__ == '__main__':
73    if len(sys.argv) <= 1:
74        usage()
75        exit -1
76    gcov_merge(sys.argv[1:], sys.stdout)
77