summaryrefslogtreecommitdiff
path: root/utils/lit
diff options
context:
space:
mode:
authorZachary Turner <zturner@google.com>2017-10-06 17:54:46 +0000
committerZachary Turner <zturner@google.com>2017-10-06 17:54:46 +0000
commit19ac6f8ea49ce239065d44c6f8f06a8f24859941 (patch)
tree23768f730b0ee5a4f388a7780bcb3c042bd39fc7 /utils/lit
parente959bd0e6085525c807b74693251ab474594c352 (diff)
[lit] Improve tool substitution in lit.
This addresses two sources of inconsistency in test configuration files. 1. Substitution boundaries. Previously you would specify a substitution, such as 'lli', and then additionally a set of characters that should fail to match before and after the tool. This was used, for example, so that matches that are parts of full paths would not be replaced. But not all tools did this, and those that did would often re-invent the set of characters themselves, leading to inconsistency. Now, every tool substitution defaults to using a sane set of reasonable defaults and you have to explicitly opt out of it. This actually fixed a few latent bugs that were never being surfaced, but only on accident. 2. There was no standard way for the system to decide how to locate a tool. Sometimes you have an explicit path, sometimes we would search for it and build up a path ourselves, and sometimes we would build up a full command line. Furthermore, there was no standardized way to handle missing tools. Do we warn, fail, ignore, etc? All of this is now encapsulated in the ToolSubst class. You either specify an exact command to run, or an instance of FindTool('<tool-name>') and everything else just works. Furthermore, you can specify an action to take if the tool cannot be resolved. Differential Revision: https://reviews.llvm.org/D38565 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@315085 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'utils/lit')
-rw-r--r--utils/lit/lit/llvm/__init__.py42
-rw-r--r--utils/lit/lit/llvm/config.py68
-rw-r--r--utils/lit/lit/llvm/subst.py140
-rw-r--r--utils/lit/lit/util.py4
4 files changed, 180 insertions, 74 deletions
diff --git a/utils/lit/lit/llvm/__init__.py b/utils/lit/lit/llvm/__init__.py
index 4a9249978dd..7a46daf2471 100644
--- a/utils/lit/lit/llvm/__init__.py
+++ b/utils/lit/lit/llvm/__init__.py
@@ -1,51 +1,9 @@
from lit.llvm import config
-import lit.util
-import re
llvm_config = None
-class ToolFilter(object):
- """
- String-like class used to build regex substitution patterns for
- llvm tools. Handles things like adding word-boundary patterns,
- and filtering characters from the beginning an end of a tool name
- """
-
- def __init__(self, name, pre=None, post=None, verbatim=False):
- """
- Construct a ToolFilter.
-
- name: the literal name of the substitution to look for.
-
- pre: If specified, the substitution will not find matches where
- the character immediately preceding the word-boundary that begins
- `name` is any of the characters in the string `pre`.
-
- post: If specified, the substitution will not find matches where
- the character immediately after the word-boundary that ends `name`
- is any of the characters specified in the string `post`.
-
- verbatim: If True, `name` is an exact regex that is passed to the
- underlying substitution
- """
- if verbatim:
- self.regex = name
- return
-
- def not_in(chars, where=''):
- if not chars:
- return ''
- pattern_str = '|'.join(re.escape(x) for x in chars)
- return r'(?{}!({}))'.format(where, pattern_str)
-
- self.regex = not_in(pre, '<') + r'\b' + name + r'\b' + not_in(post)
-
- def __str__(self):
- return self.regex
-
def initialize(lit_config, test_config):
global llvm_config
llvm_config = config.LLVMConfig(lit_config, test_config)
-
diff --git a/utils/lit/lit/llvm/config.py b/utils/lit/lit/llvm/config.py
index 292b77d3da8..87851b3cb1a 100644
--- a/utils/lit/lit/llvm/config.py
+++ b/utils/lit/lit/llvm/config.py
@@ -5,6 +5,8 @@ import subprocess
import sys
import lit.util
+from lit.llvm.subst import FindTool
+from lit.llvm.subst import ToolSubst
def binary_feature(on, feature, off_prefix):
@@ -225,41 +227,47 @@ class LLVMConfig(object):
# -win32 is not supported for non-x86 targets; use a default.
return 'i686-pc-win32'
- def add_tool_substitutions(self, tools, search_dirs, warn_missing=True):
+ def add_tool_substitutions(self, tools, search_dirs=None):
+ if not search_dirs:
+ search_dirs = [self.config.llvm_tools_dir]
+
if lit.util.is_string(search_dirs):
search_dirs = [search_dirs]
+ tools = [x if isinstance(x, ToolSubst) else ToolSubst(x)
+ for x in tools]
+
search_dirs = os.pathsep.join(search_dirs)
+ substitutions = []
+
for tool in tools:
- # Extract the tool name from the pattern. This relies on the tool
- # name being surrounded by \b word match operators. If the
- # pattern starts with "| ", include it in the string to be
- # substituted.
- if lit.util.is_string(tool):
- tool = lit.util.make_word_regex(tool)
- else:
- tool = str(tool)
+ match = tool.resolve(self, search_dirs)
- tool_match = re.match(r"^(\\)?((\| )?)\W+b([0-9A-Za-z-_\.]+)\\b\W*$",
- tool)
- if not tool_match:
+ # Either no match occurred, or there was an unresolved match that
+ # is ignored.
+ if not match:
continue
- tool_pipe = tool_match.group(2)
- tool_name = tool_match.group(4)
- tool_path = lit.util.which(tool_name, search_dirs)
- if not tool_path:
- if warn_missing:
- # Warn, but still provide a substitution.
- self.lit_config.note(
- 'Did not find ' + tool_name + ' in %s' % search_dirs)
- tool_path = self.config.llvm_tools_dir + '/' + tool_name
-
- if tool_name == 'llc' and os.environ.get('LLVM_ENABLE_MACHINE_VERIFIER') == '1':
- tool_path += ' -verify-machineinstrs'
- if tool_name == 'llvm-go':
- exe = getattr(self.config, 'go_executable', None)
- if exe:
- tool_path += ' go=' + exe
-
- self.config.substitutions.append((tool, tool_pipe + tool_path))
+ subst_key, tool_pipe, command = match
+
+ # An unresolved match occurred that can't be ignored. Fail without
+ # adding any of the previously-discovered substitutions.
+ if not command:
+ return False
+
+ substitutions.append((subst_key, tool_pipe + command))
+
+ self.config.substitutions.extend(substitutions)
+ return True
+
+ def use_default_substitutions(self):
+ tool_patterns = [
+ ToolSubst('FileCheck', unresolved='fatal'),
+ # Handle these specially as they are strings searched for during testing.
+ ToolSubst(r'\| \bcount\b', command=FindTool(
+ 'count'), verbatim=True, unresolved='fatal'),
+ ToolSubst(r'\| \bnot\b', command=FindTool('not'), verbatim=True, unresolved='fatal')]
+
+ self.config.substitutions.append(('%python', sys.executable))
+ self.add_tool_substitutions(
+ tool_patterns, [self.config.llvm_tools_dir])
diff --git a/utils/lit/lit/llvm/subst.py b/utils/lit/lit/llvm/subst.py
new file mode 100644
index 00000000000..e570f4ebf09
--- /dev/null
+++ b/utils/lit/lit/llvm/subst.py
@@ -0,0 +1,140 @@
+import os
+import re
+
+import lit.util
+
+expr = re.compile(r"^(\\)?((\| )?)\W+b(\S+)\\b\W*$")
+wordifier = re.compile(r"(\W*)(\b[^\b]+\b)")
+
+
+class FindTool(object):
+ def __init__(self, name):
+ self.name = name
+
+ def resolve(self, config, dirs):
+ command = lit.util.which(self.name, dirs)
+ if not command:
+ return None
+
+ if self.name == 'llc' and os.environ.get('LLVM_ENABLE_MACHINE_VERIFIER') == '1':
+ command += ' -verify-machineinstrs'
+ elif self.name == 'llvm-go':
+ exe = getattr(config.config, 'go_executable', None)
+ if exe:
+ command += ' go=' + exe
+ return command
+
+
+class ToolSubst(object):
+ """String-like class used to build regex substitution patterns for llvm
+ tools.
+
+ Handles things like adding word-boundary patterns, and filtering
+ characters from the beginning an end of a tool name
+
+ """
+
+ def __init__(self, key, command=None, pre=r'.-^/\<', post='-.', verbatim=False,
+ unresolved='warn', extra_args=None):
+ """Construct a ToolSubst.
+
+ key: The text which is to be substituted.
+
+ command: The command to substitute when the key is matched. By default,
+ this will treat `key` as a tool name and search for it. If it is
+ a string, it is intereprted as an exact path. If it is an instance of
+ FindTool, the specified tool name is searched for on disk.
+
+ pre: If specified, the substitution will not find matches where
+ the character immediately preceding the word-boundary that begins
+ `key` is any of the characters in the string `pre`.
+
+ post: If specified, the substitution will not find matches where
+ the character immediately after the word-boundary that ends `key`
+ is any of the characters specified in the string `post`.
+
+ verbatim: If True, `key` is an exact regex that is passed to the
+ underlying substitution
+
+ unresolved: Action to take if the tool substitution cannot be
+ resolved. Valid values:
+ 'warn' - log a warning but add the substitution anyway.
+ 'fatal' - Exit the test suite and log a fatal error.
+ 'break' - Don't add any of the substitutions from the current
+ group, and return a value indicating a failure.
+ 'ignore' - Don't add the substitution, and don't log an error
+
+ extra_args: If specified, represents a list of arguments that will be
+ appended to the tool's substitution.
+
+ explicit_path: If specified, the exact path will be used as a substitution.
+ Otherwise, the tool will be searched for as if by calling which(tool)
+
+ """
+ self.unresolved = unresolved
+ self.extra_args = extra_args
+ self.key = key
+ self.command = command if command is not None else FindTool(key)
+ if verbatim:
+ self.regex = key
+ return
+
+ def not_in(chars, where=''):
+ if not chars:
+ return ''
+ pattern_str = '|'.join(re.escape(x) for x in chars)
+ return r'(?{}!({}))'.format(where, pattern_str)
+
+ def wordify(word):
+ match = wordifier.match(word)
+ introducer = match.group(1)
+ word = match.group(2)
+ return introducer + r'\b' + word + r'\b'
+
+ self.regex = not_in(pre, '<') + wordify(key) + not_in(post)
+
+ def resolve(self, config, search_dirs):
+ # Extract the tool name from the pattern. This relies on the tool
+ # name being surrounded by \b word match operators. If the
+ # pattern starts with "| ", include it in the string to be
+ # substituted.
+
+ tool_match = expr.match(self.regex)
+ if not tool_match:
+ return None
+
+ tool_pipe = tool_match.group(2)
+ tool_name = tool_match.group(4)
+
+ if isinstance(self.command, FindTool):
+ command_str = self.command.resolve(config, search_dirs)
+ else:
+ command_str = str(self.command)
+
+ if command_str:
+ if self.extra_args:
+ command_str = ' '.join([command_str] + self.extra_args)
+ else:
+ if self.unresolved == 'warn':
+ # Warn, but still provide a substitution.
+ config.lit_config.note(
+ 'Did not find ' + tool_name + ' in %s' % search_dirs)
+ command_str = os.path.join(
+ config.config.llvm_tools_dir, tool_name)
+ elif self.unresolved == 'fatal':
+ # The function won't even return in this case, this leads to
+ # sys.exit
+ config.lit_config.fatal(
+ 'Did not find ' + tool_name + ' in %s' % search_dirs)
+ elif self.unresolved == 'break':
+ # By returning a valid result with an empty command, the
+ # caller treats this as a failure.
+ pass
+ elif self.unresolved == 'ignore':
+ # By returning None, the caller just assumes there was no
+ # match in the first place.
+ return None
+ else:
+ raise 'Unexpected value for ToolSubst.unresolved'
+
+ return (self.regex, tool_pipe, command_str)
diff --git a/utils/lit/lit/util.py b/utils/lit/lit/util.py
index 18733e15062..5f20262d4c3 100644
--- a/utils/lit/lit/util.py
+++ b/utils/lit/lit/util.py
@@ -195,7 +195,7 @@ def which(command, paths=None):
# Check for absolute match first.
if os.path.isfile(command):
- return command
+ return os.path.normpath(command)
# Would be nice if Python had a lib function for this.
if not paths:
@@ -213,7 +213,7 @@ def which(command, paths=None):
for ext in pathext:
p = os.path.join(path, command + ext)
if os.path.exists(p) and not os.path.isdir(p):
- return p
+ return os.path.normpath(p)
return None