Files
TheRock/compiler/amd-llvm/llvm/utils/update_test_body.py
T
derek.holloway 7502bf64a4
CodeQL / Analyze (actions) (push) Canceled after 0s
CodeQL / Analyze (python) (push) Canceled after 0s
pre-commit / pre-commit (push) Canceled after 0s
Multi-Arch CI / setup (push) Canceled after 0s
Publish build_manylinux_x86_64 images / publish_build_manylinux_x86_64 (push) Canceled after 0s
Setup (Multi-Arch) / setup (push) Canceled after 0s
Multi-Arch CI / Linux::${{ fromJSON(needs.setup.outputs.linux_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubi10 image / publish_no_rocm_image_ubi10 (push) Canceled after 0s
Multi-Arch CI / Windows::${{ fromJSON(needs.setup.outputs.windows_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Multi-Arch CI / Manifest Diff (push) Canceled after 0s
TheRock Manifest Diff Report / Generate Manifest Diff Report (push) Canceled after 0s
Multi-Arch CI / CI Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_ocl_rt images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_rocgdb images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Unit Tests / Unit Tests :: ubuntu-24.04 (push) Canceled after 0s
Unit Tests / Unit Tests :: windows-2022 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_media (push) Canceled after 0s
Unit Tests / Unit Tests Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_openmpi (push) Canceled after 0s
Flatten subprojects into a single base monorepo
2026-09-02 16:34:47 -07:00

111 lines
3.3 KiB
Python
Executable File

#!/usr/bin/env python3
"""Generate test body using split-file and a custom script.
The script will prepare extra files with `split-file`, invoke `gen`, and then
rewrite the part after `gen` with its stdout.
https://llvm.org/docs/TestingGuide.html#elaborated-tests
Example:
PATH=/path/to/clang_build/bin:$PATH llvm/utils/update_test_body.py path/to/test.s
"""
import argparse
import contextlib
import os
import re
import subprocess
import sys
import tempfile
@contextlib.contextmanager
def cd(directory):
cwd = os.getcwd()
os.chdir(directory)
try:
yield
finally:
os.chdir(cwd)
def process(args, path):
prolog = []
seen_gen = False
with open(path) as f:
for line in f.readlines():
line = line.rstrip()
prolog.append(line)
if (seen_gen and re.match(r"(.|//)---", line)) or line.startswith(".endif"):
break
if re.match(r"(.|//)--- gen", line):
seen_gen = True
else:
print(
"'gen' should be followed by another part (---) or .endif",
file=sys.stderr,
)
return 1
if not seen_gen:
print("'gen' does not exist", file=sys.stderr)
return 1
with tempfile.TemporaryDirectory(prefix="update_test_body_") as dir:
try:
# If the last line starts with ".endif", remove it.
sub = subprocess.run(
["split-file", "-", dir],
input="\n".join(
prolog[:-1] if prolog[-1].startswith(".endif") else prolog
).encode(),
capture_output=True,
check=True,
)
except subprocess.CalledProcessError as ex:
sys.stderr.write(ex.stderr.decode())
return 1
with cd(dir):
if args.shell:
print(f"invoke shell in the temporary directory '{dir}'")
subprocess.run([os.environ.get("SHELL", "sh")])
return 0
sub = subprocess.run(
["sh", "-eu", "gen"],
capture_output=True,
# Don't encode the directory information to the Clang output.
# Remove unneeded details (.ident) as well.
env=dict(
os.environ,
CCC_OVERRIDE_OPTIONS="#^-fno-ident",
PWD="/proc/self/cwd",
),
)
sys.stderr.write(sub.stderr.decode())
if sub.returncode != 0:
print("'gen' failed", file=sys.stderr)
return sub.returncode
if not sub.stdout:
print("stdout is empty; forgot -o - ?", file=sys.stderr)
return 1
content = sub.stdout.decode()
with open(path, "w") as f:
# Print lines up to '.endif'.
print("\n".join(prolog), file=f)
# Then print the stdout of 'gen'.
f.write(content)
parser = argparse.ArgumentParser(
description="Generate test body using split-file and a custom script"
)
parser.add_argument("files", nargs="+")
parser.add_argument(
"--shell", action="store_true", help="invoke shell instead of 'gen'"
)
args = parser.parse_args()
for path in args.files:
retcode = process(args, path)
if retcode != 0:
sys.exit(retcode)