[cfe-commits] r149197 - in /cfe/trunk: include/clang/Lex/HeaderSearch.h lib/Frontend/CompilerInstance.cpp lib/Frontend/FrontendActions.cpp lib/Lex/HeaderSearch.cpp lib/Lex/Preprocessor.cpp

Douglas Gregor dgregor at apple.com
Sun Jan 29 09:08:12 PST 2012


Author: dgregor
Date: Sun Jan 29 11:08:11 2012
New Revision: 149197

URL: http://llvm.org/viewvc/llvm-project?rev=149197&view=rev
Log:
Rework HeaderSearch's interface for getting a module from a name and
for getting the name of the module file, unifying the code for
searching for a module with a given name (into lookupModule()) and
separating out the mapping to a module file (into
getModuleFileName()). No functionality change.

Modified:
    cfe/trunk/include/clang/Lex/HeaderSearch.h
    cfe/trunk/lib/Frontend/CompilerInstance.cpp
    cfe/trunk/lib/Frontend/FrontendActions.cpp
    cfe/trunk/lib/Lex/HeaderSearch.cpp
    cfe/trunk/lib/Lex/Preprocessor.cpp

Modified: cfe/trunk/include/clang/Lex/HeaderSearch.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Lex/HeaderSearch.h?rev=149197&r1=149196&r2=149197&view=diff
==============================================================================
--- cfe/trunk/include/clang/Lex/HeaderSearch.h (original)
+++ cfe/trunk/include/clang/Lex/HeaderSearch.h Sun Jan 29 11:08:11 2012
@@ -184,6 +184,9 @@
   explicit HeaderSearch();
   explicit HeaderSearch(const HeaderSearch&);
   void operator=(const HeaderSearch&);
+  
+  friend class DirectoryLookup;
+  
 public:
   HeaderSearch(FileManager &FM, DiagnosticsEngine &Diags,
                const LangOptions &LangOpts);
@@ -343,20 +346,34 @@
   /// FileEntry, uniquing them through the the 'HeaderMaps' datastructure.
   const HeaderMap *CreateHeaderMap(const FileEntry *FE);
 
-  /// \brief Search in the module cache path for a module with the given
-  /// name.
+  /// \brief Retrieve the name of the module file that should be used to 
+  /// load the given module.
   ///
-  /// \param Module The module that was found with the given name, which 
-  /// describes the module and how to build it.
+  /// \param Module The module whose module file name will be returned.
   ///
-  /// \param If non-NULL, will be set to the module file name we expected to
-  /// find (regardless of whether it was actually found or not).
+  /// \returns The name of the module file that corresponds to this module,
+  /// or an empty string if this module does not correspond to any module file.
+  std::string getModuleFileName(Module *Module);
+
+  /// \brief Retrieve the name of the module file that should be used to 
+  /// load a module with the given name.
   ///
-  /// \returns A file describing the named module, if already available in the
-  /// cases, or NULL to indicate that the module could not be found.
-  const FileEntry *lookupModule(StringRef ModuleName,
-                                Module *&Module,
-                                std::string *ModuleFileName = 0);
+  /// \param Module The module whose module file name will be returned.
+  ///
+  /// \returns The name of the module file that corresponds to this module,
+  /// or an empty string if this module does not correspond to any module file.
+  std::string getModuleFileName(StringRef ModuleName);
+
+  /// \brief Lookup a module Search for a module with the given name.
+  ///
+  /// \param ModuleName The name of the module we're looking for.
+  ///
+  /// \param AllowSearch Whether we are allowed to search in the various
+  /// search directories to produce a module definition. If not, this lookup
+  /// will only return an already-known module.
+  ///
+  /// \returns The module with the given name.
+  Module *lookupModule(StringRef ModuleName, bool AllowSearch = true);
   
   void IncrementFrameworkLookupCount() { ++NumFrameworkLookups; }
 
@@ -383,17 +400,8 @@
   ///
   /// \returns true if an error occurred, false otherwise.
   bool loadModuleMapFile(const FileEntry *File);
-  
-  /// \brief Retrieve a module with the given name.
-  ///
-  /// \param Name The name of the module to retrieve.
-  ///
-  /// \param AllowSearch If true, we're allowed to look for module maps within
-  /// the header search path. Otherwise, the module must already be known.
-  ///
-  /// \returns The module, if found; otherwise, null.
-  Module *getModule(StringRef Name, bool AllowSearch = true);
 
+private:
   /// \brief Retrieve a module with the given name, which may be part of the
   /// given framework.
   ///
@@ -405,10 +413,11 @@
   /// frameworks.
   ///
   /// \returns The module, if found; otherwise, null.
-  Module *getFrameworkModule(StringRef Name, 
-                             const DirectoryEntry *Dir,
-                             bool IsSystem);
-
+  Module *loadFrameworkModule(StringRef Name, 
+                              const DirectoryEntry *Dir,
+                              bool IsSystem);
+  
+public:
   /// \brief Retrieve the module map.
   ModuleMap &getModuleMap() { return ModMap; }
   

