1 # Copyright (C) 2004, 2005, 2006 Nathaniel Smith
2 # Copyright (C) 2006, 2007 Holger Hans Peter Freyther
4 # Redistribution and use in source and binary forms, with or without
5 # modification, are permitted provided that the following conditions
8 # 1. Redistributions of source code must retain the above copyright
9 # notice, this list of conditions and the following disclaimer.
10 # 2. Redistributions in binary form must reproduce the above copyright
11 # notice, this list of conditions and the following disclaimer in the
12 # documentation and/or other materials provided with the distribution.
13 # 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
14 # its contributors may be used to endorse or promote products derived
15 # from this software without specific prior written permission.
17 # THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18 # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 # DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21 # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 def analyze_coverage(possible_gcov_files
, source_files
, runid
, data_dir
, base
):
35 if not os
.path
.exists(data_dir
):
38 output
= open(os
.path
.join(data_dir
, runid
+ ".csv"), "w")
39 w
= csv
.writer(output
)
40 # First row: id and time
41 w
.writerow([runid
, time
.time()])
43 results
= scan_gcov_files(possible_gcov_files
, source_files
)
44 annotated_dir
= os
.path
.join(data_dir
, runid
+ ".annotated")
45 if os
.path
.exists(annotated_dir
):
46 shutil
.rmtree(annotated_dir
)
51 (total
, covered
, annotated_data
) = results
[path
]
52 path
= path
[path
.find(base
)+len(base
):]
53 # Rest of the rows: filename, total_lines, covered_lines
54 w
.writerow([path
, total
, covered
])
58 annotated_path
= os
.path
.join(annotated_dir
, path
)
60 os
.makedirs(os
.path
.dirname(annotated_path
))
63 a
= open(annotated_path
, "w")
64 a
.write(annotated_data
)
73 def find_gcov(f
, possible_gcovs
):
75 Find .gcov files that could be of interest for us
78 return possible_gcovs
[f
]
83 def parse_source_file(file):
85 Parse one source file and return a list of lines
88 init_state
= STATE_NOT_SEEN
92 for line
in open(file, "r"):
93 code
= line
.split(":", 2)[-1]
94 if not in_test_code
and code
.startswith("#ifdef BUILD_UNIT_TESTS"):
96 if in_test_code
and code
.startswith("#if"):
98 if in_test_code
and code
.startswith("#endif"):
103 init_state
= STATE_TEST_CODE
105 init_state
= STATE_NOT_SEEN
106 f_source_list
.append([init_state
, line
.split(":", 1)[1]])
110 # Runner-up, 3rd annual "write Python that looks like Perl" competition,
111 # Well, not really. It doesn't even use regexps.
112 # He is right so I'm cleaning it up (zecke)
113 def scan_gcov_files(possible_gcov_files
, source_files
):
114 """Takes a list of gcov filenames and a list of source filenames.
116 The gcov files should have names of the form foo.o##foo.cc.gcov, as
117 created by 'gcov -l'.
119 Returns a dict mapping source filenames to tuples
120 (total_lines, tested_lines, gcov_annotated_source)
121 which are a number, a number, and a very long string, respectively.
123 The fun bit is that we merge .gcov output generated by different object
124 files; this way we can provide accurate information for header files and
125 for monotone's current unit test system."""
127 for f
in source_files
:
128 possible_gcovs
= find_gcov(f
, possible_gcov_files
)
129 base_name
= os
.path
.splitext(os
.path
.basename(f
))[0]
130 if len(possible_gcovs
) == 0:
131 print "No gcov files found for: '%s' but it was compiled" % f
134 (garbage
,extension
) = os
.path
.splitext(f
)
135 if extension
in [".cc", ".c", ".moc", ".cpp", ".cxx", ".m", ".mm"]:
136 lines
= open(f
, "r").readlines()
137 results
[f
] = (len(lines
), 0, "".join(lines
))
139 elif len(possible_gcovs
) > 1:
140 print "More than one gcov file for %s %d" % (f
,len(possible_gcovs
))
141 base_gcov_lines
= parse_source_file(possible_gcovs
[0])
143 # Now we will try hard to merge the results with others
144 # Our requirement is that we have the same amount of lines as
145 # as the original file
146 for cov_file
in possible_gcovs
:
147 lines
= open(cov_file
, "r").readlines()
149 # e.g. with phonon we have visualisation.h and we can not know
150 # which header file (foldername) it is refering to. This is a gcov
151 # limitation and i have no workaround yet. We just hope we will pick
152 # the right header file...
153 if len(lines
) != len(base_gcov_lines
):
154 print "Error Base: %s and Target: %s have different amount of lines" % (possible_gcovs
[0],cov_file
)
157 # now do the merging of the file. If it has the same basename
158 # and the same number of lines things might work out
159 # In the future take a look at the header of the file
162 accumulator
= base_gcov_lines
[i
]
163 if accumulator
[0] != STATE_TEST_CODE
:
164 info
= line
.split(":", 1)[0]
165 if info
.endswith("-"):
166 if accumulator
[0] == STATE_NOT_SEEN
:
167 accumulator
[0] = STATE_NOT_CODE
169 if info
.endswith("#"):
173 if accumulator
[0] in (STATE_NOT_SEEN
, STATE_NOT_CODE
):
175 accumulator
[0] += num
178 # post processing of ths file
179 (total_lines
, total_covered
) = (0, 0)
181 for state
, line
in base_gcov_lines
:
182 if state
== STATE_NOT_SEEN
:
184 elif state
== STATE_TEST_CODE
:
186 elif state
== STATE_NOT_CODE
:
195 annotated_lines
.append(":".join([desc
.rjust(9), line
]))
196 results
[f
] = (total_lines
, total_covered
, "".join(annotated_lines
))