Debian Codegraph @

bin/llvm2lcov

raw
1 #!/usr/bin/env perl
2 #
3 # Copyright (c) MediaTek USA Inc., 2024
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or (at
8 # your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, see
17 # <http://www.gnu.org/licenses/>.
18 #
19 #
20 # This script traverses C/C++ coverage data in JSON format, generated by
21 # 'llvm-cov export -format=text ....'
22 # Coverage data is in one or more JSON data files (generated by llvm-cov) and
23 # translates it into LCOV .info format.
24 #
25 # $ clang[++] -o myExe -fprofile-inst-generate -fcoverage-mapping \
26 # [-fcoverage-mcdc] ....
27 # $ ./myExe ...
28 # $ llvm-profdata merge -o myExe.profdata --sparse *.profraw
29 # $ llvm-cov export -format=text -instr-profile=myExe.profdata \
30 # ./myExe > myExe.json
31 # $ llvm2lcov [--output myExe.info] [--test-name name] [options] myExe.json
32 #
33 # In order to generate MC/DC data, note that you must:
34 # - use LLVM/18 or newer
35 # - enable MC/DC instrumentation in your compile/link steps, and
36 # - pass the '--mcdc-coverage' flag to llvm2lcov
37 #
38 # You can also use LLVM/21 or newer to generate MC/DC data more cleanly.
39 #
40 # See 'llvm2lcov --help' for more usage information
41 #
42 # See the LLVM documentation for more information on flags and compilation options.
43
44 use strict;
45 use version;
46 use warnings;
47 require Exporter;
48
49 use File::Basename qw(basename dirname fileparse);
50 use File::Spec::Functions qw /abs2rel catdir file_name_is_absolute splitdir
51 splitpath catpath catfile/;
52 use File::Temp;
53 use File::Copy qw(copy move);
54 use File::Path;
55 use Cwd qw/abs_path getcwd realpath/;
56 use Time::HiRes; # for profiling
57 use Capture::Tiny;
58 use FindBin;
59 use Storable;
60 use POSIX;
61
62 use lib "$FindBin::RealBin/../lib";
63 use lcovutil;
64
65 sub print_usage(*)
66 {
67 local *HANDLE = shift;
68 print(HANDLE <<EOF);
69 Usage: $lcovutil::tool_name [OPTIONS] json_file [json_file ...]
70 Translate LLVM\'s 'llvm-cov' JSON coverage data file to LCOV .info
71 file format.
72
73 In addition to common options supported by other tools in the LCOV
74 suite (e.g., --comment, --version-script, --ignore-error, --substitute,
75 --exclude, etc.), the tool options are:
76
77 --output filename:
78 The lcov data will be written to the specified file - or to
79 the file called 'llvm2lcov.info' in the current run directory
80 if this option is not used.
81
82 --testname name:
83 Coverage info will be associated with the testcase name provided.
84 It is not necessary to provide a name.
85
86 --branch-coverage:
87 Include branch coverage data in the output.
88
89 --mcdc-coverage:
90 Include MC/DC data in the output.
91 Note that you must be using LLVM/18 or higher, and must have instrumented
92 your code for MC/DC collection. See the LLVM documentation for details.
93
94 See LLVM documentation for directions on how to generate coverage data
95 in JSON format.
96
97 For example:
98
99 # compile your example, instrumenting for MC/DC coverage
100 \$ clang++ -o myExe -fprofile-inst-generate -fcoverage-mapping \\
101 -fcoverage-mcdc myCode.cpp
102 # run your testcases
103 \$ ./myExe ...
104 # convert profile data
105 \$ llvm-profdata merge -o myExe.profdata --sparse *.profraw
106 # export coverage data in JSON format
107 \$ llvm-cov export -format=text -instr-profile=myExe.profdata \\
108 ./myExe > myExe.json
109 # use this script to convert to LCOV format
110 \$ $lcovutil::tool_name --output myExe.info --test-name myTestcase \\
111 --mcdc-coverage --branch-coverage myExe.json
112 # and generate a genhtml-format coverage report:
113 \$ genhtml -o html_report myExe.info ...
114
115 EOF
116 }
117
118 sub parse
119 {
120 my $testname = shift;
121
122 die('JSON file argument required') unless @_;
123
124 my $top = TraceFile->new();
125
126 $testname = '' unless defined($testname);
127
128 my $srcReader = ReadCurrentSource->new();
129
130 foreach my $jsonFile (@_) {
131 die("no such JSON file '$jsonFile'") unless -e $jsonFile;
132 my $json = JsonSupport::load($jsonFile);
133 die("unrecognized JSON file format in $jsonFile")
134 unless (defined($json) &&
135 exists($json->{data}) &&
136 'ARRAY' eq ref($json->{data}));
137 my $json_version = version->parse($json->{version});
138
139 lcovutil::info("read $jsonFile\n");
140
141 foreach my $k (@{$json->{data}}) {
142 #lcovutil::info("starting data entry..\n");
143 foreach my $f (@{$k->{files}}) {
144 lcovutil::info('parsing ' . $f->{filename} . " ..\n");
145 my $filename =
146 ReadCurrentSource::resolve_path($f->{filename}, 1);
147 if (TraceFile::skipCurrentFile($filename)) {
148 if (!exists($lcovutil::excluded_files{$filename})) {
149 $lcovutil::excluded_files{$filename} = 1;
150 lcovutil::info("Excluding $filename\n");
151 }
152 next;
153 }
154 $srcReader->open($filename);
155
156 my $fileInfo = $top->data($filename);
157
158 my $version = lcovutil::extractFileVersion($filename);
159 $fileInfo->version($version)
160 if (defined($version) && $version ne "");
161
162 my $lineData = $fileInfo->test($testname);
163
164 my $summary = $f->{summary};
165 my $branches = $f->{branches};
166 my $segments = $f->{segments};
167 my $expansions = $f->{expansions};
168 my $mcdc = $f->{mcdc_records}
169 if $lcovutil::mcdc_coverage && exists($f->{mcdc_records});
170
171 my $index = 0;
172 my $currentLine = 0;
173
174 while ($index < $#$segments) {
175 my $segment = $segments->[$index];
176 die("unexpected segment data")
177 unless scalar(@$segment) == 6;
178 my ($line, $col, $count, $hasCount, $isRegionEntry, $isGap)
179 = @$segment;
180 $currentLine = $line if !$currentLine;
181 if ($hasCount) {
182 $segment = $segments->[$index + 1];
183 die("unexpected segment data")
184 unless scalar(@$segment) == 6;
185 my ($next_line, $next_col, $next_count, $next_hasCount,
186 $next_isRegionEntry, $next_isGap)
187 = @$segment;
188 if ($currentLine == $next_line && !$next_isRegionEntry)
189 {
190 while ($next_line == $currentLine &&
191 ++$index < $#$segments) {
192 $segment = $segments->[$index + 1];
193 die("unexpected segment data")
194 unless scalar(@$segment) == 6;
195 $next_line = $segment->[0];
196 $count = $next_count
197 if ($count &&
198 $next_count > $count &&
199 $currentLine == $next_line);
200 $next_count = $segment->[2];
201 }
202 $lineData->append($currentLine, $count);
203 ++$currentLine;
204 } else {
205 my $bound = $next_line;
206 my $i = $index;
207 while (!$next_isRegionEntry &&
208 $next_line == $bound &&
209 ++$i < $#$segments) {
210 $segment = $segments->[$i + 1];
211 die("unexpected segment data")
212 unless scalar(@$segment) == 6;
213 $next_line = $segment->[0];
214 $next_isRegionEntry = $segment->[4];
215 }
216 --$bound
217 if ($next_isRegionEntry &&
218 $next_line == $bound &&
219 !($isRegionEntry && $line == $next_line));
220 $count = $next_count
221 if $next_count > $count && $line == $next_line;
222 while ($currentLine <= $bound) {
223 $lineData->append($currentLine, $count);
224 ++$currentLine;
225 }
226 ++$index;
227 }
228 } else {
229 do {
230 ++$index;
231 $segment = $segments->[$index];
232 die("unexpected segment data")
233 unless scalar(@$segment) == 6;
234 ($line, $col, $count, $hasCount,
235 $isRegionEntry, $isGap) = @$segment;
236 } while (!$hasCount && $index < $#$segments);
237 $currentLine = $isRegionEntry ? $line : $line + 1;
238 }
239 }
240 if ($mcdc && $json_version < version->parse("3.0.1")) {
241 my $mcdcData = $fileInfo->testcase_mcdc($testname);
242 my @mcdcBranches; # array (start line, start column, expression)
243 foreach my $branch (@$branches) {
244 die("unexpected branch data")
245 unless scalar(@$branch) == 9;
246 # Consider only branches of "MCDCBranchRegion" kind.
247 next if ($branch->[-1] != 6);
248 my ($line, $startCol, $endline,
249 $endcol, $trueCount, $falseCount,
250 $fileId, $expandedId, $kind) = @$branch;
251 my $expr =
252 $srcReader->getExpr($line, $startCol, $endline,
253 $endcol)
254 if $srcReader->notEmpty();
255 push(@mcdcBranches, [$line, $startCol, $expr]);
256 }
257 foreach my $m (@$mcdc) {
258 die("unexpected MC/DC data") unless scalar(@$m) == 7;
259 my ($line, $startCol, $endLine, $endCol, $expandedId,
260 $kind, $cov)
261 = @$m;
262 die("unexpected MC/DC cov")
263 unless 'ARRAY' eq ref($cov);
264
265 # read the source line and extract the expression...
266 my $expr =
267 $srcReader->getExpr($line, $startCol, $endLine,
268 $endCol)
269 if ($srcReader->notEmpty());
270 my @brExprs;
271 foreach my $branch (@mcdcBranches) {
272 my ($brLine, $brCol, $brExpr) = @$branch;
273 if (($brLine > $line ||
274 ($brLine == $line && $brCol >= $startCol))
275 &&
276 ($brLine < $endLine ||
277 ($brLine == $endLine && $brCol <= $endCol))
278 ) {
279 push(@brExprs, [$brLine, $brCol, $brExpr]);
280 }
281 }
282 @brExprs =
283 sort { $a->[0] <=> $b->[0] || $a->[1] <=> $b->[1] }
284 @brExprs;
285 my $current_mcdc =
286 $mcdcData->new_mcdc($mcdcData, $line);
287 my $groupSize = scalar(@$cov);
288 my $idx = 0;
289 foreach my $c (@$cov) {
290 my $branchExpr = $brExprs[$idx]->[2]
291 if $groupSize == scalar(@brExprs);
292 my $fullExpr =
293 defined($branchExpr) &&
294 defined($expr) ? "'$branchExpr' in '$expr'" :
295 $idx;
296 $current_mcdc->insertExpr($filename, $groupSize, 0,
297 $c, $idx, $fullExpr);
298 $current_mcdc->insertExpr($filename, $groupSize, 1,
299 $c, $idx, $fullExpr);
300 ++$idx;
301 }
302 $mcdcData->close_mcdcBlock($current_mcdc);
303 }
304 }
305 lcovutil::info(2, "finished parsing $filename\n");
306 }
307
308 foreach my $f (@{$k->{functions}}) {
309 my $name = $f->{name};
310 my $filenames = $f->{filenames}; # array
311 my $filename =
312 ReadCurrentSource::resolve_path($filenames->[0], 1);
313 next if (TraceFile::skipCurrentFile($filename));
314 die('unexpected unknown file \'' . $filenames->[0] . '\'')
315 unless $top->file_exists($filename);
316 $srcReader->open($filename);
317
318 my $info = $top->data($filename);
319 my $count = $f->{count};
320 my $regions = $f->{regions}; # startline/col, endline/col/
321 my $branches = $f->{branches};
322 # The version "3.0.1" adds fileId to mcdc.
323 # This allows using MC/DC branches from expansions for placing MC/DC entries defined in expansions to expansions call sites.
324 my $mcdc = $f->{mcdc_records}
325 if ($lcovutil::mcdc_coverage &&
326 $json_version >= version->parse("3.0.1") &&
327 exists($f->{mcdc_records}));
328
329 my $functionMap = $info->testfnc($testname);
330 # use branch data to derive MC/DC expression - so need
331 # it, even if user didn't ask
332 my $branchData = $info->testbr($testname)
333 if $lcovutil::br_coverage || $mcdc;
334 my $mcdcData = $info->testcase_mcdc($testname)
335 if ($json_version >= version->parse("3.0.1") &&
336 $lcovutil::mcdc_coverage);
337 my $startLine = $regions->[0]->[0]; # startline of first region
338 my $endline = $regions->[0]->[2]; # endline of last region
339 if ($lcovutil::func_coverage) {
340 my $func =
341 $functionMap->define_function($name, $startLine,
342 $endline)
343 unless defined($functionMap->findName($name));
344 $functionMap->add_count($name, $count);
345 }
346
347 my @mcdcBranches; # array (fileId, start line, start column, expression)
348 my %expanded_mcdcBranches; # hash of branch's fileId -> branch's start line
349
350 if ($branchData) {
351 my $funcBranchData = BranchData->new();
352 my $regionIdx = 0;
353 foreach my $b (@$branches) {
354 die("unexpected branch data") unless scalar(@$b) == 9;
355 my ($brStartLine, $brStartCol, $endLine,
356 $endCol, $trueCount, $falseCount,
357 $fileId, $expandedId, $kind) = @$b;
358 my ($line, $col) = ($brStartLine, $brStartCol);
359 my $expr;
360
361 if ($fileId == 0) {
362 $expr =
363 $srcReader->getExpr($line, $col, $endLine,
364 $endCol)
365 if $srcReader->notEmpty();
366 } else {
367 # Find a source range, which contains the branch.
368 while ($regionIdx < scalar(@$regions)) {
369 my ($rStartLine, $rStartCol, $rEndLine,
370 $rEndCol, $rCount, $rFileId,
371 $rExpandedId, $rKind
372 ) = @{$regions->[$regionIdx]};
373 if ($rExpandedId == $fileId && $rKind == 1) {
374 if ($rFileId != 0) {
375 # Check previous regions to find one
376 # that describes lines of the function's
377 # source file.
378 my $rIdx = $regionIdx - 1;
379 $fileId = $rFileId;
380 while ($fileId != 0 && $rIdx >= 0) {
381 ($rStartLine, $rStartCol,
382 $rEndLine, $rEndCol,
383 $rCount, $rFileId,
384 $rExpandedId, $rKind
385 ) = @{$regions->[$rIdx]};
386 $fileId = $rFileId
387 if ($rExpandedId == $fileId &&
388 $rKind == 1);
389 --$rIdx;
390 }
391 }
392 ($line, $col) = ($rStartLine, $rStartCol);
393 last;
394 }
395 ++$regionIdx;
396 }
397 }
398 $fileId = $b->[6];
399 # Consider only branches of "MCDCBranchRegion" kind.
400 if ($mcdc &&
401 $kind == 6 &&
402 !defined($expanded_mcdcBranches{$fileId})) {
403 if ($fileId &&
404 scalar(@mcdcBranches) &&
405 $fileId == $mcdcBranches[-1]->[0]) {
406 pop(@mcdcBranches);
407 $expanded_mcdcBranches{$fileId} = $line;
408 } else {
409 push(@mcdcBranches,
410 [$fileId, $line, $col, $expr]);
411 }
412 }
413
414 if ($lcovutil::br_coverage) {
415 # Processed branch on the same line doesn't have to be the previous.
416 my $brEntry = $funcBranchData->value($line);
417 my $branchIdx =
418 !defined($brEntry) ? 0 :
419 scalar(@{$brEntry->getBlock(0)});
420 my $br =
421 BranchBlock->new($branchIdx, $trueCount,
422 !defined($expr) ? $branchIdx :
423 "(" . $expr . ") == True");
424 $funcBranchData->append($line, 0, $br, $filename);
425
426 ++$branchIdx;
427 $br =
428 BranchBlock->new($branchIdx, $falseCount,
429 !defined($expr) ? $branchIdx :
430 "(" . $expr . ") == False");
431 $funcBranchData->append($line, 0, $br, $filename);
432 }
433 }
434 $branchData->union($funcBranchData)
435 if $lcovutil::br_coverage;
436 }
437 if ($mcdc) {
438 foreach my $m (@$mcdc) {
439 die("unexpected MC/DC data") unless scalar(@$m) == 10;
440 my ($line, $col, $endLine, $endCol,
441 $trueCount, $falseCount, $fileId, $expandedId,
442 $kind, $cov) = @$m;
443 die("unexpected MC/DC cov")
444 unless 'ARRAY' eq ref($cov);
445 my $expr;
446 my @brExprs;
447 if ($fileId == $expandedId) {
448 foreach my $branch (@mcdcBranches) {
449 my ($brFileId, $brLine, $brCol, $brExpr) =
450 @$branch;
451 if (($brLine > $line ||
452 ($brLine == $line && $brCol >= $col))
453 &&
454 ($brLine < $endLine ||
455 ($brLine == $endLine && $brCol <= $endCol))
456 ) {
457 push(@brExprs, [$brLine, $brCol, $brExpr]);
458 }
459 }
460 @brExprs = sort {$a->[0] <=> $b->[0] ||
461 $a->[1] <=> $b->[1]
462 } @brExprs;
463 $expr =
464 $srcReader->getExpr($line, $col,
465 $endLine, $endCol)
466 if $srcReader->notEmpty();
467 } else {
468 $line = $expanded_mcdcBranches{$fileId};
469 }
470 my $current_mcdc =
471 $mcdcData->new_mcdc($mcdcData, $line);
472 my $groupSize = scalar(@$cov);
473 my $idx = 0;
474 foreach my $c (@$cov) {
475 my $brExpr = $brExprs[$idx]->[2]
476 if ($fileId == $expandedId &&
477 $idx < scalar(@brExprs));
478 my $fullExpr = defined($brExpr) &&
479 defined($expr) ? "'$brExpr' in '$expr'" : $idx;
480 $current_mcdc->insertExpr($filename, $groupSize, 0,
481 $c, $idx, $fullExpr);
482 $current_mcdc->insertExpr($filename, $groupSize, 1,
483 $c, $idx, $fullExpr);
484 ++$idx;
485 }
486 $mcdcData->close_mcdcBlock($current_mcdc);
487 }
488 }
489 $info->testbr()->remove($testname)
490 if $mcdc && !$lcovutil::br_coverage;
491 }
492 }
493 lcovutil::info(2, "finished $jsonFile\n");
494 }
495 # now create the merge summary...
496 foreach my $filename ($top->files()) {
497 my $info = $top->data($filename);
498
499 my @work;
500 push(@work, [$info->sum(), $info->test]);
501 push(@work, [$info->sumbr(), $info->testbr])
502 if $lcovutil::br_coverage;
503 push(@work, [$info->func(), $info->testfnc])
504 if $lcovutil::func_coverage;
505 push(@work, [$info->mcdc(), $info->testcase_mcdc()])
506 if $lcovutil::mcdc_coverage;
507
508 foreach my $d (@work) {
509 my ($sum, $pertest) = @$d;
510 $sum->union($pertest->value($testname));
511 }
512 }
513 return $top;
514 }
515
516 my $output_filename = 'llvm2lcov.info';
517 my $testname;
518 my %opts = ('test-name|t=s' => \$testname,
519 'output-filename|o=s' => \$output_filename,);
520 my %rc_opts;
521 if (!lcovutil::parseOptions(\%rc_opts, \%opts, \$output_filename)) {
522 print(STDERR "argparse failed\n");
523 exit(1);
524 }
525
526 my $info = parse($testname, @ARGV);
527 $info->applyFilters(ReadCurrentSource->new());
528 $info->write_info_file($output_filename);
529
530 $info->print_summary() if $lcovutil::verbose >= 0;
531 my $exit_code = 0;
532 $info->checkCoverageCriteria();
533 CoverageCriteria::summarize();
534 $exit_code = 1 if $CoverageCriteria::coverageCriteriaStatus;
535
536 lcovutil::summarize_messages();
537
538 lcovutil::cleanup_callbacks();
539
540 exit $exit_code;