[cfe-commits] r53425 - in /cfe/trunk: include/clang/AST/TranslationUnit.h lib/AST/TranslationUnit.cpp
Ted Kremenek
kremenek at apple.com
Thu Jul 10 15:10:48 PDT 2008
Author: kremenek
Date: Thu Jul 10 17:10:48 2008
New Revision: 53425
URL: http://llvm.org/viewvc/llvm-project?rev=53425&view=rev
Log:
Patch by Csaba Hruska and Peter Neumark:
"adds support (de)serialization (from)to (in memory) buffer."
Modified:
cfe/trunk/include/clang/AST/TranslationUnit.h
cfe/trunk/lib/AST/TranslationUnit.cpp
Modified: cfe/trunk/include/clang/AST/TranslationUnit.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/TranslationUnit.h?rev=53425&r1=53424&r2=53425&view=diff
==============================================================================
--- cfe/trunk/include/clang/AST/TranslationUnit.h (original)
+++ cfe/trunk/include/clang/AST/TranslationUnit.h Thu Jul 10 17:10:48 2008
@@ -84,10 +84,28 @@
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
Modified: cfe/trunk/lib/AST/TranslationUnit.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/TranslationUnit.cpp?rev=53425&r1=53424&r2=53425&view=diff
==============================================================================
--- cfe/trunk/lib/AST/TranslationUnit.cpp (original)
+++ cfe/trunk/lib/AST/TranslationUnit.cpp Thu Jul 10 17:10:48 2008
@@ -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) {
More information about the cfe-commits
mailing list