Skip to content

[lit] Fix substitutions containing backslashes #103042

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions llvm/docs/TestingGuide.rst
Original file line number Diff line number Diff line change
Expand Up @@ -864,8 +864,9 @@ Additional substitutions can be defined as follows:
- Lit configuration files (e.g., ``lit.cfg`` or ``lit.local.cfg``) can define
substitutions for all tests in a test directory. They do so by extending the
substitution list, ``config.substitutions``. Each item in the list is a tuple
consisting of a pattern and its replacement, which lit applies using python's
``re.sub`` function.
consisting of a pattern and its replacement, which lit applies as plain text
(even if it contains sequences that python's ``re.sub`` considers to be
escape sequences).
- To define substitutions within a single test file, lit supports the
``DEFINE:`` and ``REDEFINE:`` directives, described in detail below. So that
they have no effect on other test files, these directives modify a copy of the
Expand Down
8 changes: 3 additions & 5 deletions llvm/utils/lit/lit/TestRunner.py
Original file line number Diff line number Diff line change
Expand Up @@ -1587,7 +1587,6 @@ def adjust_substitutions(self, substitutions):
assert (
not self.needs_continuation()
), "expected directive continuations to be parsed before applying"
value_repl = self.value.replace("\\", "\\\\")
existing = [i for i, subst in enumerate(substitutions) if self.name in subst[0]]
existing_res = "".join(
"\nExisting pattern: " + substitutions[i][0] for i in existing
Expand All @@ -1600,7 +1599,7 @@ def adjust_substitutions(self, substitutions):
f"{self.get_location()}"
f"{existing_res}"
)
substitutions.insert(0, (self.name, value_repl))
substitutions.insert(0, (self.name, self.value))
return
if len(existing) > 1:
raise ValueError(
Expand All @@ -1622,7 +1621,7 @@ def adjust_substitutions(self, substitutions):
f"Expected pattern: {self.name}"
f"{existing_res}"
)
substitutions[existing[0]] = (self.name, value_repl)
substitutions[existing[0]] = (self.name, self.value)


def applySubstitutions(script, substitutions, conditions={}, recursion_limit=None):
Expand Down Expand Up @@ -1738,8 +1737,7 @@ def processLine(ln):
# Apply substitutions
ln = substituteIfElse(escapePercents(ln))
for a, b in substitutions:
if kIsWindows:
b = b.replace("\\", "\\\\")
b = b.replace("\\", "\\\\")
# re.compile() has a built-in LRU cache with 512 entries. In some
# test suites lit ends up thrashing that cache, which made e.g.
# check-llvm run 50% slower. Use an explicit, unbounded cache
Expand Down
4 changes: 4 additions & 0 deletions llvm/utils/lit/tests/Inputs/shtest-define/lit.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ config.substitutions.insert(
0, ("%{global:echo}", "echo GLOBAL: %{global:greeting} %{global:what}")
)

# This substitution includes an re.sub replacement string escape sequence,
# which lit should treat as plain text.
config.substitutions.insert(0, ("%{global:subst-with-escapes}", r"value-with-\g"))

# The following substitution definitions are confusing and should be avoided.
# We define them here so we can test that 'DEFINE:' and 'REDEFINE:' directives
# guard against the confusion they cause.
Expand Down
10 changes: 5 additions & 5 deletions llvm/utils/lit/tests/Inputs/shtest-define/value-escaped.txt
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
# FIXME: The doubled backslashes occur under windows. That's almost surely a
# lit issue beyond DEFINE/REDEFINE.

# Escape sequences that can appear in python re.sub replacement strings have no
# special meaning in the value.

# DEFINE: %{escape} = \g<0>\n
# RUN: echo '%{escape}'
# CHECK:# | {{\\?}}\g<0>{{\\?}}\n
# CHECK:# | \g<0>\n

# REDEFINE: %{escape} = \n \
# REDEFINE: \g<param>
# RUN: echo '%{escape}'
# CHECK:# | {{\\?}}\n {{\\?}}\g<param>
# CHECK:# | \n \g<param>

# RUN: echo '%{global:subst-with-escapes}'
# CHECK:# | value-with-\g

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