[Utils] Sanitize %t with PathSanitizingFileCheck

This adds sanitizing of the expansion of the `%t` temp dir to
PathSanitizingFileCheck. Because the expansion of this path is different
for each test case, lit.cfg cannot use the expanded version. Instead it
relies on lit expanding the `%t` substring. This requires path
normalization to occur in PathSanitizingFileCheck instead of lit.cfg.
All strings matching the expansion of `%t` are now replaced with
`TMP_DIR`. This is especially useful when source files are created in
`%t` using `split-file`.

Also sorts the patterns to sanitize based on length, to guarantee that
patterns that are substrings of some other pattern are always tested
after the longer patterns has already been tested. Otherwise the longer
pattern may never match.
This commit is contained in:
Henrik G. Olsson
2025-10-12 14:33:58 -07:00
parent 919e090316
commit 72099f2093
2 changed files with 20 additions and 11 deletions

View File

@@ -3114,17 +3114,12 @@ if hasattr(config, 'target_link_sdk_future_version'):
config.substitutions.append(('%target-link-sdk-future-version',
config.target_link_sdk_future_version))
run_filecheck = '%s %s --allow-unused-prefixes --sanitize BUILD_DIR=%s --sanitize SOURCE_DIR=%s --ignore-runtime-warnings --use-filecheck %s %s %s' % (
run_filecheck = '%s %s --allow-unused-prefixes --sanitize TMP_DIR=%s --sanitize BUILD_DIR=%s --sanitize SOURCE_DIR=%s --ignore-runtime-warnings --use-filecheck %s %s %s' % (
shell_quote(sys.executable),
shell_quote(config.PathSanitizingFileCheck),
# LLVM Lit performs realpath with the config path, so all paths are relative
# to the real path. cmake_binary_dir and swift_src_root come from CMake, which
# might not do real path. Because we have to match what Lit uses against what
# we provide we use realpath here. Because PathSanitizingFileCheck only
# understands sanitize patterns with forward slashes, and realpath normalizes
# the slashes, we have to replace them back to forward slashes.
shell_quote(lit.util.abs_path_preserve_drive(config.cmake_binary_dir).replace("\\", "/")),
shell_quote(lit.util.abs_path_preserve_drive(config.swift_src_root).replace("\\", "/")),
shell_quote('%t'),
shell_quote(config.cmake_binary_dir),
shell_quote(config.swift_src_root),
shell_quote(config.filecheck),
'--color' if config.color_output else '',
'--enable-windows-compatibility' if kIsWindows else '')

View File

@@ -11,10 +11,24 @@
import argparse
import io
import os
import platform
import re
import subprocess
import sys
# LLVM Lit performs realpath with the config path, so all paths are relative
# to the real path. Paths that come from CMake (like cmake_binary_dir and
# swift_src_root), might not do real path. Use realpath to normalize. Because
# this normalizes Windows paths to use backslashes, we have to replace them
# back to forward slashes.
def normalize_if_path(s):
if not os.path.exists(s):
return s
if platform.system() == "Windows":
return os.path.abspath(s).replace('\\', '/')
else:
return os.path.realpath(s)
def main():
parser = argparse.ArgumentParser(
@@ -78,13 +92,13 @@ constants.""")
stdin = io.open(sys.stdin.fileno(), 'r', encoding='utf-8', errors='ignore').read()
for s in args.sanitize_strings:
for s in sorted(args.sanitize_strings, key=len, reverse=True):
replacement, pattern = s.split('=', 1)
# Since we want to use pattern as a regex in some platforms, we need
# to escape it first, and then replace the escaped slash
# literal (r'\\/') for our platform-dependent slash regex.
stdin = re.sub(re.sub(r'\\/' if sys.version_info[0] < 3 else r'/',
slashes_re, re.escape(pattern)),
slashes_re, re.escape(normalize_if_path(pattern))),
replacement, stdin)
# Because we force the backtracer on in the tests, we can get runtime