Check that the comparison still sees differences it once missed.
()
| 252 | |
| 253 | |
| 254 | def self_test(): |
| 255 | """Check that the comparison still sees differences it once missed.""" |
| 256 | failures = 0 |
| 257 | with tempfile.TemporaryDirectory(prefix="codegen-selftest-") as tmp: |
| 258 | tmp = Path(tmp) |
| 259 | for name, (before_src, after_src) in SELF_TEST_CASES.items(): |
| 260 | objects = [] |
| 261 | for label, source in (("before", before_src), ("after", after_src)): |
| 262 | path = tmp / f"{label}.c" |
| 263 | path.write_text(source + "\n") |
| 264 | obj = tmp / f"{label}.o" |
| 265 | gcc(["-c", "-fPIC", "-O3", path, "-o", obj]) |
| 266 | objects.append(obj) |
| 267 | _, report = compare(*objects) |
| 268 | expected = name != "unchanged" |
| 269 | ok = bool(report) is expected |
| 270 | failures += not ok |
| 271 | print(f"{'ok ' if ok else 'FAIL'} {name}") |
| 272 | |
| 273 | # -fpermissive gives GCC 14 and newer the older default, where an |
| 274 | # implicit declaration is only a warning, so this also checks |
| 275 | # nothing lets it through on a compiler that still accepts it. |
| 276 | path = tmp / "undeclared.c" |
| 277 | path.write_text("int f(int v) { return undeclared(v); }\n") |
| 278 | for name, extra in ( |
| 279 | ("undeclared call", []), |
| 280 | ("undeclared call, -fpermissive", ["-fpermissive"]), |
| 281 | ): |
| 282 | try: |
| 283 | gcc(["-c", "-O3", *extra, path, "-o", tmp / "undeclared.o"]) |
| 284 | except BuildError: |
| 285 | ok = True |
| 286 | else: |
| 287 | ok = False |
| 288 | failures += not ok |
| 289 | print(f"{'ok ' if ok else 'FAIL'} {name}") |
| 290 | return 1 if failures else 0 |
| 291 | |
| 292 | |
| 293 | def main(): |