]> granicus.if.org Git - clang/commitdiff
Implement support for precompiled headers, preambles, and serialized
authorDouglas Gregor <dgregor@apple.com>
Sat, 3 Dec 2011 00:59:55 +0000 (00:59 +0000)
committerDouglas Gregor <dgregor@apple.com>
Sat, 3 Dec 2011 00:59:55 +0000 (00:59 +0000)
"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.

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

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

index 3233a83931c77fa629bd9c364434ae62c8f00146..3d234d9085950b11009e13ffe88cc5271ffa6043 100644 (file)
@@ -448,7 +448,11 @@ namespace clang {
       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.
index e984e306c8deddc40f25bd607d17d46ffe49e37a..203abe403cbb5973583b1988750379cb50a5dd5d 100644 (file)
@@ -541,6 +541,9 @@ private:
   /// \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,
index 22e34d62af4c54c16c996dd85233c2922039e168..c7b6b512a5d47c4178836e7e7d01f947d52fb694 100644 (file)
@@ -2385,6 +2385,20 @@ ASTReader::ReadASTBlock(ModuleFile &F) {
       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 @@ void ASTReader::InitializeContext() {
     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() {
index 00dbfabb4512d5b63cf77c21fee22be309fe8a49..15db7050e982a465057ab4bcee9db03a609d8498 100644 (file)
@@ -3335,6 +3335,27 @@ void ASTWriter::WriteASTCore(Sema &SemaRef, MemorizeStatCalls *StatCalls,
        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();
index f5b0faa424b4931ce79a6cee4d95001e15b0b31a..d0f45908cfa51aa0ec1407a752ee5c9559841d21 100644 (file)
@@ -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);