[cfe-commits] r137925 - in /cfe/trunk: include/clang/Serialization/ASTReader.h lib/Serialization/ASTReader.cpp test/Modules/ test/Modules/Inputs/ test/Modules/Inputs/diamond_bottom.h test/Modules/Inputs/diamond_left.h test/Modules/Inputs/diamond_right.h test/Modules/Inputs/diamond_top.h test/Modules/diamond.c

Douglas Gregor dgregor at apple.com
Wed Aug 17 21:41:58 PDT 2011


Author: dgregor
Date: Wed Aug 17 23:41:58 2011
New Revision: 137925

URL: http://llvm.org/viewvc/llvm-project?rev=137925&view=rev
Log:
Teach ModuleManager::addModule() to check whether a particular module
has already been loaded before allocating a new Module structure. If
the module has already been loaded (uniquing based on file name), then
just return the existing module rather than trying to load it again.

This allows us to load a DAG of modules. Introduce a simple test case
that forms a diamond-shaped module graph, and illustrates that a
source file importing the bottom of the diamond can see declarations
in all four of the modules that make up the diamond.



Added:
    cfe/trunk/test/Modules/
    cfe/trunk/test/Modules/Inputs/
    cfe/trunk/test/Modules/Inputs/diamond_bottom.h   (with props)
    cfe/trunk/test/Modules/Inputs/diamond_left.h   (with props)
    cfe/trunk/test/Modules/Inputs/diamond_right.h   (with props)
    cfe/trunk/test/Modules/Inputs/diamond_top.h   (with props)
    cfe/trunk/test/Modules/diamond.c   (with props)
Modified:
    cfe/trunk/include/clang/Serialization/ASTReader.h
    cfe/trunk/lib/Serialization/ASTReader.cpp

Modified: cfe/trunk/include/clang/Serialization/ASTReader.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Serialization/ASTReader.h?rev=137925&r1=137924&r2=137925&view=diff
==============================================================================
--- cfe/trunk/include/clang/Serialization/ASTReader.h (original)
+++ cfe/trunk/include/clang/Serialization/ASTReader.h Wed Aug 17 23:41:58 2011
@@ -507,8 +507,11 @@
   ///
   /// \param ImportedBy The module that is importing this module, or NULL if
   /// this module is imported directly by the user.
-  Module &addModule(StringRef FileName, ModuleKind Type,
-                    Module *ImportedBy);
+  ///
+  /// \return A pointer to the module that corresponds to this file name,
+  /// and a boolean indicating whether the module was newly added.
+  std::pair<Module *, bool> 
+  addModule(StringRef FileName, ModuleKind Type, Module *ImportedBy);
   
   /// \brief Add an in-memory buffer the list of known buffers
   void addInMemoryBuffer(StringRef FileName, llvm::MemoryBuffer *Buffer);

