From 02a5ac574726c14ba9fe32e48677b9d0a022de1e Mon Sep 17 00:00:00 2001
From: Bruno Cardoso Lopes <bruno.cardoso@gmail.com>
Date: Mon, 24 Oct 2016 14:30:20 -0700
Subject: [PATCH] [Modules] Fix side-effect of built-in changes from r284797

Darwin 10.11 and 10.12 SDKs cannot handle the changes from r284797 that
aren't intended to support local submodules visibility. That said, make
the behavior dependent on local visibility until there's a released SDK
with the necessary support.
---
 lib/Lex/ModuleMap.cpp | 20 ++++++++++++++++----
 1 file changed, 16 insertions(+), 4 deletions(-)

diff --git a/lib/Lex/ModuleMap.cpp b/lib/Lex/ModuleMap.cpp
index 6a261a1..4f2ba84 100644
--- a/lib/Lex/ModuleMap.cpp
+++ b/lib/Lex/ModuleMap.cpp
@@ -1486,8 +1486,11 @@ void ModuleMapParser::parseModuleDecl() {
     ActiveModule->IsSystem = true;
   if (Attrs.IsExternC)
     ActiveModule->IsExternC = true;
+  // FIXME: Remove the hardcoded "Darwin" module name after the SDK catch-up
+  // using the no_undeclared_includes keyword.
   if (Attrs.NoUndeclaredIncludes ||
-      (!ActiveModule->Parent && ModuleName == "Darwin"))
+      (!ActiveModule->Parent && Map.LangOpts.ModulesLocalVisibility &&
+       ModuleName == "Darwin"))
     ActiveModule->NoUndeclaredIncludes = true;
   ActiveModule->Directory = Directory;
 
@@ -1855,7 +1858,10 @@ void ModuleMapParser::parseHeaderDecl(MMToken::TokenKind LeadingToken,
         // If Clang supplies this header but the underlying system does not,
         // just silently swap in our builtin version. Otherwise, we'll end
         // up adding both (later).
-        if (BuiltinFile && !File) {
+        // FIXME: Right now we need a special case for the Darwin non
+        // ModulesLocalVisibility path in order to support older SDKs. Remove
+        // specific ModulesLocalVisibility check once SDK support is present.
+        if (BuiltinFile && (!File || Map.LangOpts.ModulesLocalVisibility)) {
           File = BuiltinFile;
           RelativePathName = BuiltinPathName;
           BuiltinFile = nullptr;
@@ -1888,9 +1894,15 @@ void ModuleMapParser::parseHeaderDecl(MMToken::TokenKind LeadingToken,
         // FIXME: Taking the name from the FileEntry is unstable and can give
         // different results depending on how we've previously named that file
         // in this build.
+        // FIXME: Right now we need a special case for the Darwin non
+        // ModulesLocalVisibility path in order to support older SDKs. Remove
+        // specific ModulesLocalVisibility check once SDK support is present.
         Module::Header H = { BuiltinFile->getName(), BuiltinFile };
-        Map.addHeader(ActiveModule, H, ModuleMap::ModuleHeaderRole(
-                                           Role | ModuleMap::TextualHeader));
+        if (Map.LangOpts.ModulesLocalVisibility)
+          Map.addHeader(ActiveModule, H, ModuleMap::ModuleHeaderRole(
+                                             Role | ModuleMap::TextualHeader));
+        else
+          Map.addHeader(ActiveModule, H, Role);
       }
 
       // Record this header.
