1*0b57cec5SDimitry Andric //===- LineIterator.cpp - Implementation of line iteration ----------------===//
2*0b57cec5SDimitry Andric //
3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0b57cec5SDimitry Andric //
7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
8*0b57cec5SDimitry Andric
9*0b57cec5SDimitry Andric #include "llvm/Support/LineIterator.h"
10*0b57cec5SDimitry Andric #include "llvm/Support/MemoryBuffer.h"
11*0b57cec5SDimitry Andric
12*0b57cec5SDimitry Andric using namespace llvm;
13*0b57cec5SDimitry Andric
isAtLineEnd(const char * P)14*0b57cec5SDimitry Andric static bool isAtLineEnd(const char *P) {
15*0b57cec5SDimitry Andric if (*P == '\n')
16*0b57cec5SDimitry Andric return true;
17*0b57cec5SDimitry Andric if (*P == '\r' && *(P + 1) == '\n')
18*0b57cec5SDimitry Andric return true;
19*0b57cec5SDimitry Andric return false;
20*0b57cec5SDimitry Andric }
21*0b57cec5SDimitry Andric
skipIfAtLineEnd(const char * & P)22*0b57cec5SDimitry Andric static bool skipIfAtLineEnd(const char *&P) {
23*0b57cec5SDimitry Andric if (*P == '\n') {
24*0b57cec5SDimitry Andric ++P;
25*0b57cec5SDimitry Andric return true;
26*0b57cec5SDimitry Andric }
27*0b57cec5SDimitry Andric if (*P == '\r' && *(P + 1) == '\n') {
28*0b57cec5SDimitry Andric P += 2;
29*0b57cec5SDimitry Andric return true;
30*0b57cec5SDimitry Andric }
31*0b57cec5SDimitry Andric return false;
32*0b57cec5SDimitry Andric }
33*0b57cec5SDimitry Andric
line_iterator(const MemoryBuffer & Buffer,bool SkipBlanks,char CommentMarker)34*0b57cec5SDimitry Andric line_iterator::line_iterator(const MemoryBuffer &Buffer, bool SkipBlanks,
35*0b57cec5SDimitry Andric char CommentMarker)
36*0b57cec5SDimitry Andric : line_iterator(Buffer.getMemBufferRef(), SkipBlanks, CommentMarker) {}
37*0b57cec5SDimitry Andric
line_iterator(const MemoryBufferRef & Buffer,bool SkipBlanks,char CommentMarker)38*0b57cec5SDimitry Andric line_iterator::line_iterator(const MemoryBufferRef &Buffer, bool SkipBlanks,
39*0b57cec5SDimitry Andric char CommentMarker)
40*0b57cec5SDimitry Andric : Buffer(Buffer.getBufferSize() ? std::optional<MemoryBufferRef>(Buffer)
41*0b57cec5SDimitry Andric : std::nullopt),
42*0b57cec5SDimitry Andric CommentMarker(CommentMarker), SkipBlanks(SkipBlanks),
43*0b57cec5SDimitry Andric CurrentLine(Buffer.getBufferSize() ? Buffer.getBufferStart() : nullptr,
44*0b57cec5SDimitry Andric 0) {
45*0b57cec5SDimitry Andric // Ensure that if we are constructed on a non-empty memory buffer that it is
46*0b57cec5SDimitry Andric // a null terminated buffer.
47*0b57cec5SDimitry Andric if (Buffer.getBufferSize()) {
48*0b57cec5SDimitry Andric assert(Buffer.getBufferEnd()[0] == '\0');
49*0b57cec5SDimitry Andric // Make sure we don't skip a leading newline if we're keeping blanks
50*0b57cec5SDimitry Andric if (SkipBlanks || !isAtLineEnd(Buffer.getBufferStart()))
51*0b57cec5SDimitry Andric advance();
52*0b57cec5SDimitry Andric }
53*0b57cec5SDimitry Andric }
54*0b57cec5SDimitry Andric
advance()55*0b57cec5SDimitry Andric void line_iterator::advance() {
56*0b57cec5SDimitry Andric assert(Buffer && "Cannot advance past the end!");
57*0b57cec5SDimitry Andric
58*0b57cec5SDimitry Andric const char *Pos = CurrentLine.end();
59*0b57cec5SDimitry Andric assert(Pos == Buffer->getBufferStart() || isAtLineEnd(Pos) || *Pos == '\0');
60*0b57cec5SDimitry Andric
61*0b57cec5SDimitry Andric if (skipIfAtLineEnd(Pos))
62*0b57cec5SDimitry Andric ++LineNumber;
63*0b57cec5SDimitry Andric if (!SkipBlanks && isAtLineEnd(Pos)) {
64*0b57cec5SDimitry Andric // Nothing to do for a blank line.
65*0b57cec5SDimitry Andric } else if (CommentMarker == '\0') {
66*0b57cec5SDimitry Andric // If we're not stripping comments, this is simpler.
67*0b57cec5SDimitry Andric while (skipIfAtLineEnd(Pos))
68*0b57cec5SDimitry Andric ++LineNumber;
69*0b57cec5SDimitry Andric } else {
70*0b57cec5SDimitry Andric // Skip comments and count line numbers, which is a bit more complex.
71*0b57cec5SDimitry Andric for (;;) {
72*0b57cec5SDimitry Andric if (isAtLineEnd(Pos) && !SkipBlanks)
73*0b57cec5SDimitry Andric break;
74*0b57cec5SDimitry Andric if (*Pos == CommentMarker)
75*0b57cec5SDimitry Andric do {
76*0b57cec5SDimitry Andric ++Pos;
77*0b57cec5SDimitry Andric } while (*Pos != '\0' && !isAtLineEnd(Pos));
78*0b57cec5SDimitry Andric if (!skipIfAtLineEnd(Pos))
79*0b57cec5SDimitry Andric break;
80*0b57cec5SDimitry Andric ++LineNumber;
81*0b57cec5SDimitry Andric }
82*0b57cec5SDimitry Andric }
83*0b57cec5SDimitry Andric
84*0b57cec5SDimitry Andric if (*Pos == '\0') {
85*0b57cec5SDimitry Andric // We've hit the end of the buffer, reset ourselves to the end state.
86*0b57cec5SDimitry Andric Buffer = std::nullopt;
87*0b57cec5SDimitry Andric CurrentLine = StringRef();
88*0b57cec5SDimitry Andric return;
89*0b57cec5SDimitry Andric }
90*0b57cec5SDimitry Andric
91*0b57cec5SDimitry Andric // Measure the line.
92*0b57cec5SDimitry Andric size_t Length = 0;
93*0b57cec5SDimitry Andric while (Pos[Length] != '\0' && !isAtLineEnd(&Pos[Length])) {
94 ++Length;
95 }
96
97 CurrentLine = StringRef(Pos, Length);
98 }
99