llvm.org GIT mirror llvm / 34806d2
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 Rafael Espindola 5 years ago
3 changed file(s) with 30 addition(s) and 29 deletion(s). Raw diff Collapse all Expand all
4040 #include "llvm/ADT/SmallString.h"
4141 #include "llvm/ADT/StringRef.h"
4242 #include "llvm/Support/Allocator.h"
43 #include "llvm/Support/MemoryBuffer.h"
4344 #include "llvm/Support/SMLoc.h"
4445 #include
4546 #include
4647 #include
4748
4849 namespace llvm {
49 class MemoryBuffer;
5050 class SourceMgr;
5151 class raw_ostream;
5252 class Twine;
7878 /// \brief This keeps a reference to the string referenced by \p Input.
7979 Stream(StringRef Input, SourceMgr &);
8080
81 Stream(std::unique_ptr InputBuffer, SourceMgr &);
81 Stream(MemoryBufferRef InputBuffer, SourceMgr &);
8282 ~Stream();
8383
8484 document_iterator begin();
259259 class Scanner {
260260 public:
261261 Scanner(const StringRef Input, SourceMgr &SM);
262 Scanner(std::unique_ptr Buffer, SourceMgr &SM_);
262 Scanner(MemoryBufferRef Buffer, SourceMgr &SM_);
263263
264264 /// @brief Parse the next token and return it without popping it.
265265 Token &peekNext();
293293 }
294294
295295 private:
296 void init(MemoryBufferRef Buffer);
297
296298 StringRef currentInput() {
297299 return StringRef(Current, End - Current);
298300 }
468470 SourceMgr &SM;
469471
470472 /// @brief The original input.
471 MemoryBuffer *InputBuffer;
473 MemoryBufferRef InputBuffer;
472474
473475 /// @brief The current position of the scanner.
474476 StringRef::iterator Current;
698700 return EscapedInput;
699701 }
700702
701 Scanner::Scanner(StringRef Input, SourceMgr &sm)
702 : SM(sm)
703 , Indent(-1)
704 , Column(0)
705 , Line(0)
706 , FlowLevel(0)
707 , IsStartOfStream(true)
708 , IsSimpleKeyAllowed(true)
709 , Failed(false) {
710 std::unique_ptr InputBufferOwner(
711 MemoryBuffer::getMemBuffer(Input, "YAML"));
712 InputBuffer = InputBufferOwner.get();
703 Scanner::Scanner(StringRef Input, SourceMgr &sm) : SM(sm) {
704 init(MemoryBufferRef(Input, "YAML"));
705 }
706
707 Scanner::Scanner(MemoryBufferRef Buffer, SourceMgr &SM_) : SM(SM_) {
708 init(Buffer);
709 }
710
711 void Scanner::init(MemoryBufferRef Buffer) {
712 InputBuffer = Buffer;
713 Current = InputBuffer.getBufferStart();
714 End = InputBuffer.getBufferEnd();
715 Indent = -1;
716 Column = 0;
717 Line = 0;
718 FlowLevel = 0;
719 IsStartOfStream = true;
720 IsSimpleKeyAllowed = true;
721 Failed = false;
722 std::unique_ptr InputBufferOwner =
723 MemoryBuffer::getMemBuffer(Buffer);
713724 SM.AddNewSourceBuffer(std::move(InputBufferOwner), SMLoc());
714 Current = InputBuffer->getBufferStart();
715 End = InputBuffer->getBufferEnd();
716 }
717
718 Scanner::Scanner(std::unique_ptr Buffer, SourceMgr &SM_)
719 : SM(SM_), InputBuffer(Buffer.get()),
720 Current(InputBuffer->getBufferStart()), End(InputBuffer->getBufferEnd()),
721 Indent(-1), Column(0), Line(0), FlowLevel(0), IsStartOfStream(true),
722 IsSimpleKeyAllowed(true), Failed(false) {
723 SM.AddNewSourceBuffer(std::move(Buffer), SMLoc());
724725 }
725726
726727 Token &Scanner::peekNext() {
15181519 Stream::Stream(StringRef Input, SourceMgr &SM)
15191520 : scanner(new Scanner(Input, SM)), CurrentDoc() {}
15201521
1521 Stream::Stream(std::unique_ptr InputBuffer, SourceMgr &SM)
1522 : scanner(new Scanner(std::move(InputBuffer), SM)), CurrentDoc() {}
1522 Stream::Stream(MemoryBufferRef InputBuffer, SourceMgr &SM)
1523 : scanner(new Scanner(InputBuffer, SM)), CurrentDoc() {}
15231524
15241525 Stream::~Stream() {}
15251526
211211 // we get its ID as filename in diagnostics.
212212 std::unique_ptr Buffer(
213213 MemoryBuffer::getMemBuffer("[]", "buffername.yaml"));
214 yaml::Stream Stream(std::move(Buffer), SM);
214 yaml::Stream Stream(Buffer->getMemBufferRef(), SM);
215215 Stream.printError(Stream.begin()->getRoot(), "Hello, World!");
216216 EXPECT_EQ("buffername.yaml", GeneratedDiag.getFilename());
217217 }