"""Probe a candidate Clang with the reference GCC's C++ and platform headers.""" import argparse import json import os from pathlib import Path import subprocess import tempfile FIXTURES = Path(__file__).resolve().parent def run(argv, *, source="", cwd=None, checked=True): return subprocess.run( argv, input=source, text=True, capture_output=True, check=checked, timeout=60, cwd=cwd, env={**os.environ, "LC_ALL": "C"}, ) def reference_flags(clang, gcc): triple = run([gcc, "-dumpmachine"]).stdout.strip() search = run([gcc, "-E", "-x", "c++", "-v", "-"]).stderr start = "#include <...> search starts here:" paths = search.split(start, 1)[1].split("End of search list.", 1)[0].splitlines() gcc_builtin = Path(run([gcc, "-print-file-name=include"]).stdout.strip()).resolve() resource = Path(run([clang, "-print-resource-dir"]).stdout.strip()) / "include" flags = [f"--target={triple}", "-nostdinc"] for raw in paths: if raw.strip(): path = Path(raw.strip()).resolve(strict=True) flags.extend(["-isystem", str(resource if path == gcc_builtin else path)]) return flags def probe(clang, gcc, void_include, work): report = { "clang": run([clang, "--version"]).stdout.splitlines()[0], "gcc": run([gcc, "--version"]).stdout.splitlines()[0], "cases": {}, } base = [str(clang), "--driver-mode=g++", "-std=c++26", "-freflection", "-fexpansion-statements", "-ferror-limit=4", *reference_flags(clang, gcc)] report["driver"] = base def check(name, args, source=""): result = run([*base, *args], source=source, cwd=work, checked=False) report["cases"][name] = {"exit": result.returncode, "stderr": result.stderr[:6000]} return result.returncode == 0 check("reference cstddef", ["-fsyntax-only", "-x", "c++", "-"], "#include \n#ifndef __GLIBCXX__\n#error expected libstdc++\n#endif\n") check("language reflection and splice", ["-fsyntax-only", "-x", "c++", "-"], "enum class E { x }; static_assert([:^^E::x:] == E::x);\n") check("constexpr range expansion", ["-fsyntax-only", FIXTURES / "expansion-static.cpp"]) if check("plain module interface", ["--precompile", "-x", "c++-module", "-", "-o", "control.pcm"], "export module control; export constexpr int answer = 42;\n"): check("plain module importer", ["-fmodule-file=control=control.pcm", "-fsyntax-only", "-x", "c++", "-"], "import control; static_assert(answer == 42);\n") check("reference meta library", ["-fsyntax-only", FIXTURES / "library.cpp"]) for name, flags in (("Void detail enum", []), ("Void public enum", ["-DPUBLIC_ENTRY"])): check(name, [*flags, "-I", void_include, "-fsyntax-only", FIXTURES / "enum.cpp"]) if check("Void reflection module", ["--precompile", FIXTURES / "gate.cppm", "-o", "gate.pcm"]): check("Void module importer", ["-fmodule-file=gate=gate.pcm", "-fsyntax-only", FIXTURES / "importer.cpp"]) return report if __name__ == "__main__": parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--clang", type=Path, required=True) parser.add_argument("--gcc", default="g++") parser.add_argument("--void-include", type=Path, required=True) args = parser.parse_args() with tempfile.TemporaryDirectory(prefix="vls-clang-corpus-") as scratch: result = probe(args.clang.resolve(strict=True), args.gcc, args.void_include.resolve(strict=True), Path(scratch)) print(json.dumps(result, indent=2)) raise SystemExit(int(any(record["exit"] for record in result["cases"].values())))