[modules] Start moving the module visibility information off the Module itself.

It has no place there; it's not a property of the Module, and it makes
restoring the visibility set when we leave a submodule more difficult.

llvm-svn: 236300
diff --git a/clang/lib/Lex/Preprocessor.cpp b/clang/lib/Lex/Preprocessor.cpp
index 231720b..2db1095 100644
--- a/clang/lib/Lex/Preprocessor.cpp
+++ b/clang/lib/Lex/Preprocessor.cpp
@@ -107,9 +107,6 @@
 
   // We haven't read anything from the external source.
   ReadMacrosFromExternalSource = false;
-  // We might already have some macros from an imported module (via a PCH or
-  // preamble) if modules is enabled.
-  MacroVisibilityGeneration = LangOpts.Modules ? 1 : 0;
   
   // "Poison" __VA_ARGS__, which can only appear in the expansion of a macro.
   // This gets unpoisoned where it is allowed.
@@ -757,13 +754,34 @@
                                             ModuleImportPath,
                                             Module::MacrosVisible,
                                             /*IsIncludeDirective=*/false);
-      ++MacroVisibilityGeneration;
+      if (Imported)
+        makeModuleVisible(Imported, ModuleImportLoc);
     }
     if (Callbacks && (getLangOpts().Modules || getLangOpts().DebuggerSupport))
       Callbacks->moduleImport(ModuleImportLoc, ModuleImportPath, Imported);
   }
 }
 
+void Preprocessor::makeModuleVisible(Module *M, SourceLocation Loc) {
+  if (VisibleModules.isVisible(M))
+    return;
+
+  VisibleModules.setVisible(
+      M, Loc, [](Module *) {},
+      [&](ArrayRef<Module *> Path, Module *Conflict, StringRef Message) {
+        // FIXME: Include the path in the diagnostic.
+        // FIXME: Include the import location for the conflicting module.
+        Diag(ModuleImportLoc, diag::warn_module_conflict)
+            << Path[0]->getFullModuleName()
+            << Conflict->getFullModuleName()
+            << Message;
+      });
+
+  // Add this module to the imports list of the currently-built submodule.
+  if (!BuildingSubmoduleStack.empty())
+    BuildingSubmoduleStack.back().M->Imports.push_back(M);
+}
+
 bool Preprocessor::FinishLexStringLiteral(Token &Result, std::string &String,
                                           const char *DiagnosticTag,
                                           bool AllowMacroExpansion) {