summaryrefslogtreecommitdiffstats
path: root/llvm/lib/Support/LineIterator.cpp
diff options
context:
space:
mode:
authorJustin Bogner <mail@justinbogner.com>2014-09-17 15:43:01 +0000
committerJustin Bogner <mail@justinbogner.com>2014-09-17 15:43:01 +0000
commit69fe4e98fa00cce06d64c8bf81882dfea0851302 (patch)
treebe94d5979419e62fa61aba9b303f9d165b165868 /llvm/lib/Support/LineIterator.cpp
parent253e5da7ad727b518ccfe7befe1d9cd43530cf60 (diff)
downloadbcm5719-llvm-69fe4e98fa00cce06d64c8bf81882dfea0851302.tar.gz
bcm5719-llvm-69fe4e98fa00cce06d64c8bf81882dfea0851302.zip
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. llvm-svn: 217960
Diffstat (limited to 'llvm/lib/Support/LineIterator.cpp')
-rw-r--r--llvm/lib/Support/LineIterator.cpp23
1 files changed, 17 insertions, 6 deletions
diff --git a/llvm/lib/Support/LineIterator.cpp b/llvm/lib/Support/LineIterator.cpp
index 947a8fb6062..573e21a3cba 100644
--- a/llvm/lib/Support/LineIterator.cpp
+++ b/llvm/lib/Support/LineIterator.cpp
@@ -12,16 +12,19 @@
using namespace llvm;
-line_iterator::line_iterator(const MemoryBuffer &Buffer, char CommentMarker)
+line_iterator::line_iterator(const MemoryBuffer &Buffer, bool SkipBlanks,
+ char CommentMarker)
: Buffer(Buffer.getBufferSize() ? &Buffer : nullptr),
- CommentMarker(CommentMarker), LineNumber(1),
+ CommentMarker(CommentMarker), SkipBlanks(SkipBlanks), LineNumber(1),
CurrentLine(Buffer.getBufferSize() ? Buffer.getBufferStart() : nullptr,
0) {
// Ensure that if we are constructed on a non-empty memory buffer that it is
// a null terminated buffer.
if (Buffer.getBufferSize()) {
assert(Buffer.getBufferEnd()[0] == '\0');
- advance();
+ // Make sure we don't skip a leading newline if we're keeping blanks
+ if (SkipBlanks || *Buffer.getBufferStart() != '\n')
+ advance();
}
}
@@ -31,7 +34,13 @@ void line_iterator::advance() {
const char *Pos = CurrentLine.end();
assert(Pos == Buffer->getBufferStart() || *Pos == '\n' || *Pos == '\0');
- if (CommentMarker == '\0') {
+ if (*Pos == '\n') {
+ ++Pos;
+ ++LineNumber;
+ }
+ if (!SkipBlanks && *Pos == '\n') {
+ // Nothing to do for a blank line.
+ } else if (CommentMarker == '\0') {
// If we're not stripping comments, this is simpler.
size_t Blanks = 0;
while (Pos[Blanks] == '\n')
@@ -41,6 +50,8 @@ void line_iterator::advance() {
} else {
// Skip comments and count line numbers, which is a bit more complex.
for (;;) {
+ if (*Pos == '\n' && !SkipBlanks)
+ break;
if (*Pos == CommentMarker)
do {
++Pos;
@@ -61,9 +72,9 @@ void line_iterator::advance() {
// Measure the line.
size_t Length = 0;
- do {
+ while (Pos[Length] != '\0' && Pos[Length] != '\n') {
++Length;
- } while (Pos[Length] != '\0' && Pos[Length] != '\n');
+ }
CurrentLine = StringRef(Pos, Length);
}
OpenPOWER on IntegriCloud