[cfe-commits] r145415 - in /cfe/trunk: include/clang/Lex/ModuleMap.h lib/Frontend/CompilerInstance.cpp lib/Lex/ModuleMap.cpp test/Modules/normal-module-map.cpp

Douglas Gregor dgregor at apple.com
Tue Nov 29 11:06:37 PST 2011


Author: dgregor
Date: Tue Nov 29 13:06:37 2011
New Revision: 145415

URL: http://llvm.org/viewvc/llvm-project?rev=145415&view=rev
Log:
Teach the module import mechanism how to rebuild modules expressed via
module map, rather than assuming that there is an umbrella
header. This allows us to automatically build umbrella-less modules. 

Modified:
    cfe/trunk/include/clang/Lex/ModuleMap.h
    cfe/trunk/lib/Frontend/CompilerInstance.cpp
    cfe/trunk/lib/Lex/ModuleMap.cpp
    cfe/trunk/test/Modules/normal-module-map.cpp

Modified: cfe/trunk/include/clang/Lex/ModuleMap.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Lex/ModuleMap.h?rev=145415&r1=145414&r2=145415&view=diff
==============================================================================
--- cfe/trunk/include/clang/Lex/ModuleMap.h (original)
+++ cfe/trunk/include/clang/Lex/ModuleMap.h Tue Nov 29 13:06:37 2011
@@ -166,7 +166,16 @@
   /// framework directory.
   Module *inferFrameworkModule(StringRef ModuleName, 
                                const DirectoryEntry *FrameworkDir);
-                               
+  
+  /// \brief Retrieve the module map file containing the definition of the given
+  /// module.
+  ///
+  /// \param Module The module whose module map file will be returned, if known.
+  ///
+  /// \returns The file entry for the module map file containing the given
+  /// module, or NULL if the module definition was inferred.
+  const FileEntry *getContainingModuleMapFile(ModuleMap::Module *Module);
+  
   /// \brief Parse the given module map file, and record any modules we 
   /// encounter.
   ///

Modified: cfe/trunk/lib/Frontend/CompilerInstance.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/CompilerInstance.cpp?rev=145415&r1=145414&r2=145415&view=diff
==============================================================================
--- cfe/trunk/lib/Frontend/CompilerInstance.cpp (original)
+++ cfe/trunk/lib/Frontend/CompilerInstance.cpp Tue Nov 29 13:06:37 2011
@@ -710,6 +710,21 @@
 }
 
 namespace {
+  struct CompileModuleMapData {
+    CompilerInstance &Instance;
+    GenerateModuleAction &CreateModuleAction;
+  };
+}
+
+/// \brief Helper function that executes the module-generating action under
+/// a crash recovery context.
+static void doCompileMapModule(void *UserData) {
+  CompileModuleMapData &Data
+    = *reinterpret_cast<CompileModuleMapData *>(UserData);
+  Data.Instance.ExecuteAction(Data.CreateModuleAction);
+}
+
+namespace {
   /// \brief Class that manages the creation of a lock file to aid
   /// implicit coordination between different processes.
   ///
@@ -958,17 +973,11 @@
   // Give up.
 }
 
