diff options
author | Ted Kremenek <kremenek@apple.com> | 2008-07-10 22:10:48 +0000 |
---|---|---|
committer | Ted Kremenek <kremenek@apple.com> | 2008-07-10 22:10:48 +0000 |
commit | 0ce902bb293dfefd0610e59f7441339d803979cf (patch) | |
tree | 2db2f801470af7e99ab675a03d16f5774acbc2a2 /lib/AST/TranslationUnit.cpp | |
parent | eb04751d4d7ca4365a860c46ec386a507425621c (diff) |
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
Diffstat (limited to 'lib/AST/TranslationUnit.cpp')
-rw-r--r-- | lib/AST/TranslationUnit.cpp | 81 |
1 files changed, 60 insertions, 21 deletions
diff --git a/lib/AST/TranslationUnit.cpp b/lib/AST/TranslationUnit.cpp index c3d709f035..659aa064de 100644 --- a/lib/AST/TranslationUnit.cpp +++ b/lib/AST/TranslationUnit.cpp @@ -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) { |