1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-11-24 11:42:57 +01:00
llvm-mirror/lib/Support/LineIterator.cpp
Chandler Carruth ae65e281f3 Update the file headers across all of the LLVM projects in the monorepo
to reflect the new license.

We understand that people may be surprised that we're moving the header
entirely to discuss the new license. We checked this carefully with the
Foundation's lawyer and we believe this is the correct approach.

Essentially, all code in the project is now made available by the LLVM
project under our new license, so you will see that the license headers
include that license only. Some of our contributors have contributed
code under our old license, and accordingly, we have retained a copy of
our old license notice in the top-level files in each project and
repository.

llvm-svn: 351636
2019-01-19 08:50:56 +00:00

94 lines
2.6 KiB
C++

//===- LineIterator.cpp - Implementation of line iteration ----------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "llvm/Support/LineIterator.h"
#include "llvm/Support/MemoryBuffer.h"
using namespace llvm;
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), 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');
// Make sure we don't skip a leading newline if we're keeping blanks
if (SkipBlanks || !isAtLineEnd(Buffer.getBufferStart()))
advance();
}
}
void line_iterator::advance() {
assert(Buffer && "Cannot advance past the end!");
const char *Pos = CurrentLine.end();
assert(Pos == Buffer->getBufferStart() || isAtLineEnd(Pos) || *Pos == '\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.
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' && !isAtLineEnd(Pos));
if (!skipIfAtLineEnd(Pos))
break;
++LineNumber;
}
}
if (*Pos == '\0') {
// We've hit the end of the buffer, reset ourselves to the end state.
Buffer = nullptr;
CurrentLine = StringRef();
return;
}
// Measure the line.
size_t Length = 0;
while (Pos[Length] != '\0' && !isAtLineEnd(&Pos[Length])) {
++Length;
}
CurrentLine = StringRef(Pos, Length);
}