2013-12-27 04:28:57 +00:00
|
|
|
//===- LineIterator.cpp - Implementation of line iteration ----------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Support/LineIterator.h"
|
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2014-11-03 14:09:47 +00:00
|
|
|
static bool isAtLineEnd(const char *P) {
|
|
|
|
if (*P == '\n')
|
|
|
|
return true;
|
|
|
|
if (*P == '\r' && *(P + 1) == '\n')
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool skipIfAtLineEnd(const char *&P) {
|
|
|
|
if (*P == '\n') {
|
|
|
|
++P;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (*P == '\r' && *(P + 1) == '\n') {
|
|
|
|
P += 2;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-09-17 15:43:01 +00:00
|
|
|
line_iterator::line_iterator(const MemoryBuffer &Buffer, bool SkipBlanks,
|
|
|
|
char CommentMarker)
|
2014-04-07 04:17:22 +00:00
|
|
|
: Buffer(Buffer.getBufferSize() ? &Buffer : nullptr),
|
2014-09-17 15:43:01 +00:00
|
|
|
CommentMarker(CommentMarker), SkipBlanks(SkipBlanks), LineNumber(1),
|
2014-04-07 04:17:22 +00:00
|
|
|
CurrentLine(Buffer.getBufferSize() ? Buffer.getBufferStart() : nullptr,
|
|
|
|
0) {
|
2013-12-27 04:28:57 +00:00
|
|
|
// 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');
|
2014-09-17 15:43:01 +00:00
|
|
|
// Make sure we don't skip a leading newline if we're keeping blanks
|
2014-11-03 14:09:47 +00:00
|
|
|
if (SkipBlanks || !isAtLineEnd(Buffer.getBufferStart()))
|
2014-09-17 15:43:01 +00:00
|
|
|
advance();
|
2013-12-27 04:28:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void line_iterator::advance() {
|
|
|
|
assert(Buffer && "Cannot advance past the end!");
|
|
|
|
|
|
|
|
const char *Pos = CurrentLine.end();
|
2014-11-03 14:09:47 +00:00
|
|
|
assert(Pos == Buffer->getBufferStart() || isAtLineEnd(Pos) || *Pos == '\0');
|
2013-12-27 04:28:57 +00:00
|
|
|
|
2014-11-03 14:09:47 +00:00
|
|
|
if (skipIfAtLineEnd(Pos))
|
2014-09-17 15:43:01 +00:00
|
|
|
++LineNumber;
|
2014-11-03 14:09:47 +00:00
|
|
|
if (!SkipBlanks && isAtLineEnd(Pos)) {
|
2014-09-17 15:43:01 +00:00
|
|
|
// Nothing to do for a blank line.
|
|
|
|
} else if (CommentMarker == '\0') {
|
2013-12-27 04:28:57 +00:00
|
|
|
// If we're not stripping comments, this is simpler.
|
2014-11-03 14:09:47 +00:00
|
|
|
while (skipIfAtLineEnd(Pos))
|
|
|
|
++LineNumber;
|
2013-12-27 04:28:57 +00:00
|
|
|
} else {
|
|
|
|
// Skip comments and count line numbers, which is a bit more complex.
|
|
|
|
for (;;) {
|
2014-11-03 14:09:47 +00:00
|
|
|
if (isAtLineEnd(Pos) && !SkipBlanks)
|
2014-09-17 15:43:01 +00:00
|
|
|
break;
|
2013-12-27 04:28:57 +00:00
|
|
|
if (*Pos == CommentMarker)
|
|
|
|
do {
|
|
|
|
++Pos;
|
2014-11-03 14:09:47 +00:00
|
|
|
} while (*Pos != '\0' && !isAtLineEnd(Pos));
|
|
|
|
if (!skipIfAtLineEnd(Pos))
|
2013-12-27 04:28:57 +00:00
|
|
|
break;
|
|
|
|
++LineNumber;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*Pos == '\0') {
|
|
|
|
// We've hit the end of the buffer, reset ourselves to the end state.
|
2014-04-07 04:17:22 +00:00
|
|
|
Buffer = nullptr;
|
2013-12-27 04:28:57 +00:00
|
|
|
CurrentLine = StringRef();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Measure the line.
|
2013-12-27 04:44:35 +00:00
|
|
|
size_t Length = 0;
|
2014-11-03 14:09:47 +00:00
|
|
|
while (Pos[Length] != '\0' && !isAtLineEnd(&Pos[Length])) {
|
2013-12-27 04:28:57 +00:00
|
|
|
++Length;
|
2014-09-17 15:43:01 +00:00
|
|
|
}
|
2013-12-27 04:28:57 +00:00
|
|
|
|
|
|
|
CurrentLine = StringRef(Pos, Length);
|
|
|
|
}
|