Skip to content

Commit 1c3108f

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 4f68243 commit 1c3108f

File tree

6 files changed

+67
-8
lines changed

6 files changed

+67
-8
lines changed

clang/test/lit.cfg.py

+9
Original file line numberDiff line numberDiff line change
@@ -362,3 +362,12 @@ 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+
lit_config.test_updaters.append(utc_lit_plugin)

clang/utils/UpdateVerifyTests/litplugin.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def get_default(prefix):
2222
prefixes.add(prefix)
2323
return prefixes
2424

25-
def verify_test_updater(result):
25+
def verify_test_updater(result, test):
2626
if not result.stderr:
2727
return None
2828
prefixes = get_verify_prefixes(result.command)

llvm/test/lit.cfg.py

+9
Original file line numberDiff line numberDiff line change
@@ -622,3 +622,12 @@ def have_ld64_plugin_support():
622622

623623
if config.has_logf128:
624624
config.available_features.add("has_logf128")
625+
626+
if lit_config.update_tests:
627+
import sys
628+
import os
629+
630+
utilspath = os.path.join(config.llvm_src_root, "utils")
631+
sys.path.append(utilspath)
632+
from update_any_test_checks import utc_lit_plugin
633+
lit_config.test_updaters.append(utc_lit_plugin)

llvm/utils/lit/lit/DiffUpdater.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def get_source_and_target(a, b):
2121
def filter_flags(args):
2222
return [arg for arg in args if not arg.startswith("-")]
2323

24-
def diff_test_updater(result):
24+
def diff_test_updater(result, test):
2525
args = filter_flags(result.command.args)
2626
if len(args) != 3:
2727
return None

llvm/utils/lit/lit/TestRunner.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -1046,7 +1046,6 @@ def formatOutput(title, data, limit=None):
10461046
def executeScriptInternal(
10471047
test, litConfig, tmpBase, commands, cwd, debug=True
10481048
) -> Tuple[str, str, int, Optional[str]]:
1049-
print("executeScriptInternal")
10501049
cmds = []
10511050
for i, ln in enumerate(commands):
10521051
# Within lit, we try to always add '%dbg(...)' to command lines in order
@@ -1170,7 +1169,7 @@ def executeScriptInternal(
11701169
if litConfig.update_tests:
11711170
for test_updater in litConfig.test_updaters:
11721171
try:
1173-
update_output = test_updater(result)
1172+
update_output = test_updater(result, test)
11741173
except Exception as e:
11751174
out += f"Exception occurred in test updater: {e}"
11761175
continue
@@ -1183,7 +1182,6 @@ def executeScriptInternal(
11831182

11841183

11851184
def executeScript(test, litConfig, tmpBase, commands, cwd):
1186-
print("executeScript")
11871185
bashPath = litConfig.getBashPath()
11881186
isWin32CMDEXE = litConfig.isWindows and not bashPath
11891187
script = tmpBase + ".script"

llvm/utils/update_any_test_checks.py

+46-3
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ 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], stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=environment
4040
)
4141
return (result.returncode, result.stdout, result.stderr)
4242

@@ -60,6 +60,40 @@ def expand_listfile_args(arg_list):
6060
return exp_arg_list
6161

6262

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

@@ -78,6 +112,11 @@ def main():
78112
nargs="*",
79113
help="Additional directories to scan for update_*_test_checks scripts",
80114
)
115+
parser.add_argument(
116+
"--path",
117+
help="""Additional directories to scan for executables invoked by the update_*_test_checks scripts,
118+
separated by the platform path separator""",
119+
)
81120
parser.add_argument("tests", nargs="+")
82121
config = parser.parse_args()
83122

@@ -88,6 +127,10 @@ def main():
88127
script_name = os.path.abspath(__file__)
89128
utc_search_path.append(os.path.join(os.path.dirname(script_name), os.path.pardir))
90129

130+
local_env = os.environ.copy()
131+
if config.path:
132+
local_env["PATH"] = config.path + os.pathsep + local_env["PATH"]
133+
91134
not_autogenerated = []
92135
utc_tools = {}
93136
have_error = False
@@ -117,7 +160,7 @@ def main():
117160
continue
118161

119162
future = executor.submit(
120-
run_utc_tool, utc_name, utc_tools[utc_name], testname
163+
run_utc_tool, utc_name, utc_tools[utc_name], testname, local_env
121164
)
122165
jobs.append((testname, future))
123166

0 commit comments

Comments
 (0)