Debian Codegraph @

external/llvm/releases/14.0.0/patches_external/support-for-if-expressions-lit.patch

raw
1 /*========================== begin_copyright_notice ============================
2
3 Copyright (C) 2024 Intel Corporation
4
5 SPDX-License-Identifier: MIT
6
7 ============================= end_copyright_notice ===========================*/
8
9 /*========================== begin_copyright_notice ============================
10
11 Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
12 See https://llvm.org/LICENSE.txt for license information.
13 SPDX-License-Identifier: Apache-2.0 with LLVM-exception
14
15 ============================= end_copyright_notice ===========================*/
16
17 From 1041a9642ba035fd2685f925911d705e8edf5bb0 Mon Sep 17 00:00:00 2001
18 From: Andrew Savonichev <[email protected]>
19 Date: Wed, 27 Apr 2022 19:36:54 +0300
20 Subject: [PATCH] [lit] Support %if ... %else syntax for RUN lines
21
22 This syntax allows to modify RUN lines based on features
23 available. For example:
24
25 RUN: ... | FileCheck %s --check-prefix=%if windows %{CHECK-W%} %else %{CHECK-NON-W%}
26 CHECK-W: ...
27 CHECK-NON-W: ...
28
29 The whole command can be put under %if ... %else:
30
31 RUN: %if tool_available %{ %tool %} %else %{ true %}
32
33 or:
34
35 RUN: %if tool_available %{ %tool %}
36
37 If tool_available feature is missing, we'll have an empty command in
38 this RUN line. LIT used to emit an error for empty commands, but now
39 it treats such commands as nop in all cases.
40
41 Multi-line expressions are also supported:
42
43 RUN: %if tool_available %{ \
44 RUN: %tool \
45 RUN: %} %else %{ \
46 RUN: true \
47 RUN: %}
48
49 Background and motivation:
50 D121727 [NVPTX] Integrate ptxas to LIT tests
51 https://reviews.llvm.org/D121727
52
53 Differential Revision: https://reviews.llvm.org/D122569
54 ---
55 llvm/docs/TestingGuide.rst | 7 +
56 llvm/utils/lit/lit/TestRunner.py | 127 ++++++++++++++++--
57 .../lit/tests/Inputs/shtest-if-else/lit.cfg | 8 ++
58 .../tests/Inputs/shtest-if-else/test-neg1.txt | 3 +
59 .../tests/Inputs/shtest-if-else/test-neg2.txt | 3 +
60 .../tests/Inputs/shtest-if-else/test-neg3.txt | 3 +
61 .../tests/Inputs/shtest-if-else/test-neg4.txt | 3 +
62 .../lit/tests/Inputs/shtest-if-else/test.txt | 92 +++++++++++++
63 llvm/utils/lit/tests/shtest-if-else.py | 14 ++
64 9 files changed, 250 insertions(+), 10 deletions(-)
65 create mode 100644 llvm/utils/lit/tests/Inputs/shtest-if-else/lit.cfg
66 create mode 100644 llvm/utils/lit/tests/Inputs/shtest-if-else/test-neg1.txt
67 create mode 100644 llvm/utils/lit/tests/Inputs/shtest-if-else/test-neg2.txt
68 create mode 100644 llvm/utils/lit/tests/Inputs/shtest-if-else/test-neg3.txt
69 create mode 100644 llvm/utils/lit/tests/Inputs/shtest-if-else/test-neg4.txt
70 create mode 100644 llvm/utils/lit/tests/Inputs/shtest-if-else/test.txt
71 create mode 100644 llvm/utils/lit/tests/shtest-if-else.py
72
73 diff --git a/llvm/docs/TestingGuide.rst b/llvm/docs/TestingGuide.rst
74 index 04d9ccc5ba98f..4bbc0972cdafb 100644
75 --- a/llvm/docs/TestingGuide.rst
76 +++ b/llvm/docs/TestingGuide.rst
77 @@ -612,6 +612,13 @@ RUN lines:
78
79 Example: ``Windows %errc_ENOENT: no such file or directory``
80
81 +``%if feature %{<if branch>%} %else %{<else branch>%}``
82 +
83 + Conditional substitution: if ``feature`` is available it expands to
84 + ``<if branch>``, otherwise it expands to ``<else branch>``.
85 + ``%else %{<else branch>%}`` is optional and treated like ``%else %{%}``
86 + if not present.
87 +
88 **LLVM-specific substitutions:**
89
90 ``%shlibext``
91 diff --git a/llvm/utils/lit/lit/TestRunner.py b/llvm/utils/lit/lit/TestRunner.py
92 index d3e655a324e42..de711b4eec2ae 100644
93 --- a/llvm/utils/lit/lit/TestRunner.py
94 +++ b/llvm/utils/lit/lit/TestRunner.py
95 @@ -48,7 +48,10 @@ def __init__(self, command, message):
96 # This regex captures ARG. ARG must not contain a right parenthesis, which
97 # terminates %dbg. ARG must not contain quotes, in which ARG might be enclosed
98 # during expansion.
99 -kPdbgRegex = '%dbg\\(([^)\'"]*)\\)'
100 +#
101 +# COMMAND that follows %dbg(ARG) is also captured. COMMAND can be
102 +# empty as a result of conditinal substitution.
103 +kPdbgRegex = '%dbg\\(([^)\'"]*)\\)(.*)'
104
105 class ShellEnvironment(object):
106
107 @@ -899,7 +902,11 @@ def _executeShCmd(cmd, shenv, results, timeoutHelper):
108 def executeScriptInternal(test, litConfig, tmpBase, commands, cwd):
109 cmds = []
110 for i, ln in enumerate(commands):
111 - ln = commands[i] = re.sub(kPdbgRegex, ": '\\1'; ", ln)
112 + match = re.match(kPdbgRegex, ln)
113 + if match:
114 + command = match.group(2)
115 + ln = commands[i] = \
116 + match.expand(": '\\1'; \\2" if command else ": '\\1'")
117 try:
118 cmds.append(ShUtil.ShParser(ln, litConfig.isWindows,
119 test.config.pipefail).parse())
120 @@ -987,7 +994,12 @@ def executeScript(test, litConfig, tmpBase, commands, cwd):
121 f = open(script, mode, **open_kwargs)
122 if isWin32CMDEXE:
123 for i, ln in enumerate(commands):
124 - commands[i] = re.sub(kPdbgRegex, "echo '\\1' > nul && ", ln)
125 + match = re.match(kPdbgRegex, ln)
126 + if match:
127 + command = match.group(2)
128 + commands[i] = \
129 + match.expand("echo '\\1' > nul && " if command
130 + else "echo '\\1' > nul")
131 if litConfig.echo_all_commands:
132 f.write('@echo on\n')
133 else:
134 @@ -995,7 +1007,11 @@ def executeScript(test, litConfig, tmpBase, commands, cwd):
135 f.write('\n@if %ERRORLEVEL% NEQ 0 EXIT\n'.join(commands))
136 else:
137 for i, ln in enumerate(commands):
138 - commands[i] = re.sub(kPdbgRegex, ": '\\1'; ", ln)
139 + match = re.match(kPdbgRegex, ln)
140 + if match:
141 + command = match.group(2)
142 + commands[i] = match.expand(": '\\1'; \\2" if command
143 + else ": '\\1'")
144 if test.config.pipefail:
145 f.write(b'set -o pipefail;' if mode == 'wb' else 'set -o pipefail;')
146 if litConfig.echo_all_commands:
147 @@ -1179,7 +1195,8 @@ def memoized(x):
148 def _caching_re_compile(r):
149 return re.compile(r)
150
151 -def applySubstitutions(script, substitutions, recursion_limit=None):
152 +def applySubstitutions(script, substitutions, conditions={},
153 + recursion_limit=None):
154 """
155 Apply substitutions to the script. Allow full regular expression syntax.
156 Replace each matching occurrence of regular expression pattern a with
157 @@ -1193,14 +1210,103 @@ def applySubstitutions(script, substitutions, recursion_limit=None):
158 """
159
160 # We use #_MARKER_# to hide %% while we do the other substitutions.
161 - def escape(ln):
162 + def escapePercents(ln):
163 return _caching_re_compile('%%').sub('#_MARKER_#', ln)
164
165 - def unescape(ln):
166 + def unescapePercents(ln):
167 return _caching_re_compile('#_MARKER_#').sub('%', ln)
168
169 + def substituteIfElse(ln):
170 + # early exit to avoid wasting time on lines without
171 + # conditional substitutions
172 + if ln.find('%if ') == -1:
173 + return ln
174 +
175 + def tryParseIfCond(ln):
176 + # space is important to not conflict with other (possible)
177 + # substitutions
178 + if not ln.startswith('%if '):
179 + return None, ln
180 + ln = ln[4:]
181 +
182 + # stop at '%{'
183 + match = _caching_re_compile('%{').search(ln)
184 + if not match:
185 + raise ValueError("'%{' is missing for %if substitution")
186 + cond = ln[:match.start()]
187 +
188 + # eat '%{' as well
189 + ln = ln[match.end():]
190 + return cond, ln
191 +
192 + def tryParseElse(ln):
193 + match = _caching_re_compile('^\s*%else\s*(%{)?').search(ln)
194 + if not match:
195 + return False, ln
196 + if not match.group(1):
197 + raise ValueError("'%{' is missing for %else substitution")
198 + return True, ln[match.end():]
199 +
200 + def tryParseEnd(ln):
201 + if ln.startswith('%}'):
202 + return True, ln[2:]
203 + return False, ln
204 +
205 + def parseText(ln, isNested):
206 + # parse everything until %if, or %} if we're parsing a
207 + # nested expression.
208 + match = _caching_re_compile(
209 + '(.*?)(?:%if|%})' if isNested else '(.*?)(?:%if)').search(ln)
210 + if not match:
211 + # there is no terminating pattern, so treat the whole
212 + # line as text
213 + return ln, ''
214 + text_end = match.end(1)
215 + return ln[:text_end], ln[text_end:]
216 +
217 + def parseRecursive(ln, isNested):
218 + result = ''
219 + while len(ln):
220 + if isNested:
221 + found_end, _ = tryParseEnd(ln)
222 + if found_end:
223 + break
224 +
225 + # %if cond %{ branch_if %} %else %{ branch_else %}
226 + cond, ln = tryParseIfCond(ln)
227 + if cond:
228 + branch_if, ln = parseRecursive(ln, isNested=True)
229 + found_end, ln = tryParseEnd(ln)
230 + if not found_end:
231 + raise ValueError("'%}' is missing for %if substitution")
232 +
233 + branch_else = ''
234 + found_else, ln = tryParseElse(ln)
235 + if found_else:
236 + branch_else, ln = parseRecursive(ln, isNested=True)
237 + found_end, ln = tryParseEnd(ln)
238 + if not found_end:
239 + raise ValueError("'%}' is missing for %else substitution")
240 +
241 + if BooleanExpression.evaluate(cond, conditions):
242 + result += branch_if
243 + else:
244 + result += branch_else
245 + continue
246 +
247 + # The rest is handled as plain text.
248 + text, ln = parseText(ln, isNested)
249 + result += text
250 +
251 + return result, ln
252 +
253 + result, ln = parseRecursive(ln, isNested=False)
254 + assert len(ln) == 0
255 + return result
256 +
257 def processLine(ln):
258 # Apply substitutions
259 + ln = substituteIfElse(escapePercents(ln))
260 for a,b in substitutions:
261 if kIsWindows:
262 b = b.replace("\\","\\\\")
263 @@ -1211,7 +1317,7 @@ def processLine(ln):
264 # short-lived, since the set of substitutions is fairly small, and
265 # since thrashing has such bad consequences, not bounding the cache
266 # seems reasonable.
267 - ln = _caching_re_compile(a).sub(str(b), escape(ln))
268 + ln = _caching_re_compile(a).sub(str(b), escapePercents(ln))
269
270 # Strip the trailing newline and any extra whitespace.
271 return ln.strip()
272 @@ -1235,7 +1341,7 @@ def processLineToFixedPoint(ln):
273
274 process = processLine if recursion_limit is None else processLineToFixedPoint
275
276 - return [unescape(process(ln)) for ln in script]
277 + return [unescapePercents(process(ln)) for ln in script]
278
279
280 class ParserKind(object):
281 @@ -1610,7 +1716,8 @@ def executeShTest(test, litConfig, useExternalSh,
282 substitutions = list(extra_substitutions)
283 substitutions += getDefaultSubstitutions(test, tmpDir, tmpBase,
284 normalize_slashes=useExternalSh)
285 - script = applySubstitutions(script, substitutions,
286 + conditions = { feature: True for feature in test.config.available_features }
287 + script = applySubstitutions(script, substitutions, conditions,
288 recursion_limit=test.config.recursiveExpansionLimit)
289
290 return _runShTest(test, litConfig, useExternalSh, script, tmpBase)
291 diff --git a/llvm/utils/lit/tests/Inputs/shtest-if-else/lit.cfg b/llvm/utils/lit/tests/Inputs/shtest-if-else/lit.cfg
292 new file mode 100644
293 index 0000000000000..b2243df51c20c
294 --- /dev/null
295 +++ b/llvm/utils/lit/tests/Inputs/shtest-if-else/lit.cfg
296 @@ -0,0 +1,8 @@
297 +import lit.formats
298 +config.name = 'shtest-if-else'
299 +config.test_format = lit.formats.ShTest()
300 +config.test_source_root = None
301 +config.test_exec_root = None
302 +config.suffixes = ['.txt']
303 +config.available_features.add('feature')
304 +config.substitutions.append(('%{sub}', 'ok'))
305 diff --git a/llvm/utils/lit/tests/Inputs/shtest-if-else/test-neg1.txt b/llvm/utils/lit/tests/Inputs/shtest-if-else/test-neg1.txt
306 new file mode 100644
307 index 0000000000000..ce748526979bb
308 --- /dev/null
309 +++ b/llvm/utils/lit/tests/Inputs/shtest-if-else/test-neg1.txt
310 @@ -0,0 +1,3 @@
311 +# CHECK: ValueError: '%{' is missing for %if substitution
312 +#
313 +# RUN: %if feature echo "test-1"
314 diff --git a/llvm/utils/lit/tests/Inputs/shtest-if-else/test-neg2.txt b/llvm/utils/lit/tests/Inputs/shtest-if-else/test-neg2.txt
315 new file mode 100644
316 index 0000000000000..ae7ad887a06fb
317 --- /dev/null
318 +++ b/llvm/utils/lit/tests/Inputs/shtest-if-else/test-neg2.txt
319 @@ -0,0 +1,3 @@
320 +# CHECK: ValueError: '%}' is missing for %if substitution
321 +#
322 +# RUN: %if feature %{ echo
323 diff --git a/llvm/utils/lit/tests/Inputs/shtest-if-else/test-neg3.txt b/llvm/utils/lit/tests/Inputs/shtest-if-else/test-neg3.txt
324 new file mode 100644
325 index 0000000000000..ed6594c238276
326 --- /dev/null
327 +++ b/llvm/utils/lit/tests/Inputs/shtest-if-else/test-neg3.txt
328 @@ -0,0 +1,3 @@
329 +# CHECK: ValueError: '%{' is missing for %else substitution
330 +#
331 +# RUN: %if feature %{ echo %} %else fail
332 diff --git a/llvm/utils/lit/tests/Inputs/shtest-if-else/test-neg4.txt b/llvm/utils/lit/tests/Inputs/shtest-if-else/test-neg4.txt
333 new file mode 100644
334 index 0000000000000..0ee85f2df2ed2
335 --- /dev/null
336 +++ b/llvm/utils/lit/tests/Inputs/shtest-if-else/test-neg4.txt
337 @@ -0,0 +1,3 @@
338 +# CHECK: ValueError: '%}' is missing for %else substitution
339 +#
340 +# RUN: %if feature %{ echo %} %else %{ fail
341 diff --git a/llvm/utils/lit/tests/Inputs/shtest-if-else/test.txt b/llvm/utils/lit/tests/Inputs/shtest-if-else/test.txt
342 new file mode 100644
343 index 0000000000000..805a74de3a7ee
344 --- /dev/null
345 +++ b/llvm/utils/lit/tests/Inputs/shtest-if-else/test.txt
346 @@ -0,0 +1,92 @@
347 +# CHECK: -- Testing:{{.*}}
348 +# CHECK-NEXT: PASS: shtest-if-else :: test.txt (1 of 1)
349 +# CHECK-NEXT: Script:
350 +# CHECK-NEXT: --
351 +
352 +# RUN: %if feature %{ echo "test-1" %}
353 +# CHECK-NEXT: {{^.*'RUN}}: at line [[#@LINE-1]]'; echo "test-1"
354 +
355 +# If %else is not present it is treated like %else %{%}. Empty commands
356 +# are ignored.
357 +#
358 +# RUN: %if nofeature %{ echo "fail" %}
359 +# CHECK-NEXT: {{^.*'RUN}}: at line [[#@LINE-1]]'
360 +# CHECK-NOT: fail
361 +
362 +# RUN: %if nofeature %{ echo "fail" %} %else %{ echo "test-2" %}
363 +# CHECK-NEXT: {{^.*'RUN}}: at line [[#@LINE-1]]'; echo "test-2"
364 +
365 +# Spaces inside curly braces are not ignored
366 +#
367 +# RUN: echo test-%if feature %{ 3 %} %else %{ fail %}-test
368 +# RUN: echo test-%if feature %{ 4 4 %} %else %{ fail %}-test
369 +# RUN: echo test-%if nofeature %{ fail %} %else %{ 5 5 %}-test
370 +# CHECK-NEXT: {{^.*'RUN}}: at line [[#@LINE-3]]'; echo test- 3 -test
371 +# CHECK-NEXT: {{^.*'RUN}}: at line [[#@LINE-3]]'; echo test- 4 4 -test
372 +# CHECK-NEXT: {{^.*'RUN}}: at line [[#@LINE-3]]'; echo test- 5 5 -test
373 +
374 +# Escape line breaks for multi-line expressions
375 +#
376 +# RUN: %if feature \
377 +# RUN: %{ echo \
378 +# RUN: "test-5" \
379 +# RUN: %}
380 +# CHECK-NEXT: {{^.*'RUN}}: at line [[#@LINE-4]]'; echo "test-5"
381 +
382 +# RUN: %if nofeature \
383 +# RUN: %{ echo "fail" %} \
384 +# RUN: %else \
385 +# RUN: %{ echo "test-6" %}
386 +# CHECK-NEXT: {{^.*'RUN}}: at line [[#@LINE-4]]'; echo "test-6"
387 +
388 +# RUN: echo "test%if feature %{%} %else %{%}-7"
389 +# CHECK-NEXT: {{^.*'RUN}}: at line [[#@LINE-1]]'; echo "test-7"
390 +
391 +# Escape %if. Without %if..%else context '%{' and '%}' are treated
392 +# literally.
393 +#
394 +# RUN: echo %%if feature %{ echo "test-8" %}
395 +# CHECK-NEXT: {{^.*'RUN}}: at line [[#@LINE-1]]'; echo %if feature %{ echo "test-8" %}
396 +
397 +# Nested expressions are supported:
398 +#
399 +# RUN: echo %if feature %{ %if feature %{ %if nofeature %{"fail"%} %else %{"test-9"%} %} %}
400 +# CHECK-NEXT: {{^.*'RUN}}: at line [[#@LINE-1]]'; echo "test-9"
401 +
402 +# Binary expression evaluation and regex match can be used as
403 +# conditions.
404 +#
405 +# RUN: echo %if feature && !nofeature %{ "test-10" %}
406 +# RUN: echo %if feature && nofeature %{ "fail" %} %else %{ "test-11" %}
407 +# RUN: echo %if {{fea.+}} %{ "test-12" %} %else %{ "fail" %}
408 +# CHECK-NEXT: {{^.*'RUN}}: at line [[#@LINE-3]]'; echo "test-10"
409 +# CHECK-NEXT: {{^.*'RUN}}: at line [[#@LINE-3]]'; echo "test-11"
410 +# CHECK-NEXT: {{^.*'RUN}}: at line [[#@LINE-3]]'; echo "test-12"
411 +
412 +# Spaces between %if and %else are ignored. If there is no %else -
413 +# space after %if %{...%} is not ignored.
414 +#
415 +# RUN: echo XX %if feature %{YY%} ZZ
416 +# RUN: echo AA %if feature %{BB%} %else %{CC%} DD
417 +# RUN: echo AA %if nofeature %{BB%} %else %{CC%} DD
418 +# CHECK-NEXT: {{^.*'RUN}}: at line [[#@LINE-3]]'; echo XX YY ZZ
419 +# CHECK-NEXT: {{^.*'RUN}}: at line [[#@LINE-3]]'; echo AA BB DD
420 +# CHECK-NEXT: {{^.*'RUN}}: at line [[#@LINE-3]]'; echo AA CC DD
421 +
422 +# '{' and '}' can be used without escaping
423 +#
424 +# RUN: %if feature %{echo {}%}
425 +# CHECK-NEXT: {{^.*'RUN}}: at line [[#@LINE-1]]'; echo {}
426 +
427 +# Spaces are not required
428 +#
429 +# RUN: echo %if feature%{"ok"%}%else%{"fail"%}
430 +# CHECK-NEXT: {{^.*'RUN}}: at line [[#@LINE-1]]'; echo "ok"
431 +
432 +# Substitutions with braces are handled correctly
433 +#
434 +# RUN: echo %{sub} %if feature%{test-%{sub}%}%else%{"fail"%}
435 +# CHECK-NEXT: {{^.*'RUN}}: at line [[#@LINE-1]]'; echo ok test-ok
436 +
437 +# CHECK-NEXT: --
438 +# CHECK-NEXT: Exit Code: 0
439 diff --git a/llvm/utils/lit/tests/shtest-if-else.py b/llvm/utils/lit/tests/shtest-if-else.py
440 new file mode 100644
441 index 0000000000000..aaf94a6e24372
442 --- /dev/null
443 +++ b/llvm/utils/lit/tests/shtest-if-else.py
444 @@ -0,0 +1,14 @@
445 +# RUN: %{lit} -v --show-all %{inputs}/shtest-if-else/test.txt \
446 +# RUN: | FileCheck %{inputs}/shtest-if-else/test.txt --match-full-lines
447 +
448 +# RUN: not %{lit} -v --show-all %{inputs}/shtest-if-else/test-neg1.txt 2>&1 \
449 +# RUN: | FileCheck %{inputs}/shtest-if-else/test-neg1.txt
450 +
451 +# RUN: not %{lit} -v --show-all %{inputs}/shtest-if-else/test-neg2.txt 2>&1 \
452 +# RUN: | FileCheck %{inputs}/shtest-if-else/test-neg2.txt
453 +
454 +# RUN: not %{lit} -v --show-all %{inputs}/shtest-if-else/test-neg3.txt 2>&1 \
455 +# RUN: | FileCheck %{inputs}/shtest-if-else/test-neg3.txt
456 +
457 +# RUN: not %{lit} -v --show-all %{inputs}/shtest-if-else/test-neg4.txt 2>&1 \
458 +# RUN: | FileCheck %{inputs}/shtest-if-else/test-neg4.txt