]> granicus.if.org Git - clang/commitdiff
Patch by Csaba Hruska and Peter Neumark:
authorTed Kremenek <kremenek@apple.com>
Thu, 10 Jul 2008 22:10:48 +0000 (22:10 +0000)
committerTed Kremenek <kremenek@apple.com>
Thu, 10 Jul 2008 22:10:48 +0000 (22:10 +0000)
  "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

include/clang/AST/TranslationUnit.h
lib/AST/TranslationUnit.cpp

index c5f499b6791e7e0778636b8f7d21bfe89ee2c758..4ab71e0205b32ae2e1491435b71d9bee896fe083 100644 (file)
@@ -84,10 +84,28 @@ bool EmitASTBitcodeFile(const TranslationUnit& TU,
 bool EmitASTBitcodeFile(const TranslationUnit* TU, 
                         const llvm::sys::Path& Filename);
                      
+/// EmitASTBitcodeStream - Emit a translation unit to a std::ostream.
+bool EmitASTBitcodeStream(const TranslationUnit& TU, 
+                          std::ostream& Stream);
+  
+bool EmitASTBitcodeStream(const TranslationUnit* TU, 
+                          std::ostream& Stream);
+                     
+/// EmitASTBitcodeBuffer - Emit a translation unit to a buffer.
+bool EmitASTBitcodeBuffer(const TranslationUnit& TU, 
+                          std::vector<unsigned char>& Buffer);
+
+bool EmitASTBitcodeBuffer(const TranslationUnit* TU, 
+                          std::vector<unsigned char>& Buffer);
+                                       
 /// ReadASTBitcodeFile - Reconsitute a translation unit from a bitcode file.
 TranslationUnit* ReadASTBitcodeFile(const llvm::sys::Path& Filename,
                                     FileManager& FMgr); 
                 
+/// ReadASTBitcodeBuffer - Reconsitute a translation unit from a buffer.
+TranslationUnit* ReadASTBitcodeBuffer(llvm::MemoryBuffer& MBuffer,
+                                      FileManager& FMgr); 
+                
 
 } // end namespace clang
 
index c3d709f035b70475813bfad9802e41dd7f53a27e..659aa064de142edda52dfd77185d781462824e02 100644 (file)
@@ -119,13 +119,20 @@ bool clang::EmitASTBitcodeFile(const TranslationUnit* TU,
   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 @@ bool clang::EmitASTBitcodeFile(const TranslationUnit& TU,
     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 @@ void TranslationUnit::Emit(llvm::Serializer& Sezr) const {
 }
 
 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 @@ clang::ReadASTBitcodeFile(const llvm::sys::Path& Filename, FileManager& FMgr) {
   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) {