/Users/buildslave/jenkins/sharedspace/clang-stage2-coverage-R@2/llvm/include/llvm/Support/LineIterator.h
Line | Count | Source (jump to first uncovered line) |
1 | | //===- LineIterator.h - Iterator to read a text buffer's lines --*- C++ -*-===// |
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 | | #ifndef LLVM_SUPPORT_LINEITERATOR_H |
11 | | #define LLVM_SUPPORT_LINEITERATOR_H |
12 | | |
13 | | #include "llvm/ADT/StringRef.h" |
14 | | #include "llvm/Support/DataTypes.h" |
15 | | #include <iterator> |
16 | | |
17 | | namespace llvm { |
18 | | |
19 | | class MemoryBuffer; |
20 | | |
21 | | /// \brief A forward iterator which reads text lines from a buffer. |
22 | | /// |
23 | | /// This class provides a forward iterator interface for reading one line at |
24 | | /// a time from a buffer. When default constructed the iterator will be the |
25 | | /// "end" iterator. |
26 | | /// |
27 | | /// The iterator is aware of what line number it is currently processing. It |
28 | | /// strips blank lines by default, and comment lines given a comment-starting |
29 | | /// character. |
30 | | /// |
31 | | /// Note that this iterator requires the buffer to be nul terminated. |
32 | | class line_iterator |
33 | | : public std::iterator<std::forward_iterator_tag, StringRef> { |
34 | | const MemoryBuffer *Buffer; |
35 | | char CommentMarker; |
36 | | bool SkipBlanks; |
37 | | |
38 | | unsigned LineNumber; |
39 | | StringRef CurrentLine; |
40 | | |
41 | | public: |
42 | | /// \brief Default construct an "end" iterator. |
43 | 0 | line_iterator() : Buffer(nullptr) {} |
44 | | |
45 | | /// \brief Construct a new iterator around some memory buffer. |
46 | | explicit line_iterator(const MemoryBuffer &Buffer, bool SkipBlanks = true, |
47 | | char CommentMarker = '\0'); |
48 | | |
49 | | /// \brief Return true if we've reached EOF or are an "end" iterator. |
50 | 381k | bool is_at_eof() const { return !Buffer; } |
51 | | |
52 | | /// \brief Return true if we're an "end" iterator or have reached EOF. |
53 | 55.9k | bool is_at_end() const { return is_at_eof(); } |
54 | | |
55 | | /// \brief Return the current line number. May return any number at EOF. |
56 | 331k | int64_t line_number() const { return LineNumber; } |
57 | | |
58 | | /// \brief Advance to the next (non-empty, non-comment) line. |
59 | 377k | line_iterator &operator++() { |
60 | 377k | advance(); |
61 | 377k | return *this; |
62 | 377k | } |
63 | 1.53k | line_iterator operator++(int) { |
64 | 1.53k | line_iterator tmp(*this); |
65 | 1.53k | advance(); |
66 | 1.53k | return tmp; |
67 | 1.53k | } |
68 | | |
69 | | /// \brief Get the current line as a \c StringRef. |
70 | 57.0k | StringRef operator*() const { return CurrentLine; } |
71 | 1.98k | const StringRef *operator->() const { return &CurrentLine; } |
72 | | |
73 | 2.65k | friend bool operator==(const line_iterator &LHS, const line_iterator &RHS) { |
74 | 2.65k | return LHS.Buffer == RHS.Buffer && |
75 | 46 | LHS.CurrentLine.begin() == RHS.CurrentLine.begin(); |
76 | 2.65k | } |
77 | | |
78 | 2.64k | friend bool operator!=(const line_iterator &LHS, const line_iterator &RHS) { |
79 | 2.64k | return !(LHS == RHS); |
80 | 2.64k | } |
81 | | |
82 | | private: |
83 | | /// \brief Advance the iterator to the next line. |
84 | | void advance(); |
85 | | }; |
86 | | } |
87 | | |
88 | | #endif |