LLVM  13.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/Optional.h"
13 #include "llvm/ADT/StringRef.h"
14 #include "llvm/Support/DataTypes.h"
16 #include <iterator>
17 
18 namespace llvm {
19 
20 class MemoryBuffer;
21 
22 /// A forward iterator which reads text lines from a buffer.
23 ///
24 /// This class provides a forward iterator interface for reading one line at
25 /// a time from a buffer. When default constructed the iterator will be the
26 /// "end" iterator.
27 ///
28 /// The iterator is aware of what line number it is currently processing. It
29 /// strips blank lines by default, and comment lines given a comment-starting
30 /// character.
31 ///
32 /// Note that this iterator requires the buffer to be nul terminated.
35  char CommentMarker = '\0';
36  bool SkipBlanks = true;
37 
38  unsigned LineNumber = 1;
39  StringRef CurrentLine;
40 
41 public:
42  using iterator_category = std::forward_iterator_tag;
44  using difference_type = std::ptrdiff_t;
45  using pointer = value_type *;
46  using reference = value_type &;
47 
48  /// Default construct an "end" iterator.
49  line_iterator() = default;
50 
51  /// Construct a new iterator around an unowned memory buffer.
52  explicit line_iterator(const MemoryBufferRef &Buffer, bool SkipBlanks = true,
53  char CommentMarker = '\0');
54 
55  /// Construct a new iterator around some memory buffer.
56  explicit line_iterator(const MemoryBuffer &Buffer, bool SkipBlanks = true,
57  char CommentMarker = '\0');
58 
59  /// Return true if we've reached EOF or are an "end" iterator.
60  bool is_at_eof() const { return !Buffer; }
61 
62  /// Return true if we're an "end" iterator or have reached EOF.
63  bool is_at_end() const { return is_at_eof(); }
64 
65  /// Return the current line number. May return any number at EOF.
66  int64_t line_number() const { return LineNumber; }
67 
68  /// Advance to the next (non-empty, non-comment) line.
70  advance();
71  return *this;
72  }
74  line_iterator tmp(*this);
75  advance();
76  return tmp;
77  }
78 
79  /// Get the current line as a \c StringRef.
80  StringRef operator*() const { return CurrentLine; }
81  const StringRef *operator->() const { return &CurrentLine; }
82 
83  friend bool operator==(const line_iterator &LHS, const line_iterator &RHS) {
84  return LHS.Buffer == RHS.Buffer &&
85  LHS.CurrentLine.begin() == RHS.CurrentLine.begin();
86  }
87 
88  friend bool operator!=(const line_iterator &LHS, const line_iterator &RHS) {
89  return !(LHS == RHS);
90  }
91 
92 private:
93  /// Advance the iterator to the next line.
94  void advance();
95 };
96 }
97 
98 #endif
llvm
Definition: AllocatorList.h:23
Optional.h
llvm::line_iterator
A forward iterator which reads text lines from a buffer.
Definition: LineIterator.h:33
llvm::line_iterator::difference_type
std::ptrdiff_t difference_type
Definition: LineIterator.h:44
StringRef.h
llvm::line_iterator::operator*
StringRef operator*() const
Get the current line as a StringRef.
Definition: LineIterator.h:80
MemoryBufferRef.h
llvm::Optional
Definition: APInt.h:33
tmp
alloca< 16 x float >, align 16 %tmp2=alloca< 16 x float >, align 16 store< 16 x float > %A,< 16 x float > *%tmp %s=bitcast< 16 x float > *%tmp to i8 *%s2=bitcast< 16 x float > *%tmp2 to i8 *call void @llvm.memcpy.i64(i8 *%s, i8 *%s2, i64 64, i32 16) %R=load< 16 x float > *%tmp2 ret< 16 x float > %R } declare void @llvm.memcpy.i64(i8 *nocapture, i8 *nocapture, i64, i32) nounwind which compiles to:_foo:subl $140, %esp movaps %xmm3, 112(%esp) movaps %xmm2, 96(%esp) movaps %xmm1, 80(%esp) movaps %xmm0, 64(%esp) movl 60(%esp), %eax movl %eax, 124(%esp) movl 56(%esp), %eax movl %eax, 120(%esp) movl 52(%esp), %eax< many many more 32-bit copies > movaps(%esp), %xmm0 movaps 16(%esp), %xmm1 movaps 32(%esp), %xmm2 movaps 48(%esp), %xmm3 addl $140, %esp ret On Nehalem, it may even be cheaper to just use movups when unaligned than to fall back to lower-granularity chunks. Implement processor-specific optimizations for parity with GCC on these processors. GCC does two optimizations:1. ix86_pad_returns inserts a noop before ret instructions if immediately preceded by a conditional branch or is the target of a jump. 2. ix86_avoid_jump_misspredicts inserts noops in cases where a 16-byte block of code contains more than 3 branches. The first one is done for all AMDs, Core2, and "Generic" The second one is done for:Atom, Pentium Pro, all AMDs, Pentium 4, Nocona, Core 2, and "Generic" Testcase:int x(int a) { return(a &0xf0)> >4 tmp
Definition: README.txt:1347
llvm::MemoryBufferRef
Definition: MemoryBufferRef.h:22
llvm::MemoryBuffer
This interface provides simple read-only access to a block of memory, and provides simple methods for...
Definition: MemoryBuffer.h:50
llvm::line_iterator::iterator_category
std::forward_iterator_tag iterator_category
Definition: LineIterator.h:42
llvm::line_iterator::operator->
const StringRef * operator->() const
Definition: LineIterator.h:81
llvm::line_iterator::is_at_end
bool is_at_end() const
Return true if we're an "end" iterator or have reached EOF.
Definition: LineIterator.h:63
llvm::line_iterator::operator!=
friend bool operator!=(const line_iterator &LHS, const line_iterator &RHS)
Definition: LineIterator.h:88
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm::line_iterator::operator==
friend bool operator==(const line_iterator &LHS, const line_iterator &RHS)
Definition: LineIterator.h:83
llvm::line_iterator::line_iterator
line_iterator()=default
Default construct an "end" iterator.
llvm::line_iterator::operator++
line_iterator & operator++()
Advance to the next (non-empty, non-comment) line.
Definition: LineIterator.h:69
llvm::line_iterator::is_at_eof
bool is_at_eof() const
Return true if we've reached EOF or are an "end" iterator.
Definition: LineIterator.h:60
llvm::line_iterator::line_number
int64_t line_number() const
Return the current line number. May return any number at EOF.
Definition: LineIterator.h:66
DataTypes.h
llvm::line_iterator::operator++
line_iterator operator++(int)
Definition: LineIterator.h:73
llvm::StringRef::begin
iterator begin() const
Definition: StringRef.h:128