]> granicus.if.org Git - clang/commitdiff
Bitcode: Change reader interface to take memory buffers.
authorPeter Collingbourne <peter@pcc.me.uk>
Wed, 2 Nov 2016 00:08:19 +0000 (00:08 +0000)
committerPeter Collingbourne <peter@pcc.me.uk>
Wed, 2 Nov 2016 00:08:19 +0000 (00:08 +0000)
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

lib/CodeGen/ObjectFilePCHContainerOperations.cpp
lib/Frontend/PCHContainerOperations.cpp
lib/Frontend/SerializedDiagnosticReader.cpp
lib/Serialization/ASTReader.cpp
lib/Serialization/GlobalModuleIndex.cpp

index f2090f9583e0cf082c1ab9e874d2e3c64f47cc4c..42a9782219c1233a9f62987b5cd4fa25fa805b57 100644 (file)
@@ -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());
   });
index 2d4edde43280a762d4662cff13e369d2dd1aeaa4..2c867e7c6489bae6ae88644368026565b244e568 100644 (file)
@@ -60,8 +60,7 @@ std::unique_ptr<ASTConsumer> 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() {
index 05dc8518c382ade2cc5d0355837f2b07be70b9f6..aefd0f8fbb093bc60f6e53c05840ac28b6ffad83 100644 (file)
@@ -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.
index 57de217078f6294eb10890b7479bcf64367be786..650c46e9ff0bf963904fda20c792e37fa801e213 100644 (file)
@@ -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';
index f75bc5260cc293f1d876de51c03675fa020a8364..e55ed40b227af84604f412ab70ac5d7aaa7d182f 100644 (file)
@@ -246,8 +246,7 @@ GlobalModuleIndex::readIndex(StringRef Path) {
   std::unique_ptr<llvm::MemoryBuffer> 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);