From b5f99338f6073aeb6e393caea8ac1148af69bf2c Mon Sep 17 00:00:00 2001
From: Dimitry Andric <dim@FreeBSD.org>
Date: Wed, 17 Aug 2016 19:35:22 +0000
Subject: Vendor import of compiler-rt release_39 branch r278877:
 https://llvm.org/svn/llvm-project/compiler-rt/branches/release_39@278877

---
 cmake/Modules/BuiltinTests.cmake                   | 23 +++++++++++++++++++---
 cmake/builtin-config-ix.cmake                      | 10 ++++++++++
 lib/builtins/CMakeLists.txt                        |  8 ++++++--
 .../tests/sanitizer_symbolizer_test.cc             |  2 ++
 test/asan/TestCases/Darwin/dead-strip.c            | 22 ---------------------
 test/asan/TestCases/alloca_constant_size.cc        |  2 ++
 6 files changed, 40 insertions(+), 27 deletions(-)
 delete mode 100644 test/asan/TestCases/Darwin/dead-strip.c

diff --git a/cmake/Modules/BuiltinTests.cmake b/cmake/Modules/BuiltinTests.cmake
index 1b03e94acf12..a229145d9b91 100644
--- a/cmake/Modules/BuiltinTests.cmake
+++ b/cmake/Modules/BuiltinTests.cmake
@@ -2,11 +2,15 @@
 # This function takes an OS and a list of architectures and identifies the
 # subset of the architectures list that the installed toolchain can target.
 function(try_compile_only output)
+  cmake_parse_arguments(ARG "" "" "SOURCE;FLAGS" ${ARGN})
+  if(NOT ARG_SOURCE)
+    set(ARG_SOURCE "int foo(int x, int y) { return x + y; }\n")
+  endif()
   set(SIMPLE_C ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/src.c)
-  file(WRITE ${SIMPLE_C} "int foo(int x, int y) { return x + y; }\n")
+  file(WRITE ${SIMPLE_C} "${ARG_SOURCE}\n")
   string(REGEX MATCHALL "<[A-Za-z0-9_]*>" substitutions
          ${CMAKE_C_COMPILE_OBJECT})
-  string(REPLACE ";" " " extra_flags "${ARGN}")
+  string(REPLACE ";" " " extra_flags "${ARG_FLAGS}")
 
   set(test_compile_command "${CMAKE_C_COMPILE_OBJECT}")
   foreach(substitution ${substitutions})
@@ -51,7 +55,20 @@ endfunction()
 function(builtin_check_c_compiler_flag flag output)
   if(NOT DEFINED ${output})
     message(STATUS "Performing Test ${output}")
-    try_compile_only(result ${flag})
+    try_compile_only(result FLAGS ${flag})
+    set(${output} ${result} CACHE INTERNAL "Compiler supports ${flag}")
+    if(${result})
+      message(STATUS "Performing Test ${output} - Success")
+    else()
+      message(STATUS "Performing Test ${output} - Failed")
+    endif()
+  endif()
+endfunction()
+
+function(builtin_check_c_compiler_source output source)
+  if(NOT DEFINED ${output})
+    message(STATUS "Performing Test ${output}")
+    try_compile_only(result SOURCE ${source})
     set(${output} ${result} CACHE INTERNAL "Compiler supports ${flag}")
     if(${result})
       message(STATUS "Performing Test ${output} - Success")
diff --git a/cmake/builtin-config-ix.cmake b/cmake/builtin-config-ix.cmake
index 432b1fadb177..83466d81bd2f 100644
--- a/cmake/builtin-config-ix.cmake
+++ b/cmake/builtin-config-ix.cmake
@@ -1,4 +1,5 @@
 include(BuiltinTests)
+include(CheckCSourceCompiles)
 
 # Make all the tests only check the compiler
 set(TEST_COMPILE_ONLY On)
