LLVM 22.0.0git
LineIterator.h
Go to the documentation of this file.
1//===- LineIterator.h - Iterator to read a text buffer's lines --*- C++ -*-===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLVM_SUPPORT_LINEITERATOR_H
10#define LLVM_SUPPORT_LINEITERATOR_H
11
12#include "llvm/ADT/StringRef.h"
16#include <iterator>
17#include <optional>
18
19namespace llvm {
20
21class MemoryBuffer;
22
23/// A forward iterator which reads text lines from a buffer.
24///
25/// This class provides a forward iterator interface for reading one line at
26/// a time from a buffer. When default constructed the iterator will be the
27/// "end" iterator.
28///
29/// The iterator is aware of what line number it is currently processing. It
30/// strips blank lines by default, and comment lines given a comment-starting
31/// character.
32///
33/// Note that this iterator requires the buffer to be nul terminated.
35 std::optional<MemoryBufferRef> Buffer;
36 char CommentMarker = '\0';
37 bool SkipBlanks = true;
38
39 unsigned LineNumber = 1;
40 StringRef CurrentLine;
41
42public:
43 using iterator_category = std::forward_iterator_tag;
45 using difference_type = std::ptrdiff_t;
48
49 /// Default construct an "end" iterator.
50 line_iterator() = default;
51
52 /// Construct a new iterator around an unowned memory buffer.
53 LLVM_ABI explicit line_iterator(const MemoryBufferRef &Buffer,
54 bool SkipBlanks = true,
55 char CommentMarker = '\0');
56
57 /// Construct a new iterator around some memory buffer.
58 LLVM_ABI explicit line_iterator(const MemoryBuffer &Buffer,
59 bool SkipBlanks = true,
60 char CommentMarker = '\0');
61
62 /// Return true if we've reached EOF or are an "end" iterator.
63 bool is_at_eof() const { return !Buffer; }
64
65 /// Return true if we're an "end" iterator or have reached EOF.
66 bool is_at_end() const { return is_at_eof(); }
67
68 /// Return the current line number. May return any number at EOF.
69 int64_t line_number() const { return LineNumber; }
70
71 /// Advance to the next (non-empty, non-comment) line.
73 advance();
74 return *this;
75 }
77 line_iterator tmp(*this);
78 advance();
79 return tmp;
80 }
81
82 /// Get the current line as a \c StringRef.
83 StringRef operator*() const { return CurrentLine; }
84 const StringRef *operator->() const { return &CurrentLine; }
85
86 friend bool operator==(const line_iterator &LHS, const line_iterator &RHS) {
87 return LHS.Buffer == RHS.Buffer &&
88 LHS.CurrentLine.begin() == RHS.CurrentLine.begin();
89 }
90
91 friend bool operator!=(const line_iterator &LHS, const line_iterator &RHS) {
92 return !(LHS == RHS);
93 }
94
95private:
96 /// Advance the iterator to the next line.
97 LLVM_ABI void advance();
98};
99}
100
101#endif
#define LLVM_ABI
Definition: Compiler.h:213
Value * RHS
Value * LHS
This interface provides simple read-only access to a block of memory, and provides simple methods for...
Definition: MemoryBuffer.h:52
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:55
A forward iterator which reads text lines from a buffer.
Definition: LineIterator.h:34
std::ptrdiff_t difference_type
Definition: LineIterator.h:45
friend bool operator!=(const line_iterator &LHS, const line_iterator &RHS)
Definition: LineIterator.h:91
int64_t line_number() const
Return the current line number. May return any number at EOF.
Definition: LineIterator.h:69
bool is_at_eof() const
Return true if we've reached EOF or are an "end" iterator.
Definition: LineIterator.h:63
line_iterator & operator++()
Advance to the next (non-empty, non-comment) line.
Definition: LineIterator.h:72
const StringRef * operator->() const
Definition: LineIterator.h:84
bool is_at_end() const
Return true if we're an "end" iterator or have reached EOF.
Definition: LineIterator.h:66
StringRef operator*() const
Get the current line as a StringRef.
Definition: LineIterator.h:83
friend bool operator==(const line_iterator &LHS, const line_iterator &RHS)
Definition: LineIterator.h:86
line_iterator()=default
Default construct an "end" iterator.
std::forward_iterator_tag iterator_category
Definition: LineIterator.h:43
line_iterator operator++(int)
Definition: LineIterator.h:76
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18