Merge remote-tracking branch 'origin/swift-3.1-branch' into stable

* origin/swift-3.1-branch:
  [Profile] Remove unused variable
  [Profile] suppress verbose rt message by default
  Correctly escape %.
  profile: add missing include for Windows
  [CMake] Connect Compiler-RT targets to LLVM Runtimes directory
diff --git a/cmake/Modules/AddCompilerRT.cmake b/cmake/Modules/AddCompilerRT.cmake
index 3342248..fa972c2 100644
--- a/cmake/Modules/AddCompilerRT.cmake
+++ b/cmake/Modules/AddCompilerRT.cmake
@@ -77,6 +77,15 @@
   endforeach()
 endmacro()
 
+function(add_compiler_rt_component name)
+  add_custom_target(${name})
+  set_target_properties(${name} PROPERTIES FOLDER "Compiler-RT Misc")
+  if(COMMAND runtime_register_component)
+    runtime_register_component(${name})
+  endif()
+  add_dependencies(compiler-rt ${name})
+endfunction()
+
 # Adds static or shared runtime for a list of architectures and operating
 # systems and puts it in the proper directory in the build and install trees.
 # add_compiler_rt_runtime(<name>
diff --git a/lib/asan/CMakeLists.txt b/lib/asan/CMakeLists.txt
index 7325932..be274df 100644
--- a/lib/asan/CMakeLists.txt
+++ b/lib/asan/CMakeLists.txt
@@ -106,8 +106,7 @@
 endif()
 
 # Build ASan runtimes shipped with Clang.
-add_custom_target(asan)
-set_target_properties(asan PROPERTIES FOLDER "Compiler-RT Misc")
+add_compiler_rt_component(asan)
 
 if(APPLE)
   add_compiler_rt_runtime(clang_rt.asan
@@ -224,7 +223,6 @@
 endif()
 
 add_compiler_rt_resource_file(asan_blacklist asan_blacklist.txt asan)
-add_dependencies(compiler-rt asan)
 
 add_subdirectory(scripts)
 
diff --git a/lib/cfi/CMakeLists.txt b/lib/cfi/CMakeLists.txt
index 56ef882..2064002 100644
--- a/lib/cfi/CMakeLists.txt
+++ b/lib/cfi/CMakeLists.txt
@@ -1,5 +1,4 @@
-add_custom_target(cfi)
-set_target_properties(cfi PROPERTIES FOLDER "Compiler-RT Misc")
+add_compiler_rt_component(cfi)
 
 set(CFI_SOURCES cfi.cc)
 
@@ -36,4 +35,3 @@
 endforeach()
 
 add_compiler_rt_resource_file(cfi_blacklist cfi_blacklist.txt cfi)
-add_dependencies(compiler-rt cfi)
diff --git a/lib/dfsan/CMakeLists.txt b/lib/dfsan/CMakeLists.txt
index eca402d..2c486bf 100644
--- a/lib/dfsan/CMakeLists.txt
+++ b/lib/dfsan/CMakeLists.txt
@@ -11,8 +11,7 @@
 append_list_if(COMPILER_RT_HAS_FFREESTANDING_FLAG -ffreestanding DFSAN_COMMON_CFLAGS)
 
 # Static runtime library.
-add_custom_target(dfsan)
-set_target_properties(dfsan PROPERTIES FOLDER "Compiler-RT Misc")
+add_compiler_rt_component(dfsan)
 
 foreach(arch ${DFSAN_SUPPORTED_ARCH})
   set(DFSAN_CFLAGS ${DFSAN_COMMON_CFLAGS})
@@ -46,5 +45,3 @@
 add_dependencies(dfsan dfsan_abilist)
 install(FILES ${dfsan_abilist_filename}
         DESTINATION ${COMPILER_RT_INSTALL_PATH})
-
-add_dependencies(compiler-rt dfsan)
diff --git a/lib/esan/CMakeLists.txt b/lib/esan/CMakeLists.txt
index 2a0a71b..2012ab6 100644
--- a/lib/esan/CMakeLists.txt
+++ b/lib/esan/CMakeLists.txt
@@ -1,7 +1,6 @@
 # Build for the EfficiencySanitizer runtime support library.
 
-add_custom_target(esan)
-set_target_properties(esan PROPERTIES FOLDER "Compiler-RT Misc")
+add_compiler_rt_component(esan)
 
 set(ESAN_RTL_CFLAGS ${SANITIZER_COMMON_CFLAGS})
 append_rtti_flag(OFF ESAN_RTL_CFLAGS)
@@ -36,8 +35,6 @@
     clang_rt.esan-${arch}-symbols)
 endforeach()
 
