Martin Storsjö 51ca2354d0
[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
7c9eab8fef0ed79a5911d21eb97b6b0fa9d39f82. 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.
2024-08-22 12:57:39 +03:00

57 lines
2.8 KiB
INI

import lit.formats
config.name = "shtest-define"
config.suffixes = [".txt"]
# Use lit's internal shell to avoid shell portability issues within RUN lines
# (e.g., for 'echo' commands in Windows). Those issues should be orthogonal to
# the substitution behavior we are trying to test.
config.test_format = lit.formats.ShTest(execute_external=False)
config.test_source_root = None
config.test_exec_root = None
# When config.recursiveExpansionLimit is not specified, it's important to
# prepend substitutions before substitutions they might now or later (upon a
# redefinition) depend upon. For example, %{global:greeting} and %{global:what}
# act as parameters for %{global:echo}, so we make sure the latter expands
# before the former. Moreover, some tests redefine %{global:greeting} in terms
# of %{global:what}, so we make sure the former expands before the latter.
# If we always insert at the beginning of the substitution list (as DEFINE
# does), then the rule is simple: define a substitution before you refer to it.
config.substitutions.insert(0, ("%{global:what}", "World"))
config.substitutions.insert(0, ("%{global:greeting}", ""))
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.
# Even though each of '%{global:inside}', '%{global:prefix}', and
# '%{global:suffix}' is not already the exact pattern of a substitution,
# 'DEFINE:' and 'REDEFINE:' will refuse to (re)define a substitution with that
# pattern because it is a substring of one of the following substitution's
# patterns.
config.substitutions.insert(0, ("<%{global:inside}>", "<@>"))
config.substitutions.insert(0, (r"%{global:prefix}\((.*)\)", r"@(\g<1>)"))
config.substitutions.insert(0, ("@%{global:suffix}", "@@"))
# These cannot be redefined by 'REDEFINE:', which doesn't know which one to
# redefine.
config.substitutions.insert(0, ("%{global:multiple-exact}", "first"))
config.substitutions.insert(0, ("%{global:multiple-exact}", "second"))
# Even though '%{global:multiple-once-exact}' is the exact pattern of only one
# existing substitution, 'REDEFINE:' will refuse to redefine that substitution
# because that string is a substring of another substitution's pattern.
config.substitutions.insert(0, ("%{global:multiple-once-exact}", "@"))
config.substitutions.insert(0, ("<%{global:multiple-once-exact}>", "<@>"))
recur = lit_config.params.get("recur", None)
if recur:
config.recursiveExpansionLimit = int(recur)