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