LLVM  14.0.0git
LineIterator.cpp
Go to the documentation of this file.
1 //===- LineIterator.cpp - Implementation of line iteration ----------------===//
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 
11 
12 using namespace llvm;
13 
14 static bool isAtLineEnd(const char *P) {
15  if (*P == '\n')
16  return true;
17  if (*P == '\r' && *(P + 1) == '\n')
18  return true;
19  return false;
20 }
21 
22 static bool skipIfAtLineEnd(const char *&P) {
23  if (*P == '\n') {
24  ++P;
25  return true;
26  }
27  if (*P == '\r' && *(P + 1) == '\n') {
28  P += 2;
29  return true;
30  }
31  return false;
32 }
33 
34 line_iterator::line_iterator(const MemoryBuffer &Buffer, bool SkipBlanks,
35  char CommentMarker)
36  : line_iterator(Buffer.getMemBufferRef(), SkipBlanks, CommentMarker) {}
37 
38 line_iterator::line_iterator(const MemoryBufferRef &Buffer, bool SkipBlanks,
39  char CommentMarker)
40  : Buffer(Buffer.getBufferSize() ? Optional<MemoryBufferRef>(Buffer) : None),
41  CommentMarker(CommentMarker), SkipBlanks(SkipBlanks), LineNumber(1),
42  CurrentLine(Buffer.getBufferSize() ? Buffer.getBufferStart() : nullptr,
43  0) {
44  // Ensure that if we are constructed on a non-empty memory buffer that it is
45  // a null terminated buffer.
46  if (Buffer.getBufferSize()) {
47  assert(Buffer.getBufferEnd()[0] == '\0');
48  // Make sure we don't skip a leading newline if we're keeping blanks
49  if (SkipBlanks || !isAtLineEnd(Buffer.getBufferStart()))
50  advance();
51  }
52 }
53 
54 void line_iterator::advance() {
55  assert(Buffer && "Cannot advance past the end!");
56 
57  const char *Pos = CurrentLine.end();
58  assert(Pos == Buffer->getBufferStart() || isAtLineEnd(Pos) || *Pos == '\0');
59 
60  if (skipIfAtLineEnd(Pos))
61  ++LineNumber;
62  if (!SkipBlanks && isAtLineEnd(Pos)) {
63  // Nothing to do for a blank line.
64  } else if (CommentMarker == '\0') {
65  // If we're not stripping comments, this is simpler.
66  while (skipIfAtLineEnd(Pos))
67  ++LineNumber;
68  } else {
69  // Skip comments and count line numbers, which is a bit more complex.
70  for (;;) {
71  if (isAtLineEnd(Pos) && !SkipBlanks)
72  break;
73  if (*Pos == CommentMarker)
74  do {
75  ++Pos;
76  } while (*Pos != '\0' && !isAtLineEnd(Pos));
77  if (!skipIfAtLineEnd(Pos))
78  break;
79  ++LineNumber;
80  }
81  }
82 
83  if (*Pos == '\0') {
84  // We've hit the end of the buffer, reset ourselves to the end state.
85  Buffer = None;
86  CurrentLine = StringRef();
87  return;
88  }
89 
90  // Measure the line.
91  size_t Length = 0;
92  while (Pos[Length] != '\0' && !isAtLineEnd(&Pos[Length])) {
93  ++Length;
94  }
95 
96  CurrentLine = StringRef(Pos, Length);
97 }
MemoryBuffer.h
llvm::MemoryBufferRef::getBufferStart
const char * getBufferStart() const
Definition: MemoryBufferRef.h:35
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AllocatorList.h:23
llvm::line_iterator
A forward iterator which reads text lines from a buffer.
Definition: LineIterator.h:33
P
This currently compiles esp xmm0 movsd esp eax eax esp ret We should use not the dag combiner This is because dagcombine2 needs to be able to see through the X86ISD::Wrapper which DAGCombine can t really do The code for turning x load into a single vector load is target independent and should be moved to the dag combiner The code for turning x load into a vector load can only handle a direct load from a global or a direct load from the stack It should be generalized to handle any load from P
Definition: README-SSE.txt:411
llvm::Optional
Definition: APInt.h:33
llvm::MemoryBufferRef::getBufferSize
size_t getBufferSize() const
Definition: MemoryBufferRef.h:37
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
LineIterator.h
llvm::None
const NoneType None
Definition: None.h:23
isAtLineEnd
static bool isAtLineEnd(const char *P)
Definition: LineIterator.cpp:14
llvm::MemoryBufferRef::getBufferEnd
const char * getBufferEnd() const
Definition: MemoryBufferRef.h:36
llvm::StringRef::end
iterator end() const
Definition: StringRef.h:129
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:57
llvm::line_iterator::line_iterator
line_iterator()=default
Default construct an "end" iterator.
skipIfAtLineEnd
static bool skipIfAtLineEnd(const char *&P)
Definition: LineIterator.cpp:22