Modified: cfe/trunk/lib/Serialization/ASTReader.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Serialization/ASTReader.cpp?rev=137925&r1=137924&r2=137925&view=diff
==============================================================================
--- cfe/trunk/lib/Serialization/ASTReader.cpp (original)
+++ cfe/trunk/lib/Serialization/ASTReader.cpp Wed Aug 17 23:41:58 2011
@@ -2831,7 +2831,23 @@
 ASTReader::ASTReadResult ASTReader::ReadASTCore(StringRef FileName,
                                                 ModuleKind Type,
                                                 Module *ImportedBy) {
-  Module &F = ModuleMgr.addModule(FileName, Type, ImportedBy);
+  Module *M;
+  bool NewModule;
+  llvm::tie(M, NewModule) = ModuleMgr.addModule(FileName, Type, ImportedBy);
+
+  if (!M) {
+    // We couldn't load the module.
+    std::string Msg = "Unable to load module \"" + FileName.str() + "\"";
+    Error(Msg);
+    return Failure;
+  }
+
+  if (!NewModule) {
+    // We've already loaded this module.
+    return Success;
+  }
+
+  Module &F = *M;
 
   if (FileName != "-") {
     CurrentDir = llvm::sys::path::parent_path(FileName);
@@ -5709,25 +5725,34 @@
   return InMemoryBuffers[Entry];
 }
 
-/// \brief Creates a new module and adds it to the list of known modules
-Module &ModuleManager::addModule(StringRef FileName, ModuleKind Type,
-                                 Module *ImportedBy) {
-  Module *Current = new Module(Type);
-  Current->FileName = FileName.str();
-  Chain.push_back(Current);
-
+std::pair<Module *, bool>
+ModuleManager::addModule(StringRef FileName, ModuleKind Type, 
+                         Module *ImportedBy) {
   const FileEntry *Entry = FileMgr.getFile(FileName);
-  // FIXME: Check whether we already loaded this module, before 
-  Modules[Entry] = Current;
+  if (!Entry)
+    return std::make_pair(static_cast<Module*>(0), false);
+
+  // Check whether we already loaded this module, before 
+  Module *&ModuleEntry = Modules[Entry];
+  bool NewModule = false;
+  if (!ModuleEntry) {
+    // Allocate a new module.
+    Module *New = new Module(Type);
+    New->FileName = FileName.str();
+    Chain.push_back(New);
+  
+    NewModule = true;
+    ModuleEntry = New;
+  }
 
   if (ImportedBy) {
-    Current->ImportedBy.insert(ImportedBy);
-    ImportedBy->Imports.insert(Current);
+    ModuleEntry->ImportedBy.insert(ImportedBy);
+    ImportedBy->Imports.insert(ModuleEntry);
   } else {
-    Current->DirectlyImported = true;
+    ModuleEntry->DirectlyImported = true;
   }
   
-  return *Current;
+  return std::make_pair(ModuleEntry, NewModule);
 }
 
 void ModuleManager::addInMemoryBuffer(StringRef FileName, 

Added: cfe/trunk/test/Modules/Inputs/diamond_bottom.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Modules/Inputs/diamond_bottom.h?rev=137925&view=auto
==============================================================================
--- cfe/trunk/test/Modules/Inputs/diamond_bottom.h (added)
+++ cfe/trunk/test/Modules/Inputs/diamond_bottom.h Wed Aug 17 23:41:58 2011
@@ -0,0 +1 @@
+char bottom(char *x);

Propchange: cfe/trunk/test/Modules/Inputs/diamond_bottom.h
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cfe/trunk/test/Modules/Inputs/diamond_bottom.h
------------------------------------------------------------------------------
    svn:keywords = Id

Propchange: cfe/trunk/test/Modules/Inputs/diamond_bottom.h
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: cfe/trunk/test/Modules/Inputs/diamond_left.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Modules/Inputs/diamond_left.h?rev=137925&view=auto
==============================================================================
--- cfe/trunk/test/Modules/Inputs/diamond_left.h (added)
+++ cfe/trunk/test/Modules/Inputs/diamond_left.h Wed Aug 17 23:41:58 2011
@@ -0,0 +1 @@
+float left(float *);

Propchange: cfe/trunk/test/Modules/Inputs/diamond_left.h
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cfe/trunk/test/Modules/Inputs/diamond_left.h
------------------------------------------------------------------------------
    svn:keywords = Id

Propchange: cfe/trunk/test/Modules/Inputs/diamond_left.h
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: cfe/trunk/test/Modules/Inputs/diamond_right.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Modules/Inputs/diamond_right.h?rev=137925&view=auto
==============================================================================
--- cfe/trunk/test/Modules/Inputs/diamond_right.h (added)
+++ cfe/trunk/test/Modules/Inputs/diamond_right.h Wed Aug 17 23:41:58 2011
@@ -0,0 +1 @@
+double right(double *);

Propchange: cfe/trunk/test/Modules/Inputs/diamond_right.h
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cfe/trunk/test/Modules/Inputs/diamond_right.h
------------------------------------------------------------------------------
    svn:keywords = Id

Propchange: cfe/trunk/test/Modules/Inputs/diamond_right.h
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: cfe/trunk/test/Modules/Inputs/diamond_top.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Modules/Inputs/diamond_top.h?rev=137925&view=auto
==============================================================================
--- cfe/trunk/test/Modules/Inputs/diamond_top.h (added)
+++ cfe/trunk/test/Modules/Inputs/diamond_top.h Wed Aug 17 23:41:58 2011
@@ -0,0 +1 @@
+int top(int *);

Propchange: cfe/trunk/test/Modules/Inputs/diamond_top.h
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cfe/trunk/test/Modules/Inputs/diamond_top.h
------------------------------------------------------------------------------
    svn:keywords = Id

Propchange: cfe/trunk/test/Modules/Inputs/diamond_top.h
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: cfe/trunk/test/Modules/diamond.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Modules/diamond.c?rev=137925&view=auto
==============================================================================
--- cfe/trunk/test/Modules/diamond.c (added)
+++ cfe/trunk/test/Modules/diamond.c Wed Aug 17 23:41:58 2011
@@ -0,0 +1,14 @@
+// in diamond-bottom.h: expected-note{{passing argument to parameter 'x' here}}
+void test_diamond(int i, float f, double d, char c) {
+  top(&i);
+  left(&f);
+  right(&d);
+  bottom(&c);
+  bottom(&d); // expected-warning{{incompatible pointer types passing 'double *' to parameter of type 'char *'}}
+}
+
+// RUN: %clang_cc1 -emit-pch -o %t_top.h.pch %S/Inputs/diamond_top.h
+// RUN: %clang_cc1 -import-module %t_top.h.pch -emit-pch -o %t_left.h.pch %S/Inputs/diamond_left.h
+// RUN: %clang_cc1 -import-module %t_top.h.pch -emit-pch -o %t_right.h.pch %S/Inputs/diamond_right.h
+// RUN: %clang_cc1 -import-module %t_left.h.pch -import-module %t_right.h.pch -emit-pch -o %t_bottom.h.pch %S/Inputs/diamond_bottom.h
+// RUN: %clang_cc1 -import-module %t_bottom.h.pch -verify %s 

Propchange: cfe/trunk/test/Modules/diamond.c
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cfe/trunk/test/Modules/diamond.c
------------------------------------------------------------------------------
    svn:keywords = Id

Propchange: cfe/trunk/test/Modules/diamond.c
------------------------------------------------------------------------------
    svn:mime-type = text/plain





More information about the cfe-commits mailing list