Chandler Carruth | f8c5281 | 2013-12-27 04:28:57 +0000 | [diff] [blame] | 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 | |
| 13 | using namespace llvm; |
| 14 | |
Rafael Espindola | 42bce8f | 2014-11-03 14:09:47 +0000 | [diff] [blame] | 15 | static 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 | |
| 23 | static 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 Bogner | 69fe4e9 | 2014-09-17 15:43:01 +0000 | [diff] [blame] | 35 | line_iterator::line_iterator(const MemoryBuffer &Buffer, bool SkipBlanks, |
| 36 | char CommentMarker) |
Craig Topper | c10719f | 2014-04-07 04:17:22 +0000 | [diff] [blame] | 37 | : Buffer(Buffer.getBufferSize() ? &Buffer : nullptr), |
Justin Bogner | 69fe4e9 | 2014-09-17 15:43:01 +0000 | [diff] [blame] | 38 | CommentMarker(CommentMarker), SkipBlanks(SkipBlanks), LineNumber(1), |
Craig Topper | c10719f | 2014-04-07 04:17:22 +0000 | [diff] [blame] | 39 | CurrentLine(Buffer.getBufferSize() ? Buffer.getBufferStart() : nullptr, |
| 40 | 0) { |
Chandler Carruth | f8c5281 | 2013-12-27 04:28:57 +0000 | [diff] [blame] | 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'); |
Justin Bogner | 69fe4e9 | 2014-09-17 15:43:01 +0000 | [diff] [blame] | 45 | // Make sure we don't skip a leading newline if we're keeping blanks |
Rafael Espindola | 42bce8f | 2014-11-03 14:09:47 +0000 | [diff] [blame] | 46 | if (SkipBlanks || !isAtLineEnd(Buffer.getBufferStart())) |
Justin Bogner | 69fe4e9 | 2014-09-17 15:43:01 +0000 | [diff] [blame] | 47 | advance(); |
Chandler Carruth | f8c5281 | 2013-12-27 04:28:57 +0000 | [diff] [blame] | 48 | } |
| 49 | } |
| 50 | |
| 51 | void line_iterator::advance() { |
| 52 | assert(Buffer && "Cannot advance past the end!"); |
| 53 | |
| 54 | const char *Pos = CurrentLine.end(); |
Rafael Espindola | 42bce8f | 2014-11-03 14:09:47 +0000 | [diff] [blame] | 55 | assert(Pos == Buffer->getBufferStart() || isAtLineEnd(Pos) || *Pos == '\0'); |
Chandler Carruth | f8c5281 | 2013-12-27 04:28:57 +0000 | [diff] [blame] | 56 | |
Rafael Espindola | 42bce8f | 2014-11-03 14:09:47 +0000 | [diff] [blame] | 57 | if (skipIfAtLineEnd(Pos)) |
Justin Bogner | 69fe4e9 | 2014-09-17 15:43:01 +0000 | [diff] [blame] | 58 | ++LineNumber; |
Rafael Espindola | 42bce8f | 2014-11-03 14:09:47 +0000 | [diff] [blame] | 59 | if (!SkipBlanks && isAtLineEnd(Pos)) { |
Justin Bogner | 69fe4e9 | 2014-09-17 15:43:01 +0000 | [diff] [blame] | 60 | // Nothing to do for a blank line. |
| 61 | } else if (CommentMarker == '\0') { |
Chandler Carruth | f8c5281 | 2013-12-27 04:28:57 +0000 | [diff] [blame] | 62 | // If we're not stripping comments, this is simpler. |
Rafael Espindola | 42bce8f | 2014-11-03 14:09:47 +0000 | [diff] [blame] | 63 | while (skipIfAtLineEnd(Pos)) |
| 64 | ++LineNumber; |
Chandler Carruth | f8c5281 | 2013-12-27 04:28:57 +0000 | [diff] [blame] | 65 | } else { |
| 66 | // Skip comments and count line numbers, which is a bit more complex. |
| 67 | for (;;) { |
Rafael Espindola | 42bce8f | 2014-11-03 14:09:47 +0000 | [diff] [blame] | 68 | if (isAtLineEnd(Pos) && !SkipBlanks) |
Justin Bogner | 69fe4e9 | 2014-09-17 15:43:01 +0000 | [diff] [blame] | 69 | break; |
Chandler Carruth | f8c5281 | 2013-12-27 04:28:57 +0000 | [diff] [blame] | 70 | if (*Pos == CommentMarker) |
| 71 | do { |
| 72 | ++Pos; |
Rafael Espindola | 42bce8f | 2014-11-03 14:09:47 +0000 | [diff] [blame] | 73 | } while (*Pos != '\0' && !isAtLineEnd(Pos)); |
| 74 | if (!skipIfAtLineEnd(Pos)) |
Chandler Carruth | f8c5281 | 2013-12-27 04:28:57 +0000 | [diff] [blame] | 75 | break; |
Chandler Carruth | f8c5281 | 2013-12-27 04:28:57 +0000 | [diff] [blame] | 76 | ++LineNumber; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | if (*Pos == '\0') { |
| 81 | // We've hit the end of the buffer, reset ourselves to the end state. |
Craig Topper | c10719f | 2014-04-07 04:17:22 +0000 | [diff] [blame] | 82 | Buffer = nullptr; |
Chandler Carruth | f8c5281 | 2013-12-27 04:28:57 +0000 | [diff] [blame] | 83 | CurrentLine = StringRef(); |
| 84 | return; |
| 85 | } |
| 86 | |
| 87 | // Measure the line. |
Chandler Carruth | 87c3a0c | 2013-12-27 04:44:35 +0000 | [diff] [blame] | 88 | size_t Length = 0; |
Rafael Espindola | 42bce8f | 2014-11-03 14:09:47 +0000 | [diff] [blame] | 89 | while (Pos[Length] != '\0' && !isAtLineEnd(&Pos[Length])) { |
Chandler Carruth | f8c5281 | 2013-12-27 04:28:57 +0000 | [diff] [blame] | 90 | ++Length; |
Justin Bogner | 69fe4e9 | 2014-09-17 15:43:01 +0000 | [diff] [blame] | 91 | } |
Chandler Carruth | f8c5281 | 2013-12-27 04:28:57 +0000 | [diff] [blame] | 92 | |
| 93 | CurrentLine = StringRef(Pos, Length); |
| 94 | } |