Implement a minor optimization when loading module maps to satisfy a
module import: don't re-check for a loaded module unless we've
actually loaded a new module map file. Already-loaded module map files
aren't interesting.



git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@144435 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Lex/HeaderSearch.cpp b/lib/Lex/HeaderSearch.cpp
index 3e4a1d3..9d36f97 100644
--- a/lib/Lex/HeaderSearch.cpp
+++ b/lib/Lex/HeaderSearch.cpp
@@ -137,22 +137,22 @@
       if (!SearchDirs[Idx].isNormalDir())
         continue;
       
-      // Search for a module map in this directory, if we haven't already
-      // looked there.
-      if (!loadModuleMapFile(SearchDirs[Idx].getDir())) {
-        // If we found a module map, look for the module again.
+      // Search for a module map file in this directory.
+      if (loadModuleMapFile(SearchDirs[Idx].getDir()) == LMM_NewlyLoaded) {
+        // We just loaded a module map file; check whether the module is
+        // available now.
         Module = ModMap.findModule(ModuleName);
         if (Module)
           break;
       }
-      
+                
       // Search for a module map in a subdirectory with the same name as the
       // module.
       llvm::SmallString<128> NestedModuleMapDirName;
       NestedModuleMapDirName = SearchDirs[Idx].getDir()->getName();
       llvm::sys::path::append(NestedModuleMapDirName, ModuleName);
-      if (!loadModuleMapFile(NestedModuleMapDirName)) {
-        // If we found a module map, look for the module again.
+      if (loadModuleMapFile(NestedModuleMapDirName) == LMM_NewlyLoaded) {
+        // If we just loaded a module map file, look for the module again.
         Module = ModMap.findModule(ModuleName);
         if (Module)
           break;        
@@ -766,13 +766,19 @@
       return false;
     
     // Try to load the module map file in this directory.
-    if (!loadModuleMapFile(Dir)) {      
+    switch (loadModuleMapFile(Dir)) {
+    case LMM_NewlyLoaded:
+    case LMM_AlreadyLoaded:
       // Success. All of the directories we stepped through inherit this module
       // map file.
       for (unsigned I = 0, N = FixUpDirectories.size(); I != N; ++I)
         DirectoryHasModuleMap[FixUpDirectories[I]] = true;
       
       return true;
+
+    case LMM_NoDirectory:
+    case LMM_InvalidModuleMap:
+      break;
     }
 
     // If we hit the top of our search, we're done.
@@ -794,18 +800,20 @@
   return StringRef();
 }
 
-bool HeaderSearch::loadModuleMapFile(StringRef DirName) {
+HeaderSearch::LoadModuleMapResult 
+HeaderSearch::loadModuleMapFile(StringRef DirName) {
   if (const DirectoryEntry *Dir = FileMgr.getDirectory(DirName))
     return loadModuleMapFile(Dir);
   
-  return true;
+  return LMM_NoDirectory;
 }
 
-bool HeaderSearch::loadModuleMapFile(const DirectoryEntry *Dir) {
+HeaderSearch::LoadModuleMapResult 
+HeaderSearch::loadModuleMapFile(const DirectoryEntry *Dir) {
   llvm::DenseMap<const DirectoryEntry *, bool>::iterator KnownDir
     = DirectoryHasModuleMap.find(Dir);
   if (KnownDir != DirectoryHasModuleMap.end())
-    return !KnownDir->second;
+    return KnownDir->second? LMM_AlreadyLoaded : LMM_InvalidModuleMap;
   
   llvm::SmallString<128> ModuleMapFileName;
   ModuleMapFileName += Dir->getName();
@@ -816,12 +824,12 @@
       // This directory has a module map.
       DirectoryHasModuleMap[Dir] = true;
       
-      return false;
+      return LMM_NewlyLoaded;
     }
   }
   
   // No suitable module map.
   DirectoryHasModuleMap[Dir] = false;
-  return true;
+  return LMM_InvalidModuleMap;
 }