2 """Generate test body using split-file and a custom script.
4 The script will prepare extra files with `split-file`, invoke `gen`, and then
5 rewrite the part after `gen` with its stdout.
7 https://llvm.org/docs/TestingGuide.html#elaborated-tests
10 PATH=/path/to/clang_build/bin:$PATH llvm/utils/update_test_body.py path/to/test.s
21 @contextlib.contextmanager
31 def process(args
, path
):
35 for line
in f
.readlines():
38 if (seen_gen
and re
.match(r
"(.|//)---", line
)) or line
.startswith(".endif"):
40 if re
.match(r
"(.|//)--- gen", line
):
44 "'gen' should be followed by another part (---) or .endif",
50 print("'gen' does not exist", file=sys
.stderr
)
52 with tempfile
.TemporaryDirectory(prefix
="update_test_body_") as dir:
54 # If the last line starts with ".endif", remove it.
56 ["split-file", "-", dir],
58 prolog
[:-1] if prolog
[-1].startswith(".endif") else prolog
63 except subprocess
.CalledProcessError
as ex
:
64 sys
.stderr
.write(ex
.stderr
.decode())
68 print(f
"invoke shell in the temporary directory '{dir}'")
69 subprocess
.run([os
.environ
.get("SHELL", "sh")])
75 # Don't encode the directory information to the Clang output.
76 # Remove unneeded details (.ident) as well.
79 CCC_OVERRIDE_OPTIONS
="#^-fno-ident",
83 sys
.stderr
.write(sub
.stderr
.decode())
84 if sub
.returncode
!= 0:
85 print("'gen' failed", file=sys
.stderr
)
88 print("stdout is empty; forgot -o - ?", file=sys
.stderr
)
90 content
= sub
.stdout
.decode()
92 with
open(path
, "w") as f
:
93 # Print lines up to '.endif'.
94 print("\n".join(prolog
), file=f
)
95 # Then print the stdout of 'gen'.
99 parser
= argparse
.ArgumentParser(
100 description
="Generate test body using split-file and a custom script"
102 parser
.add_argument("files", nargs
="+")
104 "--shell", action
="store_true", help="invoke shell instead of 'gen'"
106 args
= parser
.parse_args()
107 for path
in args
.files
:
108 retcode
= process(args
, path
)