[cfe-commits] r145737 - in /cfe/trunk: include/clang/Serialization/ASTBitCodes.h include/clang/Serialization/ASTReader.h lib/Serialization/ASTReader.cpp lib/Serialization/ASTWriter.cpp test/Modules/diamond-pch.c

Douglas Gregor dgregor at apple.com
Fri Dec 2 16:59:55 PST 2011


Author: dgregor
Date: Fri Dec  2 18:59:55 2011
New Revision: 145737

URL: http://llvm.org/viewvc/llvm-project?rev=145737&view=rev
Log:
Implement support for precompiled headers, preambles, and serialized
"main" files that import modules. When loading any of these kinds of
AST files, we make the modules that were imported visible into the
translation unit that loaded the PCH file or preamble.

Modified:
    cfe/trunk/include/clang/Serialization/ASTBitCodes.h
    cfe/trunk/include/clang/Serialization/ASTReader.h
    cfe/trunk/lib/Serialization/ASTReader.cpp
    cfe/trunk/lib/Serialization/ASTWriter.cpp
    cfe/trunk/test/Modules/diamond-pch.c

Modified: cfe/trunk/include/clang/Serialization/ASTBitCodes.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Serialization/ASTBitCodes.h?rev=145737&r1=145736&r2=145737&view=diff
==============================================================================
--- cfe/trunk/include/clang/Serialization/ASTBitCodes.h (original)
+++ cfe/trunk/include/clang/Serialization/ASTBitCodes.h Fri Dec  2 18:59:55 2011
@@ -448,7 +448,11 @@
       OBJC_CHAINED_CATEGORIES,
 
       /// \brief Record code for a file sorted array of DeclIDs in a module.
-      FILE_SORTED_DECLS
+      FILE_SORTED_DECLS,
+      
+      /// \brief Record code for an array of all of the (sub)modules that were
+      /// imported by the AST file.
+      IMPORTED_MODULES
     };
 
     /// \brief Record types used within a source manager block.

Modified: cfe/trunk/include/clang/Serialization/ASTReader.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Serialization/ASTReader.h?rev=145737&r1=145736&r2=145737&view=diff
==============================================================================
--- cfe/trunk/include/clang/Serialization/ASTReader.h (original)
+++ cfe/trunk/include/clang/Serialization/ASTReader.h Fri Dec  2 18:59:55 2011
@@ -541,6 +541,9 @@
   /// \brief A list of the namespaces we've seen.
   SmallVector<uint64_t, 4> KnownNamespaces;
 
+  /// \brief A list of modules that were imported by precompiled headers or
+  /// any other non-module AST file.
+  SmallVector<serialization::SubmoduleID, 2> ImportedModules;
   //@}
 
   /// \brief The original file name that was used to build the primary AST file,

Modified: cfe/trunk/lib/Serialization/ASTReader.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Serialization/ASTReader.cpp?rev=145737&r1=145736&r2=145737&view=diff
==============================================================================
--- cfe/trunk/lib/Serialization/ASTReader.cpp (original)
+++ cfe/trunk/lib/Serialization/ASTReader.cpp Fri Dec  2 18:59:55 2011
@@ -2385,6 +2385,20 @@
       for (unsigned I = 0, N = Record.size(); I != N; ++I)
         KnownNamespaces.push_back(getGlobalDeclID(F, Record[I]));
       break;
+        
+    case IMPORTED_MODULES: {
+      if (F.Kind != MK_Module) {
+        // If we aren't loading a module (which has its own exports), make
+        // all of the imported modules visible.
+        // FIXME: Deal with macros-only imports.
+        for (unsigned I = 0, N = Record.size(); I != N; ++I) {
+          if (unsigned GlobalID = getGlobalSubmoduleID(F, Record[I]))
+            ImportedModules.push_back(GlobalID);
+        }
+      }
+      break;
+      
+    }
     }
   }
   Error("premature end of bitstream in AST file");
@@ -2835,6 +2849,13 @@
     Context.setcudaConfigureCallDecl(
                            cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
   }
+  
+  // Re-export any modules that were imported by a non-module AST file.
+  for (unsigned I = 0, N = ImportedModules.size(); I != N; ++I) {
+    if (Module *Imported = getSubmodule(ImportedModules[I]))
+      makeModuleVisible(Imported, Module::AllVisible);
+  }
+  ImportedModules.clear();
 }
 
 void ASTReader::finalizeForWriting() {

Modified: cfe/trunk/lib/Serialization/ASTWriter.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Serialization/ASTWriter.cpp?rev=145737&r1=145736&r2=145737&view=diff
==============================================================================
--- cfe/trunk/lib/Serialization/ASTWriter.cpp (original)
+++ cfe/trunk/lib/Serialization/ASTWriter.cpp Fri Dec  2 18:59:55 2011
@@ -3335,6 +3335,27 @@
        I != E; ++I)
     WriteDeclContextVisibleUpdate(*I);
 
+  // Write the submodules that were imported, if any.
+  RecordData ImportedModules;
+  for (ASTContext::import_iterator I = Context.local_import_begin(),
+                                IEnd = Context.local_import_end();
+       I != IEnd; ++I) {
+    assert(SubmoduleIDs.find(I->getImportedModule()) != SubmoduleIDs.end());
+    ImportedModules.push_back(SubmoduleIDs[I->getImportedModule()]);
+  }
+  if (!ImportedModules.empty()) {
+    // Sort module IDs.
+    llvm::array_pod_sort(ImportedModules.begin(), ImportedModules.end());
+    
+    // Unique module IDs.
+    ImportedModules.erase(std::unique(ImportedModules.begin(), 
+                                      ImportedModules.end()),
+                          ImportedModules.end());
+    
+    Stream.EmitRecord(IMPORTED_MODULES, ImportedModules);
+    ImportedModules.clear();
+  }
+  
   WriteDeclUpdatesBlocks();
   WriteDeclReplacementsBlock();
   WriteChainedObjCCategories();

Modified: cfe/trunk/test/Modules/diamond-pch.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Modules/diamond-pch.c?rev=145737&r1=145736&r2=145737&view=diff
==============================================================================
--- cfe/trunk/test/Modules/diamond-pch.c (original)
+++ cfe/trunk/test/Modules/diamond-pch.c Fri Dec  2 18:59:55 2011
@@ -3,11 +3,6 @@
 
 // in diamond-bottom.h: expected-note{{passing argument to parameter 'x' here}}
 
-// FIXME: The module import below shouldn't be necessary, because importing the
-// precompiled header should make all of the modules visible that were
-// visible when the PCH file was built.
-__import_module__ diamond_bottom;
-
 void test_diamond(int i, float f, double d, char c) {
   top(&i);
   left(&f);





More information about the cfe-commits mailing list