]> granicus.if.org Git - clang/commitdiff
Allow a header to be part of multiple modules.
authorDaniel Jasper <djasper@google.com>
Tue, 22 Oct 2013 08:09:47 +0000 (08:09 +0000)
committerDaniel Jasper <djasper@google.com>
Tue, 22 Oct 2013 08:09:47 +0000 (08:09 +0000)
This patch changes two things:

a) Allow a header to be part of multiple modules. The reasoning is that
in existing codebases that have a module-like build system, the same
headers might be used in several build targets. Simple reasons might be
that they defined different classes that are declared in the same
header. Supporting a header as a part of multiple modules will make the
transistion easier for those cases. A later step in clang can then
determine whether the two modules are actually compatible and can be
merged and error out appropriately. The later check is similar to what
needs to be done for template specializations anyway.

b) Allow modules to be stored in a directory tree separate from the
headers they describe.

Review: http://llvm-reviews.chandlerc.com/D1951

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@193151 91177308-0d34-0410-b5e6-96231b3b80d8

18 files changed:
include/clang/Basic/DiagnosticLexKinds.td
include/clang/Lex/ModuleMap.h
include/clang/Lex/Preprocessor.h
lib/Lex/HeaderSearch.cpp
lib/Lex/ModuleMap.cpp
lib/Lex/PPDirectives.cpp
test/Modules/Inputs/modular_maps/common.h [new file with mode: 0644]
test/Modules/Inputs/modular_maps/modulea.map
test/Modules/Inputs/modular_maps/moduleb.map
test/Modules/Inputs/separate_map_tree/maps/modulea.map [new file with mode: 0644]
test/Modules/Inputs/separate_map_tree/maps/moduleb.map [new file with mode: 0644]
test/Modules/Inputs/separate_map_tree/maps/modulec.map [new file with mode: 0644]
test/Modules/Inputs/separate_map_tree/src/common.h [new file with mode: 0644]
test/Modules/Inputs/separate_map_tree/src/private-in-c.h [new file with mode: 0644]
test/Modules/Inputs/separate_map_tree/src/public-in-b.h [new file with mode: 0644]
test/Modules/Inputs/separate_map_tree/src/public-in-c.h [new file with mode: 0644]
test/Modules/modular_maps.cpp
test/Modules/separate_map_tree.cpp [new file with mode: 0644]

index 2603307775adbc0a9e378bd52a9d711701c2ad19..871f5f65447d65157a93a0ecb6612d84be5a87d3 100644 (file)
@@ -551,8 +551,6 @@ def err_mmap_expected_mmap_file : Error<"expected a module map file name">;
 def err_mmap_module_redefinition : Error<
   "redefinition of module '%0'">;
 def note_mmap_prev_definition : Note<"previously defined here">;
-def err_mmap_header_conflict : Error<
-  "header '%0' is already part of module '%1'">;
 def err_mmap_header_not_found : Error<
   "%select{|umbrella }0header '%1' not found">;
 def err_mmap_umbrella_dir_not_found : Error<
index 90cfc6c7e1828c2bda0aa6f16265c49f074e6edd..b7982a17aa5608c429e63d9868cad43b6ffbc577 100644 (file)
@@ -106,7 +106,8 @@ public:
   };
 
 private:
-  typedef llvm::DenseMap<const FileEntry *, KnownHeader> HeadersMap;
+  typedef llvm::DenseMap<const FileEntry *, SmallVector<KnownHeader, 1> >
+  HeadersMap;
 
   /// \brief Mapping from each header to the module that owns the contents of
   /// that header.
@@ -208,10 +209,15 @@ public:
   ///
   /// \param File The header file that is likely to be included.
   ///
+  /// \param RequestingModule Specifies the module the header is intended to be
+  /// used from.  Used to disambiguate if a header is present in multiple
+  /// modules.
+  ///
   /// \returns The module KnownHeader, which provides the module that owns the
   /// given header file.  The KnownHeader is default constructed to indicate
   /// that no module owns this header file.