-add_dependencies(compiler-rt esan)
-
 if (COMPILER_RT_INCLUDE_TESTS)
   # TODO(bruening): add tests via add_subdirectory(tests)
 endif()
diff --git a/lib/lsan/CMakeLists.txt b/lib/lsan/CMakeLists.txt
index 9412c7a..73e475d 100644
--- a/lib/lsan/CMakeLists.txt
+++ b/lib/lsan/CMakeLists.txt
@@ -16,9 +16,6 @@
 
 set(LSAN_SRC_DIR ${CMAKE_CURRENT_SOURCE_DIR})
 
-add_custom_target(lsan)
-set_target_properties(lsan PROPERTIES FOLDER "Compiler-RT Misc")
-
 add_compiler_rt_object_libraries(RTLSanCommon
     OS ${SANITIZER_COMMON_SUPPORTED_OS}
     ARCHS ${LSAN_COMMON_SUPPORTED_ARCH}
@@ -27,6 +24,8 @@
 
 if(COMPILER_RT_HAS_LSAN)
   foreach(arch ${LSAN_SUPPORTED_ARCH})
+    add_compiler_rt_component(lsan)
+    
     add_compiler_rt_runtime(clang_rt.lsan
       STATIC
       ARCHS ${arch}
@@ -39,5 +38,3 @@
       PARENT_TARGET lsan)
   endforeach()
 endif()
-
-add_dependencies(compiler-rt lsan)
diff --git a/lib/msan/CMakeLists.txt b/lib/msan/CMakeLists.txt
index e7f2877..598ae54 100644
--- a/lib/msan/CMakeLists.txt
+++ b/lib/msan/CMakeLists.txt
@@ -25,8 +25,7 @@
 set(MSAN_RUNTIME_LIBRARIES)
 
 # Static runtime library.
-add_custom_target(msan)
-set_target_properties(msan PROPERTIES FOLDER "Compiler-RT Misc")
+add_compiler_rt_component(msan)
 
 foreach(arch ${MSAN_SUPPORTED_ARCH})
   add_compiler_rt_runtime(clang_rt.msan
@@ -61,7 +60,6 @@
 endforeach()
 
 add_compiler_rt_resource_file(msan_blacklist msan_blacklist.txt msan)
-add_dependencies(compiler-rt msan)
 
 if(COMPILER_RT_INCLUDE_TESTS)
   add_subdirectory(tests)
diff --git a/lib/profile/CMakeLists.txt b/lib/profile/CMakeLists.txt
index ccf79d7..006285b 100644
--- a/lib/profile/CMakeLists.txt
+++ b/lib/profile/CMakeLists.txt
@@ -38,8 +38,7 @@
 
 " COMPILER_RT_TARGET_HAS_FCNTL_LCK)
 
-add_custom_target(profile)
-set_target_properties(profile PROPERTIES FOLDER "Compiler-RT Misc")
+add_compiler_rt_component(profile)
 
 set(PROFILE_SOURCES
   GCDAProfiling.c
@@ -99,5 +98,3 @@
     SOURCES ${PROFILE_SOURCES}
     PARENT_TARGET profile)
 endif()
-
-add_dependencies(compiler-rt profile)
diff --git a/lib/profile/InstrProfiling.c b/lib/profile/InstrProfiling.c
index 1dcd05b..6828a3d 100644
--- a/lib/profile/InstrProfiling.c
+++ b/lib/profile/InstrProfiling.c
@@ -16,7 +16,6 @@
 #define INSTR_PROF_VALUE_PROF_DATA
 #include "InstrProfData.inc"
 
-COMPILER_RT_VISIBILITY char *(*GetEnvHook)(const char *) = 0;
 
 COMPILER_RT_WEAK uint64_t INSTR_PROF_RAW_VERSION_VAR = INSTR_PROF_RAW_VERSION;
 
diff --git a/lib/profile/InstrProfilingFile.c b/lib/profile/InstrProfilingFile.c
index fab479d..e264f63 100644
--- a/lib/profile/InstrProfilingFile.c
+++ b/lib/profile/InstrProfilingFile.c
@@ -358,12 +358,14 @@
   lprofCurFilename.PNS = PNS;
 
   if (!OldFilenamePat) {
-    PROF_NOTE("Set profile file path to \"%s\" via %s.\n",
-              lprofCurFilename.FilenamePat, getPNSStr(PNS));
+    if (getenv("LLVM_PROFILE_VERBOSE"))
+      PROF_NOTE("Set profile file path to \"%s\" via %s.\n",
+                lprofCurFilename.FilenamePat, getPNSStr(PNS));
   } else {
-    PROF_NOTE("Override old profile path \"%s\" via %s to \"%s\" via %s.\n",
-              OldFilenamePat, getPNSStr(OldPNS), lprofCurFilename.FilenamePat,
-              getPNSStr(PNS));
+    if (getenv("LLVM_PROFILE_VERBOSE"))
+      PROF_NOTE("Override old profile path \"%s\" via %s to \"%s\" via %s.\n",
+                OldFilenamePat, getPNSStr(OldPNS), lprofCurFilename.FilenamePat,
+                getPNSStr(PNS));
   }
 
   truncateCurrentFile();
@@ -558,7 +560,7 @@
 int __llvm_profile_dump(void) {
   if (!doMerging())
     PROF_WARN("Later invocation of __llvm_profile_dump can lead to clobbering "
-              " of previously dumped profile data : %s. Either use \%m "
+              " of previously dumped profile data : %s. Either use %%m "
               "in profile name or change profile name before dumping.\n",
               "online profile merging is not on");
   int rc = __llvm_profile_write_file();
diff --git a/lib/profile/InstrProfilingInternal.h b/lib/profile/InstrProfilingInternal.h
index 21590d5..c73b291 100644
--- a/lib/profile/InstrProfilingInternal.h
+++ b/lib/profile/InstrProfilingInternal.h
@@ -170,7 +170,6 @@
 unsigned lprofProfileDumped();
 void lprofSetProfileDumped();
 
-COMPILER_RT_VISIBILITY extern char *(*GetEnvHook)(const char *);
 COMPILER_RT_VISIBILITY extern void (*FreeHook)(void *);
 COMPILER_RT_VISIBILITY extern uint8_t *DynamicBufferIOBuffer;
 COMPILER_RT_VISIBILITY extern uint32_t VPBufferSize;
diff --git a/lib/profile/WindowsMMap.c b/lib/profile/WindowsMMap.c
index 1f73420..f81d7da 100644
--- a/lib/profile/WindowsMMap.c
+++ b/lib/profile/WindowsMMap.c
@@ -20,6 +20,9 @@
 #include "WindowsMMap.h"
 #include "InstrProfiling.h"
 
+#define WIN32_LEAN_AND_MEAN
+#include <windows.h>
+
 #ifdef __USE_FILE_OFFSET64
 # define DWORD_HI(x) (x >> 32)
 # define DWORD_LO(x) ((x) & 0xffffffff)
diff --git a/lib/safestack/CMakeLists.txt b/lib/safestack/CMakeLists.txt
index a3870ab..5a1bac2 100644
--- a/lib/safestack/CMakeLists.txt
+++ b/lib/safestack/CMakeLists.txt
@@ -1,6 +1,4 @@
-add_custom_target(safestack)
-set_target_properties(safestack PROPERTIES
-  FOLDER "Compiler-RT Misc")
+add_compiler_rt_component(safestack)
 
 set(SAFESTACK_SOURCES safestack.cc)
 
diff --git a/lib/scudo/CMakeLists.txt b/lib/scudo/CMakeLists.txt
index 6cbb85f..6f8f7d7 100644
--- a/lib/scudo/CMakeLists.txt
+++ b/lib/scudo/CMakeLists.txt
@@ -1,5 +1,4 @@
-add_custom_target(scudo)
-set_target_properties(scudo PROPERTIES FOLDER "Compiler-RT Misc")
+add_compiler_rt_component(scudo)
 
 include_directories(..)
 
@@ -28,6 +27,3 @@
       PARENT_TARGET scudo)
   endforeach()
 endif()
-
-add_dependencies(compiler-rt scudo)
-
diff --git a/lib/tsan/CMakeLists.txt b/lib/tsan/CMakeLists.txt
index 1ce5821..8e65b37 100644
--- a/lib/tsan/CMakeLists.txt
+++ b/lib/tsan/CMakeLists.txt
@@ -96,8 +96,7 @@
   rtl/tsan_vector.h)
 
 set(TSAN_RUNTIME_LIBRARIES)
-add_custom_target(tsan)
-set_target_properties(tsan PROPERTIES FOLDER "Compiler-RT Misc")
+add_compiler_rt_component(tsan)
 
 if(APPLE)
   set(TSAN_ASM_SOURCES rtl/tsan_rtl_amd64.S)
@@ -198,8 +197,6 @@
   endforeach()
 endif()
 
-add_dependencies(compiler-rt tsan)
-
 # Make sure that non-platform-specific files don't include any system headers.
 # FreeBSD does not install a number of Clang-provided headers for the compiler
 # in the base system due to incompatibilities between FreeBSD's and Clang's
diff --git a/lib/ubsan/CMakeLists.txt b/lib/ubsan/CMakeLists.txt
index 901fef2..049b976 100644
--- a/lib/ubsan/CMakeLists.txt
+++ b/lib/ubsan/CMakeLists.txt
@@ -33,8 +33,7 @@
 append_rtti_flag(ON UBSAN_STANDALONE_CXXFLAGS)
 append_list_if(SANITIZER_CAN_USE_CXXABI -DUBSAN_CAN_USE_CXXABI UBSAN_CXXFLAGS)
 
-add_custom_target(ubsan)
-set_target_properties(ubsan PROPERTIES FOLDER "Compiler-RT Misc")
+add_compiler_rt_component(ubsan)
 
 if(APPLE)
   set(UBSAN_COMMON_SOURCES ${UBSAN_SOURCES})
@@ -116,5 +115,3 @@
     endif()
   endif()
 endif()
-
-add_dependencies(compiler-rt ubsan)
diff --git a/lib/xray/CMakeLists.txt b/lib/xray/CMakeLists.txt
index 2013bbf..c70c4fe 100644
--- a/lib/xray/CMakeLists.txt
+++ b/lib/xray/CMakeLists.txt
@@ -22,7 +22,8 @@
 		SOURCES ${XRAY_SOURCES} CFLAGS ${XRAY_CFLAGS}
 		DEFS ${XRAY_COMMON_DEFINITIONS})
 
-add_custom_target(xray)
+add_compiler_rt_component(xray)
+
 set(XRAY_COMMON_RUNTIME_OBJECT_LIBS
 		RTSanitizerCommon
 		RTSanitizerCommonLibc)
@@ -39,5 +40,3 @@
 						PARENT_TARGET xray)
 		endif ()
 endforeach()
-
-add_dependencies(compiler-rt xray)