Introduce llvm::sys::path::home_directory.
[oota-llvm.git] / include / llvm / Support / LineIterator.h
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 #include "llvm/ADT/StringRef.h"
11 #include <iterator>
12
13 namespace llvm {
14
15 class MemoryBuffer;
16
17 /// \brief A forward iterator which reads non-blank text lines from a buffer.
18 ///
19 /// This class provides a forward iterator interface for reading one line at
20 /// a time from a buffer. When default constructed the iterator will be the
21 /// "end" iterator.
22 ///
23 /// The iterator also is aware of what line number it is currently processing
24 /// and can strip comment lines given the comment-starting character.
25 ///
26 /// Note that this iterator requires the buffer to be nul terminated.
27 class line_iterator
28     : public std::iterator<std::forward_iterator_tag, StringRef, ptrdiff_t> {
29   const MemoryBuffer *Buffer;
30   char CommentMarker;
31
32   unsigned LineNumber;
33   StringRef CurrentLine;
34
35 public:
36   /// \brief Default construct an "end" iterator.
37   line_iterator() : Buffer(0) {}
38
39   /// \brief Construct a new iterator around some memory buffer.
40   explicit line_iterator(const MemoryBuffer &Buffer, char CommentMarker = '\0');
41
42   /// \brief Return true if we've reached EOF or are an "end" iterator.
43   bool is_at_eof() const { return !Buffer; }
44
45   /// \brief Return true if we're an "end" iterator or have reached EOF.
46   bool is_at_end() const { return is_at_eof(); }
47
48   /// \brief Return the current line number. May return any number at EOF.
49   int64_t line_number() const { return LineNumber; }
50
51   /// \brief Advance to the next (non-empty, non-comment) line.
52   line_iterator &operator++() {
53     advance();
54     return *this;
55   }
56
57   /// \brief Get the current line as a \c StringRef.
58   StringRef operator*() const { return CurrentLine; }
59
60   friend bool operator==(const line_iterator &LHS, const line_iterator &RHS) {
61     return LHS.Buffer == RHS.Buffer &&
62            LHS.CurrentLine.begin() == RHS.CurrentLine.begin();
63   }
64
65   friend bool operator!=(const line_iterator &LHS, const line_iterator &RHS) {
66     return !(LHS == RHS);
67   }
68
69 private:
70   /// \brief Advance the iterator to the next line.
71   void advance();
72 };
73 }