Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix parsing of gcov metadata #601

Merged
merged 4 commits into from Apr 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.rst
Expand Up @@ -17,6 +17,7 @@ Bug fixes and small improvements:
- Remove function coverage from sonarcube report. (:issue:`591`)
- Fix parallel processing of gcov data. (:issue:`592`)
- Better diagnostics when dealing with corrupted input files. (:issue:`593`)
- Accept metadata lines without values (introduced in gcc-11). (:issue:`601`)

Documentation:

Expand Down
5 changes: 4 additions & 1 deletion gcovr/gcov.py
Expand Up @@ -99,8 +99,11 @@ def process_gcov_data(data_fname, covdata, gcda_fname, options, currdir=None):
# Find the source file
# TODO: instead of heuristics, use "working directory" if available
metadata = parse_metadata(lines)
source = metadata.get("Source")
if source is None:
raise RuntimeError("Unexpected value 'None' for metadata 'Source'.")
fname = guess_source_file_name(
metadata["Source"].strip(),
source,
data_fname,
gcda_fname,
root_dir=options.root_dir,
Expand Down
16 changes: 11 additions & 5 deletions gcovr/gcov_parser.py
Expand Up @@ -122,7 +122,7 @@ class _MetadataLine(NamedTuple):
"""A gcov line with metadata: ``-: 0:KEY:VALUE``"""

key: str
value: str
value: Optional[str]


class _BlockLine(NamedTuple):
Expand Down Expand Up @@ -220,12 +220,14 @@ def parse_metadata(lines: List[str]) -> Dict[str, str]:
RuntimeError: Missing key 'Source' in metadata. GCOV data was >>
-: 0:Foo:bar
-: 0:Key:123<< End of GCOV data
>>> parse_metadata('-: 0:Source: file \n -: 0:Foo: bar \n -: 0:Key: 123 '.splitlines())
{'Source': 'file', 'Foo': 'bar', 'Key': '123'}
>>> parse_metadata('''
... -: 0:Source:file
... -: 0:Foo:bar
... -: 0:Key:123
... -: 0:Key
... '''.splitlines())
{'Source': 'file', 'Foo': 'bar', 'Key': '123'}
{'Source': 'file', 'Foo': 'bar', 'Key': None}
"""
collected = {}
for line in lines:
Expand Down Expand Up @@ -738,8 +740,12 @@ def _parse_line(line: str) -> _Line:

# METADATA (key, value)
if count_str == "-" and lineno == "0":
key, value = source_code.split(":", 1)
return _MetadataLine(key, value)
if ":" in source_code:
key, value = source_code.split(":", 1)
return _MetadataLine(key, value.strip())
else:
# Add a syntethic metadata with no value
return _MetadataLine(source_code, None)

if count_str == "-":
count = 0
Expand Down