|
| 1 | +#!/usr/bin/env bash |
| 2 | +# check_include_regions.sh — headers are included only where a system has no |
| 3 | +# module for what is needed (design 2026-11-07 D10). |
| 4 | +# |
| 5 | +# mcpp imports the standard library (`import std`), its dependencies as modules |
| 6 | +# (nlohmann.json, mcpplibs.os, the mwrap wrappers of zlib, zstd, xz and of the |
| 7 | +# C library's macros), and nothing else. The only `#include` directives of |
| 8 | +# `src/` and `modules/` are the operating system's headers of the file lock, |
| 9 | +# each in the source file of its system (src/native/<system>/lock.cpp). A |
| 10 | +# directive anywhere else is a header a module should provide: depend on the |
| 11 | +# package that wraps it, or wrap it (mcpp-index docs/mwrap.md). |
| 12 | +# |
| 13 | +# Text that only looks like a directive is not one: a raw string (the source |
| 14 | +# mcpp writes for a launcher or a template) and a comment are skipped. Tests |
| 15 | +# (`tests/`, a module's `tests/`) are outside the rule. |
| 16 | +set -euo pipefail |
| 17 | +cd "$(dirname "$0")/../.." |
| 18 | +python3 - <<'PY' |
| 19 | +import re, sys |
| 20 | +from pathlib import Path |
| 21 | +
|
| 22 | +ALLOWED = {Path("src/native/posix/lock.cpp"), Path("src/native/windows/lock.cpp")} |
| 23 | +RAW = re.compile(r'(?:u8|u|U|L)?R"([^()\\ ]{0,16})\(') |
| 24 | +
|
| 25 | +def directives(text): |
| 26 | + """Line numbers of `#include` directives outside comments and raw strings.""" |
| 27 | + out, i, n, line = [], 0, len(text), 1 |
| 28 | + at_line_start = True |
| 29 | + while i < n: |
| 30 | + c = text[i] |
| 31 | + if c == '\n': |
| 32 | + line += 1; at_line_start = True; i += 1; continue |
| 33 | + if text.startswith('//', i): |
| 34 | + j = text.find('\n', i); i = n if j < 0 else j; continue |
| 35 | + if text.startswith('/*', i): |
| 36 | + j = text.find('*/', i + 2); j = n if j < 0 else j + 2 |
| 37 | + line += text.count('\n', i, j); i = j; continue |
| 38 | + m = RAW.match(text, i) |
| 39 | + if m and (i == 0 or not (text[i - 1].isalnum() or text[i - 1] == '_')): |
| 40 | + end = text.find(')' + m.group(1) + '"', m.end()); end = n if end < 0 else end + len(m.group(1)) + 2 |
| 41 | + line += text.count('\n', i, end); i = end; at_line_start = False; continue |
| 42 | + if c == '"': |
| 43 | + j = i + 1 |
| 44 | + while j < n and text[j] not in '"\n': |
| 45 | + j += 2 if text[j] == '\\' else 1 |
| 46 | + i = j + 1; at_line_start = False; continue |
| 47 | + if at_line_start and c == '#': |
| 48 | + if re.match(r'#\s*include\b', text[i:]): out.append(line) |
| 49 | + if not c.isspace(): at_line_start = False |
| 50 | + i += 1 |
| 51 | + return out |
| 52 | +
|
| 53 | +bad = 0 |
| 54 | +for path in sorted(list(Path("src").rglob("*.cpp*")) + list(Path("modules").rglob("*.cpp*"))): |
| 55 | + if "tests" in path.parts or "test" in path.parts or path in ALLOWED: |
| 56 | + continue |
| 57 | + for line in directives(path.read_text(encoding="utf-8", errors="replace")): |
| 58 | + print(f"{path}:{line}: #include outside the system lock files; import a module instead") |
| 59 | + bad += 1 |
| 60 | +for path in ALLOWED: |
| 61 | + if not path.exists(): |
| 62 | + print(f"{path}: the allowed region is gone; update this check") |
| 63 | + bad += 1 |
| 64 | +print(f"check_include_regions: {bad} problem(s)") |
| 65 | +sys.exit(1 if bad else 0) |
| 66 | +PY |
0 commit comments