#!/usr/bin/env python3 """Source construct coverage (Phase 6A freeze criterion). Every construct of `.aji` 0.1 (spec 15) must appear in at least one `.aji` conformance test that is accepted or run, and, where the construct has rules that reject programs, in at least one reject test. The checklist is below; each construct is a regular expression over the test's source (header comments removed). python3 tools/source_coverage.py # table; exit 1 if a row fails """ import glob import os import re import sys ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) TESTS = os.path.join(ROOT, "tests", "conformance", "source") # (area, construct, regex, needs a reject test) CHECKLIST = [ ("scalars", "checked arithmetic", r"[^%]\+ [a-z0-9]", False), ("scalars", "wrapping arithmetic `+%`", r"\+%", False), ("scalars", "`as` (lossless widening)", r"\bas [ui]\d", True), ("scalars", "`as!` (checked conversion)", r"\bas! ", False), ("scalars", "`as%` (wrapping conversion)", r"\bas% ", False), ("scalars", "bitwise and shifts", r"<<|>>| & | \^ ", False), ("control flow", "`let`", r"\blet ", False), ("control flow", "`var`", r"\bvar ", False), ("control flow", "assignment", r"^\s*[a-z_][a-z0-9_]* = ", False), ("control flow", "`if`/`else`", r"\bif .*\{.*\belse\b|\} else \{", False), ("control flow", "`while`", r"\bwhile ", False), ("control flow", "`for … in a..b`", r"\bfor \w+ in ", False), ("control flow", "`loop`", r"\bloop \{", False), ("control flow", "`break` / `continue`", r"\bbreak;|\bcontinue;", True), ("control flow", "`return`", r"\breturn\b", True), ("control flow", "`assert`", r"\bassert\(", False), ("struct/enum/match", "struct declaration and literal", r"\bstruct \w+ \{", True), ("struct/enum/match", "enum declaration and `Enum::Variant`", r"\w+::\w+", True), ("struct/enum/match", "enum `match`", r"match .*\{[^}]*\w+::\w+", True), ("struct/enum/match", "integer `match` with `_`", r"\b_ =>", True), ("struct/enum/match", "`match` as an expression", r"= match ", False), ("arrays/slices", "`[e; N]`", r"\[[^\]]+; \d+\]", False), ("arrays/slices", "indexing `s[i]`", r"\w\[[a-z0-9_]+\]", True), ("arrays/slices", "ranges `s[a..b]`", r"\[[^\]]*\.\.[^\]]*\]", False), ("arrays/slices", "`split_at`", r"\.split_at\(", False), ("arrays/slices", "`slice.copy`", r"slice\.copy\(", True), ("arrays/slices", "byte strings", r'b"', False), ("borrow/move", "`ref r = …;` / `mut r = …;`", r"^\s*(ref|mut) \w+ = ", True), ("borrow/move", "`ref`/`mut` arguments", r"\((ref|mut) \w+|, (ref|mut) \w+", True), ("borrow/move", "`*r` load and store", r"\*\w+ = |= \*\w+", True), ("borrow/move", "`move`", r"\bmove \w+", True), ("own/region", "`own.new` / `own.take` / `own.free`", r"own\.(new|take|free)\(", True), ("own/region", "`own.array`", r"own\.array\(", False), ("own/region", "regions", r"region\.(create|new|array|reset|destroy)\(", True), ("own/region", "`cap.narrow.budget`", r"cap\.narrow\.budget\(", False), ("failure", "outcome `match`", r"\bok\(\w*\)? =>|\bok =>", True), ("failure", "`or trap`", r"\bor trap\b", False), ("failure", "built-in error enums", r"(alloc_error|io_error|cap_error)::", False), ("resources/I/O", "stdin/stdout", r"io\.(stdin_read|stdout_write)\(", True), ("resources/I/O", "files", r"fs\.(open_read|create|read|write_all|close)\(", True), ("resources/I/O", "TCP", r"net\.(listen|connect|accept|recv|send_all)\(", False), ("resources/I/O", "`clock`/`random`", r"(clock\.monotonic|random\.fill)\(", True), ("resources/I/O", "`cap.narrow.prefix`", r"cap\.narrow\.prefix\(", False), ("tasks/channels", "`scope` and `task.spawn`/`join`", r"\bscope \w+ \{", True), ("tasks/channels", "`task.cancel` / `task.cancelled`", r"task\.cancel(led)?\(", False), ("tasks/channels", "`channel.make` / `send` / `recv`", r"channel\.(make|send|recv)", True), ("tasks/channels", "`channel.dup`", r"channel\.dup\(", False), ("tasks/channels", "`close_receiver` and `pending`", r"close_receiver\(", True), ("mutex/select", "`lock … cancelled`", r"\block ref \w+ as \w+", True), ("mutex/select", "`mutex.make` / `mutex.take`", r"mutex\.(make|take)\(", True), ("mutex/select", "`select`", r"\bselect \{", True), ("cancellation", "`shield`", r"\bshield \{", False), ("cancellation", "`task.check_cancel`", r"task\.check_cancel\(", False), ("cancellation", "`task.join_cancel`", r"task\.join_cancel\(", True), ("unsafe", "`ref unchecked` / `assume`", r"\bunchecked\b|\bassume\(", True), ("unsafe", "`trusted` rejected", r"\btrusted\b", True), ] def load(): runs, rejects = [], [] for path in sorted(glob.glob(os.path.join(TESTS, "**", "*.aji"), recursive=True)): with open(path, encoding="utf-8") as f: text = f.read() m = re.search(r"^// expect: (\w+)", text, re.M) if not m: continue body = "\n".join(l for l in text.splitlines() if not l.lstrip().startswith("//")) (rejects if m.group(1) == "reject" else runs).append((os.path.relpath(path, TESTS), body)) return runs, rejects def main(): runs, rejects = load() failed = 0 print("| area | construct | run/accept tests | reject tests | status |") print("|---|---|---:|---:|---|") for area, name, rx, need_reject in CHECKLIST: pat = re.compile(rx, re.M) nr = sum(1 for _, b in runs if pat.search(b)) nj = sum(1 for _, b in rejects if pat.search(b)) ok = nr > 0 and (nj > 0 or not need_reject) if name == "`trusted` rejected": ok = nj > 0 failed += not ok print(f"| {area} | {name} | {nr} | {nj}{'' if need_reject else ' (not required)'} | {'covered' if ok else 'MISSING'} |") print(f"\n{len(CHECKLIST) - failed}/{len(CHECKLIST)} constructs covered " f"({len(runs)} run/accept and {len(rejects)} reject `.aji` tests)") sys.exit(1 if failed else 0) if __name__ == "__main__": main()