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

* origin/swift-3.1-branch:
  [Driver][Darwin] Disable default stack protector levels in freestanding mode.
  [Headers] Enable #include_next<float.h> on Darwin
diff --git a/lib/Driver/Tools.cpp b/lib/Driver/Tools.cpp
index 6655994..4abcd92 100644
--- a/lib/Driver/Tools.cpp
+++ b/lib/Driver/Tools.cpp
@@ -5064,9 +5064,12 @@
   Args.AddLastArg(CmdArgs, options::OPT_ftlsmodel_EQ);
 
   // -fhosted is default.
+  bool IsHosted = true;
   if (Args.hasFlag(options::OPT_ffreestanding, options::OPT_fhosted, false) ||
-      KernelOrKext)
+      KernelOrKext) {
     CmdArgs.push_back("-ffreestanding");
+    IsHosted = false;
+  }
 
   // Forward -f (flag) options which we can pass directly.
   Args.AddLastArg(CmdArgs, options::OPT_femit_all_decls);
@@ -5200,6 +5203,10 @@
   } else {
     StackProtectorLevel =
         getToolChain().GetDefaultStackProtectorLevel(KernelOrKext);
+    // Only use a default stack protector on Darwin in case -ffreestanding
+    // is not specified.
+    if (Triple.isOSDarwin() && !IsHosted)
+      StackProtectorLevel = 0;
   }
   if (StackProtectorLevel) {
     CmdArgs.push_back("-stack-protector");
diff --git a/lib/Headers/float.h b/lib/Headers/float.h
index a28269e..0f453d8 100644
--- a/lib/Headers/float.h
+++ b/lib/Headers/float.h
@@ -27,9 +27,12 @@
 /* If we're on MinGW, fall back to the system's float.h, which might have
  * additional definitions provided for Windows.
  * For more details see http://msdn.microsoft.com/en-us/library/y0ybw9fy.aspx
+ *
+ * Also fall back on Darwin to allow additional definitions and
+ * implementation-defined values.
  */
-#if (defined(__MINGW32__) || defined(_MSC_VER)) && __STDC_HOSTED__ && \
-    __has_include_next(<float.h>)
+#if (defined(__APPLE__) || (defined(__MINGW32__) || defined(_MSC_VER))) && \
+    __STDC_HOSTED__ && __has_include_next(<float.h>)
 #  include_next <float.h>
 
 /* Undefine anything that we'll be redefining below. */
diff --git a/test/Driver/stack-protector.c b/test/Driver/stack-protector.c
index 487af56..6769b65 100644
--- a/test/Driver/stack-protector.c
+++ b/test/Driver/stack-protector.c
@@ -32,3 +32,31 @@
 // RUN: %clang -target x86_64-scei-ps4 -fstack-protector --param ssp-buffer-size=16 -### %s 2>&1 | FileCheck %s -check-prefix=SSP-PS4-BUF
 // SSP-PS4-BUF: "-stack-protector" "2"
 // SSP-PS4-BUF: "-stack-protector-buffer-size" "16"
+
+// Test default stack protector values for Darwin platforms
+
+// RUN: %clang -target armv7k-apple-watchos2.0 -### %s 2>&1 | FileCheck %s -check-prefix=SSP_WATCHOS
+// SSP_WATCHOS: "-stack-protector" "1"
+// RUN: %clang -target arm64-apple-ios8.0.0 -### %s 2>&1 | FileCheck %s -check-prefix=SSP_IOS
+// SSP_IOS: "-stack-protector" "1"
+// RUN: %clang -target x86_64-apple-darwin10 -mmacosx-version-min=10.6 -### %s 2>&1 | FileCheck %s -check-prefix=SSP_MACOSX
+// SSP_MACOSX: "-stack-protector" "1"
+// RUN: %clang -target x86_64-apple-darwin10 -mmacosx-version-min=10.5 -### %s 2>&1 | FileCheck %s -check-prefix=SSP_MACOSX_10_5
+// SSP_MACOSX_10_5: "-stack-protector" "1"
+// RUN: %clang -target x86_64-apple-darwin10 -mmacosx-version-min=10.5 -mkernel -### %s 2>&1 | FileCheck %s -check-prefix=SSP_MACOSX_KERNEL
+// SSP_MACOSX_KERNEL-NOT: "-stack-protector"
+// RUN: %clang -target x86_64-apple-darwin10 -mmacosx-version-min=10.6 -### %s 2>&1 | FileCheck %s -check-prefix=SSP_MACOSX_10_6_KERNEL
+// SSP_MACOSX_10_6_KERNEL: "-stack-protector" "1"
+
+// Test default stack protector values for Darwin platforms with -ffreestanding
+
+// RUN: %clang -ffreestanding -target armv7k-apple-watchos2.0 -### %s 2>&1 | FileCheck %s -check-prefix=SSP_FREE_WATCHOS
+// SSP_FREE_WATCHOS-NOT: "-stack-protector"
+// RUN: %clang -ffreestanding -target arm64-apple-ios8.0.0 -### %s 2>&1 | FileCheck %s -check-prefix=SSP_FREE_IOS
+// SSP_FREE_IOS-NOT: "-stack-protector"
+// RUN: %clang -ffreestanding -target x86_64-apple-darwin10 -mmacosx-version-min=10.6 -### %s 2>&1 | FileCheck %s -check-prefix=SSP_FREE_MACOSX
+// SSP_FREE_MACOSX-NOT: "-stack-protector"
+// RUN: %clang -ffreestanding -target x86_64-apple-darwin10 -mmacosx-version-min=10.5 -### %s 2>&1 | FileCheck %s -check-prefix=SSP_FREE_MACOSX_10_5
+// SSP_FREE_MACOSX_10_5-NOT: "-stack-protector"
+// RUN: %clang -ffreestanding -target x86_64-apple-darwin10 -mmacosx-version-min=10.6 -### %s 2>&1 | FileCheck %s -check-prefix=SSP_FREE_MACOSX_10_6_KERNEL
+// SSP_FREE_MACOSX_10_6_KERNEL-NOT: "-stack-protector"
diff --git a/test/Headers/Inputs/usr/include/float.h b/test/Headers/Inputs/usr/include/float.h
new file mode 100644
index 0000000..9dab891
--- /dev/null
+++ b/test/Headers/Inputs/usr/include/float.h
@@ -0,0 +1,6 @@
+#ifndef SYSFLOAT_H
+#define SYSFLOAT_H
+
+#define FLT_HAS_SUBNORM 1
+
+#endif /* SYSFLOAT_H */
diff --git a/test/Headers/float-darwin.c b/test/Headers/float-darwin.c
new file mode 100644
index 0000000..54bac1a
--- /dev/null
+++ b/test/Headers/float-darwin.c
@@ -0,0 +1,13 @@
+// REQUIRES: system-darwin
+// RUN: %clang -target x86_64-apple-darwin10 -fsyntax-only -std=c11 -isysroot %S/Inputs %s
+#include <float.h>
+
+// Test the #include_next on float.h works on Darwin.
+#ifndef FLT_HAS_SUBNORM
+  #error "FLT_HAS_SUBNORM not defined"
+#endif
+
+// Test that definition from builtin are also present.
+#ifndef FLT_MAX
+  #error "FLT_MAX not defined"
+#endif