1//===- 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
| 1//===- 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
|
15line_iterator::line_iterator(const MemoryBuffer &Buffer, char CommentMarker)
| 15static 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 35line_iterator::line_iterator(const MemoryBuffer &Buffer, bool SkipBlanks, 36 char CommentMarker)
|
16 : Buffer(Buffer.getBufferSize() ? &Buffer : nullptr),
| 37 : Buffer(Buffer.getBufferSize() ? &Buffer : nullptr),
|
17 CommentMarker(CommentMarker), LineNumber(1),
| 38 CommentMarker(CommentMarker), SkipBlanks(SkipBlanks), LineNumber(1),
|
18 CurrentLine(Buffer.getBufferSize() ? Buffer.getBufferStart() : nullptr, 19 0) { 20 // Ensure that if we are constructed on a non-empty memory buffer that it is 21 // a null terminated buffer. 22 if (Buffer.getBufferSize()) { 23 assert(Buffer.getBufferEnd()[0] == '\0');
| 39 CurrentLine(Buffer.getBufferSize() ? Buffer.getBufferStart() : nullptr, 40 0) { 41 // 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');
|
24 advance();
| 45 // Make sure we don't skip a leading newline if we're keeping blanks 46 if (SkipBlanks || !isAtLineEnd(Buffer.getBufferStart())) 47 advance();
|
25 } 26} 27 28void line_iterator::advance() { 29 assert(Buffer && "Cannot advance past the end!"); 30 31 const char *Pos = CurrentLine.end();
| 48 } 49} 50 51void line_iterator::advance() { 52 assert(Buffer && "Cannot advance past the end!"); 53 54 const char *Pos = CurrentLine.end();
|
32 assert(Pos == Buffer->getBufferStart() || *Pos == '\n' || *Pos == '\0');
| 55 assert(Pos == Buffer->getBufferStart() || isAtLineEnd(Pos) || *Pos == '\0');
|
33
| 56
|
34 if (CommentMarker == '\0') {
| 57 if (skipIfAtLineEnd(Pos)) 58 ++LineNumber; 59 if (!SkipBlanks && isAtLineEnd(Pos)) { 60 // Nothing to do for a blank line. 61 } else if (CommentMarker == '\0') {
|
35 // If we're not stripping comments, this is simpler.
| 62 // If we're not stripping comments, this is simpler.
|
36 size_t Blanks = 0; 37 while (Pos[Blanks] == '\n') 38 ++Blanks; 39 Pos += Blanks; 40 LineNumber += Blanks;
| 63 while (skipIfAtLineEnd(Pos)) 64 ++LineNumber;
|
41 } else { 42 // Skip comments and count line numbers, which is a bit more complex. 43 for (;;) {
| 65 } else { 66 // Skip comments and count line numbers, which is a bit more complex. 67 for (;;) {
|
| 68 if (isAtLineEnd(Pos) && !SkipBlanks) 69 break;
|
44 if (*Pos == CommentMarker) 45 do { 46 ++Pos;
| 70 if (*Pos == CommentMarker) 71 do { 72 ++Pos;
|
47 } while (*Pos != '\0' && *Pos != '\n'); 48 if (*Pos != '\n')
| 73 } while (*Pos != '\0' && !isAtLineEnd(Pos)); 74 if (!skipIfAtLineEnd(Pos))
|
49 break;
| 75 break;
|
50 ++Pos;
| |
51 ++LineNumber; 52 } 53 } 54 55 if (*Pos == '\0') { 56 // We've hit the end of the buffer, reset ourselves to the end state. 57 Buffer = nullptr; 58 CurrentLine = StringRef(); 59 return; 60 } 61 62 // Measure the line. 63 size_t Length = 0;
| 76 ++LineNumber; 77 } 78 } 79 80 if (*Pos == '\0') { 81 // We've hit the end of the buffer, reset ourselves to the end state. 82 Buffer = nullptr; 83 CurrentLine = StringRef(); 84 return; 85 } 86 87 // Measure the line. 88 size_t Length = 0;
|
64 do {
| 89 while (Pos[Length] != '\0' && !isAtLineEnd(&Pos[Length])) {
|
65 ++Length;
| 90 ++Length;
|
66 } while (Pos[Length] != '\0' && Pos[Length] != '\n');
| 91 }
|
67 68 CurrentLine = StringRef(Pos, Length); 69}
| 92 93 CurrentLine = StringRef(Pos, Length); 94}
|