Strength reduce constant-sized vectors into arrays. No functionality change.
[oota-llvm.git] / lib / Support / LineIterator.cpp
1 //===- LineIterator.cpp - Implementation of line iteration ----------------===//
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/Support/LineIterator.h"
11 #include "llvm/Support/MemoryBuffer.h"
12
13 using namespace llvm;
14
15 line_iterator::line_iterator(const MemoryBuffer &Buffer, bool SkipBlanks,
16                              char CommentMarker)
17     : Buffer(Buffer.getBufferSize() ? &Buffer : nullptr),
18       CommentMarker(CommentMarker), SkipBlanks(SkipBlanks), LineNumber(1),
19       CurrentLine(Buffer.getBufferSize() ? Buffer.getBufferStart() : nullptr,
20                   0) {
21   // Ensure that if we are constructed on a non-empty memory buffer that it is
22   // a null terminated buffer.
23   if (Buffer.getBufferSize()) {
24     assert(Buffer.getBufferEnd()[0] == '\0');
25     // Make sure we don't skip a leading newline if we're keeping blanks
26     if (SkipBlanks || *Buffer.getBufferStart() != '\n')
27       advance();
28   }
29 }
30
31 void line_iterator::advance() {
32   assert(Buffer && "Cannot advance past the end!");
33
34   const char *Pos = CurrentLine.end();
35   assert(Pos == Buffer->getBufferStart() || *Pos == '\n' || *Pos == '\0');
36
37   if (*Pos == '\n') {
38     ++Pos;
39     ++LineNumber;
40   }
41   if (!SkipBlanks && *Pos == '\n') {
42     // Nothing to do for a blank line.
43   } else if (CommentMarker == '\0') {
44     // If we're not stripping comments, this is simpler.
45     size_t Blanks = 0;
46     while (Pos[Blanks] == '\n')
47       ++Blanks;
48     Pos += Blanks;
49     LineNumber += Blanks;
50   } else {
51     // Skip comments and count line numbers, which is a bit more complex.
52     for (;;) {
53       if (*Pos == '\n' && !SkipBlanks)
54         break;
55       if (*Pos == CommentMarker)
56         do {
57           ++Pos;
58         } while (*Pos != '\0' && *Pos != '\n');
59       if (*Pos != '\n')
60         break;
61       ++Pos;
62       ++LineNumber;
63     }
64   }
65
66   if (*Pos == '\0') {
67     // We've hit the end of the buffer, reset ourselves to the end state.
68     Buffer = nullptr;
69     CurrentLine = StringRef();
70     return;
71   }
72
73   // Measure the line.
74   size_t Length = 0;
75   while (Pos[Length] != '\0' && Pos[Length] != '\n') {
76     ++Length;
77   }
78
79   CurrentLine = StringRef(Pos, Length);
80 }