LLVM  9.0.0svn
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"
13 #include "llvm/Support/DataTypes.h"
14 #include <iterator>
15 
16 namespace llvm {
17 
18 class MemoryBuffer;
19 
20 /// A forward iterator which reads text lines from a buffer.
21 ///
22 /// This class provides a forward iterator interface for reading one line at
23 /// a time from a buffer. When default constructed the iterator will be the
24 /// "end" iterator.
25 ///
26 /// The iterator is aware of what line number it is currently processing. It
27 /// strips blank lines by default, and comment lines given a comment-starting
28 /// character.
29 ///
30 /// Note that this iterator requires the buffer to be nul terminated.
32  : public std::iterator<std::forward_iterator_tag, StringRef> {
33  const MemoryBuffer *Buffer;
34  char CommentMarker;
35  bool SkipBlanks;
36 
37  unsigned LineNumber;
38  StringRef CurrentLine;
39 
40 public:
41  /// Default construct an "end" iterator.
42  line_iterator() : Buffer(nullptr) {}
43 
44  /// Construct a new iterator around some memory buffer.
45  explicit line_iterator(const MemoryBuffer &Buffer, bool SkipBlanks = true,
46  char CommentMarker = '\0');
47 
48  /// Return true if we've reached EOF or are an "end" iterator.
49  bool is_at_eof() const { return !Buffer; }
50 
51  /// Return true if we're an "end" iterator or have reached EOF.
52  bool is_at_end() const { return is_at_eof(); }
53 
54  /// Return the current line number. May return any number at EOF.
55  int64_t line_number() const { return LineNumber; }
56 
57  /// Advance to the next (non-empty, non-comment) line.
59  advance();
60  return *this;
61  }
63  line_iterator tmp(*this);
64  advance();
65  return tmp;
66  }
67 
68  /// Get the current line as a \c StringRef.
69  StringRef operator*() const { return CurrentLine; }
70  const StringRef *operator->() const { return &CurrentLine; }
71 
72  friend bool operator==(const line_iterator &LHS, const line_iterator &RHS) {
73  return LHS.Buffer == RHS.Buffer &&
74  LHS.CurrentLine.begin() == RHS.CurrentLine.begin();
75  }
76 
77  friend bool operator!=(const line_iterator &LHS, const line_iterator &RHS) {
78  return !(LHS == RHS);
79  }
80 
81 private:
82  /// Advance the iterator to the next line.
83  void advance();
84 };
85 }
86 
87 #endif
friend bool operator!=(const line_iterator &LHS, const line_iterator &RHS)
Definition: LineIterator.h:77
This class represents lattice values for constants.
Definition: AllocatorList.h:23
A forward iterator which reads text lines from a buffer.
Definition: LineIterator.h:31
bool is_at_end() const
Return true if we&#39;re an "end" iterator or have reached EOF.
Definition: LineIterator.h:52
friend bool operator==(const line_iterator &LHS, const line_iterator &RHS)
Definition: LineIterator.h:72
bool is_at_eof() const
Return true if we&#39;ve reached EOF or are an "end" iterator.
Definition: LineIterator.h:49
line_iterator()
Default construct an "end" iterator.
Definition: LineIterator.h:42
int64_t line_number() const
Return the current line number. May return any number at EOF.
Definition: LineIterator.h:55
line_iterator operator++(int)
Definition: LineIterator.h:62
line_iterator & operator++()
Advance to the next (non-empty, non-comment) line.
Definition: LineIterator.h:58
StringRef operator*() const
Get the current line as a StringRef.
Definition: LineIterator.h:69
This interface provides simple read-only access to a block of memory, and provides simple methods for...
Definition: MemoryBuffer.h:41
iterator begin() const
Definition: StringRef.h:101
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
const StringRef * operator->() const
Definition: LineIterator.h:70