149 lines
5.2 KiB
Python
149 lines
5.2 KiB
Python
"""The compiler's diagnostic wording, and the fixes it offers, from its source.
|
|
|
|
Programming+'s checker (Stage 2) should say what the real compiler says, in
|
|
the same words, and offer the same fixes. This scans the parser
|
|
(lib/MojoParser) and the borrow checker (lib/LowerLIT) for diagnostic calls
|
|
(emitError, emitWarning, emitNote, emitRemark, emitTokenError, attachNote,
|
|
emitErrorIfNotDiagnosed) and records, per statement:
|
|
|
|
message the string literals in order; a streamed value (`<< name`) is {}
|
|
fixits FixIt::replaceToken / insert / remove ... and their literal text
|
|
severity error | warning | note | remark
|
|
pass parser | lifetimes
|
|
|
|
This is a scan of C++ text, not a C++ parser: a message assembled in a helper
|
|
function shows up as {} pieces, and that is reported, not hidden.
|
|
|
|
Writes messages/messages.json.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import re
|
|
|
|
from ..common import write_json
|
|
from . import Context, fresh_dir
|
|
|
|
NAME = "messages"
|
|
SOURCE = "modular"
|
|
INPUTS = ["lib/MojoParser"]
|
|
|
|
CALL = re.compile(r"\b(?P<fn>emitError|emitWarning|emitNote|emitRemark|emitTokenError|emitErrorIfNotDiagnosed|attachNote|emitOpError)\s*\(")
|
|
FIXIT = re.compile(r"FixIt::(?P<kind>\w+)\s*\(")
|
|
DIRS = {"MojoParser": "parser", "LowerLIT": "lifetimes"}
|
|
|
|
|
|
def statement_end(text: str, i: int) -> int:
|
|
"""Index just past the `;` that ends the statement containing position i."""
|
|
depth = 0
|
|
n = len(text)
|
|
while i < n:
|
|
c = text[i]
|
|
if c == '"':
|
|
i += 1
|
|
while i < n and text[i] != '"':
|
|
i += 2 if text[i] == "\\" else 1
|
|
elif c == "'":
|
|
i += 1
|
|
while i < n and text[i] != "'":
|
|
i += 2 if text[i] == "\\" else 1
|
|
elif text.startswith("//", i):
|
|
i = text.find("\n", i)
|
|
if i < 0:
|
|
return n
|
|
continue
|
|
elif c in "([{":
|
|
depth += 1
|
|
elif c in ")]}":
|
|
depth -= 1
|
|
if depth < 0:
|
|
return i
|
|
elif c == ";" and depth == 0:
|
|
return i + 1
|
|
i += 1
|
|
return n
|
|
|
|
|
|
LIT = re.compile(r'"((?:\\.|[^"\\])*)"')
|
|
|
|
|
|
def unescape(s: str) -> str:
|
|
return s.encode("utf-8").decode("unicode_escape").encode("latin-1").decode("utf-8", errors="replace") if "\\" in s else s
|
|
|
|
|
|
def literals(fragment: str) -> str:
|
|
"""Adjacent C++ literals concatenate."""
|
|
return "".join(unescape(m.group(1)) for m in LIT.finditer(fragment))
|
|
|
|
|
|
def split_stream(stmt: str) -> list[str]:
|
|
"""Split a statement on top-level `<<`."""
|
|
parts, depth, cur, i = [], 0, [], 0
|
|
while i < len(stmt):
|
|
c = stmt[i]
|
|
if c == '"':
|
|
j = i + 1
|
|
while j < len(stmt) and stmt[j] != '"':
|
|
j += 2 if stmt[j] == "\\" else 1
|
|
cur.append(stmt[i:j + 1]); i = j + 1; continue
|
|
if c in "([{":
|
|
depth += 1
|
|
elif c in ")]}":
|
|
depth -= 1
|
|
if depth == 0 and stmt.startswith("<<", i):
|
|
parts.append("".join(cur)); cur = []; i += 2; continue
|
|
cur.append(c); i += 1
|
|
parts.append("".join(cur))
|
|
return parts
|
|
|
|
|
|
def message_of(stmt: str, call_end: int) -> tuple[str, list[dict]]:
|
|
head_close = statement_end(stmt, call_end) # the `)` that closes the call's arguments
|
|
head_args = stmt[call_end:head_close]
|
|
msg = literals(head_args)
|
|
fixits = []
|
|
for piece in split_stream(stmt[head_close + 1:]):
|
|
p = piece.strip()
|
|
if m := FIXIT.search(p):
|
|
fixits.append({"kind": m.group("kind"), "text": literals(p[m.end():]) or None})
|
|
elif p.startswith('"'):
|
|
msg += literals(p)
|
|
elif p:
|
|
msg += "{}"
|
|
return msg, fixits
|
|
|
|
|
|
def scan(text: str) -> list[dict]:
|
|
out = []
|
|
for m in CALL.finditer(text):
|
|
start = m.start()
|
|
close = statement_end(text, m.end()) # the `)` closing the call's arguments
|
|
end = statement_end(text, close + 1) # then on to the `;` that ends the statement
|
|
stmt = text[start:end]
|
|
msg, fixits = message_of(stmt, m.end() - start)
|
|
if not msg.strip("{} "):
|
|
continue
|
|
fn = m.group("fn")
|
|
sev = "warning" if "Warning" in fn else "note" if "Note" in fn else "remark" if "Remark" in fn else "error"
|
|
out.append({"line": text.count("\n", 0, start) + 1, "call": fn, "severity": sev,
|
|
"message": msg, "fixits": fixits})
|
|
return out
|
|
|
|
|
|
def run(ctx: Context) -> dict:
|
|
out = fresh_dir(NAME)
|
|
entries, seen = [], set()
|
|
for d, p in DIRS.items():
|
|
for f in sorted((ctx.root / "lib" / d).rglob("*.cpp")):
|
|
text = f.read_text(encoding="utf-8", errors="replace")
|
|
r = f.relative_to(ctx.root).as_posix()
|
|
for e in scan(text):
|
|
key = (r, e["message"])
|
|
if key in seen:
|
|
continue
|
|
seen.add(key)
|
|
entries.append({**e, "file": r, "pass": p})
|
|
write_json(out / "messages.json", {"processor": NAME, "provenance": ctx.prov("lib/MojoParser, lib/LowerLIT"),
|
|
"messages": entries})
|
|
return {"messages": len(entries), "withFixits": sum(1 for e in entries if e["fixits"]),
|
|
"byPass": {p: sum(1 for e in entries if e["pass"] == p) for p in DIRS.values()}}
|