Skip to content

Commit 51ca235

Browse files
authored
[lit] Fix substitutions containing backslashes (#103042)
Substitutions can be added in a couple different ways; they can be added via the calling python scripts by adding entries to the config.substitutions dictionary, or via DEFINE lines in the scripts themselves. The substitution strings passed to Python's re classes are interpreted so that backslashes expand to escape sequences, and literal backslashes need to be escaped. On Unix, the script defined substitutions don't (usually, so far) contain backslashes - but on Windows, they often do, due to paths containing backslashes. This lead to a Windows specific escaping of backslashes before doing Python re substitutions - since 7c9eab8. There's nothing inherently Windows specific about this though - any intended literal backslashes in the substitution strings need to be escaped; this is how the Python re API works. The DEFINE lines were added later, and in order to cope with backslashes, escaping of backslashes was added in the SubstDirective class in TestRunner, applying to DEFINE lines in the tests only. The fact that the escaping right before passing to the Python re API was done conditionally on Windows led to two inconsistencies: - DEFINE lines in the tests that contain backslashes got double backslashes on Windows. (This was visible as a FIXME in llvm/utils/lit/tests/Inputs/shtest-define/value-escaped.txt.) - Script provided substitutions containing backslashes did not work on Unix, but they did work on Windows. By removing the escaping from SubstDirective and escaping it unconditionally in the processLine function, before feeding the substitutions to Python's re classes, we should have consistent behaviour across platforms, and get rid of the FIXME in the lit test. This fixes issues with substitutions containing backslashes on Unix platforms, as encountered in PR #86649.
1 parent 57dc093 commit 51ca235

File tree

4 files changed

+15
-12
lines changed

4 files changed

+15
-12
lines changed

llvm/docs/TestingGuide.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -864,8 +864,9 @@ Additional substitutions can be defined as follows:
864864
- Lit configuration files (e.g., ``lit.cfg`` or ``lit.local.cfg``) can define
865865
substitutions for all tests in a test directory. They do so by extending the
866866
substitution list, ``config.substitutions``. Each item in the list is a tuple
867-
consisting of a pattern and its replacement, which lit applies using python's
868-
``re.sub`` function.
867+
consisting of a pattern and its replacement, which lit applies as plain text
868+
(even if it contains sequences that python's ``re.sub`` considers to be
869+
escape sequences).
869870
- To define substitutions within a single test file, lit supports the
870871
``DEFINE:`` and ``REDEFINE:`` directives, described in detail below. So that
871872
they have no effect on other test files, these directives modify a copy of the

llvm/utils/lit/lit/TestRunner.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1591,7 +1591,6 @@ def adjust_substitutions(self, substitutions):
15911591
assert (
15921592
not self.needs_continuation()
15931593
), "expected directive continuations to be parsed before applying"
1594-
value_repl = self.value.replace("\\", "\\\\")
15951594
existing = [i for i, subst in enumerate(substitutions) if self.name in subst[0]]
15961595
existing_res = "".join(
15971596
"\nExisting pattern: " + substitutions[i][0] for i in existing
@@ -1604,7 +1603,7 @@ def adjust_substitutions(self, substitutions):
16041603
f"{self.get_location()}"
16051604
f"{existing_res}"
16061605
)
1607-
substitutions.insert(0, (self.name, value_repl))
1606+
substitutions.insert(0, (self.name, self.value))
16081607
return
16091608
if len(existing) > 1:
16101609
raise ValueError(
@@ -1626,7 +1625,7 @@ def adjust_substitutions(self, substitutions):
16261625
f"Expected pattern: {self.name}"
16271626
f"{existing_res}"
16281627
)
1629-
substitutions[existing[0]] = (self.name, value_repl)
1628+
substitutions[existing[0]] = (self.name, self.value)
16301629

16311630

16321631
def applySubstitutions(script, substitutions, conditions={}, recursion_limit=None):
@@ -1742,8 +1741,7 @@ def processLine(ln):
17421741
# Apply substitutions
17431742
ln = substituteIfElse(escapePercents(ln))
17441743
for a, b in substitutions:
1745-
if kIsWindows:
1746-
b = b.replace("\\", "\\\\")
1744+
b = b.replace("\\", "\\\\")
17471745
# re.compile() has a built-in LRU cache with 512 entries. In some
17481746
# test suites lit ends up thrashing that cache, which made e.g.
17491747
# check-llvm run 50% slower. Use an explicit, unbounded cache

llvm/utils/lit/tests/Inputs/shtest-define/lit.cfg

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ config.substitutions.insert(
2323
0, ("%{global:echo}", "echo GLOBAL: %{global:greeting} %{global:what}")
2424
)
2525

26+
# This substitution includes an re.sub replacement string escape sequence,
27+
# which lit should treat as plain text.
28+
config.substitutions.insert(0, ("%{global:subst-with-escapes}", r"value-with-\g"))
29+
2630
# The following substitution definitions are confusing and should be avoided.
2731
# We define them here so we can test that 'DEFINE:' and 'REDEFINE:' directives
2832
# guard against the confusion they cause.
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
# FIXME: The doubled backslashes occur under windows. That's almost surely a
2-
# lit issue beyond DEFINE/REDEFINE.
3-
41
# Escape sequences that can appear in python re.sub replacement strings have no
52
# special meaning in the value.
63

74
# DEFINE: %{escape} = \g<0>\n
85
# RUN: echo '%{escape}'
9-
# CHECK:# | {{\\?}}\g<0>{{\\?}}\n
6+
# CHECK:# | \g<0>\n
107

118
# REDEFINE: %{escape} = \n \
129
# REDEFINE: \g<param>
1310
# RUN: echo '%{escape}'
14-
# CHECK:# | {{\\?}}\n {{\\?}}\g<param>
11+
# CHECK:# | \n \g<param>
12+
13+
# RUN: echo '%{global:subst-with-escapes}'
14+
# CHECK:# | value-with-\g
1515

1616
# CHECK: Passed: 1 {{\([0-9]*.[0-9]*%\)}}

0 commit comments

Comments
 (0)