Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/clang/include/clang/Serialization/ContinuousRangeMap.h
Line
Count
Source (jump to first uncovered line)
1
//===- ContinuousRangeMap.h - Map with int range as key ---------*- 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
//  This file defines the ContinuousRangeMap class, which is a highly
10
//  specialized container used by serialization.
11
//
12
//===----------------------------------------------------------------------===//
13
14
#ifndef LLVM_CLANG_SERIALIZATION_CONTINUOUSRANGEMAP_H
15
#define LLVM_CLANG_SERIALIZATION_CONTINUOUSRANGEMAP_H
16
17
#include "clang/Basic/LLVM.h"
18
#include "llvm/ADT/STLExtras.h"
19
#include "llvm/ADT/SmallVector.h"
20
#include <algorithm>
21
#include <cassert>
22
#include <utility>
23
24
namespace clang {
25
26
/// A map from continuous integer ranges to some value, with a very
27
/// specialized interface.
28
///
29
/// CRM maps from integer ranges to values. The ranges are continuous, i.e.
30
/// where one ends, the next one begins. So if the map contains the stops I0-3,
31
/// the first range is from I0 to I1, the second from I1 to I2, the third from
32
/// I2 to I3 and the last from I3 to infinity.
33
///
34
/// Ranges must be inserted in order. Inserting a new stop I4 into the map will
35
/// shrink the fourth range to I3 to I4 and add the new range I4 to inf.
36
template <typename Int, typename V, unsigned InitialCapacity>
37
class ContinuousRangeMap {
38
public:
39
  using value_type = std::pair<Int, V>;
40
  using reference = value_type &;
41
  using const_reference = const value_type &;
42
  using pointer = value_type *;
43
  using const_pointer = const value_type *;
44
45
private:
46
  using Representation = SmallVector<value_type, InitialCapacity>;
47
48
  Representation Rep;
49
50
  struct Compare {
51
    bool operator ()(const_reference L, Int R) const {
52
      return L.first < R;
53
    }
54
5.42M
    bool operator ()(Int L, const_reference R) const {
55
5.42M
      return L < R.first;
56
5.42M
    }
clang::ContinuousRangeMap<unsigned int, int, 2u>::Compare::operator()(unsigned int, std::__1::pair<unsigned int, int> const&) const
Line
Count
Source
54
4.69M
    bool operator ()(Int L, const_reference R) const {
55
4.69M
      return L < R.first;
56
4.69M
    }
clang::ContinuousRangeMap<unsigned int, clang::serialization::ModuleFile*, 64u>::Compare::operator()(unsigned int, std::__1::pair<unsigned int, clang::serialization::ModuleFile*> const&) const
Line
Count
Source
54
21.0k
    bool operator ()(Int L, const_reference R) const {
55
21.0k
      return L < R.first;
56
21.0k
    }
clang::ContinuousRangeMap<unsigned int, clang::serialization::ModuleFile*, 4u>::Compare::operator()(unsigned int, std::__1::pair<unsigned int, clang::serialization::ModuleFile*> const&) const
Line
Count
Source
54
690k
    bool operator ()(Int L, const_reference R) const {
55
690k
      return L < R.first;
56
690k
    }
clang::ContinuousRangeMap<unsigned long long, clang::serialization::ModuleFile*, 4u>::Compare::operator()(unsigned long long, std::__1::pair<unsigned long long, clang::serialization::ModuleFile*> const&) const
Line
Count
Source
54
11.1k
    bool operator ()(Int L, const_reference R) const {
55
11.1k
      return L < R.first;
56
11.1k
    }
57
    bool operator ()(Int L, Int R) const {
58
      return L < R;
59
    }
60
283k
    bool operator ()(const_reference L, const_reference R) const {
61
283k
      return L.first < R.first;
62
283k
    }
63
  };
64
65
public:
66
45.2k
  void insert(const value_type &Val) {
67
45.2k
    if (!Rep.empty() && 
Rep.back() == Val14.8k
)
68
0
      return;
69
45.2k
70
45.2k
    assert((Rep.empty() || Rep.back().first < Val.first) &&
71
45.2k
           "Must insert keys in order.");
72
45.2k
    Rep.push_back(Val);
73
45.2k
  }
clang::ContinuousRangeMap<unsigned int, clang::serialization::ModuleFile*, 4u>::insert(std::__1::pair<unsigned int, clang::serialization::ModuleFile*> const&)
Line
Count
Source
66
25.1k
  void insert(const value_type &Val) {
67
25.1k
    if (!Rep.empty() && 
Rep.back() == Val7.64k
)
68
0
      return;
69
25.1k
70
25.1k
    assert((Rep.empty() || Rep.back().first < Val.first) &&
71
25.1k
           "Must insert keys in order.");
72
25.1k
    Rep.push_back(Val);
73
25.1k
  }
clang::ContinuousRangeMap<unsigned int, clang::serialization::ModuleFile*, 64u>::insert(std::__1::pair<unsigned int, clang::serialization::ModuleFile*> const&)
Line
Count
Source
66
12.2k
  void insert(const value_type &Val) {
67
12.2k
    if (!Rep.empty() && 
Rep.back() == Val4.20k
)
68
0
      return;
69
12.2k
70
12.2k
    assert((Rep.empty() || Rep.back().first < Val.first) &&
71
12.2k
           "Must insert keys in order.");
72
12.2k
    Rep.push_back(Val);
73
12.2k
  }
clang::ContinuousRangeMap<unsigned int, int, 2u>::insert(std::__1::pair<unsigned int, int> const&)
Line
Count
Source
66
1.73k
  void insert(const value_type &Val) {
67
1.73k
    if (!Rep.empty() && 
Rep.back() == Val869
)
68
0
      return;
69
1.73k
70
1.73k
    assert((Rep.empty() || Rep.back().first < Val.first) &&
71
1.73k
           "Must insert keys in order.");
72
1.73k
    Rep.push_back(Val);
73
1.73k
  }
clang::ContinuousRangeMap<unsigned long long, clang::serialization::ModuleFile*, 4u>::insert(std::__1::pair<unsigned long long, clang::serialization::ModuleFile*> const&)
Line
Count
Source
66
6.13k
  void insert(const value_type &Val) {
67
6.13k
    if (!Rep.empty() && 
Rep.back() == Val2.09k
)
68
0
      return;
69
6.13k
70
6.13k
    assert((Rep.empty() || Rep.back().first < Val.first) &&
71
6.13k
           "Must insert keys in order.");
72
6.13k
    Rep.push_back(Val);
73
6.13k
  }
74
75
37.3k
  void insertOrReplace(const value_type &Val) {
76
37.3k
    iterator I = llvm::lower_bound(Rep, Val, Compare());
77
37.3k
    if (I != Rep.end() && 
I->first == Val.first1.73k
) {
78
1.73k
      I->second = Val.second;
79
1.73k
      return;
80
1.73k
    }
81
35.5k
82
35.5k
    Rep.insert(I, Val);
83
35.5k
  }
84
85
  using iterator = typename Representation::iterator;
86
  using const_iterator = typename Representation::const_iterator;
87
88
  iterator begin() { return Rep.begin(); }
89
3.26k
  iterator end() { return Rep.end(); }
clang::ContinuousRangeMap<unsigned int, int, 2u>::end()
Line
Count
Source
89
1.79k
  iterator end() { return Rep.end(); }
clang::ContinuousRangeMap<unsigned int, clang::serialization::ModuleFile*, 4u>::end()
Line
Count
Source
89
1.46k
  iterator end() { return Rep.end(); }
90
103
  const_iterator begin() const { return Rep.begin(); }
clang::ContinuousRangeMap<unsigned long long, clang::serialization::ModuleFile*, 4u>::begin() const
Line
Count
Source
90
6
  const_iterator begin() const { return Rep.begin(); }
clang::ContinuousRangeMap<unsigned int, clang::serialization::ModuleFile*, 64u>::begin() const
Line
Count
Source
90
6
  const_iterator begin() const { return Rep.begin(); }
clang::ContinuousRangeMap<unsigned int, clang::serialization::ModuleFile*, 4u>::begin() const
Line
Count
Source
90
35
  const_iterator begin() const { return Rep.begin(); }
clang::ContinuousRangeMap<unsigned int, int, 2u>::begin() const
Line
Count
Source
90
56
  const_iterator begin() const { return Rep.begin(); }
91
562
  const_iterator end() const { return Rep.end(); }
clang::ContinuousRangeMap<unsigned int, clang::serialization::ModuleFile*, 64u>::end() const
Line
Count
Source
91
465
  const_iterator end() const { return Rep.end(); }
clang::ContinuousRangeMap<unsigned long long, clang::serialization::ModuleFile*, 4u>::end() const
Line
Count
Source
91
6
  const_iterator end() const { return Rep.end(); }
clang::ContinuousRangeMap<unsigned int, clang::serialization::ModuleFile*, 4u>::end() const
Line
Count
Source
91
35
  const_iterator end() const { return Rep.end(); }
clang::ContinuousRangeMap<unsigned int, int, 2u>::end() const
Line
Count
Source
91
56
  const_iterator end() const { return Rep.end(); }
92
93
4.70M
  iterator find(Int K) {
94
4.70M
    iterator I = llvm::upper_bound(Rep, K, Compare());
95
4.70M
    // I points to the first entry with a key > K, which is the range that
96
4.70M
    // follows the one containing K.
97
4.70M
    if (I == Rep.begin())
98
869
      return Rep.end();
99
4.70M
    --I;
100
4.70M
    return I;
101
4.70M
  }
clang::ContinuousRangeMap<unsigned int, int, 2u>::find(unsigned int)
Line
Count
Source
93
4.08M
  iterator find(Int K) {
94
4.08M
    iterator I = llvm::upper_bound(Rep, K, Compare());
95
4.08M
    // I points to the first entry with a key > K, which is the range that
96
4.08M
    // follows the one containing K.
97
4.08M
    if (I == Rep.begin())
98
869
      return Rep.end();
99
4.07M
    --I;
100
4.07M
    return I;
101
4.07M
  }
clang::ContinuousRangeMap<unsigned int, clang::serialization::ModuleFile*, 64u>::find(unsigned int)
Line
Count
Source
93
14.1k
  iterator find(Int K) {
94
14.1k
    iterator I = llvm::upper_bound(Rep, K, Compare());
95
14.1k
    // I points to the first entry with a key > K, which is the range that
96
14.1k
    // follows the one containing K.
97
14.1k
    if (I == Rep.begin())
98
0
      return Rep.end();
99
14.1k
    --I;
100
14.1k
    return I;
101
14.1k
  }
clang::ContinuousRangeMap<unsigned int, clang::serialization::ModuleFile*, 4u>::find(unsigned int)
Line
Count
Source
93
604k
  iterator find(Int K) {
94
604k
    iterator I = llvm::upper_bound(Rep, K, Compare());
95
604k
    // I points to the first entry with a key > K, which is the range that
96
604k
    // follows the one containing K.
97
604k
    if (I == Rep.begin())
98
0
      return Rep.end();
99
604k
    --I;
100
604k
    return I;
101
604k
  }
clang::ContinuousRangeMap<unsigned long long, clang::serialization::ModuleFile*, 4u>::find(unsigned long long)
Line
Count
Source
93
10.3k
  iterator find(Int K) {
94
10.3k
    iterator I = llvm::upper_bound(Rep, K, Compare());
95
10.3k
    // I points to the first entry with a key > K, which is the range that
96
10.3k
    // follows the one containing K.
97
10.3k
    if (I == Rep.begin())
98
0
      return Rep.end();
99
10.3k
    --I;
100
10.3k
    return I;
101
10.3k
  }
102
1.67k
  const_iterator find(Int K) const {
103
1.67k
    return const_cast<ContinuousRangeMap*>(this)->find(K);
104
1.67k
  }
105
106
  reference back() { return Rep.back(); }
107
  const_reference back() const { return Rep.back(); }
108
109
  /// An object that helps properly build a continuous range map
110
  /// from a set of values.
111
  class Builder {
112
    ContinuousRangeMap &Self;
113
114
  public:
115
14.3k
    explicit Builder(ContinuousRangeMap &Self) : Self(Self) {}
116
    Builder(const Builder&) = delete;
117
    Builder &operator=(const Builder&) = delete;
118
119
14.3k
    ~Builder() {
120
14.3k
      llvm::sort(Self.Rep, Compare());
121
14.3k
      std::unique(Self.Rep.begin(), Self.Rep.end(),
122
55.5k
                  [](const_reference A, const_reference B) {
123
55.5k
        // FIXME: we should not allow any duplicate keys, but there are a lot of
124
55.5k
        // duplicate 0 -> 0 mappings to remove first.
125
55.5k
        assert((A == B || A.first != B.first) &&
126
55.5k
               "ContinuousRangeMap::Builder given non-unique keys");
127
55.5k
        return A == B;
128
55.5k
      });
129
14.3k
    }
130
131
56.2k
    void insert(const value_type &Val) {
132
56.2k
      Self.Rep.push_back(Val);
133
56.2k
    }
134
  };
135
136
  friend class Builder;
137
};
138
139
} // namespace clang
140
141
#endif // LLVM_CLANG_SERIALIZATION_CONTINUOUSRANGEMAP_H