Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
CCF/scripts/coverage_lines.py at main · microsoft/CCF · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
See all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
microsoft
CCF
Repository navigation
Code
Issues
23
(23)
Pull requests
17
(17)
Discussions
Actions
Security and quality
Insights
More
items
Files
Expand file tree
main
Breadcrumbs
CCF
/
scripts
/
coverage_lines.py
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
204 lines (172 loc) · 8.09 KB
main
Breadcrumbs
CCF
/
scripts
/
coverage_lines.py
Copy path
Top
File metadata and controls
Code
Blame
204 lines (172 loc) · 8.09 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
#!/usr/bin/env python3
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the Apache 2.0 License.
"""Patch an ``llvm-cov report``'s per-file and TOTAL line coverage to count
each physical source line once, instead of llvm-cov's own per-function
summary.
llvm-cov computes line coverage per function, then sums per file (``report``,
the HTML index, and ``llvm-cov export``'s own ``LF``/``LH`` records). This
distorts C++ totals two ways:
1. Function templates are grouped into "instantiation groups" and merged
with ``LineCoverageInfo::merge``, which takes the *maximum*
covered/total line count across instantiations rather than their union,
so lines covered only by *different* instantiations never add up even
though every line ran. See llvm/tools/llvm-cov/CoverageSummaryInfo.h/.cpp.
2. Each function's line count includes lines of nested lambdas, which have
their own summary too, so a lambda's body is counted once for it and
again for its enclosing function.
The per-line ``DA:`` records (and ``llvm-cov show``'s source view) are not
affected: each physical line is reported once. This module recomputes each
file's line counts, and their total, from ``DA:`` records and substitutes
them into the per-file and TOTAL rows of an ``llvm-cov report`` text,
leaving every other column as llvm-cov printed it. This does move what
counts as "one line" for macros: a macro's definition gets one aggregate
``DA:`` record (summed across expansions) plus one per expansion site,
since these are different physical lines -- matching ``llvm-cov show``.
Branch coverage is deliberately NOT recomputed here. ``BranchCoverageInfo::
merge`` has the same max()-merge problem, but counting ``BRDA:`` records
directly does not fix it: unlike ``DA:``, a macro/template expansion adds an
aggregate ``BRDA:`` record at the definition line *in addition to* one per
expansion/instantiation site, all describing the same branch, so counting
them all over-counts found/missed branches (verified: a two-line macro
invoked twice moves llvm-cov's correct 4/2 to a naive 6/3). CCF's headers use
branching macros heavily (LOG_*_FMT, CCF_ASSERT*, RAFT_TRACE_JSON_OUT), so
this is not a corner case. Treat llvm-cov's own branch numbers (``report``,
or this export's ``BRF``/``BRH``) as authoritative until a correct
per-branch-region reconciliation is implemented and verified.
"""
import
argparse
import
re
import
sys
from
typing
import
Dict
,
List
,
NamedTuple
,
Optional
def
_percentage
(
hit
:
int
,
found
:
int
)
->
Optional
[
float
]:
if
found
==
0
:
return
None
return
100.0
*
hit
/
found
def
_format_percentage
(
value
:
Optional
[
float
])
->
str
:
return
f"
{
value
:.2f
}
%"
if
value
is
not
None
else
"-"
class
FileLineCoverage
(
NamedTuple
):
lines_found
:
int
lines_hit
:
int
@
property
def
line_coverage
(
self
)
->
Optional
[
float
]:
return
_percentage
(
self
.
lines_hit
,
self
.
lines_found
)
def
parse_lcov
(
text
:
str
)
->
Dict
[
str
,
FileLineCoverage
]:
"""Return per-file distinct line counts from LCOV text.
Counts each ``DA:`` record as one found line (hit if its count is
non-zero), ignoring the file's own ``LF``/``LH`` and all ``BRDA:``
records; see the module docstring for why.
"""
files
:
Dict
[
str
,
FileLineCoverage
]
=
{}
current_file
:
Optional
[
str
]
=
None
lines_found
=
lines_hit
=
0
def
flush
()
->
None
:
nonlocal
current_file
,
lines_found
,
lines_hit
if
current_file
is
not
None
:
files
[
current_file
]
=
FileLineCoverage
(
lines_found
,
lines_hit
)
current_file
=
None
lines_found
=
lines_hit
=
0
for
line
in
text
.
splitlines
():
if
line
.
startswith
(
"SF:"
):
flush
()
current_file
=
line
[
len
(
"SF:"
) :]
elif
line
.
startswith
(
"DA:"
):
fields
=
line
[
len
(
"DA:"
) :].
split
(
","
)
count
=
int
(
fields
[
1
])
lines_found
+=
1
if
count
>
0
:
lines_hit
+=
1
elif
line
.
startswith
(
"end_of_record"
):
flush
()
# A trailing section without "end_of_record" is unexpected for llvm-cov's
# output, but handle it rather than silently dropping the last file.
flush
()
return
files
def
aggregate
(
files
:
Dict
[
str
,
FileLineCoverage
])
->
FileLineCoverage
:
lines_found
=
sum
(
f
.
lines_found
for
f
in
files
.
values
())
lines_hit
=
sum
(
f
.
lines_hit
for
f
in
files
.
values
())
return
FileLineCoverage
(
lines_found
,
lines_hit
)
# Token indices of an ``llvm-cov report`` row's Lines group, after splitting
# on runs of whitespace while keeping the whitespace itself (so every other
# column's original spacing survives untouched), e.g.:
# TOTAL 123860 19651 84.13% 4579 1274 72.18% 84414 26245 68.91% ...
# Index 0 is the file name, or "TOTAL"; 2/4/6 are Regions, 8/10/12
# Functions, 14/16/18 Lines (replaced below), 20/22/24 Branches (if present).
_LINES_FOUND_INDEX
=
14
_MISSED_LINES_INDEX
=
16
_LINE_COVER_INDEX
=
18
def
patch_report
(
report_text
:
str
,
files
:
Dict
[
str
,
FileLineCoverage
])
->
str
:
"""Replace the Lines/Missed Lines/Cover columns of each per-file row, and
of the TOTAL row, of an ``llvm-cov report`` text with the corrected
distinct-line counts.
``llvm-cov report`` names each file by its path with the leading
components common to all files removed, so a row is matched to the
shortest LCOV ``SF:`` path ending with its name (any other is in a deeper
directory). Every other column is returned unmodified. Raises
``ValueError`` if a row with lines has no LCOV record, rather than leave
it uncorrected.
"""
total
:
FileLineCoverage
=
aggregate
(
files
)
patched_total
=
False
lines
:
List
[
str
]
=
report_text
.
splitlines
()
for
i
,
line
in
enumerate
(
lines
):
tokens
:
List
[
str
]
=
re
.
split
(
r"(\s+)"
,
line
)
if
len
(
tokens
)
<=
_LINE_COVER_INDEX
or
not
tokens
[
_LINES_FOUND_INDEX
].
isdigit
():
continue
name
:
str
=
tokens
[
0
]
if
name
==
"TOTAL"
:
coverage
=
total
patched_total
=
True
else
:
matches
=
[
p
for
p
in
files
if
p
==
name
or
p
.
endswith
(
"/"
+
name
)]
if
matches
:
coverage
=
files
[
min
(
matches
,
key
=
len
)]
elif
tokens
[
_LINES_FOUND_INDEX
]
==
"0"
:
continue
else
:
raise
ValueError
(
f"No LCOV record found for
{
name
}
"
)
replacements
=
{
_LINES_FOUND_INDEX
:
str
(
coverage
.
lines_found
),
_MISSED_LINES_INDEX
:
str
(
coverage
.
lines_found
-
coverage
.
lines_hit
),
_LINE_COVER_INDEX
:
_format_percentage
(
coverage
.
line_coverage
),
}
for
index
,
value
in
replacements
.
items
():
tokens
[
index
]
=
value
.
rjust
(
max
(
len
(
tokens
[
index
]),
len
(
value
)))
lines
[
i
]
=
""
.
join
(
tokens
)
if
not
patched_total
:
raise
ValueError
(
"No TOTAL row found in llvm-cov report output"
)
return
"
\n
"
.
join
(
lines
)
def
main
(
argv
:
Optional
[
List
[
str
]]
=
None
)
->
int
:
parser
=
argparse
.
ArgumentParser
(
description
=
(
"Patch an 'llvm-cov report''s per-file and TOTAL line coverage "
"from an 'llvm-cov export -format=lcov' report, counting each "
"physical source line once regardless of how many template "
"instantiations or enclosing lambdas cover it. Every other "
"column is left unmodified. Branch coverage is not recomputed; "
"see the module docstring for why."
)
)
parser
.
add_argument
(
"report_file"
,
help
=
"Path to 'llvm-cov report' text output"
)
parser
.
add_argument
(
"lcov_file"
,
nargs
=
"?"
,
help
=
"Path to an LCOV file produced by 'llvm-cov export -format=lcov' "
"(default: read from stdin)"
,
)
args
=
parser
.
parse_args
(
argv
)
with
open
(
args
.
report_file
,
"r"
,
encoding
=
"utf-8"
)
as
f
:
report_text
=
f
.
read
()
if
args
.
lcov_file
:
with
open
(
args
.
lcov_file
,
"r"
,
encoding
=
"utf-8"
)
as
f
:
lcov_text
=
f
.
read
()
else
:
lcov_text
=
sys
.
stdin
.
read
()
files
=
parse_lcov
(
lcov_text
)
if
not
files
:
print
(
"No LCOV records found."
,
file
=
sys
.
stderr
)
return
1
print
(
patch_report
(
report_text
,
files
))
return
0
if
__name__
==
"__main__"
:
sys
.
exit
(
main
())
You can’t perform that action at this time.