[llvm] r216575 - yaml::Stream doesn't need to take ownership of the buffer.

Rafael Espindola rafael.espindola at gmail.com
Wed Aug 27 12:03:22 PDT 2014


Author: rafael
Date: Wed Aug 27 14:03:22 2014
New Revision: 216575

URL: http://llvm.org/viewvc/llvm-project?rev=216575&view=rev
Log:
yaml::Stream doesn't need to take ownership of the buffer.

In fact, most users were already using the StringRef version.

Modified:
    llvm/trunk/include/llvm/Support/YAMLParser.h
    llvm/trunk/lib/Support/YAMLParser.cpp
    llvm/trunk/unittests/Support/YAMLParserTest.cpp

Modified: llvm/trunk/include/llvm/Support/YAMLParser.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/YAMLParser.h?rev=216575&r1=216574&r2=216575&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Support/YAMLParser.h (original)
+++ llvm/trunk/include/llvm/Support/YAMLParser.h Wed Aug 27 14:03:22 2014
@@ -41,13 +41,13 @@
 #include "llvm/ADT/SmallString.h"
 #include "llvm/ADT/StringRef.h"
 #include "llvm/Support/Allocator.h"
+#include "llvm/Support/MemoryBuffer.h"
 #include "llvm/Support/SMLoc.h"
 #include <limits>
 #include <map>
 #include <utility>
 
 namespace llvm {
-class MemoryBuffer;
 class SourceMgr;
 class raw_ostream;
 class Twine;
@@ -79,7 +79,7 @@ public:
   /// \brief This keeps a reference to the string referenced by \p Input.
   Stream(StringRef Input, SourceMgr &);
 
-  Stream(std::unique_ptr<MemoryBuffer> InputBuffer, SourceMgr &);
+  Stream(MemoryBufferRef InputBuffer, SourceMgr &);
   ~Stream();
 
   document_iterator begin();

Modified: llvm/trunk/lib/Support/YAMLParser.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/YAMLParser.cpp?rev=216575&r1=216574&r2=216575&view=diff
==============================================================================
--- llvm/trunk/lib/Support/YAMLParser.cpp (original)
+++ llvm/trunk/lib/Support/YAMLParser.cpp Wed Aug 27 14:03:22 2014
@@ -260,7 +260,7 @@ namespace yaml {
 class Scanner {
 public:
   Scanner(const StringRef Input, SourceMgr &SM);
-  Scanner(std::unique_ptr<MemoryBuffer> Buffer, SourceMgr &SM_);
+  Scanner(MemoryBufferRef Buffer, SourceMgr &SM_);
 
   /// @brief Parse the next token and return it without popping it.
   Token &peekNext();
@@ -294,6 +294,8 @@ public:
   }
 
 private:
+  void init(MemoryBufferRef Buffer);
+
   StringRef currentInput() {
     return StringRef(Current, End - Current);
   }
@@ -469,7 +471,7 @@ private:
   SourceMgr &SM;
 
   /// @brief The original input.
-  MemoryBuffer *InputBuffer;
+  MemoryBufferRef InputBuffer;
 
   /// @brief The current position of the scanner.
   StringRef::iterator Current;
@@ -699,29 +701,28 @@ std::string yaml::escape(StringRef Input
   return EscapedInput;
 }
 
-Scanner::Scanner(StringRef Input, SourceMgr &sm)
-  : SM(sm)
-  , Indent(-1)
-  , Column(0)
-  , Line(0)
-  , FlowLevel(0)
-  , IsStartOfStream(true)
-  , IsSimpleKeyAllowed(true)
-  , Failed(false) {
-  std::unique_ptr<MemoryBuffer> InputBufferOwner(
-      MemoryBuffer::getMemBuffer(Input, "YAML"));
-  InputBuffer = InputBufferOwner.get();
-  SM.AddNewSourceBuffer(std::move(InputBufferOwner), SMLoc());
-  Current = InputBuffer->getBufferStart();
-  End = InputBuffer->getBufferEnd();
+Scanner::Scanner(StringRef Input, SourceMgr &sm) : SM(sm) {
+  init(MemoryBufferRef(Input, "YAML"));
+}
+
+Scanner::Scanner(MemoryBufferRef Buffer, SourceMgr &SM_) : SM(SM_) {
+  init(Buffer);
 }
 
-Scanner::Scanner(std::unique_ptr<MemoryBuffer> Buffer, SourceMgr &SM_)
-    : SM(SM_), InputBuffer(Buffer.get()),
-      Current(InputBuffer->getBufferStart()), End(InputBuffer->getBufferEnd()),
-      Indent(-1), Column(0), Line(0), FlowLevel(0), IsStartOfStream(true),
-      IsSimpleKeyAllowed(true), Failed(false) {
-  SM.AddNewSourceBuffer(std::move(Buffer), SMLoc());
+void Scanner::init(MemoryBufferRef Buffer) {
+  InputBuffer = Buffer;
+  Current = InputBuffer.getBufferStart();
+  End = InputBuffer.getBufferEnd();
+  Indent = -1;
+  Column = 0;
+  Line = 0;
+  FlowLevel = 0;
+  IsStartOfStream = true;
+  IsSimpleKeyAllowed = true;
+  Failed = false;
+  std::unique_ptr<MemoryBuffer> InputBufferOwner =
+      MemoryBuffer::getMemBuffer(Buffer);
+  SM.AddNewSourceBuffer(std::move(InputBufferOwner), SMLoc());
 }
 
 Token &Scanner::peekNext() {
@@ -1519,8 +1520,8 @@ bool Scanner::fetchMoreTokens() {
 Stream::Stream(StringRef Input, SourceMgr &SM)
     : scanner(new Scanner(Input, SM)), CurrentDoc() {}
 
-Stream::Stream(std::unique_ptr<MemoryBuffer> InputBuffer, SourceMgr &SM)
-    : scanner(new Scanner(std::move(InputBuffer), SM)), CurrentDoc() {}
+Stream::Stream(MemoryBufferRef InputBuffer, SourceMgr &SM)
+    : scanner(new Scanner(InputBuffer, SM)), CurrentDoc() {}
 
 Stream::~Stream() {}
 

Modified: llvm/trunk/unittests/Support/YAMLParserTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Support/YAMLParserTest.cpp?rev=216575&r1=216574&r2=216575&view=diff
==============================================================================
--- llvm/trunk/unittests/Support/YAMLParserTest.cpp (original)
+++ llvm/trunk/unittests/Support/YAMLParserTest.cpp Wed Aug 27 14:03:22 2014
@@ -212,7 +212,7 @@ TEST(YAMLParser, DiagnosticFilenameFromB
   // we get its ID as filename in diagnostics.
   std::unique_ptr<MemoryBuffer> Buffer(
       MemoryBuffer::getMemBuffer("[]", "buffername.yaml"));
-  yaml::Stream Stream(std::move(Buffer), SM);
+  yaml::Stream Stream(Buffer->getMemBufferRef(), SM);
   Stream.printError(Stream.begin()->getRoot(), "Hello, World!");
   EXPECT_EQ("buffername.yaml", GeneratedDiag.getFilename());
 }





More information about the llvm-commits mailing list