From 80b6904baf371fb587e166df221994a44f4a27a4 Mon Sep 17 00:00:00 2001 From: Douglas Gregor Date: Sat, 12 Nov 2011 00:22:19 +0000 Subject: [PATCH] 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. llvm-svn: 144435 --- clang/include/clang/Lex/HeaderSearch.h | 28 +++++++++++++++++--- clang/lib/Lex/HeaderSearch.cpp | 36 ++++++++++++++++---------- 2 files changed, 46 insertions(+), 18 deletions(-) diff --git a/clang/include/clang/Lex/HeaderSearch.h b/clang/include/clang/Lex/HeaderSearch.h index 98286c26f2c5..092d5470911d 100644 --- a/clang/include/clang/Lex/HeaderSearch.h +++ b/clang/include/clang/Lex/HeaderSearch.h @@ -408,15 +408,35 @@ public: size_t getTotalMemory() const; private: + /// \brief Describes what happened when we tried to load a module map file. + enum LoadModuleMapResult { + /// \brief The module map file had already been loaded. + LMM_AlreadyLoaded, + /// \brief The module map file was loaded by this invocation. + LMM_NewlyLoaded, + /// \brief There is was directory with the given name. + LMM_NoDirectory, + /// \brief There was either no module map file or the module map file was + /// invalid. + LMM_InvalidModuleMap + }; + /// \brief Try to load the module map file in the given directory. /// - /// \returns false if the module map was loaded successfully, true otherwise. - bool loadModuleMapFile(StringRef DirName); + /// \param DirName The name of the directory where we will look for a module + /// map file. + /// + /// \returns The result of attempting to load the module map file from the + /// named directory. + LoadModuleMapResult loadModuleMapFile(StringRef DirName); /// \brief Try to load the module map file in the given directory. /// - /// \returns false if the module map was loaded successfully, true otherwise. - bool loadModuleMapFile(const DirectoryEntry *Dir); + /// \param Dir The directory where we will look for a module map file. + /// + /// \returns The result of attempting to load the module map file from the + /// named directory. + LoadModuleMapResult loadModuleMapFile(const DirectoryEntry *Dir); /// getFileInfo - Return the HeaderFileInfo structure for the specified /// FileEntry. diff --git a/clang/lib/Lex/HeaderSearch.cpp b/clang/lib/Lex/HeaderSearch.cpp index 3e4a1d32a0c7..9d36f9733530 100644 --- a/clang/lib/Lex/HeaderSearch.cpp +++ b/clang/lib/Lex/HeaderSearch.cpp @@ -137,22 +137,22 @@ const FileEntry *HeaderSearch::lookupModule(StringRef ModuleName, 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 @@ bool HeaderSearch::hasModuleMap(StringRef FileName, 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 @@ StringRef HeaderSearch::findModuleForHeader(const FileEntry *File) { 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::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 @@ bool HeaderSearch::loadModuleMapFile(const DirectoryEntry *Dir) { // 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; }