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
This commit is contained in:
Rafael Espindola 2014-08-27 19:03:22 +00:00
parent 7132e325d5
commit 34806d20dd
3 changed files with 29 additions and 28 deletions

View File

@ -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();

View File

@ -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(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());
Scanner::Scanner(MemoryBufferRef Buffer, SourceMgr &SM_) : SM(SM_) {
init(Buffer);
}
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() {}

View File

@ -212,7 +212,7 @@ TEST(YAMLParser, DiagnosticFilenameFromBufferID) {
// 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());
}