From: Rafael Espindola Date: Wed, 27 Aug 2014 19:03:22 +0000 (+0000) Subject: yaml::Stream doesn't need to take ownership of the buffer. X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=34806d20dd83b40412b36469baae4c7a1780c9ac;p=oota-llvm.git yaml::Stream doesn't need to take ownership of the buffer. In fact, most users were already using the StringRef version. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@216575 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/llvm/Support/YAMLParser.h b/include/llvm/Support/YAMLParser.h index d1167e5f35d..de6e6544e25 100644 --- a/include/llvm/Support/YAMLParser.h +++ b/include/llvm/Support/YAMLParser.h @@ -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 #include #include 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 InputBuffer, SourceMgr &); + Stream(MemoryBufferRef InputBuffer, SourceMgr &); ~Stream(); document_iterator begin(); diff --git a/lib/Support/YAMLParser.cpp b/lib/Support/YAMLParser.cpp index 07a326ee554..a44397ff0ee 100644 --- a/lib/Support/YAMLParser.cpp +++ b/lib/Support/YAMLParser.cpp @@ -260,7 +260,7 @@ namespace yaml { class Scanner { public: Scanner(const StringRef Input, SourceMgr &SM); - Scanner(std::unique_ptr 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 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 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 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 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() {} diff --git a/unittests/Support/YAMLParserTest.cpp b/unittests/Support/YAMLParserTest.cpp index 1a5188a3018..63746388946 100644 --- a/unittests/Support/YAMLParserTest.cpp +++ b/unittests/Support/YAMLParserTest.cpp @@ -212,7 +212,7 @@ TEST(YAMLParser, DiagnosticFilenameFromBufferID) { // we get its ID as filename in diagnostics. std::unique_ptr 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()); }