Skip to content
This repository was archived by the owner on Feb 5, 2019. It is now read-only.

Commit 0a277ea

Browse files
committed
LineIterator: Provide a variant that keeps blank lines
It isn't always useful to skip blank lines, as evidenced by the somewhat awkward use of line_iterator in llvm-cov. This adds a knob to control whether or not to skip blanks. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@217960 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 27608d8 commit 0a277ea

File tree

6 files changed

+117
-29
lines changed

6 files changed

+117
-29
lines changed

include/llvm/ProfileData/InstrProfReader.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ class TextInstrProfReader : public InstrProfReader {
120120
LLVM_DELETED_FUNCTION;
121121
public:
122122
TextInstrProfReader(std::unique_ptr<MemoryBuffer> DataBuffer_)
123-
: DataBuffer(std::move(DataBuffer_)), Line(*DataBuffer, '#') {}
123+
: DataBuffer(std::move(DataBuffer_)), Line(*DataBuffer, true, '#') {}
124124

125125
/// Read the header.
126126
std::error_code readHeader() override { return success(); }

include/llvm/Support/LineIterator.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,22 @@ namespace llvm {
1818

1919
class MemoryBuffer;
2020

21-
/// \brief A forward iterator which reads non-blank text lines from a buffer.
21+
/// \brief A forward iterator which reads text lines from a buffer.
2222
///
2323
/// This class provides a forward iterator interface for reading one line at
2424
/// a time from a buffer. When default constructed the iterator will be the
2525
/// "end" iterator.
2626
///
27-
/// The iterator also is aware of what line number it is currently processing
28-
/// and can strip comment lines given the comment-starting character.
27+
/// The iterator is aware of what line number it is currently processing. It
28+
/// strips blank lines by default, and comment lines given a comment-starting
29+
/// character.
2930
///
3031
/// Note that this iterator requires the buffer to be nul terminated.
3132
class line_iterator
3233
: public std::iterator<std::forward_iterator_tag, StringRef> {
3334
const MemoryBuffer *Buffer;
3435
char CommentMarker;
36+
bool SkipBlanks;
3537

3638
unsigned LineNumber;
3739
StringRef CurrentLine;
@@ -41,7 +43,8 @@ class line_iterator
4143
line_iterator() : Buffer(nullptr) {}
4244

4345
/// \brief Construct a new iterator around some memory buffer.
44-
explicit line_iterator(const MemoryBuffer &Buffer, char CommentMarker = '\0');
46+
explicit line_iterator(const MemoryBuffer &Buffer, bool SkipBlanks = true,
47+
char CommentMarker = '\0');
4548

4649
/// \brief Return true if we've reached EOF or are an "end" iterator.
4750
bool is_at_eof() const { return !Buffer; }

lib/ProfileData/SampleProfReader.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ bool SampleProfileReader::loadText() {
159159
return false;
160160
}
161161
MemoryBuffer &Buffer = *BufferOrErr.get();
162-
line_iterator LineIt(Buffer, '#');
162+
line_iterator LineIt(Buffer, /*SkipBlanks=*/true, '#');
163163

164164
// Read the profile of each function. Since each function may be
165165
// mentioned more than once, and we are collecting flat profiles,

lib/Support/LineIterator.cpp

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,19 @@
1212

1313
using namespace llvm;
1414

15-
line_iterator::line_iterator(const MemoryBuffer &Buffer, char CommentMarker)
15+
line_iterator::line_iterator(const MemoryBuffer &Buffer, bool SkipBlanks,
16+
char CommentMarker)
1617
: Buffer(Buffer.getBufferSize() ? &Buffer : nullptr),
17-
CommentMarker(CommentMarker), LineNumber(1),
18+
CommentMarker(CommentMarker), SkipBlanks(SkipBlanks), LineNumber(1),
1819
CurrentLine(Buffer.getBufferSize() ? Buffer.getBufferStart() : nullptr,
1920
0) {
2021
// Ensure that if we are constructed on a non-empty memory buffer that it is
2122
// a null terminated buffer.
2223
if (Buffer.getBufferSize()) {
2324
assert(Buffer.getBufferEnd()[0] == '\0');
24-
advance();
25+
// Make sure we don't skip a leading newline if we're keeping blanks
26+
if (SkipBlanks || *Buffer.getBufferStart() != '\n')
27+
advance();
2528
}
2629
}
2730

@@ -31,7 +34,13 @@ void line_iterator::advance() {
3134
const char *Pos = CurrentLine.end();
3235
assert(Pos == Buffer->getBufferStart() || *Pos == '\n' || *Pos == '\0');
3336

34-
if (CommentMarker == '\0') {
37+
if (*Pos == '\n') {
38+
++Pos;
39+
++LineNumber;
40+
}
41+
if (!SkipBlanks && *Pos == '\n') {
42+
// Nothing to do for a blank line.
43+
} else if (CommentMarker == '\0') {
3544
// If we're not stripping comments, this is simpler.
3645
size_t Blanks = 0;
3746
while (Pos[Blanks] == '\n')
@@ -41,6 +50,8 @@ void line_iterator::advance() {
4150
} else {
4251
// Skip comments and count line numbers, which is a bit more complex.
4352
for (;;) {
53+
if (*Pos == '\n' && !SkipBlanks)
54+
break;
4455
if (*Pos == CommentMarker)
4556
do {
4657
++Pos;
@@ -61,9 +72,9 @@ void line_iterator::advance() {
6172

6273
// Measure the line.
6374
size_t Length = 0;
64-
do {
75+
while (Pos[Length] != '\0' && Pos[Length] != '\n') {
6576
++Length;
66-
} while (Pos[Length] != '\0' && Pos[Length] != '\n');
77+
}
6778

6879
CurrentLine = StringRef(Pos, Length);
6980
}

tools/llvm-cov/SourceCoverageView.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ void SourceCoverageView::render(raw_ostream &OS, unsigned IndentLevel) {
206206
size_t CurrentHighlightRange = 0;
207207
size_t CurrentRegionMarker = 0;
208208

209-
line_iterator Lines(File);
209+
line_iterator Lines(File, /*SkipBlanks=*/false);
210210
// Advance the line iterator to the first line.
211211
while (Lines.line_number() < LineOffset)
212212
++Lines;
@@ -228,8 +228,9 @@ void SourceCoverageView::render(raw_ostream &OS, unsigned IndentLevel) {
228228
auto NextISV = InstantiationSubViews.begin();
229229
auto EndISV = InstantiationSubViews.end();
230230

231-
for (size_t I = 0, E = LineStats.size(); I < E; ++I) {
232-
unsigned LineNo = I + LineOffset;
231+
for (size_t I = 0, E = LineStats.size(); I < E && !Lines.is_at_eof();
232+
++I, ++Lines) {
233+
unsigned LineNo = Lines.line_number();
233234

234235
renderIndent(OS, IndentLevel);
235236
if (Options.ShowLineStats)
@@ -252,11 +253,6 @@ void SourceCoverageView::render(raw_ostream &OS, unsigned IndentLevel) {
252253

253254
// Display the source code for the current line.
254255
StringRef Line = *Lines;
255-
// Check if the line is empty, as line_iterator skips blank lines.
256-
if (LineNo < Lines.line_number())
257-
Line = "";
258-
else if (!Lines.is_at_eof())
259-
++Lines;
260256
renderLine(OS, Line, LineRanges);
261257

262258
// Show the region markers.

unittests/Support/LineIteratorTest.cpp

Lines changed: 87 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,17 @@ TEST(LineIteratorTest, Basic) {
4040
EXPECT_EQ(E, I);
4141
}
4242

43-
TEST(LineIteratorTest, CommentSkipping) {
43+
TEST(LineIteratorTest, CommentAndBlankSkipping) {
4444
std::unique_ptr<MemoryBuffer> Buffer(
4545
MemoryBuffer::getMemBuffer("line 1\n"
4646
"line 2\n"
4747
"# Comment 1\n"
48-
"line 4\n"
48+
"\n"
49+
"line 5\n"
50+
"\n"
4951
"# Comment 2"));
5052

51-
line_iterator I = line_iterator(*Buffer, '#'), E;
53+
line_iterator I = line_iterator(*Buffer, true, '#'), E;
5254

5355
EXPECT_FALSE(I.is_at_eof());
5456
EXPECT_NE(E, I);
@@ -59,14 +61,51 @@ TEST(LineIteratorTest, CommentSkipping) {
5961
EXPECT_EQ("line 2", *I);
6062
EXPECT_EQ(2, I.line_number());
6163
++I;
62-
EXPECT_EQ("line 4", *I);
63-
EXPECT_EQ(4, I.line_number());
64+
EXPECT_EQ("line 5", *I);
65+
EXPECT_EQ(5, I.line_number());
66+
++I;
67+
68+
EXPECT_TRUE(I.is_at_eof());
69+
EXPECT_EQ(E, I);
70+
}
71+
72+
TEST(LineIteratorTest, CommentSkippingKeepBlanks) {
73+
std::unique_ptr<MemoryBuffer> Buffer(
74+
MemoryBuffer::getMemBuffer("line 1\n"
75+
"line 2\n"
76+
"# Comment 1\n"
77+
"# Comment 2\n"
78+
"\n"
79+
"line 6\n"
80+
"\n"
81+
"# Comment 3"));
82+
83+
line_iterator I = line_iterator(*Buffer, false, '#'), E;
84+
85+
EXPECT_FALSE(I.is_at_eof());
86+
EXPECT_NE(E, I);
87+
88+
EXPECT_EQ("line 1", *I);
89+
EXPECT_EQ(1, I.line_number());
90+
++I;
91+
EXPECT_EQ("line 2", *I);
92+
EXPECT_EQ(2, I.line_number());
93+
++I;
94+
EXPECT_EQ("", *I);
95+
EXPECT_EQ(5, I.line_number());
96+
++I;
97+
EXPECT_EQ("line 6", *I);
98+
EXPECT_EQ(6, I.line_number());
99+
++I;
100+
EXPECT_EQ("", *I);
101+
EXPECT_EQ(7, I.line_number());
64102
++I;
65103

66104
EXPECT_TRUE(I.is_at_eof());
67105
EXPECT_EQ(E, I);
68106
}
69107

108+
70109
TEST(LineIteratorTest, BlankSkipping) {
71110
std::unique_ptr<MemoryBuffer> Buffer = MemoryBuffer::getMemBuffer("\n\n\n"
72111
"line 1\n"
@@ -90,10 +129,49 @@ TEST(LineIteratorTest, BlankSkipping) {
90129
EXPECT_EQ(E, I);
91130
}
92131

132+
TEST(LineIteratorTest, BlankKeeping) {
133+
std::unique_ptr<MemoryBuffer> Buffer = MemoryBuffer::getMemBuffer("\n\n"
134+
"line 3\n"
135+
"\n"
136+
"line 5\n"
137+
"\n\n");
138+
line_iterator I = line_iterator(*Buffer, false), E;
139+
140+
EXPECT_FALSE(I.is_at_eof());
141+
EXPECT_NE(E, I);
142+
143+
EXPECT_EQ("", *I);
144+
EXPECT_EQ(1, I.line_number());
145+
++I;
146+
EXPECT_EQ("", *I);
147+
EXPECT_EQ(2, I.line_number());
148+
++I;
149+
EXPECT_EQ("line 3", *I);
150+
EXPECT_EQ(3, I.line_number());
151+
++I;
152+
EXPECT_EQ("", *I);
153+
EXPECT_EQ(4, I.line_number());
154+
++I;
155+
EXPECT_EQ("line 5", *I);
156+
EXPECT_EQ(5, I.line_number());
157+
++I;
158+
EXPECT_EQ("", *I);
159+
EXPECT_EQ(6, I.line_number());
160+
++I;
161+
EXPECT_EQ("", *I);
162+
EXPECT_EQ(7, I.line_number());
163+
++I;
164+
165+
EXPECT_TRUE(I.is_at_eof());
166+
EXPECT_EQ(E, I);
167+
}
168+
93169
TEST(LineIteratorTest, EmptyBuffers) {
94170
std::unique_ptr<MemoryBuffer> Buffer = MemoryBuffer::getMemBuffer("");
95171
EXPECT_TRUE(line_iterator(*Buffer).is_at_eof());
96172
EXPECT_EQ(line_iterator(), line_iterator(*Buffer));
173+
EXPECT_TRUE(line_iterator(*Buffer, false).is_at_eof());
174+
EXPECT_EQ(line_iterator(), line_iterator(*Buffer, false));
97175

98176
Buffer = MemoryBuffer::getMemBuffer("\n\n\n");
99177
EXPECT_TRUE(line_iterator(*Buffer).is_at_eof());
@@ -102,14 +180,14 @@ TEST(LineIteratorTest, EmptyBuffers) {
102180
Buffer = MemoryBuffer::getMemBuffer("# foo\n"
103181
"\n"
104182
"# bar");
105-
EXPECT_TRUE(line_iterator(*Buffer, '#').is_at_eof());
106-
EXPECT_EQ(line_iterator(), line_iterator(*Buffer, '#'));
183+
EXPECT_TRUE(line_iterator(*Buffer, true, '#').is_at_eof());
184+
EXPECT_EQ(line_iterator(), line_iterator(*Buffer, true, '#'));
107185

108186
Buffer = MemoryBuffer::getMemBuffer("\n"
109187
"# baz\n"
110188
"\n");
111-
EXPECT_TRUE(line_iterator(*Buffer, '#').is_at_eof());
112-
EXPECT_EQ(line_iterator(), line_iterator(*Buffer, '#'));
189+
EXPECT_TRUE(line_iterator(*Buffer, true, '#').is_at_eof());
190+
EXPECT_EQ(line_iterator(), line_iterator(*Buffer, true, '#'));
113191
}
114192

115193
} // anonymous namespace

0 commit comments

Comments
 (0)