@@ -14,6 +15,15 @@ builtin_check_c_compiler_flag(-mfloat-abi=soft      COMPILER_RT_HAS_FLOAT_ABI_SO
 builtin_check_c_compiler_flag(-mfloat-abi=hard      COMPILER_RT_HAS_FLOAT_ABI_HARD_FLAG)
 builtin_check_c_compiler_flag(-static               COMPILER_RT_HAS_STATIC_FLAG)
 
+builtin_check_c_compiler_source(COMPILER_RT_SUPPORTS_ATOMIC_KEYWORD
+"
+int foo(int x, int y) {
+ _Atomic int result = x * y;
+ return result;
+}
+")
+
+
 set(ARM64 aarch64)
 set(ARM32 arm armhf)
 set(X86 i386 i686)
diff --git a/lib/builtins/CMakeLists.txt b/lib/builtins/CMakeLists.txt
index d5a1efe79026..44a660f5d49d 100644
--- a/lib/builtins/CMakeLists.txt
+++ b/lib/builtins/CMakeLists.txt
@@ -38,8 +38,6 @@ set(GENERIC_SOURCES
   ashlti3.c
   ashrdi3.c
   ashrti3.c
-  # FIXME: atomic.c may only be compiled if host compiler understands _Atomic
-  # atomic.c
   clear_cache.c
   clzdi2.c
   clzsi2.c
@@ -162,6 +160,12 @@ set(GENERIC_SOURCES
   umodsi3.c
   umodti3.c)
 
+if(COMPILER_RT_SUPPORTS_ATOMIC_KEYWORD)
+  set(GENERIC_SOURCES
+    ${GENERIC_SOURCES}
+    atomic.c)
+endif()
+
 set(MSVC_SOURCES
  divsc3.c
  divdc3.c
diff --git a/lib/sanitizer_common/tests/sanitizer_symbolizer_test.cc b/lib/sanitizer_common/tests/sanitizer_symbolizer_test.cc
index 3d5678ab7fb8..4c4d2a8c3fb2 100644
--- a/lib/sanitizer_common/tests/sanitizer_symbolizer_test.cc
+++ b/lib/sanitizer_common/tests/sanitizer_symbolizer_test.cc
@@ -62,7 +62,9 @@ TEST(Symbolizer, DemangleSwiftAndCXX) {
   EXPECT_STREQ("_TtSd", DemangleSwiftAndCXX("_TtSd"));
   // Check that the rest demangles properly.
   EXPECT_STREQ("f1(char*, int)", DemangleSwiftAndCXX("_Z2f1Pci"));
+#if !SANITIZER_FREEBSD // QoI issue with libcxxrt on FreeBSD
   EXPECT_STREQ("foo", DemangleSwiftAndCXX("foo"));
+#endif
   EXPECT_STREQ("", DemangleSwiftAndCXX(""));
 }
 #endif
diff --git a/test/asan/TestCases/Darwin/dead-strip.c b/test/asan/TestCases/Darwin/dead-strip.c
deleted file mode 100644
index 212dedd469c1..000000000000
--- a/test/asan/TestCases/Darwin/dead-strip.c
+++ /dev/null
@@ -1,22 +0,0 @@
-// Test that AddressSanitizer does not re-animate dead globals when dead
-// stripping is turned on.
-//
-// This test verifies that an out-of-bounds access on a global variable is
-// detected after dead stripping has been performed. This proves that the
-// runtime is able to register globals in the __DATA,__asan_globals section.
-
-// REQUIRES: osx-ld64-live_support
-// RUN: %clang_asan -mllvm -asan-globals-live-support -Xlinker -dead_strip -o %t %s
-// RUN: llvm-nm -format=posix %t | FileCheck --check-prefix NM-CHECK %s
-// RUN: not %run %t 2>&1 | FileCheck --check-prefix ASAN-CHECK %s
-
-int alive[1] = {};
-int dead[1] = {};
-// NM-CHECK: {{^_alive }}
-// NM-CHECK-NOT: {{^_dead }}
-
-int main(int argc, char *argv[]) {
-  alive[argc] = 0;
-  // ASAN-CHECK: {{0x.* is located 0 bytes to the right of global variable}}
-  return 0;
-}
diff --git a/test/asan/TestCases/alloca_constant_size.cc b/test/asan/TestCases/alloca_constant_size.cc
index 61f6da710116..a766ae75be07 100644
--- a/test/asan/TestCases/alloca_constant_size.cc
+++ b/test/asan/TestCases/alloca_constant_size.cc
@@ -10,6 +10,8 @@
 // MSVC provides _alloca instead of alloca.
 #if defined(_MSC_VER) && !defined(alloca)
 # define alloca _alloca
+#elif defined(__FreeBSD__)
+#include <stdlib.h>
 #else
 #include <alloca.h>
 #endif
-- 
cgit v1.2.3