Merge remote-tracking branch 'origin/swift-4.0-branch' into stable
diff --git a/include/clang/Basic/DiagnosticDriverKinds.td b/include/clang/Basic/DiagnosticDriverKinds.td
index 2cd8086..8133db5 100644
--- a/include/clang/Basic/DiagnosticDriverKinds.td
+++ b/include/clang/Basic/DiagnosticDriverKinds.td
@@ -135,9 +135,10 @@
     "unable to open CC_PRINT_OPTIONS file: %0">;
 def err_drv_preamble_format : Error<
     "incorrect format for -preamble-bytes=N,END">;
-def err_invalid_ios_deployment_target : Error<
+def warn_invalid_ios_deployment_target : Warning<
   "invalid iOS deployment version '%0', iOS 10 is the maximum deployment "
-  "target for 32-bit targets">;
+  "target for 32-bit targets">, InGroup<InvalidIOSDeploymentTarget>,
+  DefaultError;
 def err_drv_conflicting_deployment_targets : Error<
   "conflicting deployment targets, both '%0' and '%1' are present in environment">;
 def err_arc_unsupported_on_runtime : Error<
diff --git a/include/clang/Basic/DiagnosticGroups.td b/include/clang/Basic/DiagnosticGroups.td
index 86eb46f..42bb164 100644
--- a/include/clang/Basic/DiagnosticGroups.td
+++ b/include/clang/Basic/DiagnosticGroups.td
@@ -146,6 +146,9 @@
 def GNUFoldingConstant : DiagGroup<"gnu-folding-constant">;
 def FormatExtraArgs : DiagGroup<"format-extra-args">;
 def FormatZeroLength : DiagGroup<"format-zero-length">;
+
+def InvalidIOSDeploymentTarget : DiagGroup<"invalid-ios-deployment-target">;
+
 def CXX1zCompatMangling : DiagGroup<"c++1z-compat-mangling">;
 
 // Warnings for C++1y code which is not compatible with prior C++ standards.
diff --git a/lib/Driver/ToolChains.cpp b/lib/Driver/ToolChains.cpp
index 1e49d19..a11c817 100644
--- a/lib/Driver/ToolChains.cpp
+++ b/lib/Driver/ToolChains.cpp
@@ -596,13 +596,12 @@
   unsigned Major, Minor, Micro;
   bool HadExtra;
 
-  // iOS 10 is the maximum deployment target for 32-bit targets.
-  if (iOSVersion && getTriple().isArch32Bit() &&
-      Driver::GetReleaseVersion(iOSVersion->getValue(), Major, Minor, Micro,
-                                HadExtra) &&
-      Major > 10)
-    getDriver().Diag(diag::err_invalid_ios_deployment_target)
-        << iOSVersion->getAsString(Args);
+  // The iOS deployment target that is explicitly specified via a command line
+  // option or an environment variable.
+  std::string ExplicitIOSDeploymentTargetStr;
+
+  if (iOSVersion)
+    ExplicitIOSDeploymentTargetStr = iOSVersion->getAsString(Args);
 
   // Add a macro to differentiate between m(iphone|tv|watch)os-version-min=X.Y and
   // -m(iphone|tv|watch)simulator-version-min=X.Y.
@@ -645,13 +644,9 @@
     if (char *env = ::getenv("WATCHOS_DEPLOYMENT_TARGET"))
       WatchOSTarget = env;
 
-    // iOS 10 is the maximum deployment target for 32-bit targets.
-    if (!iOSTarget.empty() && getTriple().isArch32Bit() &&
-        Driver::GetReleaseVersion(iOSTarget.c_str(), Major, Minor, Micro,
-                                  HadExtra) &&
-        Major > 10)
-      getDriver().Diag(diag::err_invalid_ios_deployment_target)
-          << std::string("IPHONEOS_DEPLOYMENT_TARGET=") + iOSTarget;
+    if (!iOSTarget.empty())
+      ExplicitIOSDeploymentTargetStr =
+          std::string("IPHONEOS_DEPLOYMENT_TARGET=") + iOSTarget;
 
     // If there is no command-line argument to specify the Target version and
     // no environment variable defined, see if we can set the default based
@@ -785,12 +780,19 @@
         HadExtra || Major >= 100 || Minor >= 100 || Micro >= 100)
       getDriver().Diag(diag::err_drv_invalid_version_number)
           << iOSVersion->getAsString(Args);
-    // iOS 10 is the maximum deployment target for 32-bit targets. If the
-    // inferred deployment target is iOS 11 or later, set it to 10.99.
+    // For 32-bit targets, the deployment target for iOS has to be earlier than
+    // iOS 11.
     if (getTriple().isArch32Bit() && Major >= 11) {
-      Major = 10;
-      Minor = 99;
-      Micro = 99;
+      // If the deployment target is explicitly specified, print a diagnostic.
+      if (!ExplicitIOSDeploymentTargetStr.empty()) {
+        getDriver().Diag(diag::warn_invalid_ios_deployment_target)
+            << ExplicitIOSDeploymentTargetStr;
+      // Otherwise, set it to 10.99.99.
+      } else {
+        Major = 10;
+        Minor = 99;
+        Micro = 99;
+      }
     }
   } else if (Platform == TvOS) {
     if (!Driver::GetReleaseVersion(TvOSVersion->getValue(), Major, Minor,
diff --git a/test/Driver/darwin-version.c b/test/Driver/darwin-version.c
index c11a2df..903def4 100644
--- a/test/Driver/darwin-version.c
+++ b/test/Driver/darwin-version.c
@@ -26,6 +26,8 @@
 
 // RUN: %clang -target armv7-apple-ios11.1 -c -### %s 2>&1 | \
 // RUN: FileCheck --check-prefix=CHECK-VERSION-IOS7 %s
+// RUN: %clang -target armv7-apple-ios9 -Wno-missing-sysroot -isysroot SDKs/iPhoneOS11.0.sdk -c -### %s 2>&1 | \
+// RUN: FileCheck --check-prefix=CHECK-VERSION-IOS7 %s
 // CHECK-VERSION-IOS7: thumbv7-apple-ios10.99.99
 
 // RUN: env IPHONEOS_DEPLOYMENT_TARGET=11.0 \
@@ -45,6 +47,10 @@
 // RUN: FileCheck --check-prefix=CHECK-VERSION-IOS11 %s
 // CHECK-VERSION-IOS11: arm64-apple-ios11.1.0
 
+// RUN: %clang -target armv7-apple-ios9.0 -miphoneos-version-min=11.0 -c -Wno-invalid-ios-deployment-target -### %s 2>&1 | \
+// RUN: FileCheck --check-prefix=CHECK-VERSION-IOS12 %s
+// CHECK-VERSION-IOS12: thumbv7-apple-ios11.0.0
+
 // RUN: %clang -target i686-apple-darwin8 -c %s -### 2>&1 | \
 // RUN:   FileCheck --check-prefix=CHECK-VERSION-OSX4 %s
 // RUN: %clang -target i686-apple-darwin9 -mmacosx-version-min=10.4 -c %s -### 2>&1 | \