Skip to content

Commit 3dc7039

Browse files
authored
[lit] Fix shell commands with newlines (#67898)
In PR #65242 (landed as 9e739fd), I claimed that RUN lines cannot contain newlines. Actually, they can after substitution expansion. More generally, a lit config file can define substitutions or preamble commands containing newlines. While both of those cases seem unlikely in practice, [D154987](https://reviews.llvm.org/D154987) proposes PYTHON directives where it seems very likely. Regardless of the use case, without this patch, such newlines break expansion of `%dbg(RUN: at line N)`, and the fix is simple.
1 parent 9084934 commit 3dc7039

File tree

8 files changed

+42
-4
lines changed

8 files changed

+42
-4
lines changed

llvm/utils/lit/lit/TestRunner.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def __init__(self, command, message):
5555
#
5656
# COMMAND that follows %dbg(ARG) is also captured. COMMAND can be
5757
# empty as a result of conditinal substitution.
58-
kPdbgRegex = "%dbg\\(([^)'\"]*)\\)(.*)"
58+
kPdbgRegex = "%dbg\\(([^)'\"]*)\\)((?:.|\\n)*)"
5959

6060

6161
def buildPdbgCommand(msg, cmd):

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import lit
22

3-
preamble_commands = ['echo "THIS WAS"', 'echo "INJECTED"']
3+
# Check multiple commands, and check newlines.
4+
preamble_commands = ['echo "THIS WAS"', 'echo\n"INJECTED"']
45

56
config.name = "shtest-inject"
67
config.suffixes = [".txt"]
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11
import lit.formats
22

33
config.test_format = lit.formats.ShTest(execute_external=True)
4+
config.substitutions.append(("%{cmds-with-newlines}", """
5+
echo abc |
6+
FileCheck %s &&
7+
false
8+
"""))
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# RUN: %{cmds-with-newlines}
2+
# CHECK: abc
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11
import lit.formats
22

33
config.test_format = lit.formats.ShTest(execute_external=False)
4+
config.substitutions.append(("%{cmds-with-newlines}", """
5+
echo abc |
6+
FileCheck %s &&
7+
false
8+
"""))
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# RUN: %{cmds-with-newlines}
2+
# CHECK: abc

llvm/utils/lit/tests/shtest-inject.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
# CHECK-TEST1-NEXT: # | THIS WAS
1515
# CHECK-TEST1-NEXT: # `---{{-*}}
1616
# CHECK-TEST1-NEXT: # preamble command line
17-
# CHECK-TEST1-NEXT: echo "INJECTED"
17+
# CHECK-TEST1-NEXT: echo
18+
# CHECK-TEST1-NEXT: "INJECTED"
1819
# CHECK-TEST1-NEXT: # executed command: echo INJECTED
1920
# CHECK-TEST1-NEXT: # .---command stdout{{-*}}
2021
# CHECK-TEST1-NEXT: # | INJECTED

llvm/utils/lit/tests/shtest-run-at-line.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
# END.
88

99

10-
# CHECK: Testing: 6 tests
10+
# CHECK: Testing: 8 tests
1111

1212

1313
# In the case of the external shell, we check for only RUN lines in stderr in
@@ -48,6 +48,15 @@
4848
# CHECK-NOT: RUN
4949
# CHECK: --
5050

51+
# CHECK-LABEL: FAIL: shtest-run-at-line :: external-shell/run-line-with-newline.txt
52+
53+
# CHECK: Command Output (stderr)
54+
# CHECK-NEXT: --
55+
# CHECK-NEXT: {{^}}RUN: at line 1: echo abc |
56+
# CHECK-NEXT: FileCheck {{.*}} &&
57+
# CHECK-NEXT: false
58+
# CHECK-NOT: RUN
59+
5160

5261
# CHECK-LABEL: FAIL: shtest-run-at-line :: internal-shell/basic.txt
5362

@@ -87,3 +96,16 @@
8796
# CHECK-NEXT: # executed command: echo 'foo baz'
8897
# CHECK-NEXT: # executed command: FileCheck {{.*}}
8998
# CHECK-NOT: RUN
99+
100+
# CHECK-LABEL: FAIL: shtest-run-at-line :: internal-shell/run-line-with-newline.txt
101+
102+
# CHECK: Command Output (stdout)
103+
# CHECK-NEXT: --
104+
# CHECK-NEXT: # RUN: at line 1
105+
# CHECK-NEXT: echo abc |
106+
# CHECK-NEXT: FileCheck {{.*}} &&
107+
# CHECK-NEXT: false
108+
# CHECK-NEXT: # executed command: echo abc
109+
# CHECK-NEXT: # executed command: FileCheck {{.*}}
110+
# CHECK-NEXT: # executed command: false
111+
# CHECK-NOT: RUN

0 commit comments

Comments
 (0)