1 : // Copyright 2012 Google Inc. All Rights Reserved.
2 : //
3 : // Licensed under the Apache License, Version 2.0 (the "License");
4 : // you may not use this file except in compliance with the License.
5 : // You may obtain a copy of the License at
6 : //
7 : // http://www.apache.org/licenses/LICENSE-2.0
8 : //
9 : // Unless required by applicable law or agreed to in writing, software
10 : // distributed under the License is distributed on an "AS IS" BASIS,
11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 : // See the License for the specific language governing permissions and
13 : // limitations under the License.
14 :
15 : #include "syzygy/grinder/lcov_writer.h"
16 :
17 : #include "base/files/file_util.h"
18 :
19 : namespace grinder {
20 :
21 : bool WriteLcovCoverageFile(const CoverageData& coverage,
22 E : const base::FilePath& path) {
23 E : base::ScopedFILE file(base::OpenFile(path, "wb"));
24 E : if (file.get() == NULL) {
25 i : LOG(ERROR) << "Failed to open file for writing: " << path.value();
26 i : return false;
27 : }
28 :
29 E : if (!WriteLcovCoverageFile(coverage, file.get())) {
30 i : LOG(ERROR) << "Failed to write LCOV file: " << path.value();
31 i : return false;
32 : }
33 :
34 E : return true;
35 E : }
36 :
37 E : bool WriteLcovCoverageFile(const CoverageData& coverage, FILE* file) {
38 E : DCHECK(file != NULL);
39 :
40 : CoverageData::SourceFileCoverageDataMap::const_iterator source_it =
41 E : coverage.source_file_coverage_data_map().begin();
42 : CoverageData::SourceFileCoverageDataMap::const_iterator source_it_end =
43 E : coverage.source_file_coverage_data_map().end();
44 E : for (; source_it != source_it_end; ++source_it) {
45 E : if (::fprintf(file, "SF:%s\n", source_it->first.c_str()) < 0)
46 i : return false;
47 :
48 : // Iterate over the line execution data, keeping summary statistics as we
49 : // go.
50 E : size_t lines_executed = 0;
51 : CoverageData::LineExecutionCountMap::const_iterator line_it =
52 E : source_it->second.line_execution_count_map.begin();
53 : CoverageData::LineExecutionCountMap::const_iterator line_it_end =
54 E : source_it->second.line_execution_count_map.end();
55 E : for (; line_it != line_it_end; ++line_it) {
56 E : if (::fprintf(file, "DA:%d,%d\n", line_it->first, line_it->second) < 0)
57 i : return false;
58 E : if (line_it->second > 0)
59 E : ++lines_executed;
60 E : }
61 :
62 : // Output the summary statistics for this file.
63 : if (::fprintf(file, "LH:%d\n", lines_executed) < 0 ||
64 : ::fprintf(file, "LF:%d\n",
65 : source_it->second.line_execution_count_map.size()) < 0 ||
66 E : ::fprintf(file, "end_of_record\n") < 0) {
67 i : return false;
68 : }
69 E : }
70 :
71 E : return true;
72 E : }
73 :
74 : } // namespace grinder
|