-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlcov-fixer.php
73 lines (66 loc) · 2.16 KB
/
lcov-fixer.php
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
#!/usr/bin/php
<?php
/**
* This script supposed to fix all the bug came out from lcov:
* - ability to exclude files from lcov
* - excluding uncovered closing courly braces lines..
* Run it right after lcov finished and before lcov starts with genhtml in your Makefile or whatever is your building process.
* For e.g:
* lcov --no-external --directory . --capture --output-file coverage.info
* php lcov-fixer.php coverage.info src/NoNeedCoverage.cpp
* genhtml -s --demangle-cpp -o coverage coverage.info
*
* usage:
* php lcov-fixer.php [coverage info file (optional, default: coverage.info)] [exluded files... (optional)]
*
* example:
* php lcov-fixer.php coverage.info src/NoNeedCoverage.cpp
*/
echo "---=[ LCOV-FIXER ]=---\n";
$finfo = realpath($argv[1] ?? "coverage.info");
echo "Processing: $finfo\n";
$blocks = explode("\nend_of_record\n", file_get_contents($finfo));
for ($i = 2; $i < $argc; $i++) {
$file = realpath($argv[$i]);
if (!$file) {
echo "\nERROR: File not found: {$argv[$i]}\n";
exit(-1);
}
echo "Removing coverage info: $file\t";
$results = [];
foreach($blocks as $block) {
$lines = explode("\n", $block);
$remove = false;
foreach ($lines as $line) {
if (preg_match('/^SF:(.*)/', $line, $matches)) {
$fname = realpath($matches[1]);
if ($file === $fname) {
echo ".";
$remove = true;
break;
}
}
}
if (!$remove) {
$results[] = $block;
}
}
$blocks = $results;
file_put_contents($finfo, implode("\nend_of_record\n", $blocks));
echo "\t[OK]\n";
}
echo "Removing uncovered closing courly braces lines...\n";
$nfolines = explode("\n", file_get_contents($finfo));
$currentfile = "";
foreach ($nfolines as $nfoline) {
if (preg_match("/^DA:(\d+),0/", $nfoline, $matches)) { // uncovered line?
$fcontent = explode("\n", file_get_contents($currentfile));
if (trim($fcontent[$matches[1] - 1]) === "}") continue; // gotcha
}
if (preg_match("/^SF:(.*)$/", $nfoline, $matches)) {
$currentfile = $matches[1];
}
$cleannfos[] = $nfoline;
}
file_put_contents($finfo, implode("\n", $cleannfos));
echo "All done. enjoy!\n";