[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

llvm-svn: 315085
diff --git a/clang/test/lit.cfg.py b/clang/test/lit.cfg.py
index f9266e5..a6f9bb3 100644
--- a/clang/test/lit.cfg.py
+++ b/clang/test/lit.cfg.py
@@ -10,7 +10,8 @@
 import lit.util
 
 from lit.llvm import llvm_config
-from lit.llvm import ToolFilter
+from lit.llvm.subst import ToolSubst
+from lit.llvm.subst import FindTool
 
 # Configuration file for the 'lit' test runner.
 
@@ -76,6 +77,8 @@
 llvm_config.with_system_environment(
     ['ASAN_SYMBOLIZER_PATH', 'MSAN_SYMBOLIZER_PATH'])
 
+llvm_config.use_default_substitutions()
+
 # Discover the 'clang' and 'clangcc' to use.
 
 
@@ -120,44 +123,37 @@
     config.available_features.add('examples')
 
 builtin_include_dir = llvm_config.get_clang_builtin_include_dir(config.clang)
-config.substitutions.append(('%clang_analyze_cc1',
-                             '%clang_cc1 -analyze %analyze'))
-config.substitutions.append(('%clang_cc1',
-                             '%s -cc1 -internal-isystem %s -nostdsysteminc'
-                             % (config.clang, builtin_include_dir)))
-config.substitutions.append(('%clang_cpp', ' ' + config.clang +
-                             ' --driver-mode=cpp '))
-config.substitutions.append(('%clang_cl', ' ' + config.clang +
-                             ' --driver-mode=cl '))
-config.substitutions.append(('%clangxx', ' ' + config.clang +
-                             ' --driver-mode=g++ '))
 
-clang_func_map = lit.util.which(
-    'clang-func-mapping', config.environment['PATH'])
-if clang_func_map:
-    config.substitutions.append(
-        ('%clang_func_map', ' ' + clang_func_map + ' '))
+tools = [
+    # By specifying %clang_cc1 as part of the substitution, this substitution
+    # relies on repeated substitution, so must come before %clang_cc1.
+    ToolSubst('%clang_analyze_cc1', command='%clang_cc1',
+              extra_args=['-analyze', '%analyze']),
+    ToolSubst('%clang_cc1', command=config.clang, extra_args=[
+              '-cc1', '-internal-isystem', builtin_include_dir, '-nostdsysteminc']),
+    ToolSubst('%clang_cpp', command=config.clang,
+              extra_args=['--driver-mode=cpp']),
+    ToolSubst('%clang_cl', command=config.clang,
+              extra_args=['--driver-mode=cl']),
+    ToolSubst('%clangxx', command=config.clang,
+              extra_args=['--driver-mode=g++']),
+    ToolSubst('%clang_func_map', command=FindTool(
+        'clang-func-mapping'), unresolved='ignore'),
+    ToolSubst('%clang', command=config.clang),
+    ToolSubst('%test_debuginfo', command=os.path.join(
+        config.llvm_src_root, 'utils', 'test_debuginfo.pl')),
+    'c-index-test', 'clang-check', 'clang-diff', 'clang-format', 'opt']
 
-config.substitutions.append(('%clang', ' ' + config.clang + ' '))
-config.substitutions.append(('%test_debuginfo',
-                             ' ' + config.llvm_src_root + '/utils/test_debuginfo.pl '))
-config.substitutions.append(('%itanium_abi_triple',
-                             llvm_config.make_itanium_abi_triple(config.target_triple)))
-config.substitutions.append(('%ms_abi_triple',
-                             llvm_config.make_msabi_triple(config.target_triple)))
-config.substitutions.append(('%resource_dir', builtin_include_dir))
-config.substitutions.append(('%python', config.python_executable))
+if config.clang_examples:
+    tools.append('clang-interpreter')
 
-# The host triple might not be set, at least if we're compiling clang from
-# an already installed llvm.
-if config.host_triple and config.host_triple != '@LLVM_HOST_TRIPLE@':
-    config.substitutions.append(('%target_itanium_abi_host_triple',
-                                 '--target=%s' % llvm_config.make_itanium_abi_triple(config.host_triple)))
-else:
-    config.substitutions.append(('%target_itanium_abi_host_triple', ''))
+# For each occurrence of a clang tool name, replace it with the full path to
+# the build directory holding that tool.  We explicitly specify the directories
+# to search to ensure that we get the tools just built and not some random
+# tools that might happen to be in the user's PATH.
+tool_dirs = [config.clang_tools_dir, config.llvm_tools_dir]
 
-config.substitutions.append(
-    ('%src_include_dir', config.clang_src_dir + '/include'))
+llvm_config.add_tool_substitutions(tools, tool_dirs)
 
 # FIXME: Find nicer way to prohibit this.
 config.substitutions.append(
@@ -183,27 +179,22 @@
     (' %clang-cl ',
      """*** invalid substitution, use '%clang_cl'. ***"""))
 
-# For each occurrence of a clang tool name, replace it with the full path to
-# the build directory holding that tool.  We explicitly specify the directories
-# to search to ensure that we get the tools just built and not some random
-# tools that might happen to be in the user's PATH.
-tool_dirs = [config.clang_tools_dir, config.llvm_tools_dir]
+config.substitutions.append(('%itanium_abi_triple',
+                             llvm_config.make_itanium_abi_triple(config.target_triple)))
+config.substitutions.append(('%ms_abi_triple',
+                             llvm_config.make_msabi_triple(config.target_triple)))
+config.substitutions.append(('%resource_dir', builtin_include_dir))
 
-tool_patterns = [
-    'FileCheck', 'c-index-test',
-    ToolFilter('clang-check', pre='-.', post='-.'),
-    ToolFilter('clang-diff', pre='-.', post='-.'),
-    ToolFilter('clang-format', pre='-.', post='-.'),
-    # FIXME: Some clang test uses opt?
-    ToolFilter('opt', pre='-.', post=r'/\-.'),
-    # Handle these specially as they are strings searched for during testing.
-    ToolFilter(r'\| \bcount\b', verbatim=True),
-    ToolFilter(r'\| \bnot\b', verbatim=True)]
+# The host triple might not be set, at least if we're compiling clang from
+# an already installed llvm.
+if config.host_triple and config.host_triple != '@LLVM_HOST_TRIPLE@':
+    config.substitutions.append(('%target_itanium_abi_host_triple',
+                                 '--target=%s' % llvm_config.make_itanium_abi_triple(config.host_triple)))
+else:
+    config.substitutions.append(('%target_itanium_abi_host_triple', ''))
 
-if config.clang_examples:
-    tool_patterns.append(ToolFilter('clang-interpreter', '-.', '-.'))
-
-llvm_config.add_tool_substitutions(tool_patterns, tool_dirs)
+config.substitutions.append(
+    ('%src_include_dir', config.clang_src_dir + '/include'))
 
 # Set available features we allow tests to conditionalize on.
 #
@@ -236,8 +227,6 @@
     config.available_features.add('libgcc')
 
 # Case-insensitive file system
-
-
 def is_filesystem_case_insensitive():
     handle, path = tempfile.mkstemp(
         prefix='case-test', dir=config.test_exec_root)