Skip to content

Commit 72099f2

Browse files
committed
[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.
1 parent 919e090 commit 72099f2

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

test/lit.cfg

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3114,17 +3114,12 @@ if hasattr(config, 'target_link_sdk_future_version'):
31143114
config.substitutions.append(('%target-link-sdk-future-version',
31153115
config.target_link_sdk_future_version))
31163116

3117-
run_filecheck = '%s %s --allow-unused-prefixes --sanitize BUILD_DIR=%s --sanitize SOURCE_DIR=%s --ignore-runtime-warnings --use-filecheck %s %s %s' % (
3117+
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' % (
31183118
shell_quote(sys.executable),
31193119
shell_quote(config.PathSanitizingFileCheck),
3120-
# LLVM Lit performs realpath with the config path, so all paths are relative
3121-
# to the real path. cmake_binary_dir and swift_src_root come from CMake, which
3122-
# might not do real path. Because we have to match what Lit uses against what
3123-
# we provide we use realpath here. Because PathSanitizingFileCheck only
3124-
# understands sanitize patterns with forward slashes, and realpath normalizes
3125-
# the slashes, we have to replace them back to forward slashes.
3126-
shell_quote(lit.util.abs_path_preserve_drive(config.cmake_binary_dir).replace("\\", "/")),
3127-
shell_quote(lit.util.abs_path_preserve_drive(config.swift_src_root).replace("\\", "/")),
3120+
shell_quote('%t'),
3121+
shell_quote(config.cmake_binary_dir),
3122+
shell_quote(config.swift_src_root),
31283123
shell_quote(config.filecheck),
31293124
'--color' if config.color_output else '',
31303125
'--enable-windows-compatibility' if kIsWindows else '')

utils/PathSanitizingFileCheck

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,24 @@
1111

1212
import argparse
1313
import io
14+
import os
15+
import platform
1416
import re
1517
import subprocess
1618
import sys
1719

20+
# LLVM Lit performs realpath with the config path, so all paths are relative
21+
# to the real path. Paths that come from CMake (like cmake_binary_dir and
22+
# swift_src_root), might not do real path. Use realpath to normalize. Because
23+
# this normalizes Windows paths to use backslashes, we have to replace them
24+
# back to forward slashes.
25+
def normalize_if_path(s):
26+
if not os.path.exists(s):
27+
return s
28+
if platform.system() == "Windows":
29+
return os.path.abspath(s).replace('\\', '/')
30+
else:
31+
return os.path.realpath(s)
1832

1933
def main():
2034
parser = argparse.ArgumentParser(
@@ -78,13 +92,13 @@ constants.""")
7892

7993
stdin = io.open(sys.stdin.fileno(), 'r', encoding='utf-8', errors='ignore').read()
8094

81-
for s in args.sanitize_strings:
95+
for s in sorted(args.sanitize_strings, key=len, reverse=True):
8296
replacement, pattern = s.split('=', 1)
8397
# Since we want to use pattern as a regex in some platforms, we need
8498
# to escape it first, and then replace the escaped slash
8599
# literal (r'\\/') for our platform-dependent slash regex.
86100
stdin = re.sub(re.sub(r'\\/' if sys.version_info[0] < 3 else r'/',
87-
slashes_re, re.escape(pattern)),
101+
slashes_re, re.escape(normalize_if_path(pattern))),
88102
replacement, stdin)
89103

90104
# Because we force the backtracer on in the tests, we can get runtime

0 commit comments

Comments
 (0)