Skip to content

Commit 20d7d62

Browse files
committed
[Utils] Add UTC support for lit's --update-tests
Adds support for invoking the appropriate update_*_test_checks.py script from lit. Checks the header comment for which script was used to generate it in the first place, so only test cases that were already generated are affected. To support this the interface for test updater functions is expanded to not only take a ShellCommandResult, but also the Test object. This makes it easy to get the file path of the current test. Also adds a --path flag to update_any_test_checks.py as a convenience to avoid having to manually set the PATH variable.
1 parent 6cdb6be commit 20d7d62

File tree

4 files changed

+72
-4
lines changed

4 files changed

+72
-4
lines changed

clang/test/lit.cfg.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,3 +362,13 @@ def calculate_arch_features(arch_string):
362362
# possibly be present in system and user configuration files, so disable
363363
# default configs for the test runs.
364364
config.environment["CLANG_NO_DEFAULT_CONFIG"] = "1"
365+
366+
if lit_config.update_tests:
367+
import sys
368+
import os
369+
370+
utilspath = os.path.join(config.llvm_src_root, "utils")
371+
sys.path.append(utilspath)
372+
from update_any_test_checks import utc_lit_plugin
373+
374+
lit_config.test_updaters.append(utc_lit_plugin)

llvm/test/lit.cfg.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -630,3 +630,13 @@ def have_ld64_plugin_support():
630630

631631
if config.has_logf128:
632632
config.available_features.add("has_logf128")
633+
634+
if lit_config.update_tests:
635+
import sys
636+
import os
637+
638+
utilspath = os.path.join(config.llvm_src_root, "utils")
639+
sys.path.append(utilspath)
640+
from update_any_test_checks import utc_lit_plugin
641+
642+
lit_config.test_updaters.append(utc_lit_plugin)

llvm/utils/lit/lit/TestRunner.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1193,7 +1193,7 @@ def executeScriptInternal(
11931193
if litConfig.update_tests:
11941194
for test_updater in litConfig.test_updaters:
11951195
try:
1196-
update_output = test_updater(result)
1196+
update_output = test_updater(result, test)
11971197
except Exception as e:
11981198
out += f"Exception occurred in test updater: {e}"
11991199
continue

llvm/utils/update_any_test_checks.py

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,12 @@ def find_utc_tool(search_path, utc_name):
3434
return None
3535

3636

37-
def run_utc_tool(utc_name, utc_tool, testname):
37+
def run_utc_tool(utc_name, utc_tool, testname, environment):
3838
result = subprocess.run(
39-
[utc_tool, testname], stdout=subprocess.PIPE, stderr=subprocess.PIPE
39+
[utc_tool, testname],
40+
stdout=subprocess.PIPE,
41+
stderr=subprocess.PIPE,
42+
env=environment,
4043
)
4144
return (result.returncode, result.stdout, result.stderr)
4245

@@ -60,6 +63,42 @@ def expand_listfile_args(arg_list):
6063
return exp_arg_list
6164

6265

66+
def utc_lit_plugin(result, test):
67+
testname = test.getFilePath()
68+
if not testname:
69+
return None
70+
71+
script_name = os.path.abspath(__file__)
72+
utc_search_path = os.path.join(os.path.dirname(script_name), os.path.pardir)
73+
74+
with open(testname, "r") as f:
75+
header = f.readline().strip()
76+
77+
m = RE_ASSERTIONS.search(header)
78+
if m is None:
79+
return None
80+
81+
utc_name = m.group(1)
82+
utc_tool = find_utc_tool([utc_search_path], utc_name)
83+
if not utc_tool:
84+
return f"update-utc-tests: {utc_name} not found"
85+
86+
return_code, stdout, stderr = run_utc_tool(
87+
utc_name, utc_tool, testname, test.config.environment
88+
)
89+
90+
stderr = stderr.decode(errors="replace")
91+
if return_code != 0:
92+
if stderr:
93+
return f"update-utc-tests: {utc_name} exited with return code {return_code}\n{stderr.rstrip()}"
94+
return f"update-utc-tests: {utc_name} exited with return code {return_code}"
95+
96+
stdout = stdout.decode(errors="replace")
97+
if stdout:
98+
return f"update-utc-tests: updated {testname}\n{stdout.rstrip()}"
99+
return f"update-utc-tests: updated {testname}"
100+
101+
63102
def main():
64103
from argparse import RawTextHelpFormatter
65104

@@ -78,6 +117,11 @@ def main():
78117
nargs="*",
79118
help="Additional directories to scan for update_*_test_checks scripts",
80119
)
120+
parser.add_argument(
121+
"--path",
122+
help="""Additional directories to scan for executables invoked by the update_*_test_checks scripts,
123+
separated by the platform path separator""",
124+
)
81125
parser.add_argument("tests", nargs="+")
82126
config = parser.parse_args()
83127

@@ -88,6 +132,10 @@ def main():
88132
script_name = os.path.abspath(__file__)
89133
utc_search_path.append(os.path.join(os.path.dirname(script_name), os.path.pardir))
90134

135+
local_env = os.environ.copy()
136+
if config.path:
137+
local_env["PATH"] = config.path + os.pathsep + local_env["PATH"]
138+
91139
not_autogenerated = []
92140
utc_tools = {}
93141
have_error = False
@@ -117,7 +165,7 @@ def main():
117165
continue
118166

119167
future = executor.submit(
120-
run_utc_tool, utc_name, utc_tools[utc_name], testname
168+
run_utc_tool, utc_name, utc_tools[utc_name], testname, local_env
121169
)
122170
jobs.append((testname, future))
123171

0 commit comments

Comments
 (0)