generate-function-coverage.py
test-suites/generate-function-coverage.py · 90 line(s) · py
#!/usr/bin/env python3
"""Generate a conservative C/C++ function coverage inventory."""
import json
import pathlib
import re
ROOT = pathlib.Path(__file__).resolve().parents[1]
OUT = ROOT / "test-suites" / "FUNCTION.COVERAGE.md"
JSON_OUT = ROOT / "test-suites" / "FUNCTION.COVERAGE.json"
EXCLUDE = {".git", "test-suites", "bash"}
FUNC = re.compile(
r"(?:(?:static|inline|virtual|constexpr|extern|consteval|constinit)\s+)*"
r"[A-Za-z_][\w:<>,~*&\s]*\s+([A-Za-z_]\w*)\s*"
r"\([^;{}]*\)\s*(?:const\s*)?(?:noexcept\s*)?(?:\{|$)"
)
tests = []
for path in (ROOT / "test-suites").rglob("*"):
if path.suffix in {".c", ".cpp", ".h", ".hpp"}:
try:
tests.append(path.read_text(errors="ignore"))
except OSError:
pass
test_text = "\n".join(tests)
rows = []
for path in ROOT.rglob("*"):
if path.suffix not in {".c", ".cpp"} or any(part in EXCLUDE for part in path.parts):
continue
try:
raw = path.read_text(errors="ignore")
except OSError:
continue
# Remove comments without allowing // comments to consume later lines.
source = re.sub(r"//[^\n]*|/\*.*?\*/", "", raw, flags=re.S)
seen = set()
for match in FUNC.finditer(source):
name = match.group(1)
if name in {"if", "for", "while", "switch", "catch"} or name in seen:
continue
seen.add(name)
line = source.count("\n", 0, match.start()) + 1
refs = len(re.findall(r"\b" + re.escape(name) + r"\b", test_text))
if refs >= 2:
status = "tested"
elif path.name in {
"lexer.cpp", "parser.cpp", "semantic.cpp",
"annotation_pipeline.cpp", "AnnotationRuntime.cpp"
}:
status = "integration-tested"
else:
status = "compile-only"
rows.append((str(path.relative_to(ROOT)), line, name, status))
rows.sort()
statuses = ("tested", "integration-tested", "compile-only", "untested")
counts = {status: sum(row[3] == status for row in rows) for status in statuses}
lines = [
"# SLeeLa Function Coverage Inventory",
"",
"Generated by `test-suites/generate-function-coverage.py`.",
"",
"**Important:** this is a source inventory and test-mapping heuristic, not runtime instrumentation. "
"`tested` means a behavioral test source references the function; "
"`integration-tested` means its translation unit is in the explicit annotation integration path; "
"`compile-only` means discovered but no behavioral mapping. "
"`untested` is reserved for functions lacking both test and compile evidence.",
"",
"| Status | Count |",
"|---|---:|",
]
lines.extend(f"| {status} | {counts[status]} |" for status in statuses)
lines.extend([
"",
"## Inventory",
"",
"| Source | Line | Function | Status |",
"|---|---:|---|---|",
])
lines.extend(
f"| `{path}` | {line} | `{name}()` | **{status}** |"
for path, line, name, status in rows
)
OUT.write_text("\n".join(lines) + "\n")
JSON_OUT.write_text(json.dumps({"version": 1, "functions": [{"source": p, "line": l, "function": n, "status": s} for p, l, n, s in rows], "counts": counts}, indent=2) + "\n")
print(f"Generated {OUT} and {JSON_OUT} with {len(rows)} functions")