Modified: cfe/trunk/lib/Frontend/CompilerInstance.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/CompilerInstance.cpp?rev=149197&r1=149196&r2=149197&view=diff
==============================================================================
--- cfe/trunk/lib/Frontend/CompilerInstance.cpp (original)
+++ cfe/trunk/lib/Frontend/CompilerInstance.cpp Sun Jan 29 11:08:11 2012
@@ -1116,11 +1116,25 @@
     Known = KnownModules.insert(std::make_pair(Path[0].first, Module)).first;
   } else {
     // Search for a module with the given name.
+    Module = PP->getHeaderSearchInfo().lookupModule(ModuleName);
     std::string ModuleFileName;
-    ModuleFile
-      = PP->getHeaderSearchInfo().lookupModule(ModuleName, Module,
-                                               &ModuleFileName);
-
+    if (Module)
+      ModuleFileName = PP->getHeaderSearchInfo().getModuleFileName(Module);
+    else
+      ModuleFileName = PP->getHeaderSearchInfo().getModuleFileName(ModuleName);
+    
+    if (ModuleFileName.empty()) {
+      getDiagnostics().Report(ModuleNameLoc, diag::err_module_not_found)
+        << ModuleName
+        << SourceRange(ImportLoc, ModuleNameLoc);
+      LastModuleImportLoc = ImportLoc;
+      LastModuleImportResult = 0;
+      return 0;
+    }
+    
+    const FileEntry *ModuleFile
+      = getFileManager().getFile(ModuleFileName, /*OpenFile=*/false,
+                                 /*CacheFailure=*/false);
     bool BuildingModule = false;
     if (!ModuleFile && Module) {
       // The module is not cached, but we have a module map from which we can

Modified: cfe/trunk/lib/Frontend/FrontendActions.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/FrontendActions.cpp?rev=149197&r1=149196&r2=149197&view=diff
==============================================================================
--- cfe/trunk/lib/Frontend/FrontendActions.cpp (original)
+++ cfe/trunk/lib/Frontend/FrontendActions.cpp Sun Jan 29 11:08:11 2012
@@ -226,7 +226,8 @@
   }
   
   // Dig out the module definition.
-  Module = HS.getModule(CI.getLangOpts().CurrentModule, /*AllowSearch=*/false);
+  Module = HS.lookupModule(CI.getLangOpts().CurrentModule, 
+                           /*AllowSearch=*/false);
   if (!Module) {
     CI.getDiagnostics().Report(diag::err_missing_module)
       << CI.getLangOpts().CurrentModule << Filename;

Modified: cfe/trunk/lib/Lex/HeaderSearch.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Lex/HeaderSearch.cpp?rev=149197&r1=149196&r2=149197&view=diff
==============================================================================
--- cfe/trunk/lib/Lex/HeaderSearch.cpp (original)
+++ cfe/trunk/lib/Lex/HeaderSearch.cpp Sun Jan 29 11:08:11 2012
@@ -103,79 +103,81 @@
   return 0;
 }
 
-const FileEntry *HeaderSearch::lookupModule(StringRef ModuleName,
-                                            Module *&Module,
-                                            std::string *ModuleFileName) {
-  Module = 0;
-  
+std::string HeaderSearch::getModuleFileName(Module *Module) {
   // If we don't have a module cache path, we can't do anything.
-  if (ModuleCachePath.empty()) {
-    if (ModuleFileName)
-      ModuleFileName->clear();
-    return 0;
-  }
+  if (ModuleCachePath.empty()) 
+    return std::string();
+
+
+  llvm::SmallString<256> Result(ModuleCachePath);
+  llvm::sys::path::append(Result, Module->getTopLevelModule()->Name + ".pcm");
+  return Result.str().str();
+}
+
+std::string HeaderSearch::getModuleFileName(StringRef ModuleName) {
+  // If we don't have a module cache path, we can't do anything.
+  if (ModuleCachePath.empty()) 
+    return std::string();
   
-  // Try to find the module path.
-  llvm::SmallString<256> FileName(ModuleCachePath);
-  llvm::sys::path::append(FileName, ModuleName + ".pcm");
-  if (ModuleFileName)
-    *ModuleFileName = FileName.str();
-      
+  
+  llvm::SmallString<256> Result(ModuleCachePath);
+  llvm::sys::path::append(Result, ModuleName + ".pcm");
+  return Result.str().str();
+}
+
+Module *HeaderSearch::lookupModule(StringRef ModuleName, bool AllowSearch) {
   // Look in the module map to determine if there is a module by this name.
-  Module = ModMap.findModule(ModuleName);
-  if (!Module) {
-    // Look through the various header search paths to load any avaiable module 
-    // maps, searching for a module map that describes this module.
-    for (unsigned Idx = 0, N = SearchDirs.size(); Idx != N; ++Idx) {
-      if (SearchDirs[Idx].isFramework()) {
-        // Search for or infer a module map for a framework.
-        llvm::SmallString<128> FrameworkDirName;
-        FrameworkDirName += SearchDirs[Idx].getFrameworkDir()->getName();
-        llvm::sys::path::append(FrameworkDirName, ModuleName + ".framework");
-        if (const DirectoryEntry *FrameworkDir 
-              = FileMgr.getDirectory(FrameworkDirName)) {
-          bool IsSystem
-            = SearchDirs[Idx].getDirCharacteristic() != SrcMgr::C_User;
-          Module = getFrameworkModule(ModuleName, FrameworkDir, IsSystem);
-          if (Module)
-            break;
-        }
-      }
-      
-      // FIXME: Figure out how header maps and module maps will work together.
-      
-      // Only deal with normal search directories.
-      if (!SearchDirs[Idx].isNormalDir())
-        continue;
-      
-      // 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);
+  Module *Module = ModMap.findModule(ModuleName);
+  if (Module || !AllowSearch)
+    return Module;
+  
+  // Look through the various header search paths to load any avai;able module 
+  // maps, searching for a module map that describes this module.
+  for (unsigned Idx = 0, N = SearchDirs.size(); Idx != N; ++Idx) {
+    if (SearchDirs[Idx].isFramework()) {
+      // Search for or infer a module map for a framework.
+      llvm::SmallString<128> FrameworkDirName;
+      FrameworkDirName += SearchDirs[Idx].getFrameworkDir()->getName();
+      llvm::sys::path::append(FrameworkDirName, ModuleName + ".framework");
+      if (const DirectoryEntry *FrameworkDir 
+            = FileMgr.getDirectory(FrameworkDirName)) {
+        bool IsSystem
+          = SearchDirs[Idx].getDirCharacteristic() != SrcMgr::C_User;
+        Module = loadFrameworkModule(ModuleName, FrameworkDir, IsSystem);
         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) == LMM_NewlyLoaded) {
-        // If we just loaded a module map file, look for the module again.
-        Module = ModMap.findModule(ModuleName);
-        if (Module)
-          break;        
-      }
+    }
+    
+    // FIXME: Figure out how header maps and module maps will work together.
+    
+    // Only deal with normal search directories.
+    if (!SearchDirs[Idx].isNormalDir())
+      continue;
+    
+    // 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) == LMM_NewlyLoaded) {
+      // If we just loaded a module map file, look for the module again.
+      Module = ModMap.findModule(ModuleName);
+      if (Module)
+        break;
     }
   }
-
-  // Look for the module file in the module cache.
-  // FIXME: If we didn't find a description of the module itself, should we
-  // even try to find the module in the cache?
-  return getFileMgr().getFile(FileName, /*OpenFile=*/false,
-                              /*CacheFailure=*/false);
+  
+  return Module;
 }
 
 //===----------------------------------------------------------------------===//
@@ -323,7 +325,7 @@
     if (const DirectoryEntry *FrameworkDir
                                         = FileMgr.getDirectory(FrameworkName)) {
       bool IsSystem = getDirCharacteristic() != SrcMgr::C_User;
-      Module = HS.getFrameworkModule(ModuleName, FrameworkDir, IsSystem);
+      Module = HS.loadFrameworkModule(ModuleName, FrameworkDir, IsSystem);
     }
   }
   
@@ -834,36 +836,9 @@
   return Result;
 }
 
