()
| 165 | |
| 166 | |
| 167 | def main() -> int: |
| 168 | ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) |
| 169 | ap.add_argument("--write-baseline", action="store_true") |
| 170 | ap.add_argument("--strict", action="store_true", help="fail when a file is BELOW its baseline") |
| 171 | ap.add_argument("--list", action="store_true", help="print every raw site") |
| 172 | args = ap.parse_args() |
| 173 | |
| 174 | hits = scan() |
| 175 | total = sum(len(v) for v in hits.values()) |
| 176 | |
| 177 | if args.write_baseline: |
| 178 | write_baseline(hits) |
| 179 | print(f"memory-core baseline written: {len(hits)} files, {total} raw sites") |
| 180 | return 0 |
| 181 | |
| 182 | if args.list: |
| 183 | for rel in sorted(hits): |
| 184 | for lineno, call in hits[rel]: |
| 185 | print(f"{rel}:{lineno}: {call}(") |
| 186 | print(f"-- {total} raw sites in {len(hits)} files") |
| 187 | return 0 |
| 188 | |
| 189 | baseline = read_baseline() |
| 190 | if not baseline: |
| 191 | print(f"ERROR: no baseline at {BASELINE.relative_to(ROOT)}; run --write-baseline", file=sys.stderr) |
| 192 | return 2 |
| 193 | |
| 194 | grew: list[str] = [] |
| 195 | shrank: list[str] = [] |
| 196 | for rel, sites in sorted(hits.items()): |
| 197 | now = len(sites) |
| 198 | allowed = baseline.get(rel, 0) |
| 199 | if now > allowed: |
| 200 | # Show the LAST sites: new code is usually appended, so these are the |
| 201 | # likeliest culprits. The linter cannot know which sites are new |
| 202 | # without a diff; --list prints them all. |
| 203 | delta = now - allowed |
| 204 | tail = ", ".join(f"{ln}:{c}" for ln, c in sites[-max(delta, 1):][-6:]) |
| 205 | grew.append(f" {rel}: grew by {delta} ({allowed} -> {now}); latest sites: {tail}" |
| 206 | f" [run --list for all]") |
| 207 | elif now < allowed: |
| 208 | shrank.append(f" {rel}: {now} (baseline {allowed}) -- lower the baseline") |
| 209 | for rel, allowed in sorted(baseline.items()): |
| 210 | if rel not in hits and allowed > 0: |
| 211 | shrank.append(f" {rel}: 0 (baseline {allowed}) -- remove the line") |
| 212 | |
| 213 | if grew: |
| 214 | print("memory-core linter FAILED: raw allocator use grew. Allocate through") |
| 215 | print("src/foundation/mem_core.h (cbm_alloc/cbm_calloc/cbm_realloc/cbm_free/") |
| 216 | print("cbm_mem_strdup) instead of malloc/calloc/realloc/free/strdup.") |
| 217 | print("\n".join(grew)) |
| 218 | if shrank: |
| 219 | print("memory-core ratchet: these files improved; tighten scripts/memory-core-baseline.txt:") |
| 220 | print("\n".join(shrank)) |
| 221 | if grew or (args.strict and shrank): |
| 222 | return 1 |
| 223 | print(f"memory-core linter passed: {total} raw sites across {len(hits)} files, none grew") |
| 224 | return 0 |
no test coverage detected