-
Notifications
You must be signed in to change notification settings - Fork 242
/
Copy pathllvm2lcov
executable file
·344 lines (302 loc) · 13.5 KB
/
llvm2lcov
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
#!/usr/bin/env perl
#
# Copyright (c) MediaTek USA Inc., 2024
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or (at
# your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, see
# <http://www.gnu.org/licenses/>.
#
#
# This script traverses C/C++ coverage data in JSON format, generated by
# 'llvm-cov export -format=text ....'
# Coverage data is in one or more JSON data files (generated by llvm-cov) and
# translates it into LCOV .info format.
#
# $ clang[++] -o myExe -fprofile-inst-generate -fcoverage-mapping \
# [-fcoverage-mcdc] ....
# $ ./myExe ...
# $ llvm-profdata merge -o myExe.profdata --sparse *.profraw
# $ llvm-cov export -format=text -instr-profile=myExe.profdata \
# ./myExe > myExe.json
# $ llvm2lcov [--output myExe.info] [--test-name name] [options] myExe.json
#
# In order to generate MC/DC data, note that you must:
# - use LLVM/18 or newer
# - enable MC/DC instrumentation in your compile/link steps, and
# - pass the '--mcdc-coverage' flag to llvm2lcov
#
# See 'llvm2lcov --help' for more usage information
#
# See the LLVM documentation for more information on flags and compilation options.
use strict;
use warnings;
require Exporter;
use File::Basename qw(basename dirname fileparse);
use File::Spec::Functions qw /abs2rel catdir file_name_is_absolute splitdir
splitpath catpath catfile/;
use File::Temp;
use File::Copy qw(copy move);
use File::Path;
use Cwd qw/abs_path getcwd realpath/;
use Time::HiRes; # for profiling
use Capture::Tiny;
use FindBin;
use Storable;
use POSIX;
use lib "$FindBin::RealBin/../lib";
use lcovutil;
sub print_usage(*)
{
local *HANDLE = shift;
print(HANDLE <<EOF);
Usage: $lcovutil::tool_name [OPTIONS] json_file [json_file ...]
Translate LLVM\'s 'llvm-cov' JSON coverage data file to LCOV .info
file format.
In addition to common options supported by other tools in the LCOV
suite (e.g., --comment, --version-script, --ignore-error, --substitute,
--exclude, etc.), the tool options are:
--output filename:
The lcov data will be written to the specified file - or to
the file called 'llvm2lcov.info' in the current run directory
if this option is not used.
--testname name:
Coverage info will be associated with the testcase name provided.
It is not necessary to provide a name.
--branch-coverage:
Include branch coverage data in the output.
--mcdc-coverage:
Include MC/DC data in the output.
Note that you must be using LLVM/18 or higher, and must have instrumented
your code for MC/DC collection. See the LLVM documentation for details.
See LLVM documentation for directions on how to generate coverage data
in JSON format.
For example:
# compile your example, instrumenting for MC/DC coverage
\$ clang++ -o myExe -fprofile-inst-generate -fcoverage-mapping \\
-fcoverage-mcdc myCode.cpp
# run your testcases
\$ ./myExe ...
# convert profile data
\$ llvm-profdata merge -o myExe.profdata --sparse *.profraw
# export coverage data in JSON format
\$ llvm-cov export -format=text -instr-profile=myExe.profdata \\
./myExe > myExe.json
# use this script to convert to LCOV format
\$ $lcovutil::tool_name --output myExe.info --test-name myTestcase \\
--mcdc-coverage --branch-coverage myExe.json
# and generate a genhtml-format coverage report:
\$ genhtml -o html_report myExe.info ...
EOF
}
sub parse
{
my $testname = shift;
die('JSON file argument required') unless @_;
my $top = TraceFile->new();
$testname = '' unless defined($testname);
my $srcReader = ReadCurrentSource->new();
foreach my $jsonFile (@_) {
die("no such JSON file '$jsonFile'") unless -e $jsonFile;
my $json = JsonSupport::load($jsonFile);
die("unrecognized JSON file format in $jsonFile")
unless (defined($json) &&
exists($json->{data}) &&
'ARRAY' eq ref($json->{data}));
lcovutil::info("read $jsonFile\n");
foreach my $k (@{$json->{data}}) {
#lcovutil::info("starting data entry..\n");
foreach my $f (@{$k->{files}}) {
lcovutil::info('parsing ' . $f->{filename} . " ..\n");
my $filename =
ReadCurrentSource::resolve_path($f->{filename}, 1);
if (TraceFile::skipCurrentFile($filename)) {
if (!exists($lcovutil::excluded_files{$filename})) {
$lcovutil::excluded_files{$filename} = 1;
lcovutil::info("Excluding $filename\n");
}
next;
}
$srcReader->open($filename);
my $fileInfo = $top->data($filename);
my $version = lcovutil::extractFileVersion($filename);
$fileInfo->version($version)
if (defined($version) && $version ne "");
my $lineData = $fileInfo->test($testname);
# use branch data to derive MC/DC expression - so need
# it, even if user didn't ask
my $branchData = $fileInfo->testbr($testname)
if $lcovutil::br_coverage || $lcovutil::mcdc_coverage;
my $mcdcData = $fileInfo->testcase_mcdc($testname)
if $lcovutil::mcdc_coverage;
my $summary = $f->{summary};
my $branches = $f->{branches};
my $segments = $f->{segments};
my $expansions = $f->{expansions};
my $mcdc = $f->{mcdc_records}
if $lcovutil::mcdc_coverage && exists($f->{mcdc_records});
foreach my $s (@$segments) {
die("unexpected segment data") unless scalar(@$s) == 6;
my ($line, $col, $count, $hasCount, $isRegion, $isGap) =
@$s;
next unless $hasCount;
$lineData->append($line, $count);
}
if ($branchData) {
my $currentLine = -1;
my $branchIdx;
foreach my $branch (@$branches) {
die("unexpected branch data")
unless scalar(@$branch) == 9;
my ($line, $startCol, $endline,
$endcol, $trueCount, $falseCount,
$fileId, $expandedId, $kind) = @$branch;
if ($line != $currentLine &&
defined($lineData->value($line))) {
$branchIdx = 0; # restart counter
$currentLine = $line;
} else {
# this branch is part of the current group
++$branchIdx;
}
my $expr =
$srcReader->getExpr($line, $startCol, $endline,
$endcol)
if $srcReader->notEmpty();
my $br =
BranchBlock->new($branchIdx, $trueCount, $expr);
$branchData->append($line, 0, $br, $filename);
}
}
if ($mcdc) {
foreach my $m (@$mcdc) {
# what are fileID and kind?
die("unexpected MC/DC data") unless scalar(@$m) == 7;
my ($line, $startCol, $endLine, $endcol, $fileId,
$kind, $cov)
= @$m;
die("unexpected MC/DC cov")
unless 'ARRAY' eq ref($cov);
my $groupSize = scalar(@$cov);
# read the source line and extract the expression...
my $expr =
$srcReader->getExpr($line, $startCol, $endLine,
$endcol)
if ($srcReader->notEmpty());
my $current_mcdc =
$mcdcData->new_mcdc($mcdcData, $line);
my $branch = $branchData->value($line);
my $idx = 0;
foreach my $c (@$cov) {
my $branchExpr =
$branch->getBlock(0)->[$idx]->expr()
if $branch &&
(scalar(@{$branch->getBlock(0)}) > $idx);
$branchExpr =
defined($branchExpr) ?
"'$branchExpr' in '$expr'" :
$idx;
$current_mcdc->insertExpr($filename, $groupSize, 0,
$c, $idx, $branchExpr);
$current_mcdc->insertExpr($filename, $groupSize, 1,
$c, $idx, $branchExpr);
++$idx;
}
$mcdcData->close_mcdcBlock($current_mcdc);
}
} # MCDC
$fileInfo->testbr()->remove($testname)
if $lcovutil::mcdc_coverage && !$lcovutil::br_coverage;
lcovutil::info(2, "finished parsing $filename\n");
}
next unless $lcovutil::func_coverage;
foreach my $f (@{$k->{functions}}) {
my $name = $f->{name};
my $filenames = $f->{filenames}; # array
if ($#$filenames != 0) {
lcovutil::ignorable_error($lcovutil::ERROR_USAGE,
"unsupported: function $name associated with multiple files"
);
next;
}
my $filename =
ReadCurrentSource::resolve_path($filenames->[0], 1);
if (TraceFile::skipCurrentFile($filename)) {
if (!exists($lcovutil::excluded_files{$filename})) {
$lcovutil::excluded_files{$filename} = 1;
lcovutil::info("Excluding $filename\n");
}
next;
}
die('unexpected unknown file \'' . $filenames->[0] . '\'')
unless $top->file_exists($filename);
my $info = $top->data($filename);
my $count = $f->{count};
my $regions = $f->{regions}; # startline/col, endline/col/
my $functionMap = $info->testfnc($testname);
my $startLine = $regions->[0]->[0]; # startline of first region
# NOTE: might be a mistake to grab the end line of the last region -
# LCOV follows GCC behaviour and associates lines with where they
# start - not where they end...
my $endline = $regions->[-1]->[2]; # endline of last region
my $func =
$functionMap->define_function($name, $startLine, $endline)
unless defined($functionMap->findName($name));
$functionMap->add_count($name, $count);
# for the moment - don't worry about the coverpoints in the function
#my $branches = $f->{branches};
#my $mcdc = $f->{mcdc_records} if exists($f->{mcdc_records});
#foreach my $r (@$regions) {
# my ($startLine, $startCol, $endLine, $endCol, $count, $fileId,
# $expandedId, $kind) = @$r;
#}
}
}
lcovutil::info(2, "finished $jsonFile\n");
}
# now create the merge summary...
foreach my $filename ($top->files()) {
my $info = $top->data($filename);
my @work;
push(@work, [$info->sum(), $info->test]);
push(@work, [$info->sumbr(), $info->testbr])
if $lcovutil::br_coverage;
push(@work, [$info->func(), $info->testfnc])
if $lcovutil::func_coverage;
push(@work, [$info->mcdc(), $info->testcase_mcdc()])
if $lcovutil::mcdc_coverage;
foreach my $d (@work) {
my ($sum, $pertest) = @$d;
$sum->union($pertest->value($testname));
}
}
return $top;
}
my $output_filename = 'llvm2lcov.info';
my $testname;
my %opts = ('test-name|t=s' => \$testname,
'output-filename|o=s' => \$output_filename,);
my %rc_opts;
if (!lcovutil::parseOptions(\%rc_opts, \%opts, \$output_filename)) {
print(STDERR "argparse failed");
}
my $info = parse($testname, @ARGV);
$info->applyFilters(ReadCurrentSource->new());
$info->write_info_file($output_filename);
$info->print_summary() if $lcovutil::verbose >= 0;
my $exit_code = 0;
$info->checkCoverageCriteria();
CoverageCriteria::summarize();
$exit_code = 1 if $CoverageCriteria::coverageCriteriaStatus;
lcovutil::summarize_messages();
lcovutil::cleanup_callbacks();
exit $exit_code;