[lld] [llvm] [LIT] Use forward slashes in substitutions when LLVM_WINDOWS_PREFER_FORWARD_SLASH is set (PR #179865)
Junji Watanabe via llvm-commits
llvm-commits at lists.llvm.org
Thu Feb 19 05:51:03 PST 2026
https://github.com/Jwata updated https://github.com/llvm/llvm-project/pull/179865
>From e73a8be75e8295a992539feda149d2892617d377 Mon Sep 17 00:00:00 2001
From: Junji Watanabe <jwata at google.com>
Date: Tue, 3 Feb 2026 13:06:36 +0900
Subject: [PATCH 1/5] [LIT][LLD] Fix Windows test failures due to path
separator mismatches
When building with -DLLVM_WINDOWS_PREFER_FORWARD_SLASH=ON, tools like lld output paths with forward slashes on Windows. However, lit's default substitutions (%t, %p) typically use backslashes on Windows, causing FileCheck failures in tests that strictly match path separators.
---
lld/test/COFF/linkreprofullpathrsp.test | 2 +-
llvm/utils/lit/lit/TestRunner.py | 6 +++++-
llvm/utils/llvm-lit/llvm-lit.in | 3 +++
3 files changed, 9 insertions(+), 2 deletions(-)
diff --git a/lld/test/COFF/linkreprofullpathrsp.test b/lld/test/COFF/linkreprofullpathrsp.test
index 66f2e2ba2f859..4d6f0a8a90f1a 100644
--- a/lld/test/COFF/linkreprofullpathrsp.test
+++ b/lld/test/COFF/linkreprofullpathrsp.test
@@ -14,7 +14,7 @@ Test link.exe-style /linkreprofullpathrsp: flag.
# RUN: lld-link /subsystem:console %t.obj %p/Inputs/std32.lib /defaultlib:%p/Inputs/library.lib \
# RUN: /libpath:%p/Inputs /defaultlib:std64.lib ret42.lib /entry:main at 0 /linkreprofullpathrsp:%t.rsp \
# RUN: %t.pdb /wholearchive:%t.archive.lib /out:%t.exe /timestamp:0
-# # RUN: FileCheck %s --check-prefix=RSP -DT=%t -DP=%p < %t.rsp
+# RUN: FileCheck %s --check-prefix=RSP -DT=%t -DP=%p < %t.rsp
# RUN: lld-link /subsystem:console @%t.rsp /out:%t2.exe /entry:main at 0 /timestamp:0
# RUN: diff %t.exe %t2.exe
diff --git a/llvm/utils/lit/lit/TestRunner.py b/llvm/utils/lit/lit/TestRunner.py
index 2683180d2864d..01f14bad315de 100644
--- a/llvm/utils/lit/lit/TestRunner.py
+++ b/llvm/utils/lit/lit/TestRunner.py
@@ -2457,7 +2457,11 @@ def executeShTest(
tmpDir, tmpBase = getTempPaths(test)
substitutions = list(extra_substitutions)
substitutions += getDefaultSubstitutions(
- test, tmpDir, tmpBase, normalize_slashes=useExternalSh
+ test,
+ tmpDir,
+ tmpBase,
+ normalize_slashes=useExternalSh
+ or litConfig.params.get("use_normalized_slashes", False),
)
conditions = {feature: True for feature in test.config.available_features}
script = applySubstitutions(
diff --git a/llvm/utils/llvm-lit/llvm-lit.in b/llvm/utils/llvm-lit/llvm-lit.in
index 0b6683b6b6230..62ee5bf21fc60 100755
--- a/llvm/utils/llvm-lit/llvm-lit.in
+++ b/llvm/utils/llvm-lit/llvm-lit.in
@@ -17,6 +17,9 @@ def map_config(source_dir, site_config):
# configuration file knows how to find the object tree.
builtin_parameters = { 'build_mode' : '@BUILD_MODE@' }
+if "@LLVM_WINDOWS_PREFER_FORWARD_SLASH@".upper() in ["ON", "TRUE", "1"]:
+ builtin_parameters["use_normalized_slashes"] = True
+
@LLVM_LIT_CONFIG_MAP@
builtin_parameters['config_map'] = config_map
>From 55e23f33cbe0e3781722dd0c7e8f57247a5267ac Mon Sep 17 00:00:00 2001
From: Junji Watanabe <jwata at google.com>
Date: Fri, 6 Feb 2026 11:52:40 +0900
Subject: [PATCH 2/5] Address code reviews - revert comment out fix - use
lit.util.pythonize_bool()
---
lld/test/COFF/linkreprofullpathrsp.test | 2 +-
llvm/utils/llvm-lit/llvm-lit.in | 8 +++++---
2 files changed, 6 insertions(+), 4 deletions(-)
diff --git a/lld/test/COFF/linkreprofullpathrsp.test b/lld/test/COFF/linkreprofullpathrsp.test
index 4d6f0a8a90f1a..66f2e2ba2f859 100644
--- a/lld/test/COFF/linkreprofullpathrsp.test
+++ b/lld/test/COFF/linkreprofullpathrsp.test
@@ -14,7 +14,7 @@ Test link.exe-style /linkreprofullpathrsp: flag.
# RUN: lld-link /subsystem:console %t.obj %p/Inputs/std32.lib /defaultlib:%p/Inputs/library.lib \
# RUN: /libpath:%p/Inputs /defaultlib:std64.lib ret42.lib /entry:main at 0 /linkreprofullpathrsp:%t.rsp \
# RUN: %t.pdb /wholearchive:%t.archive.lib /out:%t.exe /timestamp:0
-# RUN: FileCheck %s --check-prefix=RSP -DT=%t -DP=%p < %t.rsp
+# # RUN: FileCheck %s --check-prefix=RSP -DT=%t -DP=%p < %t.rsp
# RUN: lld-link /subsystem:console @%t.rsp /out:%t2.exe /entry:main at 0 /timestamp:0
# RUN: diff %t.exe %t2.exe
diff --git a/llvm/utils/llvm-lit/llvm-lit.in b/llvm/utils/llvm-lit/llvm-lit.in
index 62ee5bf21fc60..62d42e062e041 100755
--- a/llvm/utils/llvm-lit/llvm-lit.in
+++ b/llvm/utils/llvm-lit/llvm-lit.in
@@ -17,9 +17,6 @@ def map_config(source_dir, site_config):
# configuration file knows how to find the object tree.
builtin_parameters = { 'build_mode' : '@BUILD_MODE@' }
-if "@LLVM_WINDOWS_PREFER_FORWARD_SLASH@".upper() in ["ON", "TRUE", "1"]:
- builtin_parameters["use_normalized_slashes"] = True
-
@LLVM_LIT_CONFIG_MAP@
builtin_parameters['config_map'] = config_map
@@ -28,6 +25,11 @@ builtin_parameters['config_map'] = config_map
llvm_source_root = path(r'@LLVM_SOURCE_DIR@')
sys.path.insert(0, os.path.join(llvm_source_root, 'utils', 'lit'))
+import lit.util
+
+if lit.util.pythonize_bool('@LLVM_WINDOWS_PREFER_FORWARD_SLASH@'):
+ builtin_parameters['use_normalized_slashes'] = True
+
if __name__=='__main__':
from lit.main import main
main(builtin_parameters)
>From a17f4c921ef2163042fcab4ff87f68e427838ee6 Mon Sep 17 00:00:00 2001
From: Junji Watanabe <jwata at google.com>
Date: Fri, 6 Feb 2026 22:32:44 +0900
Subject: [PATCH 3/5] fix other substitutions to respect
normalized_slashes=True
---
llvm/utils/lit/lit/TestRunner.py | 20 +++++++++++++++++---
1 file changed, 17 insertions(+), 3 deletions(-)
diff --git a/llvm/utils/lit/lit/TestRunner.py b/llvm/utils/lit/lit/TestRunner.py
index 01f14bad315de..606f7f936ef51 100644
--- a/llvm/utils/lit/lit/TestRunner.py
+++ b/llvm/utils/lit/lit/TestRunner.py
@@ -1525,11 +1525,25 @@ def getDefaultSubstitutions(test, tmpDir, tmpBase, normalize_slashes=False):
substitutions.append(("%{s:basename}", sourceBaseName))
substitutions.append(("%{t:stem}", tmpBaseName))
+ fs_sep = os.path.sep
+ if normalize_slashes:
+ fs_sep = "/"
+
substitutions.extend(
[
- ("%{fs-src-root}", pathlib.Path(sourcedir).anchor),
- ("%{fs-tmp-root}", pathlib.Path(tmpBase).anchor),
- ("%{fs-sep}", os.path.sep),
+ (
+ "%{fs-src-root}",
+ pathlib.Path(sourcedir).anchor.replace("\\", "/")
+ if normalize_slashes
+ else pathlib.Path(sourcedir).anchor,
+ ),
+ (
+ "%{fs-tmp-root}",
+ pathlib.Path(tmpBase).anchor.replace("\\", "/")
+ if normalize_slashes
+ else pathlib.Path(tmpBase).anchor,
+ ),
+ ("%{fs-sep}", fs_sep),
]
)
>From d584df498f387669a5235442533939d0973d2811 Mon Sep 17 00:00:00 2001
From: Junji Watanabe <jwata at google.com>
Date: Tue, 17 Feb 2026 15:00:50 +0900
Subject: [PATCH 4/5] relax path maches for lit tests
---
llvm/utils/lit/tests/shtest-readfile.py | 6 +++---
llvm/utils/lit/tests/use-llvm-tool.py | 4 ++--
2 files changed, 5 insertions(+), 5 deletions(-)
diff --git a/llvm/utils/lit/tests/shtest-readfile.py b/llvm/utils/lit/tests/shtest-readfile.py
index ca57db82e6617..26ea2911f96f2 100644
--- a/llvm/utils/lit/tests/shtest-readfile.py
+++ b/llvm/utils/lit/tests/shtest-readfile.py
@@ -3,13 +3,13 @@
# TODO(boomanaiden154): This sometimes fails, possibly due to buffers not being flushed.
# ALLOW_RETRIES: 2
-# RUN: env LIT_USE_INTERNAL_SHELL=1 not %{lit} -v %{inputs}/shtest-readfile | FileCheck -match-full-lines -DTEMP_PATH=%S%{fs-sep}Inputs%{fs-sep}shtest-readfile%{fs-sep}Output %s
+# RUN: env LIT_USE_INTERNAL_SHELL=1 not %{lit} -v %{inputs}/shtest-readfile | FileCheck -match-full-lines %s
# CHECK: -- Testing: 5 tests{{.*}}
# CHECK-LABEL: FAIL: shtest-readfile :: absolute-paths.txt ({{[^)]*}})
# CHECK: echo hello
-# CHECK: # executed command: echo '%{readfile:[[TEMP_PATH]]{{[\\\/]}}absolute-paths.txt.tmp}'
+# CHECK: # executed command: echo '%{readfile:{{.*}}absolute-paths.txt.tmp}'
# CHECK-LABEL: FAIL: shtest-readfile :: env.txt ({{[^)]*}})
# CHECK: env TEST=hello {{.*}} -c "import os; print(os.environ['TEST'])"
@@ -25,4 +25,4 @@
# CHECK-LABEL: FAIL: shtest-readfile :: two-same-line.txt ({{[^)]*}})
# CHECK: echo hello bye
-# CHECK: # executed command: echo '%{readfile:[[TEMP_PATH]]{{[\\\/]}}two-same-line.txt.tmp.1}' '%{readfile:[[TEMP_PATH]]{{[\\\/]}}two-same-line.txt.tmp.2}'
+# CHECK: # executed command: echo '%{readfile:{{.*}}two-same-line.txt.tmp.1}' '%{readfile:{{.*}}two-same-line.txt.tmp.2}'
diff --git a/llvm/utils/lit/tests/use-llvm-tool.py b/llvm/utils/lit/tests/use-llvm-tool.py
index 3e190a3e40f71..cfaa976ff1314 100644
--- a/llvm/utils/lit/tests/use-llvm-tool.py
+++ b/llvm/utils/lit/tests/use-llvm-tool.py
@@ -5,7 +5,7 @@
## 3) The PATH, if requested.
# RUN: %{lit} %{inputs}/use-llvm-tool 2>&1 | \
-# RUN: FileCheck %s -DDIR=%p
+# RUN: FileCheck %s
## The exact breakdown of cases is:
## Case | Env | Build Dir | PATH |
@@ -22,7 +22,7 @@
## Check the exact path reported for the first case, but don't bother for the
## others.
-# CHECK: note: using case1: [[DIR]]{{[\\/]}}Inputs{{[\\/]}}use-llvm-tool{{[\\/]}}env-case1
+# CHECK: note: using case1: {{.*}}{{[\\/]}}Inputs{{[\\/]}}use-llvm-tool{{[\\/]}}env-case1
# CHECK-NEXT: note: using case2: {{.*}}build{{[\\/]}}case2
# CHECK-NEXT: note: using case3: {{.*}}build{{[\\/]}}case3
# CHECK-NEXT: note: using case4: {{.*}}path{{[\\/]}}case4
>From 06fd9e6d5a1fe650f3b1fe2d6bf49ba763bf5155 Mon Sep 17 00:00:00 2001
From: Junji Watanabe <jwata at google.com>
Date: Thu, 19 Feb 2026 22:49:58 +0900
Subject: [PATCH 5/5] change the outputs of lit to be aligned with
substitutions
---
llvm/utils/lit/lit/LitConfig.py | 2 ++
llvm/utils/lit/lit/TestRunner.py | 23 ++++++++++++++++++-----
llvm/utils/lit/lit/llvm/config.py | 4 ++++
llvm/utils/lit/tests/lit.cfg | 13 ++++++++++---
llvm/utils/lit/tests/shtest-readfile.py | 6 +++---
llvm/utils/lit/tests/use-llvm-tool.py | 4 ++--
6 files changed, 39 insertions(+), 13 deletions(-)
diff --git a/llvm/utils/lit/lit/LitConfig.py b/llvm/utils/lit/lit/LitConfig.py
index 71dad85bbaddd..7e7ebb6f29534 100644
--- a/llvm/utils/lit/lit/LitConfig.py
+++ b/llvm/utils/lit/lit/LitConfig.py
@@ -216,6 +216,8 @@ def _write_message(self, kind, message):
# Step out of _write_message, and then out of wrapper.
f = f.f_back.f_back
file = os.path.abspath(inspect.getsourcefile(f))
+ if self.params.get("use_normalized_slashes", "0") == "1":
+ file = file.replace("\\", "/")
line = inspect.getlineno(f)
sys.stderr.write(
"%s: %s:%d: %s: %s\n" % (self.progname, file, line, kind, message)
diff --git a/llvm/utils/lit/lit/TestRunner.py b/llvm/utils/lit/lit/TestRunner.py
index 606f7f936ef51..1e9468a7ed644 100644
--- a/llvm/utils/lit/lit/TestRunner.py
+++ b/llvm/utils/lit/lit/TestRunner.py
@@ -91,12 +91,13 @@ class ShellEnvironment(object):
we maintain a dir stack for pushd/popd.
"""
- def __init__(self, cwd, env, umask=-1, ulimit=None):
+ def __init__(self, cwd, env, umask=-1, ulimit=None, normalize_slashes=False):
self.cwd = cwd
self.env = dict(env)
self.umask = umask
self.dirStack = []
self.ulimit = ulimit if ulimit else {}
+ self.normalize_slashes = normalize_slashes
def change_dir(self, newdir):
if os.path.isabs(newdir):
@@ -725,7 +726,7 @@ def processRedirects(cmd, stdin_source, cmd_shenv, opened_files):
return std_fds
-def _expandLateSubstitutions(cmd, arguments, cwd):
+def _expandLateSubstitutions(cmd, arguments, cwd, normalize_slashes=False):
for i, arg in enumerate(arguments):
if not isinstance(arg, str):
continue
@@ -734,6 +735,8 @@ def _replaceReadFile(match):
filePath = match.group(1)
if not os.path.isabs(filePath):
filePath = os.path.join(cwd, filePath)
+ if normalize_slashes:
+ filePath = filePath.replace("\\", "/")
try:
with open(filePath) as fileHandle:
return fileHandle.read()
@@ -817,7 +820,9 @@ def _executeShCmd(cmd, shenv, results, timeoutHelper):
not_crash = False
# Expand all late substitutions.
- args = _expandLateSubstitutions(j, args, cmd_shenv.cwd)
+ args = _expandLateSubstitutions(
+ j, args, cmd_shenv.cwd, cmd_shenv.normalize_slashes
+ )
while True:
if args[0] == "env":
@@ -828,7 +833,12 @@ def _executeShCmd(cmd, shenv, results, timeoutHelper):
# env FOO=1 llc < %s | env BAR=2 llvm-mc | FileCheck %s
# env FOO=1 %{another_env_plus_cmd} | FileCheck %s
if cmd_shenv is shenv:
- cmd_shenv = ShellEnvironment(shenv.cwd, shenv.env, shenv.umask)
+ cmd_shenv = ShellEnvironment(
+ shenv.cwd,
+ shenv.env,
+ shenv.umask,
+ normalize_slashes=shenv.normalize_slashes,
+ )
args = updateEnv(cmd_shenv, args)
if not args:
# Return the environment variables if no argument is provided.
@@ -1226,7 +1236,10 @@ def executeScriptInternal(
results = []
timeoutInfo = None
- shenv = ShellEnvironment(cwd, test.config.environment)
+ normalize_slashes = litConfig.params.get("use_normalized_slashes", False)
+ shenv = ShellEnvironment(
+ cwd, test.config.environment, normalize_slashes=normalize_slashes
+ )
shenv.env["LIT_CURRENT_TESTCASE"] = test.getFullName()
exitCode, timeoutInfo = executeShCmd(
diff --git a/llvm/utils/lit/lit/llvm/config.py b/llvm/utils/lit/lit/llvm/config.py
index 8b794c6978890..4ddc6adb75a83 100644
--- a/llvm/utils/lit/lit/llvm/config.py
+++ b/llvm/utils/lit/lit/llvm/config.py
@@ -57,6 +57,8 @@ def __init__(self, lit_config, config):
self.lit_config.diagnostic_level_enabled("note")
and lit_path_displayed is False
):
+ if self.lit_config.params.get("use_normalized_slashes", "0") == "1":
+ path = path.replace("\\", "/")
self.lit_config.note("using lit tools: {}".format(path))
lit_path_displayed = True
@@ -557,6 +559,8 @@ def use_llvm_tool(
if tool:
tool = os.path.normpath(tool)
+ if self.lit_config.params.get("use_normalized_slashes", "0") == "1":
+ tool = tool.replace("\\", "/")
if not quiet:
self.lit_config.note("using {}: {}".format(name, tool))
return tool
diff --git a/llvm/utils/lit/tests/lit.cfg b/llvm/utils/lit/tests/lit.cfg
index 922fbd6c9c872..a033bea9f4f36 100644
--- a/llvm/utils/lit/tests/lit.cfg
+++ b/llvm/utils/lit/tests/lit.cfg
@@ -64,14 +64,21 @@ for attribute in ("llvm_tools_dir", "lit_tools_dir"):
# within %{inputs}'s test suites. Thus, %{lit} clears environment variables
# that can affect FileCheck's output. It also includes "--order=lexical -j1"
# to ensure predictable test order, as it is often required for FileCheck
-# matches.
+# matches. It also specifies "-D use_normalized_slashes={0,1}" to be aligned
+# with the slashes in the substitutions (e.g, %t, %p) for Windows since it
+# switches between forward slashes and backslashes based on
+# LLVM_WINDOWS_PREFER_FORWARD_SLASH build config.
config.substitutions.append(("%{inputs}", "Inputs"))
config.substitutions.append(("%{lit}", "%{lit-no-order-opt} --order=lexical"))
+use_normalized_slashes = "0"
+if lit_config.params.get("use_normalized_slashes"):
+ use_normalized_slashes = "1"
config.substitutions.append(
(
"%{lit-no-order-opt}",
- "{env} %{{python}} {lit} -j1".format(
- env="env -u FILECHECK_OPTS", lit=os.path.join(lit_path, "lit.py")
+ "{env} %{{python}} {lit} -j1 -D use_normalized_slashes={use_normalized_slashes}".format(
+ env="env -u FILECHECK_OPTS", lit=os.path.join(lit_path, "lit.py"),
+ use_normalized_slashes=use_normalized_slashes,
),
)
)
diff --git a/llvm/utils/lit/tests/shtest-readfile.py b/llvm/utils/lit/tests/shtest-readfile.py
index 26ea2911f96f2..ca57db82e6617 100644
--- a/llvm/utils/lit/tests/shtest-readfile.py
+++ b/llvm/utils/lit/tests/shtest-readfile.py
@@ -3,13 +3,13 @@
# TODO(boomanaiden154): This sometimes fails, possibly due to buffers not being flushed.
# ALLOW_RETRIES: 2
-# RUN: env LIT_USE_INTERNAL_SHELL=1 not %{lit} -v %{inputs}/shtest-readfile | FileCheck -match-full-lines %s
+# RUN: env LIT_USE_INTERNAL_SHELL=1 not %{lit} -v %{inputs}/shtest-readfile | FileCheck -match-full-lines -DTEMP_PATH=%S%{fs-sep}Inputs%{fs-sep}shtest-readfile%{fs-sep}Output %s
# CHECK: -- Testing: 5 tests{{.*}}
# CHECK-LABEL: FAIL: shtest-readfile :: absolute-paths.txt ({{[^)]*}})
# CHECK: echo hello
-# CHECK: # executed command: echo '%{readfile:{{.*}}absolute-paths.txt.tmp}'
+# CHECK: # executed command: echo '%{readfile:[[TEMP_PATH]]{{[\\\/]}}absolute-paths.txt.tmp}'
# CHECK-LABEL: FAIL: shtest-readfile :: env.txt ({{[^)]*}})
# CHECK: env TEST=hello {{.*}} -c "import os; print(os.environ['TEST'])"
@@ -25,4 +25,4 @@
# CHECK-LABEL: FAIL: shtest-readfile :: two-same-line.txt ({{[^)]*}})
# CHECK: echo hello bye
-# CHECK: # executed command: echo '%{readfile:{{.*}}two-same-line.txt.tmp.1}' '%{readfile:{{.*}}two-same-line.txt.tmp.2}'
+# CHECK: # executed command: echo '%{readfile:[[TEMP_PATH]]{{[\\\/]}}two-same-line.txt.tmp.1}' '%{readfile:[[TEMP_PATH]]{{[\\\/]}}two-same-line.txt.tmp.2}'
diff --git a/llvm/utils/lit/tests/use-llvm-tool.py b/llvm/utils/lit/tests/use-llvm-tool.py
index cfaa976ff1314..3e190a3e40f71 100644
--- a/llvm/utils/lit/tests/use-llvm-tool.py
+++ b/llvm/utils/lit/tests/use-llvm-tool.py
@@ -5,7 +5,7 @@
## 3) The PATH, if requested.
# RUN: %{lit} %{inputs}/use-llvm-tool 2>&1 | \
-# RUN: FileCheck %s
+# RUN: FileCheck %s -DDIR=%p
## The exact breakdown of cases is:
## Case | Env | Build Dir | PATH |
@@ -22,7 +22,7 @@
## Check the exact path reported for the first case, but don't bother for the
## others.
-# CHECK: note: using case1: {{.*}}{{[\\/]}}Inputs{{[\\/]}}use-llvm-tool{{[\\/]}}env-case1
+# CHECK: note: using case1: [[DIR]]{{[\\/]}}Inputs{{[\\/]}}use-llvm-tool{{[\\/]}}env-case1
# CHECK-NEXT: note: using case2: {{.*}}build{{[\\/]}}case2
# CHECK-NEXT: note: using case3: {{.*}}build{{[\\/]}}case3
# CHECK-NEXT: note: using case4: {{.*}}path{{[\\/]}}case4
More information about the llvm-commits
mailing list