From: Peter Collingbourne Date: Wed, 2 Nov 2016 00:08:19 +0000 (+0000) Subject: Bitcode: Change reader interface to take memory buffers. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=202d09ece44fd6b8dd1c15195920c4746bbce237;p=clang Bitcode: Change reader interface to take memory buffers. As proposed on llvm-dev: http://lists.llvm.org/pipermail/llvm-dev/2016-October/106595.html This change also fixes an API oddity where BitstreamCursor::Read() would return zero for the first read past the end of the bitstream, but would report_fatal_error for subsequent reads. Now we always report_fatal_error for all reads past the end. Updated clients to check for the end of the bitstream before reading from it. I also needed to add padding to the invalid bitcode tests in test/Bitcode/. This is because the streaming interface was not checking that the file size is a multiple of 4. Differential Revision: https://reviews.llvm.org/D26219 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@285773 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/CodeGen/ObjectFilePCHContainerOperations.cpp b/lib/CodeGen/ObjectFilePCHContainerOperations.cpp index f2090f9583..42a9782219 100644 --- a/lib/CodeGen/ObjectFilePCHContainerOperations.cpp +++ b/lib/CodeGen/ObjectFilePCHContainerOperations.cpp @@ -325,8 +325,8 @@ void ObjectFilePCHContainerReader::ExtractPCH( if ((!IsCOFF && Name == "__clangast") || (IsCOFF && Name == "clangast")) { StringRef Buf; Section.getContents(Buf); - return StreamFile.init((const unsigned char *)Buf.begin(), - (const unsigned char *)Buf.end()); + StreamFile = llvm::BitstreamReader(Buf); + return; } } } @@ -334,8 +334,7 @@ void ObjectFilePCHContainerReader::ExtractPCH( if (EIB.convertToErrorCode() == llvm::object::object_error::invalid_file_type) // As a fallback, treat the buffer as a raw AST. - StreamFile.init((const unsigned char *)Buffer.getBufferStart(), - (const unsigned char *)Buffer.getBufferEnd()); + StreamFile = llvm::BitstreamReader(Buffer); else EIB.log(llvm::errs()); }); diff --git a/lib/Frontend/PCHContainerOperations.cpp b/lib/Frontend/PCHContainerOperations.cpp index 2d4edde432..2c867e7c64 100644 --- a/lib/Frontend/PCHContainerOperations.cpp +++ b/lib/Frontend/PCHContainerOperations.cpp @@ -60,8 +60,7 @@ std::unique_ptr RawPCHContainerWriter::CreatePCHContainerGenerator( void RawPCHContainerReader::ExtractPCH( llvm::MemoryBufferRef Buffer, llvm::BitstreamReader &StreamFile) const { - StreamFile.init((const unsigned char *)Buffer.getBufferStart(), - (const unsigned char *)Buffer.getBufferEnd()); + StreamFile = llvm::BitstreamReader(Buffer); } PCHContainerOperations::PCHContainerOperations() { diff --git a/lib/Frontend/SerializedDiagnosticReader.cpp b/lib/Frontend/SerializedDiagnosticReader.cpp index 05dc8518c3..aefd0f8fbb 100644 --- a/lib/Frontend/SerializedDiagnosticReader.cpp +++ b/lib/Frontend/SerializedDiagnosticReader.cpp @@ -24,10 +24,7 @@ std::error_code SerializedDiagnosticReader::readDiagnostics(StringRef File) { if (!Buffer) return SDError::CouldNotLoad; - llvm::BitstreamReader StreamFile; - StreamFile.init((const unsigned char *)(*Buffer)->getBufferStart(), - (const unsigned char *)(*Buffer)->getBufferEnd()); - + llvm::BitstreamReader StreamFile(**Buffer); llvm::BitstreamCursor Stream(StreamFile); // Sniff for the signature. diff --git a/lib/Serialization/ASTReader.cpp b/lib/Serialization/ASTReader.cpp index 57de217078..650c46e9ff 100644 --- a/lib/Serialization/ASTReader.cpp +++ b/lib/Serialization/ASTReader.cpp @@ -3802,7 +3802,8 @@ static ASTFileSignature readASTFileSignature(llvm::BitstreamReader &StreamFile); /// \brief Whether \p Stream starts with the AST/PCH file magic number 'CPCH'. static bool startsWithASTFileMagic(BitstreamCursor &Stream) { - return Stream.Read(8) == 'C' && + return Stream.canSkipToPos(4) && + Stream.Read(8) == 'C' && Stream.Read(8) == 'P' && Stream.Read(8) == 'C' && Stream.Read(8) == 'H'; diff --git a/lib/Serialization/GlobalModuleIndex.cpp b/lib/Serialization/GlobalModuleIndex.cpp index f75bc5260c..e55ed40b22 100644 --- a/lib/Serialization/GlobalModuleIndex.cpp +++ b/lib/Serialization/GlobalModuleIndex.cpp @@ -246,8 +246,7 @@ GlobalModuleIndex::readIndex(StringRef Path) { std::unique_ptr Buffer = std::move(BufferOrErr.get()); /// \brief The bitstream reader from which we'll read the AST file. - llvm::BitstreamReader Reader((const unsigned char *)Buffer->getBufferStart(), - (const unsigned char *)Buffer->getBufferEnd()); + llvm::BitstreamReader Reader(*Buffer); /// \brief The main bitstream cursor for the main block. llvm::BitstreamCursor Cursor(Reader);