Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/Support/LineIterator.cpp
Line
Count
Source
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
9
#include "llvm/Support/LineIterator.h"
10
#include "llvm/Support/MemoryBuffer.h"
11
12
using namespace llvm;
13
14
21.7M
static bool isAtLineEnd(const char *P) {
15
21.7M
  if (*P == '\n')
16
677k
    return true;
17
21.0M
  if (*P == '\r' && 
*(P + 1) == '\n'2
)
18
2
    return true;
19
21.0M
  return false;
20
21.0M
}
21
22
1.14M
static bool skipIfAtLineEnd(const char *&P) {
23
1.14M
  if (*P == '\n') {
24
598k
    ++P;
25
598k
    return true;
26
598k
  }
27
549k
  if (*P == '\r' && 
*(P + 1) == '\n'2
) {
28
2
    P += 2;
29
2
    return true;
30
2
  }
31
549k
  return false;
32
549k
}
33
34
line_iterator::line_iterator(const MemoryBuffer &Buffer, bool SkipBlanks,
35
                             char CommentMarker)
36
    : Buffer(Buffer.getBufferSize() ? &Buffer : nullptr),
37
      CommentMarker(CommentMarker), SkipBlanks(SkipBlanks), LineNumber(1),
38
      CurrentLine(Buffer.getBufferSize() ? Buffer.getBufferStart() : nullptr,
39
15.5k
                  0) {
40
15.5k
  // Ensure that if we are constructed on a non-empty memory buffer that it is
41
15.5k
  // a null terminated buffer.
42
15.5k
  if (Buffer.getBufferSize()) {
43
15.5k
    assert(Buffer.getBufferEnd()[0] == '\0');
44
15.5k
    // Make sure we don't skip a leading newline if we're keeping blanks
45
15.5k
    if (SkipBlanks || 
!isAtLineEnd(Buffer.getBufferStart())15.0k
)
46
15.5k
      advance();
47
15.5k
  }
48
15.5k
}
49
50
612k
void line_iterator::advance() {
51
612k
  assert(Buffer && "Cannot advance past the end!");
52
612k
53
612k
  const char *Pos = CurrentLine.end();
54
612k
  assert(Pos == Buffer->getBufferStart() || isAtLineEnd(Pos) || *Pos == '\0');
55
612k
56
612k
  if (skipIfAtLineEnd(Pos))
57
596k
    ++LineNumber;
58
612k
  if (!SkipBlanks && 
isAtLineEnd(Pos)607k
) {
59
78.4k
    // Nothing to do for a blank line.
60
533k
  } else if (CommentMarker == '\0') {
61
529k
    // If we're not stripping comments, this is simpler.
62
529k
    while (skipIfAtLineEnd(Pos))
63
12
      ++LineNumber;
64
529k
  } else {
65
4.23k
    // Skip comments and count line numbers, which is a bit more complex.
66
6.07k
    for (;;) {
67
6.07k
      if (isAtLineEnd(Pos) && 
!SkipBlanks407
)
68
1
        break;
69
6.07k
      if (*Pos == CommentMarker)
70
37.7k
        
do 1.42k
{
71
37.7k
          ++Pos;
72
37.7k
        } while (*Pos != '\0' && 
!isAtLineEnd(Pos)37.7k
);
73
6.07k
      if (!skipIfAtLineEnd(Pos))
74
4.23k
        break;
75
1.83k
      ++LineNumber;
76
1.83k
    }
77
4.23k
  }
78
612k
79
612k
  if (*Pos == '\0') {
80
14.5k
    // We've hit the end of the buffer, reset ourselves to the end state.
81
14.5k
    Buffer = nullptr;
82
14.5k
    CurrentLine = StringRef();
83
14.5k
    return;
84
14.5k
  }
85
597k
86
597k
  // Measure the line.
87
597k
  size_t Length = 0;
88
21.0M
  while (Pos[Length] != '\0' && 
!isAtLineEnd(&Pos[Length])21.0M
) {
89
20.4M
    ++Length;
90
20.4M
  }
91
597k
92
597k
  CurrentLine = StringRef(Pos, Length);
93
597k
}