-/// \brief Compile a module file for the given module name with the given
-/// umbrella header, using the options provided by the importing compiler
-/// instance.
+/// \brief Compile a module file for the given module, using the options 
+/// provided by the importing compiler instance.
 static void compileModule(CompilerInstance &ImportingInstance,
                           ModuleMap::Module *Module,
                           StringRef ModuleFileName) {
-  // FIXME: Currently, we can only handle modules that have an umbrella 
-  // header. That's lame.
-  if (!Module->UmbrellaHeader)
-    return;
-  
   LockFileManager Locked(ModuleFileName);
   switch (Locked) {
   case LockFileManager::LFS_Error:
@@ -985,6 +994,9 @@
     break;
   }
 
+  ModuleMap &ModMap 
+    = ImportingInstance.getPreprocessor().getHeaderSearchInfo().getModuleMap();
+    
   // Construct a compiler invocation for creating this module.
   llvm::IntrusiveRefCntPtr<CompilerInvocation> Invocation
     (new CompilerInvocation(ImportingInstance.getInvocation()));
@@ -1002,6 +1014,50 @@
   Invocation->getPreprocessorOpts().ModuleBuildPath
     .push_back(Module->getTopLevelModuleName());
 
+  if (const FileEntry *ModuleMapFile
+                                  = ModMap.getContainingModuleMapFile(Module)) {
+    // If there is a module map file, build the module using the module map.
+    // Set up the inputs/outputs so that we build the module from its umbrella
+    // header.
+    FrontendOptions &FrontendOpts = Invocation->getFrontendOpts();
+    FrontendOpts.OutputFile = ModuleFileName.str();
+    FrontendOpts.DisableFree = false;
+    FrontendOpts.Inputs.clear();
+    FrontendOpts.Inputs.push_back(
+      std::make_pair(getSourceInputKindFromOptions(*Invocation->getLangOpts()),
+                     ModuleMapFile->getName()));
+    
+    Invocation->getDiagnosticOpts().VerifyDiagnostics = 0;
+    
+    
+    assert(ImportingInstance.getInvocation().getModuleHash() ==
+           Invocation->getModuleHash() && "Module hash mismatch!");
+    
+    // Construct a compiler instance that will be used to actually create the
+    // module.
+    CompilerInstance Instance;
+    Instance.setInvocation(&*Invocation);
+    Instance.createDiagnostics(/*argc=*/0, /*argv=*/0,
+                               &ImportingInstance.getDiagnosticClient(),
+                               /*ShouldOwnClient=*/true,
+                               /*ShouldCloneClient=*/true);
+    
+    // Construct a module-generating action.
+    GenerateModuleAction CreateModuleAction;
+    
+    // Execute the action to actually build the module in-place. Use a separate
+    // thread so that we get a stack large enough.
+    const unsigned ThreadStackSize = 8 << 20;
+    llvm::CrashRecoveryContext CRC;
+    CompileModuleMapData Data = { Instance, CreateModuleAction };
+    CRC.RunSafelyOnThread(&doCompileMapModule, &Data, ThreadStackSize);
+    return;
+  } 
+  
+  // FIXME: Temporary fallback: generate the module from the umbrella header.
+  // This is currently used when we infer a module map from a framework.
+  assert(Module->UmbrellaHeader && "Inferred module map needs umbrella header");
+
   // Set up the inputs/outputs so that we build the module from its umbrella
   // header.
   FrontendOptions &FrontendOpts = Invocation->getFrontendOpts();

Modified: cfe/trunk/lib/Lex/ModuleMap.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Lex/ModuleMap.cpp?rev=145415&r1=145414&r2=145415&view=diff
==============================================================================
--- cfe/trunk/lib/Lex/ModuleMap.cpp (original)
+++ cfe/trunk/lib/Lex/ModuleMap.cpp Tue Nov 29 13:06:37 2011
@@ -206,6 +206,15 @@
   return Result;
 }
 
+const FileEntry *
+ModuleMap::getContainingModuleMapFile(ModuleMap::Module *Module) {
+  if (Module->DefinitionLoc.isInvalid() || !SourceMgr)
+    return 0;
+
+  return SourceMgr->getFileEntryForID(
+           SourceMgr->getFileID(Module->DefinitionLoc));
+}
+
 void ModuleMap::dump() {
   llvm::errs() << "Modules:";
   for (llvm::StringMap<Module *>::iterator M = Modules.begin(), 

Modified: cfe/trunk/test/Modules/normal-module-map.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Modules/normal-module-map.cpp?rev=145415&r1=145414&r2=145415&view=diff
==============================================================================
--- cfe/trunk/test/Modules/normal-module-map.cpp (original)
+++ cfe/trunk/test/Modules/normal-module-map.cpp Tue Nov 29 13:06:37 2011
@@ -1,8 +1,4 @@
 // RUN: rm -rf %t
-// FIXME: Eventually, we should be able to remove these explicit module creation lines
-// RUN: %clang_cc1 -x objective-c -fmodule-cache-path %t -fmodule-name=libA -emit-module-from-map %S/Inputs/normal-module-map/module.map
-// RUN: %clang_cc1 -x objective-c -fmodule-cache-path %t -fmodule-name=libB -emit-module-from-map %S/Inputs/normal-module-map/module.map
-// RUN: %clang_cc1 -x objective-c -fmodule-cache-path %t -fmodule-name=libNested -emit-module-from-map %S/Inputs/normal-module-map/nested/module.map
 // RUN: %clang_cc1 -x objective-c -fmodule-cache-path %t -fauto-module-import -I %S/Inputs/normal-module-map %s -verify
 #include "Umbrella/umbrella_sub.h"
 





More information about the cfe-commits mailing list