191bc56edSDimitry Andric //===- LineIterator.cpp - Implementation of line iteration ----------------===//
291bc56edSDimitry Andric //
391bc56edSDimitry Andric // The LLVM Compiler Infrastructure
491bc56edSDimitry Andric //
591bc56edSDimitry Andric // This file is distributed under the University of Illinois Open Source
691bc56edSDimitry Andric // License. See LICENSE.TXT for details.
791bc56edSDimitry Andric //
891bc56edSDimitry Andric //===----------------------------------------------------------------------===//
991bc56edSDimitry Andric
1091bc56edSDimitry Andric #include "llvm/Support/LineIterator.h"
1191bc56edSDimitry Andric #include "llvm/Support/MemoryBuffer.h"
1291bc56edSDimitry Andric
1391bc56edSDimitry Andric using namespace llvm;
1491bc56edSDimitry Andric
isAtLineEnd(const char * P)15*39d628a0SDimitry Andric static bool isAtLineEnd(const char *P) {
16*39d628a0SDimitry Andric if (*P == '\n')
17*39d628a0SDimitry Andric return true;
18*39d628a0SDimitry Andric if (*P == '\r' && *(P + 1) == '\n')
19*39d628a0SDimitry Andric return true;
20*39d628a0SDimitry Andric return false;
21*39d628a0SDimitry Andric }
22*39d628a0SDimitry Andric
skipIfAtLineEnd(const char * & P)23*39d628a0SDimitry Andric static bool skipIfAtLineEnd(const char *&P) {
24*39d628a0SDimitry Andric if (*P == '\n') {
25*39d628a0SDimitry Andric ++P;
26*39d628a0SDimitry Andric return true;
27*39d628a0SDimitry Andric }
28*39d628a0SDimitry Andric if (*P == '\r' && *(P + 1) == '\n') {
29*39d628a0SDimitry Andric P += 2;
30*39d628a0SDimitry Andric return true;
31*39d628a0SDimitry Andric }
32*39d628a0SDimitry Andric return false;
33*39d628a0SDimitry Andric }
34*39d628a0SDimitry Andric
line_iterator(const MemoryBuffer & Buffer,bool SkipBlanks,char CommentMarker)35*39d628a0SDimitry Andric line_iterator::line_iterator(const MemoryBuffer &Buffer, bool SkipBlanks,
36*39d628a0SDimitry Andric char CommentMarker)
3791bc56edSDimitry Andric : Buffer(Buffer.getBufferSize() ? &Buffer : nullptr),
38*39d628a0SDimitry Andric CommentMarker(CommentMarker), SkipBlanks(SkipBlanks), LineNumber(1),
3991bc56edSDimitry Andric CurrentLine(Buffer.getBufferSize() ? Buffer.getBufferStart() : nullptr,
4091bc56edSDimitry Andric 0) {
4191bc56edSDimitry Andric // Ensure that if we are constructed on a non-empty memory buffer that it is
4291bc56edSDimitry Andric // a null terminated buffer.
4391bc56edSDimitry Andric if (Buffer.getBufferSize()) {
4491bc56edSDimitry Andric assert(Buffer.getBufferEnd()[0] == '\0');
45*39d628a0SDimitry Andric // Make sure we don't skip a leading newline if we're keeping blanks
46*39d628a0SDimitry Andric if (SkipBlanks || !isAtLineEnd(Buffer.getBufferStart()))
4791bc56edSDimitry Andric advance();
4891bc56edSDimitry Andric }
4991bc56edSDimitry Andric }
5091bc56edSDimitry Andric
advance()5191bc56edSDimitry Andric void line_iterator::advance() {
5291bc56edSDimitry Andric assert(Buffer && "Cannot advance past the end!");
5391bc56edSDimitry Andric
5491bc56edSDimitry Andric const char *Pos = CurrentLine.end();
55*39d628a0SDimitry Andric assert(Pos == Buffer->getBufferStart() || isAtLineEnd(Pos) || *Pos == '\0');
5691bc56edSDimitry Andric
57*39d628a0SDimitry Andric if (skipIfAtLineEnd(Pos))
58*39d628a0SDimitry Andric ++LineNumber;
59*39d628a0SDimitry Andric if (!SkipBlanks && isAtLineEnd(Pos)) {
60*39d628a0SDimitry Andric // Nothing to do for a blank line.
61*39d628a0SDimitry Andric } else if (CommentMarker == '\0') {
6291bc56edSDimitry Andric // If we're not stripping comments, this is simpler.
63*39d628a0SDimitry Andric while (skipIfAtLineEnd(Pos))
64*39d628a0SDimitry Andric ++LineNumber;
6591bc56edSDimitry Andric } else {
6691bc56edSDimitry Andric // Skip comments and count line numbers, which is a bit more complex.
6791bc56edSDimitry Andric for (;;) {
68*39d628a0SDimitry Andric if (isAtLineEnd(Pos) && !SkipBlanks)
69*39d628a0SDimitry Andric break;
7091bc56edSDimitry Andric if (*Pos == CommentMarker)
7191bc56edSDimitry Andric do {
7291bc56edSDimitry Andric ++Pos;
73*39d628a0SDimitry Andric } while (*Pos != '\0' && !isAtLineEnd(Pos));
74*39d628a0SDimitry Andric if (!skipIfAtLineEnd(Pos))
7591bc56edSDimitry Andric break;
7691bc56edSDimitry Andric ++LineNumber;
7791bc56edSDimitry Andric }
7891bc56edSDimitry Andric }
7991bc56edSDimitry Andric
8091bc56edSDimitry Andric if (*Pos == '\0') {
8191bc56edSDimitry Andric // We've hit the end of the buffer, reset ourselves to the end state.
8291bc56edSDimitry Andric Buffer = nullptr;
8391bc56edSDimitry Andric CurrentLine = StringRef();
8491bc56edSDimitry Andric return;
8591bc56edSDimitry Andric }
8691bc56edSDimitry Andric
8791bc56edSDimitry Andric // Measure the line.
8891bc56edSDimitry Andric size_t Length = 0;
89*39d628a0SDimitry Andric while (Pos[Length] != '\0' && !isAtLineEnd(&Pos[Length])) {
9091bc56edSDimitry Andric ++Length;
91*39d628a0SDimitry Andric }
9291bc56edSDimitry Andric
9391bc56edSDimitry Andric CurrentLine = StringRef(Pos, Length);
9491bc56edSDimitry Andric }
95