-Module *HeaderSearch::getModule(StringRef Name, bool AllowSearch) {
-  if (Module *Module = ModMap.findModule(Name))
-    return Module;
-  
-  if (!AllowSearch)
-    return 0;
-  
-  for (unsigned I = 0, N = SearchDirs.size(); I != N; ++I) {
-    if (!SearchDirs[I].isNormalDir())
-      continue;
-    
-    switch (loadModuleMapFile(SearchDirs[I].getDir())) {
-    case LMM_AlreadyLoaded:
-    case LMM_InvalidModuleMap:
-    case LMM_NoDirectory:
-      break;
-        
-    case LMM_NewlyLoaded:
-      if (Module *Module = ModMap.findModule(Name))
-        return Module;
-      break;
-    }
-  }
-  
-  return 0;
-}
-  
-Module *HeaderSearch::getFrameworkModule(StringRef Name, 
-                                         const DirectoryEntry *Dir,
-                                         bool IsSystem) {
+Module *HeaderSearch::loadFrameworkModule(StringRef Name, 
+                                          const DirectoryEntry *Dir,
+                                          bool IsSystem) {
   if (Module *Module = ModMap.findModule(Name))
     return Module;
   

Modified: cfe/trunk/lib/Lex/Preprocessor.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Lex/Preprocessor.cpp?rev=149197&r1=149196&r2=149197&view=diff
==============================================================================
--- cfe/trunk/lib/Lex/Preprocessor.cpp (original)
+++ cfe/trunk/lib/Lex/Preprocessor.cpp Sun Jan 29 11:08:11 2012
@@ -383,7 +383,7 @@
   if (getLangOptions().CurrentModule.empty())
     return 0;
   
-  return getHeaderSearchInfo().getModule(getLangOptions().CurrentModule);
+  return getHeaderSearchInfo().lookupModule(getLangOptions().CurrentModule);
 }
 
 //===----------------------------------------------------------------------===//





More information about the cfe-commits mailing list