116 lines
4.9 KiB
Python
116 lines
4.9 KiB
Python
"""The compiler's own diagnostic tests: what it rejects, where, and in what words.
|
|
|
|
Files under test/ that run with `-verify-diagnostics` carry annotations in
|
|
MLIR's style:
|
|
|
|
x = y # expected-error {{use of unknown declaration 'y'}} this line
|
|
# expected-error @below {{...}} the next code line
|
|
# expected-error @+2 {{...}} two lines down
|
|
# expected-note-re @above {{...{{[0-9]+}}...}} a regex message
|
|
|
|
`@above` and `@below` name the nearest line above or below that is not itself
|
|
an annotation. Each file is tagged with the pass that reports its diagnostics,
|
|
read from its RUN lines: `lifetimes` (the borrow checker, -check-lifetimes),
|
|
`parser` (parse and type check), or `other`.
|
|
|
|
These are the conformance tests for Programming+'s own checker (Stage 2) and
|
|
the source of the compiler's exact wording. Many use internal modules or the
|
|
isolated parser and cannot be reproduced on godbolt; `reproducible` says
|
|
which look runnable, and verify (opt-in, `--only diagnostics`) decides.
|
|
|
|
Writes:
|
|
diagnostics/manifest.json one item per file, with its annotations
|
|
diagnostics/files/<path>.mojo the test files, verbatim
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import re
|
|
|
|
from ..common import dialect_flags, write_json, write_text
|
|
from . import Context, fresh_dir
|
|
|
|
NAME = "diagnostics"
|
|
SOURCE = "modular"
|
|
INPUTS = ["test"]
|
|
|
|
ANN = re.compile(r"expected-(?P<kind>error|warning|note|remark)(?P<re>-re)?\s*(?P<at>@\s*(?:above|below|[+-]\s*\d+))?\s*\{\{")
|
|
RUN = re.compile(r"^\s*#\s*RUN:\s*(.*)$")
|
|
PRIVATE = re.compile(r"(?m)^\s*(?:from|import)\s+std(?:\.\w+)*\._\w+|__mlir_(?:op|type|attr)\b|__extension\b")
|
|
|
|
|
|
def parse_annotations(text: str) -> list[dict]:
|
|
lines = text.split("\n")
|
|
is_ann = [bool(ANN.search(l)) and l.lstrip().startswith("#") for l in lines]
|
|
out = []
|
|
for idx, line in enumerate(lines):
|
|
starts = [m for m in ANN.finditer(line)]
|
|
for k, m in enumerate(starts):
|
|
end_limit = starts[k + 1].start() if k + 1 < len(starts) else len(line)
|
|
close = line.rfind("}}", m.end(), end_limit)
|
|
if close < 0:
|
|
continue
|
|
message = line[m.end():close]
|
|
at = (m.group("at") or "").replace(" ", "")
|
|
n = idx + 1 # 1-based line of the annotation
|
|
if not at:
|
|
target = n
|
|
elif at == "@below":
|
|
j = idx + 1
|
|
while j < len(lines) and (is_ann[j] or not lines[j].strip()):
|
|
j += 1
|
|
target = j + 1
|
|
elif at == "@above":
|
|
j = idx - 1
|
|
while j >= 0 and (is_ann[j] or not lines[j].strip()):
|
|
j -= 1
|
|
target = j + 1
|
|
else:
|
|
target = n + int(at[1:])
|
|
out.append({"kind": m.group("kind"), "regex": bool(m.group("re")), "line": target,
|
|
"annotationLine": n, "message": message})
|
|
return out
|
|
|
|
|
|
def pass_of(runs: list[str]) -> str:
|
|
verify = [r for r in runs if "-verify-diagnostics" in r] or runs
|
|
joined = " ".join(verify)
|
|
if "-check-lifetimes" in joined:
|
|
return "lifetimes"
|
|
if "parse-mojo" in joined or "-import-mojo" in joined:
|
|
return "parser"
|
|
return "other"
|
|
|
|
|
|
def run(ctx: Context) -> dict:
|
|
out = fresh_dir(NAME)
|
|
items = []
|
|
by_pass: dict[str, int] = {}
|
|
by_kind: dict[str, int] = {}
|
|
for f in sorted((ctx.root / "test").rglob("*.mojo")):
|
|
text = f.read_text(encoding="utf-8", errors="replace")
|
|
if "expected-" not in text:
|
|
continue
|
|
runs = [m.group(1).strip() for m in map(RUN.match, text.split("\n")) if m]
|
|
if runs and not any("-verify-diagnostics" in r for r in runs):
|
|
continue
|
|
anns = parse_annotations(text)
|
|
if not anns:
|
|
continue
|
|
r = f.relative_to(ctx.root / "test").as_posix()
|
|
write_text(out / "files" / r, text)
|
|
p = pass_of(runs)
|
|
by_pass[p] = by_pass.get(p, 0) + 1
|
|
for a in anns:
|
|
by_kind[a["kind"]] = by_kind.get(a["kind"], 0) + 1
|
|
private = bool(PRIVATE.search(text))
|
|
items.append({
|
|
"id": f"diagnostics/{r.removesuffix('.mojo')}", "file": f"files/{r}", "mode": "run",
|
|
"pass": p, "run": runs, "annotations": anns,
|
|
"reproducible": "unlikely" if private else "maybe",
|
|
"expect": {"kind": "compile-error", "messages": [a for a in anns if a["kind"] == "error"]},
|
|
"dialect": dialect_flags(text, ctx.pin), "provenance": ctx.prov(f"test/{r}"),
|
|
})
|
|
write_json(out / "manifest.json", {"processor": NAME, "items": items})
|
|
return {"files": len(items), "annotations": sum(len(i["annotations"]) for i in items),
|
|
"byPass": by_pass, "byKind": by_kind, "maybeReproducible": sum(1 for i in items if i["reproducible"] == "maybe")}
|