diff --git a/llvm/docs/TestingGuide.rst b/llvm/docs/TestingGuide.rst --- a/llvm/docs/TestingGuide.rst +++ b/llvm/docs/TestingGuide.rst @@ -612,6 +612,13 @@ Example: ``Windows %errc_ENOENT: no such file or directory`` +``%if feature {} %else {}`` + + Conditional substitution: if ``feature`` is available it expands to + ````, otherwise it expands to ````. + ``%else {}`` is optional and treated like ``%else {}`` + if not present. + **LLVM-specific substitutions:** ``%shlibext`` diff --git a/llvm/utils/lit/lit/TestRunner.py b/llvm/utils/lit/lit/TestRunner.py --- a/llvm/utils/lit/lit/TestRunner.py +++ b/llvm/utils/lit/lit/TestRunner.py @@ -1179,7 +1179,7 @@ def _caching_re_compile(r): return re.compile(r) -def applySubstitutions(script, substitutions, recursion_limit=None): +def applySubstitutions(script, substitutions, conditions={}, recursion_limit=None): """ Apply substitutions to the script. Allow full regular expression syntax. Replace each matching occurrence of regular expression pattern a with @@ -1213,6 +1213,18 @@ # seems reasonable. ln = _caching_re_compile(a).sub(str(b), escape(ln)) + if_regex = r'%if\s+([\w\-_]+)\s+{(.*?)}(?:\s+%else\s+{(.*?)})?' + if_match = _caching_re_compile(if_regex).search(ln) + if if_match: + cond = if_match.group(1) + branch_if = if_match.group(2) + branch_else = if_match.group(3) + if conditions.get(cond, False): + result = branch_if or '' + else: + result = branch_else or '' + ln = ln[:if_match.start()] + result + ln[if_match.end():] + # Strip the trailing newline and any extra whitespace. return ln.strip() @@ -1610,7 +1622,8 @@ substitutions = list(extra_substitutions) substitutions += getDefaultSubstitutions(test, tmpDir, tmpBase, normalize_slashes=useExternalSh) - script = applySubstitutions(script, substitutions, + conditions = { feature: True for feature in test.config.available_features } + script = applySubstitutions(script, substitutions, conditions, recursion_limit=test.config.recursiveExpansionLimit) return _runShTest(test, litConfig, useExternalSh, script, tmpBase) diff --git a/llvm/utils/lit/tests/Inputs/shtest-if-else/lit.cfg b/llvm/utils/lit/tests/Inputs/shtest-if-else/lit.cfg new file mode 100644 --- /dev/null +++ b/llvm/utils/lit/tests/Inputs/shtest-if-else/lit.cfg @@ -0,0 +1,7 @@ +import lit.formats +config.name = 'shtest-if-else' +config.test_format = lit.formats.ShTest() +config.test_source_root = None +config.test_exec_root = None +config.suffixes = ['.txt'] +config.available_features.add("feature") diff --git a/llvm/utils/lit/tests/Inputs/shtest-if-else/test.txt b/llvm/utils/lit/tests/Inputs/shtest-if-else/test.txt new file mode 100644 --- /dev/null +++ b/llvm/utils/lit/tests/Inputs/shtest-if-else/test.txt @@ -0,0 +1,21 @@ +# RUN: %if feature { echo "test-1" } + +# RUN: %if nofeature { echo "fail" } %else { echo "test-2" } + +# Spaces inside curly braces are not ignored +# RUN: echo test-%if feature { 3 } %else { fail }-test +# RUN: echo test-%if feature { 4 4 } %else { fail }-test + +# RUN: %if feature \ +# RUN: { echo \ +# RUN: "test-5" \ +# RUN: } + +# RUN: %if nofeature \ +# RUN: { echo "fail" } \ +# RUN: %else \ +# RUN: { echo "test-6" } + +# RUN: echo "test%if feature {} %else {}-7" + +# RUN: echo %%if feature { echo "test-8" } diff --git a/llvm/utils/lit/tests/shtest-if-else.py b/llvm/utils/lit/tests/shtest-if-else.py new file mode 100644 --- /dev/null +++ b/llvm/utils/lit/tests/shtest-if-else.py @@ -0,0 +1,19 @@ +# RUN: %{lit} -v --show-all %{inputs}/shtest-if-else > %t.out +# RUN: FileCheck --input-file %t.out %s +# END. + +# CHECK: -- Testing: +# CHECK-NEXT: PASS: shtest-if-else :: test.txt (1 of 1) +# CHECK-NEXT: Script: +# CHECK-NEXT: -- +# CHECK-NEXT: 'RUN: at line 1'; echo "test-1" +# CHECK-NEXT: 'RUN: at line 3'; echo "test-2" +# CHECK-NEXT: 'RUN: at line 6'; echo test- 3 -test +# CHECK-NEXT: 'RUN: at line 7'; echo test- 4 4 -test +# CHECK-NEXT: 'RUN: at line 9'; echo "test-5" +# CHECK-NEXT: 'RUN: at line 14'; echo "test-6" +# CHECK-NEXT: 'RUN: at line 19'; echo "test-7" +# CHECK-NEXT: 'RUN: at line 21'; echo %if feature { echo "test-8" } +# CHECK-NEXT: -- +# CHECK-NEXT: Exit Code: 0 +