Patch by Csaba Hruska and Peter Neumark:

  "adds support (de)serialization (from)to (in memory) buffer."


git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@53425 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/AST/TranslationUnit.cpp b/lib/AST/TranslationUnit.cpp
index c3d709f..659aa06 100644
--- a/lib/AST/TranslationUnit.cpp
+++ b/lib/AST/TranslationUnit.cpp
@@ -119,13 +119,20 @@
   return TU ? EmitASTBitcodeFile(*TU, Filename) : false;
 }
   
-bool clang::EmitASTBitcodeFile(const TranslationUnit& TU, 
-                               const llvm::sys::Path& Filename) {  
-  
-  // Reserve 256K for bitstream buffer.
-  std::vector<unsigned char> Buffer;
-  Buffer.reserve(256*1024);
-  
+bool clang::EmitASTBitcodeBuffer(const TranslationUnit* TU, 
+                                 std::vector<unsigned char>& Buffer) {
+
+  return TU ? EmitASTBitcodeBuffer(*TU, Buffer) : false;
+}
+
+bool clang::EmitASTBitcodeStream(const TranslationUnit* TU, 
+                                 std::ostream& Stream) {
+
+  return TU ? EmitASTBitcodeStream(*TU, Stream) : false;
+}
+
+bool clang::EmitASTBitcodeBuffer(const TranslationUnit& TU, 
+                                 std::vector<unsigned char>& Buffer) {
   // Create bitstream.
   llvm::BitstreamWriter Stream(Buffer);
   
@@ -146,6 +153,32 @@
     TU.Emit(Sezr);
   }
   
+  return true;
+}
+
+bool clang::EmitASTBitcodeStream(const TranslationUnit& TU, 
+                                 std::ostream& Stream) {  
+  
+  // Reserve 256K for bitstream buffer.
+  std::vector<unsigned char> Buffer;
+  Buffer.reserve(256*1024);
+  
+  EmitASTBitcodeBuffer(TU,Buffer);
+  
+  // Write the bits to disk.
+  Stream.write((char*)&Buffer.front(), Buffer.size());
+  return true;
+}
+
+bool clang::EmitASTBitcodeFile(const TranslationUnit& TU, 
+                               const llvm::sys::Path& Filename) {  
+  
+  // Reserve 256K for bitstream buffer.
+  std::vector<unsigned char> Buffer;
+  Buffer.reserve(256*1024);
+  
+  EmitASTBitcodeBuffer(TU,Buffer);
+  
   // Write the bits to disk. 
   if (FILE* fp = fopen(Filename.c_str(),"wb")) {
     fwrite((char*)&Buffer.front(), sizeof(char), Buffer.size(), fp);
@@ -207,26 +240,17 @@
 }
 
 TranslationUnit*
-clang::ReadASTBitcodeFile(const llvm::sys::Path& Filename, FileManager& FMgr) {
-  
-  // Create the memory buffer that contains the contents of the file.  
-  llvm::OwningPtr<llvm::MemoryBuffer> 
-    MBuffer(llvm::MemoryBuffer::getFile(Filename.c_str()));
-  
-  if (!MBuffer) {
-    // FIXME: Provide diagnostic.
-    return NULL;
-  }
-  
+clang::ReadASTBitcodeBuffer(llvm::MemoryBuffer& MBuffer, FileManager& FMgr) {
+
   // Check if the file is of the proper length.
-  if (MBuffer->getBufferSize() & 0x3) {
+  if (MBuffer.getBufferSize() & 0x3) {
     // FIXME: Provide diagnostic: "Length should be a multiple of 4 bytes."
     return NULL;
   }
   
   // Create the bitstream reader.
-  unsigned char *BufPtr = (unsigned char *) MBuffer->getBufferStart();
-  llvm::BitstreamReader Stream(BufPtr,BufPtr+MBuffer->getBufferSize());
+  unsigned char *BufPtr = (unsigned char *) MBuffer.getBufferStart();
+  llvm::BitstreamReader Stream(BufPtr,BufPtr+MBuffer.getBufferSize());
   
   if (Stream.Read(8) != 'B' ||
       Stream.Read(8) != 'C' ||
@@ -244,6 +268,21 @@
   return TranslationUnit::Create(Dezr,FMgr);
 }
 
+TranslationUnit*
+clang::ReadASTBitcodeFile(const llvm::sys::Path& Filename, FileManager& FMgr) {
+  
+  // Create the memory buffer that contains the contents of the file.  
+  llvm::OwningPtr<llvm::MemoryBuffer> 
+    MBuffer(llvm::MemoryBuffer::getFile(Filename.c_str()));
+  
+  if (!MBuffer) {
+    // FIXME: Provide diagnostic.
+    return NULL;
+  }
+  
+  return ReadASTBitcodeBuffer(*MBuffer, FMgr);
+}
+
 TranslationUnit* TranslationUnit::Create(llvm::Deserializer& Dezr,
                                          FileManager& FMgr) {