blob: 164436a2c48ed531785e69a21341264ac9300d0f [file] [log] [blame]
Chandler Carruthf8c52812013-12-27 04:28:57 +00001//===- LineIterator.cpp - Implementation of line iteration ----------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Chandler Carruthf8c52812013-12-27 04:28:57 +00006//
7//===----------------------------------------------------------------------===//
8
9#include "llvm/Support/LineIterator.h"
10#include "llvm/Support/MemoryBuffer.h"
11
12using namespace llvm;
13
Rafael Espindola42bce8f2014-11-03 14:09:47 +000014static bool isAtLineEnd(const char *P) {
15 if (*P == '\n')
16 return true;
17 if (*P == '\r' && *(P + 1) == '\n')
18 return true;
19 return false;
20}
21
22static bool skipIfAtLineEnd(const char *&P) {
23 if (*P == '\n') {
24 ++P;
25 return true;
26 }
27 if (*P == '\r' && *(P + 1) == '\n') {
28 P += 2;
29 return true;
30 }
31 return false;
32}
33
Justin Bogner69fe4e92014-09-17 15:43:01 +000034line_iterator::line_iterator(const MemoryBuffer &Buffer, bool SkipBlanks,
35 char CommentMarker)
Craig Topperc10719f2014-04-07 04:17:22 +000036 : Buffer(Buffer.getBufferSize() ? &Buffer : nullptr),
Justin Bogner69fe4e92014-09-17 15:43:01 +000037 CommentMarker(CommentMarker), SkipBlanks(SkipBlanks), LineNumber(1),
Craig Topperc10719f2014-04-07 04:17:22 +000038 CurrentLine(Buffer.getBufferSize() ? Buffer.getBufferStart() : nullptr,
39 0) {
Chandler Carruthf8c52812013-12-27 04:28:57 +000040 // Ensure that if we are constructed on a non-empty memory buffer that it is
41 // a null terminated buffer.
42 if (Buffer.getBufferSize()) {
43 assert(Buffer.getBufferEnd()[0] == '\0');
Justin Bogner69fe4e92014-09-17 15:43:01 +000044 // Make sure we don't skip a leading newline if we're keeping blanks
Rafael Espindola42bce8f2014-11-03 14:09:47 +000045 if (SkipBlanks || !isAtLineEnd(Buffer.getBufferStart()))
Justin Bogner69fe4e92014-09-17 15:43:01 +000046 advance();
Chandler Carruthf8c52812013-12-27 04:28:57 +000047 }
48}
49
50void line_iterator::advance() {
51 assert(Buffer && "Cannot advance past the end!");
52
53 const char *Pos = CurrentLine.end();
Rafael Espindola42bce8f2014-11-03 14:09:47 +000054 assert(Pos == Buffer->getBufferStart() || isAtLineEnd(Pos) || *Pos == '\0');
Chandler Carruthf8c52812013-12-27 04:28:57 +000055
Rafael Espindola42bce8f2014-11-03 14:09:47 +000056 if (skipIfAtLineEnd(Pos))
Justin Bogner69fe4e92014-09-17 15:43:01 +000057 ++LineNumber;
Rafael Espindola42bce8f2014-11-03 14:09:47 +000058 if (!SkipBlanks && isAtLineEnd(Pos)) {
Justin Bogner69fe4e92014-09-17 15:43:01 +000059 // Nothing to do for a blank line.
60 } else if (CommentMarker == '\0') {
Chandler Carruthf8c52812013-12-27 04:28:57 +000061 // If we're not stripping comments, this is simpler.
Rafael Espindola42bce8f2014-11-03 14:09:47 +000062 while (skipIfAtLineEnd(Pos))
63 ++LineNumber;
Chandler Carruthf8c52812013-12-27 04:28:57 +000064 } else {
65 // Skip comments and count line numbers, which is a bit more complex.
66 for (;;) {
Rafael Espindola42bce8f2014-11-03 14:09:47 +000067 if (isAtLineEnd(Pos) && !SkipBlanks)
Justin Bogner69fe4e92014-09-17 15:43:01 +000068 break;
Chandler Carruthf8c52812013-12-27 04:28:57 +000069 if (*Pos == CommentMarker)
70 do {
71 ++Pos;
Rafael Espindola42bce8f2014-11-03 14:09:47 +000072 } while (*Pos != '\0' && !isAtLineEnd(Pos));
73 if (!skipIfAtLineEnd(Pos))
Chandler Carruthf8c52812013-12-27 04:28:57 +000074 break;
Chandler Carruthf8c52812013-12-27 04:28:57 +000075 ++LineNumber;
76 }
77 }
78
79 if (*Pos == '\0') {
80 // We've hit the end of the buffer, reset ourselves to the end state.
Craig Topperc10719f2014-04-07 04:17:22 +000081 Buffer = nullptr;
Chandler Carruthf8c52812013-12-27 04:28:57 +000082 CurrentLine = StringRef();
83 return;
84 }
85
86 // Measure the line.
Chandler Carruth87c3a0c2013-12-27 04:44:35 +000087 size_t Length = 0;
Rafael Espindola42bce8f2014-11-03 14:09:47 +000088 while (Pos[Length] != '\0' && !isAtLineEnd(&Pos[Length])) {
Chandler Carruthf8c52812013-12-27 04:28:57 +000089 ++Length;
Justin Bogner69fe4e92014-09-17 15:43:01 +000090 }
Chandler Carruthf8c52812013-12-27 04:28:57 +000091
92 CurrentLine = StringRef(Pos, Length);
93}