X-Git-Url: http://plrg.eecs.uci.edu/git/?a=blobdiff_plain;f=lib%2FSupport%2FLineIterator.cpp;h=5baa1a37f385df9f8e099726dea3601b6107f41d;hb=c848236c936decfe077c92bf6232cf0feb913b26;hp=947a8fb6062d04c997b81945b1f573df858bbcd0;hpb=34bc6b6e787f27b5c9e05c82de4c1b4ac9b117bc;p=oota-llvm.git diff --git a/lib/Support/LineIterator.cpp b/lib/Support/LineIterator.cpp index 947a8fb6062..5baa1a37f38 100644 --- a/lib/Support/LineIterator.cpp +++ b/lib/Support/LineIterator.cpp @@ -12,16 +12,39 @@ using namespace llvm; -line_iterator::line_iterator(const MemoryBuffer &Buffer, char CommentMarker) +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; +} + +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 || !isAtLineEnd(Buffer.getBufferStart())) + advance(); } } @@ -29,25 +52,27 @@ void line_iterator::advance() { assert(Buffer && "Cannot advance past the end!"); const char *Pos = CurrentLine.end(); - assert(Pos == Buffer->getBufferStart() || *Pos == '\n' || *Pos == '\0'); + assert(Pos == Buffer->getBufferStart() || isAtLineEnd(Pos) || *Pos == '\0'); - if (CommentMarker == '\0') { + if (skipIfAtLineEnd(Pos)) + ++LineNumber; + if (!SkipBlanks && isAtLineEnd(Pos)) { + // 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') - ++Blanks; - Pos += Blanks; - LineNumber += Blanks; + while (skipIfAtLineEnd(Pos)) + ++LineNumber; } else { // Skip comments and count line numbers, which is a bit more complex. for (;;) { + if (isAtLineEnd(Pos) && !SkipBlanks) + break; if (*Pos == CommentMarker) do { ++Pos; - } while (*Pos != '\0' && *Pos != '\n'); - if (*Pos != '\n') + } while (*Pos != '\0' && !isAtLineEnd(Pos)); + if (!skipIfAtLineEnd(Pos)) break; - ++Pos; ++LineNumber; } } @@ -61,9 +86,9 @@ void line_iterator::advance() { // Measure the line. size_t Length = 0; - do { + while (Pos[Length] != '\0' && !isAtLineEnd(&Pos[Length])) { ++Length; - } while (Pos[Length] != '\0' && Pos[Length] != '\n'); + } CurrentLine = StringRef(Pos, Length); }