mirror of
https://github.com/RPCS3/llvm-mirror.git
synced 2025-01-31 12:41:49 +01:00
Support: Allow use of MemoryBufferRef with line_iterator
Split out from https://reviews.llvm.org/D66782, use `Optional<MemoryBufferRef>` in `line_iterator` so you don't need access to a `MemoryBuffer*`. Follow up patches in `clang/` will leverage this. Differential Revision: https://reviews.llvm.org/D89280
This commit is contained in:
parent
b7c96281d2
commit
42d5edd276
@ -9,8 +9,10 @@
|
|||||||
#ifndef LLVM_SUPPORT_LINEITERATOR_H
|
#ifndef LLVM_SUPPORT_LINEITERATOR_H
|
||||||
#define LLVM_SUPPORT_LINEITERATOR_H
|
#define LLVM_SUPPORT_LINEITERATOR_H
|
||||||
|
|
||||||
|
#include "llvm/ADT/Optional.h"
|
||||||
#include "llvm/ADT/StringRef.h"
|
#include "llvm/ADT/StringRef.h"
|
||||||
#include "llvm/Support/DataTypes.h"
|
#include "llvm/Support/DataTypes.h"
|
||||||
|
#include "llvm/Support/MemoryBufferRef.h"
|
||||||
#include <iterator>
|
#include <iterator>
|
||||||
|
|
||||||
namespace llvm {
|
namespace llvm {
|
||||||
@ -30,7 +32,7 @@ class MemoryBuffer;
|
|||||||
/// Note that this iterator requires the buffer to be nul terminated.
|
/// Note that this iterator requires the buffer to be nul terminated.
|
||||||
class line_iterator
|
class line_iterator
|
||||||
: public std::iterator<std::forward_iterator_tag, StringRef> {
|
: public std::iterator<std::forward_iterator_tag, StringRef> {
|
||||||
const MemoryBuffer *Buffer = nullptr;
|
Optional<MemoryBufferRef> Buffer;
|
||||||
char CommentMarker = '\0';
|
char CommentMarker = '\0';
|
||||||
bool SkipBlanks = true;
|
bool SkipBlanks = true;
|
||||||
|
|
||||||
@ -41,6 +43,10 @@ public:
|
|||||||
/// Default construct an "end" iterator.
|
/// Default construct an "end" iterator.
|
||||||
line_iterator() = default;
|
line_iterator() = default;
|
||||||
|
|
||||||
|
/// Construct a new iterator around an unowned memory buffer.
|
||||||
|
explicit line_iterator(const MemoryBufferRef &Buffer, bool SkipBlanks = true,
|
||||||
|
char CommentMarker = '\0');
|
||||||
|
|
||||||
/// Construct a new iterator around some memory buffer.
|
/// Construct a new iterator around some memory buffer.
|
||||||
explicit line_iterator(const MemoryBuffer &Buffer, bool SkipBlanks = true,
|
explicit line_iterator(const MemoryBuffer &Buffer, bool SkipBlanks = true,
|
||||||
char CommentMarker = '\0');
|
char CommentMarker = '\0');
|
||||||
|
@ -33,7 +33,11 @@ static bool skipIfAtLineEnd(const char *&P) {
|
|||||||
|
|
||||||
line_iterator::line_iterator(const MemoryBuffer &Buffer, bool SkipBlanks,
|
line_iterator::line_iterator(const MemoryBuffer &Buffer, bool SkipBlanks,
|
||||||
char CommentMarker)
|
char CommentMarker)
|
||||||
: Buffer(Buffer.getBufferSize() ? &Buffer : nullptr),
|
: line_iterator(Buffer.getMemBufferRef(), SkipBlanks, CommentMarker) {}
|
||||||
|
|
||||||
|
line_iterator::line_iterator(const MemoryBufferRef &Buffer, bool SkipBlanks,
|
||||||
|
char CommentMarker)
|
||||||
|
: Buffer(Buffer.getBufferSize() ? Optional<MemoryBufferRef>(Buffer) : None),
|
||||||
CommentMarker(CommentMarker), SkipBlanks(SkipBlanks), LineNumber(1),
|
CommentMarker(CommentMarker), SkipBlanks(SkipBlanks), LineNumber(1),
|
||||||
CurrentLine(Buffer.getBufferSize() ? Buffer.getBufferStart() : nullptr,
|
CurrentLine(Buffer.getBufferSize() ? Buffer.getBufferStart() : nullptr,
|
||||||
0) {
|
0) {
|
||||||
@ -78,7 +82,7 @@ void line_iterator::advance() {
|
|||||||
|
|
||||||
if (*Pos == '\0') {
|
if (*Pos == '\0') {
|
||||||
// We've hit the end of the buffer, reset ourselves to the end state.
|
// We've hit the end of the buffer, reset ourselves to the end state.
|
||||||
Buffer = nullptr;
|
Buffer = None;
|
||||||
CurrentLine = StringRef();
|
CurrentLine = StringRef();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -39,6 +39,30 @@ TEST(LineIteratorTest, Basic) {
|
|||||||
EXPECT_EQ(E, I);
|
EXPECT_EQ(E, I);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST(LineIteratorTest, Ref) {
|
||||||
|
std::unique_ptr<MemoryBuffer> Buffer = MemoryBuffer::getMemBuffer("line 1\n"
|
||||||
|
"line 2\n"
|
||||||
|
"line 3");
|
||||||
|
|
||||||
|
line_iterator I = line_iterator(Buffer->getMemBufferRef()), E;
|
||||||
|
|
||||||
|
EXPECT_FALSE(I.is_at_eof());
|
||||||
|
EXPECT_NE(E, I);
|
||||||
|
|
||||||
|
EXPECT_EQ("line 1", *I);
|
||||||
|
EXPECT_EQ(1, I.line_number());
|
||||||
|
++I;
|
||||||
|
EXPECT_EQ("line 2", *I);
|
||||||
|
EXPECT_EQ(2, I.line_number());
|
||||||
|
++I;
|
||||||
|
EXPECT_EQ("line 3", *I);
|
||||||
|
EXPECT_EQ(3, I.line_number());
|
||||||
|
++I;
|
||||||
|
|
||||||
|
EXPECT_TRUE(I.is_at_eof());
|
||||||
|
EXPECT_EQ(E, I);
|
||||||
|
}
|
||||||
|
|
||||||
TEST(LineIteratorTest, CommentAndBlankSkipping) {
|
TEST(LineIteratorTest, CommentAndBlankSkipping) {
|
||||||
std::unique_ptr<MemoryBuffer> Buffer(
|
std::unique_ptr<MemoryBuffer> Buffer(
|
||||||
MemoryBuffer::getMemBuffer("line 1\n"
|
MemoryBuffer::getMemBuffer("line 1\n"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user