-  KnownHeader findModuleForHeader(const FileEntry *File);
+  KnownHeader findModuleForHeader(const FileEntry *File,
+                                  Module *RequestingModule = NULL);
 
   /// \brief Determine whether the given header is part of a module
   /// marked 'unavailable'.
index f9eae9763482a35dbaf69f8a24ea1e9fef2c7fab..adfc6f1951b66deba73b226a742fa8aae8b7e6e2 100644 (file)
@@ -1461,10 +1461,9 @@ private:
   /// \brief Verify that it is legal for the source file that \p FilenameLoc
   /// points to to include the file \p Filename.
   ///
-  /// Tries to reuse \p IncFileEnt and \p SuggestedModule.
+  /// Tries to reuse \p IncFileEnt.
   void verifyModuleInclude(SourceLocation FilenameLoc, StringRef Filename,
-                           const FileEntry *IncFileEnt,
-                           ModuleMap::KnownHeader *SuggestedModule);
+                           const FileEntry *IncFileEnt);
 
   // Macro handling.
   void HandleDefineDirective(Token &Tok, bool ImmediatelyAfterTopLevelIfndef);
index 5bcc4ea044159d48c22f13b2294e21ebfb44bf2f..84d205acdaeccd94a7f0f5ddf35fe8f9f8e637f9 100644 (file)
@@ -247,16 +247,18 @@ const FileEntry *DirectoryLookup::LookupFile(
     
     // If we have a module map that might map this header, load it and
     // check whether we'll have a suggestion for a module.
-    if (SuggestedModule &&
-        HS.hasModuleMap(TmpDir, getDir(), isSystemHeaderDirectory())) {
-      const FileEntry *File = HS.getFileMgr().getFile(TmpDir.str(), 
+    HS.hasModuleMap(TmpDir, getDir(), isSystemHeaderDirectory());
+    if (SuggestedModule) {
+      const FileEntry *File = HS.getFileMgr().getFile(TmpDir.str(),
                                                       /*openFile=*/false);
       if (!File)
         return File;
       
-      // If there is a module that corresponds to this header, 
-      // suggest it.
+      // If there is a module that corresponds to this header, suggest it.
       *SuggestedModule = HS.findModuleForHeader(File);
+      if (!SuggestedModule->getModule() &&
+          HS.hasModuleMap(TmpDir, getDir(), isSystemHeaderDirectory()))
+        *SuggestedModule = HS.findModuleForHeader(File);
       return File;
     }
     
@@ -503,6 +505,24 @@ const FileEntry *HeaderSearch::LookupFile(
     ModuleMap::KnownHeader *SuggestedModule,
     bool SkipCache)
 {
+  if (!HSOpts->ModuleMapFiles.empty()) {
+    // Preload all explicitly specified module map files. This enables modules
+    // map files lying in a directory structure separate from the header files
+    // that they describe. These cannot be loaded lazily upon encountering a
+    // header file, as there is no other knwon mapping from a header file to its
+    // module map file.
+    for (llvm::SetVector<std::string>::iterator
+             I = HSOpts->ModuleMapFiles.begin(),
+             E = HSOpts->ModuleMapFiles.end();
+         I != E; ++I) {
+      const FileEntry *File = FileMgr.getFile(*I);
+      if (!File)
+        continue;
+      loadModuleMapFile(File, /*IsSystem=*/false);
+    }
+    HSOpts->ModuleMapFiles.clear();
+  }
+
   if (SuggestedModule)
     *SuggestedModule = ModuleMap::KnownHeader();
     
@@ -946,43 +966,20 @@ bool HeaderSearch::hasModuleMap(StringRef FileName,
     const DirectoryEntry *Dir = FileMgr.getDirectory(DirName);
     if (!Dir)
       return false;
-    
-    // Load user-specified module map files in 'Dir'.
-    bool ModuleMapFound = false;
-    for (llvm::SetVector<std::string>::iterator
-             I = HSOpts->ModuleMapFiles.begin(),
-             E = HSOpts->ModuleMapFiles.end();
-         I != E; ++I) {
-      StringRef ModuleMapFileDir = llvm::sys::path::parent_path(*I);
-      if (!llvm::sys::fs::equivalent(ModuleMapFileDir, DirName))
-        continue;
-
-      const FileEntry *File = FileMgr.getFile(*I);
-      if (!File)
-        continue;
-
-      loadModuleMapFile(File, /*IsSystem=*/false);
-      ModuleMapFound = true;
-    }
 
     // Try to load the "module.map" file in this directory.
     switch (loadModuleMapFile(Dir, IsSystem)) {
     case LMM_NewlyLoaded:
     case LMM_AlreadyLoaded:
-      ModuleMapFound = true;
-      break;
-
-    case LMM_NoDirectory:
-    case LMM_InvalidModuleMap:
-      break;
-    }
-
-    if (ModuleMapFound) {
       // 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.
index a10679af158726bf128c83baa0f64e27c701ed19..25d68ace0f199871a262a5ad03ec4f477bc5b2b7 100644 (file)
@@ -168,14 +168,41 @@ static bool isBuiltinHeader(StringRef FileName) {
            .Default(false);
 }
 
-ModuleMap::KnownHeader ModuleMap::findModuleForHeader(const FileEntry *File) {
+ModuleMap::KnownHeader
+ModuleMap::findModuleForHeader(const FileEntry *File,
+                               Module *RequestingModule) {
   HeadersMap::iterator Known = Headers.find(File);
   if (Known != Headers.end()) {
-    // If a header is not available, don't report that it maps to anything.
-    if (!Known->second.isAvailable())
-      return KnownHeader();
+    ModuleMap::KnownHeader Result = KnownHeader();
+
+    // Iterate over all modules that 'File' is part of to find the best fit.
+    for (SmallVectorImpl<KnownHeader>::iterator I = Known->second.begin(),
+                                                E = Known->second.end();
+         I != E; ++I) {
+      // Cannot use a module if the header is excluded or unavailable in it.
+      if (I->getRole() == ModuleMap::ExcludedHeader ||
+          !I->getModule()->isAvailable())
+        continue;
 
-    return Known->second;
+      // If 'File' is part of 'RequestingModule', 'RequestingModule' is the
+      // module we are looking for.
+      if (I->getModule() == RequestingModule)
+        return *I;
+
+      // If uses need to be specified explicitly, we are only allowed to return
+      // modules that are explicitly used by the requesting module.
+      if (RequestingModule && LangOpts.ModulesDeclUse &&
+          std::find(RequestingModule->DirectUses.begin(),
+                    RequestingModule->DirectUses.end(),
+                    I->getModule()) == RequestingModule->DirectUses.end())
+        continue;
+      Result = *I;
+      // If 'File' is a public header of this module, this is as good as we
+      // are going to get.
+      if (I->getRole() == ModuleMap::NormalHeader)
+        break;
+    }
+    return Result;
   }
 
   // If we've found a builtin header within Clang's builtin include directory,
@@ -186,14 +213,8 @@ ModuleMap::KnownHeader ModuleMap::findModuleForHeader(const FileEntry *File) {
     HeaderInfo.loadTopLevelSystemModules();
 
     // Check again.
-    Known = Headers.find(File);
-    if (Known != Headers.end()) {
-      // If a header is not available, don't report that it maps to anything.
-      if (!Known->second.isAvailable())
-        return KnownHeader();
-
-      return Known->second;
-    }
+    if (Headers.find(File) != Headers.end())
+      return findModuleForHeader(File, RequestingModule);
   }
   
   const DirectoryEntry *Dir = File->getDir();
@@ -262,14 +283,14 @@ ModuleMap::KnownHeader ModuleMap::findModuleForHeader(const FileEntry *File) {
           UmbrellaDirs[SkippedDirs[I]] = Result;
       }
       
-      Headers[File] = KnownHeader(Result, NormalHeader);
+      Headers[File].push_back(KnownHeader(Result, NormalHeader));
 
       // If a header corresponds to an unavailable module, don't report
       // that it maps to anything.
       if (!Result->isAvailable())
         return KnownHeader();
 
-      return Headers[File];
+      return Headers[File].back();
     }
     
     SkippedDirs.push_back(Dir);
@@ -288,8 +309,16 @@ ModuleMap::KnownHeader ModuleMap::findModuleForHeader(const FileEntry *File) {
 
 bool ModuleMap::isHeaderInUnavailableModule(const FileEntry *Header) const {
   HeadersMap::const_iterator Known = Headers.find(Header);
-  if (Known != Headers.end())
-    return !Known->second.isAvailable();
+  if (Known != Headers.end()) {
+    for (SmallVectorImpl<KnownHeader>::const_iterator
+             I = Known->second.begin(),
+             E = Known->second.end();
+         I != E; ++I) {
+      if (I->isAvailable())
+        return false;
+    }
+    return true;
+  }
   
   const DirectoryEntry *Dir = Header->getDir();
   SmallVector<const DirectoryEntry *, 2> SkippedDirs;
@@ -534,7 +563,7 @@ ModuleMap::inferFrameworkModule(StringRef ModuleName,
   
   // umbrella header "umbrella-header-name"
   Result->Umbrella = UmbrellaHeader;
-  Headers[UmbrellaHeader] = KnownHeader(Result, NormalHeader);
+  Headers[UmbrellaHeader].push_back(KnownHeader(Result, NormalHeader));
   UmbrellaDirs[UmbrellaHeader->getDir()] = Result;
   
   // export *
@@ -598,7 +627,7 @@ ModuleMap::inferFrameworkModule(StringRef ModuleName,
 }
 
 void ModuleMap::setUmbrellaHeader(Module *Mod, const FileEntry *UmbrellaHeader){
-  Headers[UmbrellaHeader] = KnownHeader(Mod, NormalHeader);
+  Headers[UmbrellaHeader].push_back(KnownHeader(Mod, NormalHeader));
   Mod->Umbrella = UmbrellaHeader;
   UmbrellaDirs[UmbrellaHeader->getDir()] = Mod;
 }
@@ -620,7 +649,7 @@ void ModuleMap::addHeader(Module *Mod, const FileEntry *Header,
     bool isCompilingModuleHeader = Mod->getTopLevelModule() == CompilingModule;
     HeaderInfo.MarkFileModuleHeader(Header, Role, isCompilingModuleHeader);
   }
-  Headers[Header] = KnownHeader(Mod, Role);
+  Headers[Header].push_back(KnownHeader(Mod, Role));
 }
 
 const FileEntry *
@@ -642,8 +671,15 @@ void ModuleMap::dump() {
   llvm::errs() << "Headers:";
   for (HeadersMap::iterator H = Headers.begin(), HEnd = Headers.end();
        H != HEnd; ++H) {
-    llvm::errs() << "  \"" << H->first->getName() << "\" -> " 
-                 << H->second.getModule()->getFullModuleName() << "\n";
+    llvm::errs() << "  \"" << H->first->getName() << "\" -> ";
+    for (SmallVectorImpl<KnownHeader>::const_iterator I = H->second.begin(),
+                                                      E = H->second.end();
+         I != E; ++I) {
+      if (I != H->second.begin())
+        llvm::errs() << ",";
+      llvm::errs() << I->getModule()->getFullModuleName();
+    }
+    llvm::errs() << "\n";
   }
 }
 
@@ -1494,11 +1530,7 @@ void ModuleMapParser::parseHeaderDecl(MMToken::TokenKind LeadingToken,
   // FIXME: We shouldn't be eagerly stat'ing every file named in a module map.
   // Come up with a lazy way to do this.
   if (File) {
-    if (ModuleMap::KnownHeader OwningModule = Map.Headers[File]) {
-      Diags.Report(FileNameLoc, diag::err_mmap_header_conflict)
-        << FileName << OwningModule.getModule()->getFullModuleName();
-      HadError = true;
-    } else if (LeadingToken == MMToken::UmbrellaKeyword) {
+    if (LeadingToken == MMToken::UmbrellaKeyword) {
       const DirectoryEntry *UmbrellaDir = File->getDir();
       if (Module *UmbrellaModule = Map.UmbrellaDirs[UmbrellaDir]) {
         Diags.Report(LeadingLoc, diag::err_mmap_umbrella_clash)
index 99b30c67ac36aa2a26df634d5b3e09d843cc2de0..a952b2ef1b90676f1643d1d56879e9f19718a66c 100644 (file)
@@ -583,33 +583,33 @@ bool Preprocessor::violatesUseDeclarations(
   return Declared == AllowedUses.end();
 }
 
-void Preprocessor::verifyModuleInclude(
-    SourceLocation FilenameLoc,
-    StringRef Filename,
-    const FileEntry *IncFileEnt,
-    ModuleMap::KnownHeader *SuggestedModule) {
+void Preprocessor::verifyModuleInclude(SourceLocation FilenameLoc,
+                                       StringRef Filename,
+                                       const FileEntry *IncFileEnt) {
   Module *RequestingModule = getModuleForLocation(FilenameLoc);
-  Module *RequestedModule = SuggestedModule->getModule();
-  if (!RequestedModule)
-    RequestedModule = HeaderInfo.findModuleForHeader(IncFileEnt).getModule();
+  if (RequestingModule)
+    HeaderInfo.getModuleMap().resolveUses(RequestingModule, /*Complain=*/false);
+  ModuleMap::KnownHeader RequestedModule =
+      HeaderInfo.getModuleMap().findModuleForHeader(IncFileEnt,
+                                                    RequestingModule);
 
-  if (RequestingModule == RequestedModule)
+  if (RequestingModule == RequestedModule.getModule())
     return; // No faults wihin a module, or between files both not in modules.
 
   if (RequestingModule != HeaderInfo.getModuleMap().SourceModule)
     return; // No errors for indirect modules.
             // This may be a bit of a problem for modules with no source files.
 
-  if (RequestedModule &&
-      violatesPrivateInclude(RequestingModule, IncFileEnt,
-                             SuggestedModule->getRole(), RequestedModule))
+  if (RequestedModule && violatesPrivateInclude(RequestingModule, IncFileEnt,
+                                                RequestedModule.getRole(),
+                                                RequestedModule.getModule()))
     Diag(FilenameLoc, diag::error_use_of_private_header_outside_module)
         << Filename;
 
   // FIXME: Add support for FixIts in module map files and offer adding the
   // required use declaration.
   if (RequestingModule && getLangOpts().ModulesDeclUse &&
-      violatesUseDeclarations(RequestingModule, RequestedModule))
+      violatesUseDeclarations(RequestingModule, RequestedModule.getModule()))
     Diag(FilenameLoc, diag::error_undeclared_use_of_module)
         << Filename;
 }
@@ -650,7 +650,7 @@ const FileEntry *Preprocessor::LookupFile(
       SearchPath, RelativePath, SuggestedModule, SkipCache);
   if (FE) {
     if (SuggestedModule)
-      verifyModuleInclude(FilenameLoc, Filename, FE, SuggestedModule);
+      verifyModuleInclude(FilenameLoc, Filename, FE);
     return FE;
   }
 
diff --git a/test/Modules/Inputs/modular_maps/common.h b/test/Modules/Inputs/modular_maps/common.h
new file mode 100644 (file)
index 0000000..f690bcb
--- /dev/null
@@ -0,0 +1,4 @@
+#ifndef COMMON_H
+#define COMMON_H
+const int c = 2;
+#endif
index 7018c413042870ed778177595938f3ae0e29ed31..58c5f6464e40e32c1ee720d63c06bfb7dd61eb98 100644 (file)
@@ -1,4 +1,5 @@
 module A {
+  header "common.h"
   header "a.h"
 }
 
index 6f36ccd3baa9c8469814fa718ef448dd2efa0a65..7b35e8f91e44fa6be768cb34104ed698eda0b932 100644 (file)
@@ -1,3 +1,4 @@
 module B {
+  header "common.h"
   private header "b.h"
 }
diff --git a/test/Modules/Inputs/separate_map_tree/maps/modulea.map b/test/Modules/Inputs/separate_map_tree/maps/modulea.map
new file mode 100644 (file)
index 0000000..736503e
--- /dev/null
@@ -0,0 +1,12 @@
+module D {
+  header "../src/common.h"
+}
+
+module A {
+  header "../src/common.h"
+  use C
+}
+
+extern module B "moduleb.map"
+extern module C "modulec.map"
+
diff --git a/test/Modules/Inputs/separate_map_tree/maps/moduleb.map b/test/Modules/Inputs/separate_map_tree/maps/moduleb.map
new file mode 100644 (file)
index 0000000..d387796
--- /dev/null
@@ -0,0 +1,4 @@
+module B {
+  header "../src/public-in-b.h"
+  private header "../src/public-in-c.h"
+}
diff --git a/test/Modules/Inputs/separate_map_tree/maps/modulec.map b/test/Modules/Inputs/separate_map_tree/maps/modulec.map
new file mode 100644 (file)
index 0000000..91063b6
--- /dev/null
@@ -0,0 +1,5 @@
+module C {
+  header "../src/public-in-c.h"
+  private header "../src/public-in-b.h"
+  private header "../src/private-in-c.h"
+}
diff --git a/test/Modules/Inputs/separate_map_tree/src/common.h b/test/Modules/Inputs/separate_map_tree/src/common.h
new file mode 100644 (file)
index 0000000..1d2ecb5
--- /dev/null
@@ -0,0 +1,4 @@
+#ifndef COMMON_H
+#define COMMON_H
+const int common = 2;
+#endif
diff --git a/test/Modules/Inputs/separate_map_tree/src/private-in-c.h b/test/Modules/Inputs/separate_map_tree/src/private-in-c.h
new file mode 100644 (file)
index 0000000..bc9e2c1
--- /dev/null
@@ -0,0 +1,4 @@
+#ifndef PRIVATE_IN_C_H
+#define PRIVATE_IN_C_H
+const int c_ = 2;
+#endif
diff --git a/test/Modules/Inputs/separate_map_tree/src/public-in-b.h b/test/Modules/Inputs/separate_map_tree/src/public-in-b.h
new file mode 100644 (file)
index 0000000..9ea6c1b
--- /dev/null
@@ -0,0 +1,4 @@
+#ifndef PUBLIC_IN_B_H
+#define PUBLIC_IN_B_H
+const int b = 3;
+#endif
diff --git a/test/Modules/Inputs/separate_map_tree/src/public-in-c.h b/test/Modules/Inputs/separate_map_tree/src/public-in-c.h
new file mode 100644 (file)
index 0000000..fa3d2fd
--- /dev/null
@@ -0,0 +1,4 @@
+#ifndef PUBLIC_IN_C_H
+#define PUBLIC_IN_C_H
+const int c = 2;
+#endif
index 5070f7d5bf95da02ecbbaac1bd10ea8843d837ef..9c9aba85a918371ea6454784854b4b090f1cb657 100644 (file)
@@ -1,6 +1,8 @@
 // RUN: rm -rf %t
 // RUN: %clang_cc1 -x objective-c++ -fmodules-cache-path=%t -fmodules -fmodule-map-file=%S/Inputs/modular_maps/modulea.map -I %S/Inputs/modular_maps %s -verify
 
+#include "common.h"
 #include "a.h"
 #include "b.h" // expected-error {{private header}}
-const int val = a + b; // expected-error {{undeclared identifier}}
+const int v = a + c;
+const int val = a + b + c; // expected-error {{undeclared identifier}}
diff --git a/test/Modules/separate_map_tree.cpp b/test/Modules/separate_map_tree.cpp
new file mode 100644 (file)
index 0000000..5a1fff4
--- /dev/null
@@ -0,0 +1,8 @@
+// RUN: rm -rf %t
+// RUN: %clang_cc1 -fmodules-cache-path=%t -fmodules -fmodules-decluse -fmodule-name=A -fmodule-map-file=%S/Inputs/separate_map_tree/maps/modulea.map -I %S/Inputs/separate_map_tree/src %s -verify
+
+#include "common.h"
+#include "public-in-b.h" // expected-error {{private header}}
+#include "public-in-c.h"
+#include "private-in-c.h" // expected-error {{private header}}
+const int val = common + b + c + c_; // expected-error {{undeclared identifier}}