From c39fc75bc09a25f265d08e7f7e3b5e93b3efb1e5 Mon Sep 17 00:00:00 2001
From: Richard Smith <richard-llvm@metafoo.co.uk>
Date: Fri, 23 Jan 2015 00:01:13 +0000
Subject: [PATCH] PR22299: Relocate code for handling -fmodule-map-file= so
 that we don't try to produce diagnostics with source locations before the
 diagnostics system is ready for them.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@226882 91177308-0d34-0410-b5e6-96231b3b80d8
---
 lib/Frontend/CompilerInstance.cpp  | 8 --------
 lib/Frontend/FrontendAction.cpp    | 9 +++++++++
 test/Modules/diagnostics.modulemap | 8 ++++++++
 3 files changed, 17 insertions(+), 8 deletions(-)
 create mode 100644 test/Modules/diagnostics.modulemap

diff --git a/lib/Frontend/CompilerInstance.cpp b/lib/Frontend/CompilerInstance.cpp
index 93a34b72227..041a1e95225 100644
--- a/lib/Frontend/CompilerInstance.cpp
+++ b/lib/Frontend/CompilerInstance.cpp
@@ -371,14 +371,6 @@ void CompilerInstance::createPreprocessor(TranslationUnitKind TUKind) {
     AttachHeaderIncludeGen(*PP, /*ShowAllHeaders=*/false, /*OutputPath=*/"",
                            /*ShowDepth=*/true, /*MSStyle=*/true);
   }
-
-  // Load all explictly-specified module map files.
-  for (const auto &Filename : getFrontendOpts().ModuleMapFiles) {
-    if (auto *File = getFileManager().getFile(Filename))
-      PP->getHeaderSearchInfo().loadModuleMapFile(File, /*IsSystem*/false);
-    else
-      getDiagnostics().Report(diag::err_module_map_not_found) << Filename;
-  }
 }
 
 // ASTContext
diff --git a/lib/Frontend/FrontendAction.cpp b/lib/Frontend/FrontendAction.cpp
index c81c81aba4d..0bb5d87407b 100644
--- a/lib/Frontend/FrontendAction.cpp
+++ b/lib/Frontend/FrontendAction.cpp
@@ -383,6 +383,15 @@ bool FrontendAction::BeginSourceFile(CompilerInstance &CI,
            "doesn't support modules");
   }
 
+  // If we were asked to load any module map files, do so now.
+  for (const auto &Filename : CI.getFrontendOpts().ModuleMapFiles) {
+    if (auto *File = CI.getFileManager().getFile(Filename))
+      CI.getPreprocessor().getHeaderSearchInfo().loadModuleMapFile(
+          File, /*IsSystem*/false);
+    else
+      CI.getDiagnostics().Report(diag::err_module_map_not_found) << Filename;
+  }
+
   // If we were asked to load any module files, do so now.
   for (const auto &ModuleFile : CI.getFrontendOpts().ModuleFiles)
     if (!CI.loadModuleFile(ModuleFile))
diff --git a/test/Modules/diagnostics.modulemap b/test/Modules/diagnostics.modulemap
new file mode 100644
index 00000000000..e28239fea36
--- /dev/null
+++ b/test/Modules/diagnostics.modulemap
@@ -0,0 +1,8 @@
+// RUN: cp %s %t-duplicate.modulemap
+// RUN: not %clang_cc1 -fmodules -fmodules-cache-path=%t -fmodule-map-file=%s -fmodule-map-file=%t-duplicate.modulemap -fsyntax-only -x c++ /dev/null 2>&1 | FileCheck --check-prefix=CHECK-DUPLICATE %s
+
+// PR22299: Ensure we can produce diagnostics for duplicate modules from -fmodule-map-file=.
+//
+// CHECK-DUPLICATE: duplicate.modulemap:[[@LINE+2]]:8: error: redefinition of module 'foo'
+// CHECK-DUPLICATE: diagnostics.modulemap:[[@LINE+1]]:8: note: previously defined here
+module foo {}
-- 
GitLab