Skip to content

Commit

Permalink
[Clang] - Update code to match upcoming llvm::zlib API.
Browse files Browse the repository at this point in the history
D28684 changed llvm::zlib to return Error instead of Status.
It was accepted and committed in r292214, but then reverted in r292217
because I missed that clang code also needs to be updated.

Patch do that.

D28684 recommitted again as r292226

Differential revision: https://reviews.llvm.org/D28807

llvm-svn: 292227
  • Loading branch information
George Rimar committed Jan 17, 2017
1 parent 167ca4a commit c39f549
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 17 deletions.
12 changes: 9 additions & 3 deletions clang/lib/Serialization/ASTReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
#include "llvm/Bitcode/BitstreamReader.h"
#include "llvm/Support/Compression.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/MemoryBuffer.h"
Expand Down Expand Up @@ -1278,10 +1279,15 @@ bool ASTReader::ReadSLocEntry(int ID) {
unsigned RecCode = SLocEntryCursor.readRecord(Code, Record, &Blob);

if (RecCode == SM_SLOC_BUFFER_BLOB_COMPRESSED) {
if (!llvm::zlib::isAvailable()) {
Error("zlib is not available");
return nullptr;
}
SmallString<0> Uncompressed;
if (llvm::zlib::uncompress(Blob, Uncompressed, Record[0]) !=
llvm::zlib::StatusOK) {
Error("could not decompress embedded file contents");
if (llvm::Error E =
llvm::zlib::uncompress(Blob, Uncompressed, Record[0])) {
Error("could not decompress embedded file contents: " +
llvm::toString(std::move(E)));
return nullptr;
}
return llvm::MemoryBuffer::getMemBufferCopy(Uncompressed, Name);
Expand Down
41 changes: 27 additions & 14 deletions clang/lib/Serialization/ASTWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
#include "llvm/Support/Casting.h"
#include "llvm/Support/Compression.h"
#include "llvm/Support/EndianStream.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/OnDiskHashTable.h"
Expand Down Expand Up @@ -1986,6 +1987,30 @@ void ASTWriter::WriteHeaderSearch(const HeaderSearch &HS) {
free(const_cast<char *>(SavedStrings[I]));
}

static void emitBlob(llvm::BitstreamWriter &Stream, StringRef Blob,
unsigned SLocBufferBlobCompressedAbbrv,
unsigned SLocBufferBlobAbbrv) {
typedef ASTWriter::RecordData::value_type RecordDataType;

// Compress the buffer if possible. We expect that almost all PCM
// consumers will not want its contents.
SmallString<0> CompressedBuffer;
if (llvm::zlib::isAvailable()) {
llvm::Error E = llvm::zlib::compress(Blob.drop_back(1), CompressedBuffer);
if (!E) {
RecordDataType Record[] = {SM_SLOC_BUFFER_BLOB_COMPRESSED,
Blob.size() - 1};
Stream.EmitRecordWithBlob(SLocBufferBlobCompressedAbbrv, Record,
CompressedBuffer);
return;
}
llvm::consumeError(std::move(E));
}

RecordDataType Record[] = {SM_SLOC_BUFFER_BLOB};
Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record, Blob);
}

/// \brief Writes the block containing the serialized form of the
/// source manager.
///
Expand Down Expand Up @@ -2094,20 +2119,8 @@ void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
const llvm::MemoryBuffer *Buffer =
Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
StringRef Blob(Buffer->getBufferStart(), Buffer->getBufferSize() + 1);

// Compress the buffer if possible. We expect that almost all PCM
// consumers will not want its contents.
SmallString<0> CompressedBuffer;
if (llvm::zlib::compress(Blob.drop_back(1), CompressedBuffer) ==
llvm::zlib::StatusOK) {
RecordData::value_type Record[] = {SM_SLOC_BUFFER_BLOB_COMPRESSED,
Blob.size() - 1};
Stream.EmitRecordWithBlob(SLocBufferBlobCompressedAbbrv, Record,
CompressedBuffer);
} else {
RecordData::value_type Record[] = {SM_SLOC_BUFFER_BLOB};
Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record, Blob);
}
emitBlob(Stream, Blob, SLocBufferBlobCompressedAbbrv,
SLocBufferBlobAbbrv);
}
} else {
// The source location entry is a macro expansion.
Expand Down

0 comments on commit c39f549

Please sign in to comment.