Skip to content

Commit

Permalink
cov: add function coverage option (#1108)
Browse files Browse the repository at this point in the history
  • Loading branch information
karineek authored Jan 22, 2021
1 parent 805b7ed commit 0db602f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
17 changes: 17 additions & 0 deletions gfauto/gfauto/cov_from_gcov.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,21 @@ def main() -> None:
"/cov/001 /cov/002 /cov/blah etc. will be computed and the results will be merged.",
)

parser.add_argument(
"--gcov_functions",
help="Pass to indicate that the output measures coverage of functions (instead of lines)."
"This requires using --gcov_uses_json and you must have gcc 9+.",
action="store_true",
)

parsed_args = parser.parse_args(sys.argv[1:])

if not parsed_args.gcov_path:
parser.error("Please provide gcov_path")

if parsed_args.gcov_functions and not parsed_args.gcov_uses_json:
parser.error("Function coverage requires using --gcov_uses_json with gcc 9+.")

gcov_path: str = parsed_args.gcov_path

build_dir = parsed_args.build_dir
Expand All @@ -87,12 +97,19 @@ def main() -> None:
gcov_prefix_dir = os.path.abspath(gcov_prefix_dir)
gcov_prefix_dir = os.path.normpath(gcov_prefix_dir)

gcov_tags = (
["functions", "start_line", "execution_count"]
if parsed_args.gcov_functions
else ["lines", "line_number", "count"]
)

data = cov_util.GetLineCountsData(
gcov_path=gcov_path,
gcov_uses_json_output=parsed_args.gcov_uses_json,
build_dir=build_dir,
gcov_prefix_dir=gcov_prefix_dir,
num_threads=parsed_args.num_threads,
gcov_json_tags=gcov_tags,
)

output_coverage_path: str = parsed_args.out
Expand Down
11 changes: 7 additions & 4 deletions gfauto/gfauto/cov_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import threading
import typing
from collections import Counter
from dataclasses import dataclass
from dataclasses import dataclass, field
from queue import Queue
from typing import Any, Dict, List, Optional, Tuple

Expand All @@ -49,6 +49,9 @@ class GetLineCountsData: # pylint: disable=too-many-instance-attributes;
gcno_files_queue: "Queue[DirAndItsFiles]" = dataclasses.field(default_factory=Queue)
stdout_queue: "Queue[DirAndItsOutput]" = dataclasses.field(default_factory=Queue)
line_counts: LineCounts = dataclasses.field(default_factory=dict)
gcov_json_tags: List[str] = field(
default_factory=lambda: ["lines", "line_number", "count"]
)


def _thread_gcov(data: GetLineCountsData) -> None:
Expand Down Expand Up @@ -145,9 +148,9 @@ def _process_json_lines(data: GetLineCountsData, lines: typing.TextIO) -> None:
file_path = os.path.join(current_working_directory, file_path)
file_path = os.path.normpath(file_path)
file_line_counts = data.line_counts.setdefault(file_path, Counter())
for line in file_coverage_info["lines"]:
line_number = line["line_number"]
line_count = int(line["count"])
for line in file_coverage_info[data.gcov_json_tags[0]]:
line_number = line[data.gcov_json_tags[1]]
line_count = int(line[data.gcov_json_tags[2]])
file_line_counts.update({line_number: line_count})


Expand Down

0 comments on commit 0db602f

Please sign in to comment.