First tiny move towards integrating AddressSanitizer regressions test into LLVM lit-based testing infrastructure.
The goal is to be able to run ASan tests by simply running "make check-asan" command from CMake build tree:
* tests should use fresh clang binary from current build tree.
* tests should use the same RUN-lines syntax as llvm/clang reg tests.

Next steps:
- restricting tests to machines where target is equal to host, i.e. where we can produce working binaries.
- moving AddressSanitizer unit tests to lit as well.


git-svn-id: https://llvm.org/svn/llvm-project/compiler-rt/trunk@161050 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/asan/CMakeLists.txt b/lib/asan/CMakeLists.txt
index ce985f5..753755d 100644
--- a/lib/asan/CMakeLists.txt
+++ b/lib/asan/CMakeLists.txt
@@ -80,3 +80,8 @@
 if(LLVM_INCLUDE_TESTS)
   add_subdirectory(tests)
 endif()
+
+# ASan output tests.
+# FIXME: move all output tests from output_tests/ to lit_tests/ and get rid
+# of the first directory.
+add_subdirectory(lit_tests)
diff --git a/lib/asan/lit_tests/CMakeLists.txt b/lib/asan/lit_tests/CMakeLists.txt
new file mode 100644
index 0000000..e48163c
--- /dev/null
+++ b/lib/asan/lit_tests/CMakeLists.txt
@@ -0,0 +1,15 @@
+configure_lit_site_cfg(
+  ${CMAKE_CURRENT_SOURCE_DIR}/lit.site.cfg.in
+  ${CMAKE_CURRENT_BINARY_DIR}/lit.site.cfg
+  )
+
+set(ASAN_TEST_DEPS
+  clang clang-headers FileCheck count not
+  clang-rt.asan-x86_64 clang-rt.asan-i386
+  )
+
+add_lit_testsuite(check-asan "Running the AddressSanitizer tests"
+  ${CMAKE_CURRENT_BINARY_DIR}
+  DEPENDS ${ASAN_TEST_DEPS}
+  )
+set_target_properties(check-asan PROPERTIES FOLDER "ASan tests")
diff --git a/lib/asan/lit_tests/deep_tail_call.cc b/lib/asan/lit_tests/deep_tail_call.cc
new file mode 100644
index 0000000..452e473
--- /dev/null
+++ b/lib/asan/lit_tests/deep_tail_call.cc
@@ -0,0 +1,18 @@
+// RUN: %clang_asan -m64 -O2 %s -o %t
+// RUN: %t 2>&1 | %symbolizer | FileCheck %s
+
+// CHECK: AddressSanitizer global-buffer-overflow
+int global[10];
+// CHECK: {{#0.*call4}}
+void __attribute__((noinline)) call4(int i) { global[i+10]++; }
+// CHECK: {{#1.*call3}}
+void __attribute__((noinline)) call3(int i) { call4(i); }
+// CHECK: {{#2.*call2}}
+void __attribute__((noinline)) call2(int i) { call3(i); }
+// CHECK: {{#3.*call1}}
+void __attribute__((noinline)) call1(int i) { call2(i); }
+// CHECK: {{#4.*main}}
+int main(int argc, char **argv) {
+  call1(argc);
+  return global[0];
+}
diff --git a/lib/asan/lit_tests/lit.cfg b/lib/asan/lit_tests/lit.cfg
new file mode 100644
index 0000000..28f5179
--- /dev/null
+++ b/lib/asan/lit_tests/lit.cfg
@@ -0,0 +1,46 @@
+# -*- Python -*-
+
+import os
+
+def get_required_attr(config, attr_name):
+  attr_value = getattr(config, attr_name, None)
+  if not attr_value:
+    lit.fatal("No attribute %r in test configuration! You may need to run "
+              "tests from your build directory or add this attribute "
+              "to lit.site.cfg " % attr_name)
+  return attr_value
+
+# Setup attributes common for all compiler-rt projects.
+llvm_src_root = get_required_attr(config, 'llvm_src_root')
+compiler_rt_lit_cfg = os.path.join(llvm_src_root, "projects", "compiler-rt",
+                                   "lib", "lit.common.cfg")
+lit.load_config(config, compiler_rt_lit_cfg)
+
+# Setup config name.
+config.name = 'AddressSanitizer'
+
+# Setup source root.
+config.test_source_root = os.path.dirname(__file__)
+
+# Setup default compiler flags used with -faddress-sanitizer option.
+# FIXME: Review the set of required flags and check if it can be reduced.
+clang_asan_cxxflags = ("-faddress-sanitizer "
+                      + "-mno-omit-leaf-frame-pointer "
+                      + "-fno-omit-frame-pointer "
+                      + "-fno-optimize-sibling-calls "
+                      + "-g")
+config.substitutions.append( ("%clang_asan ", (" " + config.clang + " " +
+                                              clang_asan_cxxflags + " ")) )
+
+# Setup path to symbolizer script.
+# FIXME: Instead we should copy this script to the build tree and point
+#        at it there.
+asan_source_dir = os.path.join(config.test_source_root, "..")
+symbolizer = os.path.join(asan_source_dir,
+                         'scripts', 'asan_symbolize.py')
+if not os.path.exists(symbolizer):
+  lit.fatal("Can't find symbolizer script on path %r" % symbolizer)
+config.substitutions.append( ('%symbolizer', (" " + symbolizer + " " )))
+
+# Default test suffixes.
+config.suffixes = ['.c', '.cc', '.cpp']
diff --git a/lib/asan/lit_tests/lit.site.cfg.in b/lib/asan/lit_tests/lit.site.cfg.in
new file mode 100644
index 0000000..b4ae829
--- /dev/null
+++ b/lib/asan/lit_tests/lit.site.cfg.in
@@ -0,0 +1,9 @@
+## Autogenerated by LLVM/Clang configuration.
+# Do not edit!
+
+config.target_triple = "@TARGET_TRIPLE@"
+config.llvm_src_root = "@LLVM_SOURCE_DIR@"
+config.clang = "@LLVM_BINARY_DIR@/bin/clang"
+
+# Let the main config do the real work.
+lit.load_config(config, "@CMAKE_CURRENT_SOURCE_DIR@/lit.cfg")
diff --git a/lib/lit.common.cfg b/lib/lit.common.cfg
new file mode 100644
index 0000000..725156b
--- /dev/null
+++ b/lib/lit.common.cfg
@@ -0,0 +1,45 @@
+# -*- Python -*-
+
+# Configuration file for 'lit' test runner.
+# This file contains common rules for various compiler-rt testsuites.
+# It is mostly copied from lit.cfg used by Clang.
+import os
+import platform
+
+# Setup test format
+execute_external = (platform.system() != 'Windows'
+                    or lit.getBashPath() not in [None, ""])
+config.test_format = lit.formats.ShTest(execute_external)
+
+# Setup clang binary.
+clang_path = getattr(config, 'clang', None)
+if (not clang_path) or (not os.path.exists(clang_path)):
+  lit.fatal("Can't find Clang on path %r" % clang_path)
+if not lit.quiet:
+  lit.note("using clang: %r" % clang_path)
+
+# Clear some environment variables that might affect Clang.
+possibly_dangerous_env_vars = ['COMPILER_PATH', 'RC_DEBUG_OPTIONS',
+                               'CINDEXTEST_PREAMBLE_FILE', 'LIBRARY_PATH',
+                               'CPATH', 'C_INCLUDE_PATH', 'CPLUS_INCLUDE_PATH',
+                               'OBJC_INCLUDE_PATH', 'OBJCPLUS_INCLUDE_PATH',
+                               'LIBCLANG_TIMING', 'LIBCLANG_OBJTRACKING',
+                               'LIBCLANG_LOGGING', 'LIBCLANG_BGPRIO_INDEX',
+                               'LIBCLANG_BGPRIO_EDIT', 'LIBCLANG_NOTHREADS',
+                               'LIBCLANG_RESOURCE_USAGE',
+                               'LIBCLANG_CODE_COMPLETION_LOGGING']
+# Clang/Win32 may refer to %INCLUDE%. vsvarsall.bat sets it.
+if platform.system() != 'Windows':
+    possibly_dangerous_env_vars.append('INCLUDE')
+for name in possibly_dangerous_env_vars:
+  if name in config.environment:
+    del config.environment[name]
+
+# Define %clang substitution to use in test RUN lines.
+config.substitutions.append( ("%clang ", (" " + config.clang + " ")) )
+
+# Use ugly construction to explicitly prohibit "clang", "clang++" etc.
+# in RUN lines.
+config.substitutions.append(
+    (' clang', """\n\n*** Do not use 'clangXXX' in tests,
+     instead define '%clangXXX' substitution in lit config. ***\n\n""") )