Collects codegen diagnostics during a run. ``pending`` is the queue of unflushed entries; ``fired_count`` survives the flush so ``main()`` can check whether ``--strict`` should exit non-zero.
| 40 | |
| 41 | |
| 42 | class DiagBuffer: |
| 43 | """Collects codegen diagnostics during a run. ``pending`` is the |
| 44 | queue of unflushed entries; ``fired_count`` survives the flush so |
| 45 | ``main()`` can check whether ``--strict`` should exit non-zero.""" |
| 46 | |
| 47 | def __init__(self) -> None: |
| 48 | self.pending: List[Diagnostic] = [] |
| 49 | self.fired_count: int = 0 |
| 50 | |
| 51 | def add(self, message: str, *, source: str = "") -> None: |
| 52 | self.pending.append(Diagnostic(message=message, source=source)) |
| 53 | |
| 54 | def flush(self) -> None: |
| 55 | """Print collected diagnostics to stderr, advance ``fired_count`` |
| 56 | by the number flushed, and clear the pending list.""" |
| 57 | if not self.pending: |
| 58 | return |
| 59 | print(f"\n--- codegen diagnostics ({len(self.pending)}) ---", |
| 60 | file=sys.stderr) |
| 61 | for d in self.pending: |
| 62 | print(d.message, file=sys.stderr) |
| 63 | print( |
| 64 | "(diagnostics are non-fatal; the codegen has emitted what it " |
| 65 | "can — fix the rules to silence them.)", |
| 66 | file=sys.stderr, |
| 67 | ) |
| 68 | self.fired_count += len(self.pending) |
| 69 | self.pending.clear() |
| 70 | |
| 71 | def reset(self) -> None: |
| 72 | self.pending.clear() |
| 73 | self.fired_count = 0 |
| 74 | |
| 75 | |
| 76 | _DIAGS_BUFFER = DiagBuffer() |