blob: 5baa1a37f385df9f8e099726dea3601b6107f41d [file] [log] [blame]
Chandler Carruthf8c52812013-12-27 04:28:57 +00001//===- LineIterator.cpp - Implementation of line iteration ----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "llvm/Support/LineIterator.h"
11#include "llvm/Support/MemoryBuffer.h"
12
13using namespace llvm;
14
Rafael Espindola42bce8f2014-11-03 14:09:47 +000015static bool isAtLineEnd(const char *P) {
16 if (*P == '\n')
17 return true;
18 if (*P == '\r' && *(P + 1) == '\n')
19 return true;
20 return false;
21}
22
23static bool skipIfAtLineEnd(const char *&P) {
24 if (*P == '\n') {
25 ++P;
26 return true;
27 }
28 if (*P == '\r' && *(P + 1) == '\n') {
29 P += 2;
30 return true;
31 }
32 return false;
33}
34
Justin Bogner69fe4e92014-09-17 15:43:01 +000035line_iterator::line_iterator(const MemoryBuffer &Buffer, bool SkipBlanks,
36 char CommentMarker)
Craig Topperc10719f2014-04-07 04:17:22 +000037 : Buffer(Buffer.getBufferSize() ? &Buffer : nullptr),
Justin Bogner69fe4e92014-09-17 15:43:01 +000038 CommentMarker(CommentMarker), SkipBlanks(SkipBlanks), LineNumber(1),
Craig Topperc10719f2014-04-07 04:17:22 +000039 CurrentLine(Buffer.getBufferSize() ? Buffer.getBufferStart() : nullptr,
40 0) {
Chandler Carruthf8c52812013-12-27 04:28:57 +000041 // Ensure that if we are constructed on a non-empty memory buffer that it is
42 // a null terminated buffer.
43 if (Buffer.getBufferSize()) {
44 assert(Buffer.getBufferEnd()[0] == '\0');
Justin Bogner69fe4e92014-09-17 15:43:01 +000045 // Make sure we don't skip a leading newline if we're keeping blanks
Rafael Espindola42bce8f2014-11-03 14:09:47 +000046 if (SkipBlanks || !isAtLineEnd(Buffer.getBufferStart()))
Justin Bogner69fe4e92014-09-17 15:43:01 +000047 advance();
Chandler Carruthf8c52812013-12-27 04:28:57 +000048 }
49}
50
51void line_iterator::advance() {
52 assert(Buffer && "Cannot advance past the end!");
53
54 const char *Pos = CurrentLine.end();
Rafael Espindola42bce8f2014-11-03 14:09:47 +000055 assert(Pos == Buffer->getBufferStart() || isAtLineEnd(Pos) || *Pos == '\0');
Chandler Carruthf8c52812013-12-27 04:28:57 +000056
Rafael Espindola42bce8f2014-11-03 14:09:47 +000057 if (skipIfAtLineEnd(Pos))
Justin Bogner69fe4e92014-09-17 15:43:01 +000058 ++LineNumber;
Rafael Espindola42bce8f2014-11-03 14:09:47 +000059 if (!SkipBlanks && isAtLineEnd(Pos)) {
Justin Bogner69fe4e92014-09-17 15:43:01 +000060 // Nothing to do for a blank line.
61 } else if (CommentMarker == '\0') {
Chandler Carruthf8c52812013-12-27 04:28:57 +000062 // If we're not stripping comments, this is simpler.
Rafael Espindola42bce8f2014-11-03 14:09:47 +000063 while (skipIfAtLineEnd(Pos))
64 ++LineNumber;
Chandler Carruthf8c52812013-12-27 04:28:57 +000065 } else {
66 // Skip comments and count line numbers, which is a bit more complex.
67 for (;;) {
Rafael Espindola42bce8f2014-11-03 14:09:47 +000068 if (isAtLineEnd(Pos) && !SkipBlanks)
Justin Bogner69fe4e92014-09-17 15:43:01 +000069 break;
Chandler Carruthf8c52812013-12-27 04:28:57 +000070 if (*Pos == CommentMarker)
71 do {
72 ++Pos;
Rafael Espindola42bce8f2014-11-03 14:09:47 +000073 } while (*Pos != '\0' && !isAtLineEnd(Pos));
74 if (!skipIfAtLineEnd(Pos))
Chandler Carruthf8c52812013-12-27 04:28:57 +000075 break;
Chandler Carruthf8c52812013-12-27 04:28:57 +000076 ++LineNumber;
77 }
78 }
79
80 if (*Pos == '\0') {
81 // We've hit the end of the buffer, reset ourselves to the end state.
Craig Topperc10719f2014-04-07 04:17:22 +000082 Buffer = nullptr;
Chandler Carruthf8c52812013-12-27 04:28:57 +000083 CurrentLine = StringRef();
84 return;
85 }
86
87 // Measure the line.
Chandler Carruth87c3a0c2013-12-27 04:44:35 +000088 size_t Length = 0;
Rafael Espindola42bce8f2014-11-03 14:09:47 +000089 while (Pos[Length] != '\0' && !isAtLineEnd(&Pos[Length])) {
Chandler Carruthf8c52812013-12-27 04:28:57 +000090 ++Length;
Justin Bogner69fe4e92014-09-17 15:43:01 +000091 }
Chandler Carruthf8c52812013-12-27 04:28:57 +000092
93 CurrentLine = StringRef(Pos, Length);
94}