Coverage Report

Created: 2021-01-26 06:56

/Users/buildslave/jenkins/workspace/coverage/llvm-project/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
684M
    bool operator ()(Int L, const_reference R) const {
55
684M
      return L < R.first;
56
684M
    }
clang::ContinuousRangeMap<unsigned int, int, 2u>::Compare::operator()(unsigned int, std::__1::pair<unsigned int, int> const&) const
Line
Count
Source
54
621M
    bool operator ()(Int L, const_reference R) const {
55
621M
      return L < R.first;
56
621M
    }
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
18.0M
    bool operator ()(Int L, const_reference R) const {
55
18.0M
      return L < R.first;
56
18.0M
    }
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
43.9M
    bool operator ()(Int L, const_reference R) const {
55
43.9M
      return L < R.first;
56
43.9M
    }
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
504k
    bool operator ()(Int L, const_reference R) const {
55
504k
      return L < R.first;
56
504k
    }
57
    bool operator ()(Int L, Int R) const {
58
      return L < R;
59
    }
60
1.23M
    bool operator ()(const_reference L, const_reference R) const {
61
1.23M
      return L.first < R.first;
62
1.23M
    }
63
  };
64
65
public:
66
150k
  void insert(const value_type &Val) {
67
150k
    if (!Rep.empty() && 
Rep.back() == Val93.6k
)
68
0
      return;
69
70
150k
    assert((Rep.empty() || Rep.back().first < Val.first) &&
71
150k
           "Must insert keys in order.");
72
150k
    Rep.push_back(Val);
73
150k
  }
clang::ContinuousRangeMap<unsigned int, clang::serialization::ModuleFile*, 4u>::insert(std::__1::pair<unsigned int, clang::serialization::ModuleFile*> const&)
Line
Count
Source
66
85.9k
  void insert(const value_type &Val) {
67
85.9k
    if (!Rep.empty() && 
Rep.back() == Val54.6k
)
68
0
      return;
69
70
85.9k
    assert((Rep.empty() || Rep.back().first < Val.first) &&
71
85.9k
           "Must insert keys in order.");
72
85.9k
    Rep.push_back(Val);
73
85.9k
  }
clang::ContinuousRangeMap<unsigned int, clang::serialization::ModuleFile*, 64u>::insert(std::__1::pair<unsigned int, clang::serialization::ModuleFile*> const&)
Line
Count
Source
66
36.8k
  void insert(const value_type &Val) {
67
36.8k
    if (!Rep.empty() && 
Rep.back() == Val22.9k
)
68
0
      return;
69
70
36.8k
    assert((Rep.empty() || Rep.back().first < Val.first) &&
71
36.8k
           "Must insert keys in order.");
72
36.8k
    Rep.push_back(Val);
73
36.8k
  }
clang::ContinuousRangeMap<unsigned int, int, 2u>::insert(std::__1::pair<unsigned int, int> const&)
Line
Count
Source
66
9.06k
  void insert(const value_type &Val) {
67
9.06k
    if (!Rep.empty() && 
Rep.back() == Val4.53k
)
68
0
      return;
69
70
9.06k
    assert((Rep.empty() || Rep.back().first < Val.first) &&
71
9.06k
           "Must insert keys in order.");
72
9.06k
    Rep.push_back(Val);
73
9.06k
  }
clang::ContinuousRangeMap<unsigned long long, clang::serialization::ModuleFile*, 4u>::insert(std::__1::pair<unsigned long long, clang::serialization::ModuleFile*> const&)
Line
Count
Source
66
18.4k
  void insert(const value_type &Val) {
67
18.4k
    if (!Rep.empty() && 
Rep.back() == Val11.4k
)
68
0
      return;
69
70
18.4k
    assert((Rep.empty() || Rep.back().first < Val.first) &&
71
18.4k
           "Must insert keys in order.");
72
18.4k
    Rep.push_back(Val);
73
18.4k
  }
74
75
122k
  void insertOrReplace(const value_type &Val) {
76
122k
    iterator I = llvm::lower_bound(Rep, Val, Compare());
77
122k
    if (I != Rep.end() && 
I->first == Val.first9.06k
) {
78
9.06k
      I->second = Val.second;
79
9.06k
      return;
80
9.06k
    }
81
82
113k
    Rep.insert(I, Val);
83
113k
  }
84
85
  using iterator = typename Representation::iterator;
86
  using const_iterator = typename Representation::const_iterator;
87
88
  iterator begin() { return Rep.begin(); }
89
223M
  iterator end() { return Rep.end(); }
clang::ContinuousRangeMap<unsigned int, int, 2u>::end()
Line
Count
Source
89
203M
  iterator end() { return Rep.end(); }
clang::ContinuousRangeMap<unsigned int, clang::serialization::ModuleFile*, 4u>::end()
Line
Count
Source
89
19.4M
  iterator end() { return Rep.end(); }
clang::ContinuousRangeMap<unsigned long long, clang::serialization::ModuleFile*, 4u>::end()
Line
Count
Source
89
260k
  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
2.23k
  const_iterator end() const { return Rep.end(); }
clang::ContinuousRangeMap<unsigned int, clang::serialization::ModuleFile*, 64u>::end() const
Line
Count
Source
91
2.13k
  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
320M
  iterator find(Int K) {
94
320M
    iterator I = llvm::upper_bound(Rep, K, Compare());
95
    // I points to the first entry with a key > K, which is the range that
96
    // follows the one containing K.
97
320M
    if (I == Rep.begin())
98
4.53k
      return Rep.end();
99
320M
    --I;
100
320M
    return I;
101
320M
  }
clang::ContinuousRangeMap<unsigned int, int, 2u>::find(unsigned int)
Line
Count
Source
93
293M
  iterator find(Int K) {
94
293M
    iterator I = llvm::upper_bound(Rep, K, Compare());
95
    // I points to the first entry with a key > K, which is the range that
96
    // follows the one containing K.
97
293M
    if (I == Rep.begin())
98
4.53k
      return Rep.end();
99
293M
    --I;
100
293M
    return I;
101
293M
  }
clang::ContinuousRangeMap<unsigned int, clang::serialization::ModuleFile*, 64u>::find(unsigned int)
Line
Count
Source
93
6.79M
  iterator find(Int K) {
94
6.79M
    iterator I = llvm::upper_bound(Rep, K, Compare());
95
    // I points to the first entry with a key > K, which is the range that
96
    // follows the one containing K.
97
6.79M
    if (I == Rep.begin())
98
0
      return Rep.end();
99
6.79M
    --I;
100
6.79M
    return I;
101
6.79M
  }
clang::ContinuousRangeMap<unsigned int, clang::serialization::ModuleFile*, 4u>::find(unsigned int)
Line
Count
Source
93
19.4M
  iterator find(Int K) {
94
19.4M
    iterator I = llvm::upper_bound(Rep, K, Compare());
95
    // I points to the first entry with a key > K, which is the range that
96
    // follows the one containing K.
97
19.4M
    if (I == Rep.begin())
98
0
      return Rep.end();
99
19.4M
    --I;
100
19.4M
    return I;
101
19.4M
  }
clang::ContinuousRangeMap<unsigned long long, clang::serialization::ModuleFile*, 4u>::find(unsigned long long)
Line
Count
Source
93
260k
  iterator find(Int K) {
94
260k
    iterator I = llvm::upper_bound(Rep, K, Compare());
95
    // I points to the first entry with a key > K, which is the range that
96
    // follows the one containing K.
97
260k
    if (I == Rep.begin())
98
0
      return Rep.end();
99
260k
    --I;
100
260k
    return I;
101
260k
  }
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
80.1k
    explicit Builder(ContinuousRangeMap &Self) : Self(Self) {}
116
    Builder(const Builder&) = delete;
117
    Builder &operator=(const Builder&) = delete;
118
119
80.1k
    ~Builder() {
120
80.1k
      llvm::sort(Self.Rep, Compare());
121
80.1k
      Self.Rep.erase(
122
80.1k
          std::unique(
123
80.1k
              Self.Rep.begin(), Self.Rep.end(),
124
342k
              [](const_reference A, const_reference B) {
125
                // FIXME: we should not allow any duplicate keys, but there are
126
                // a lot of duplicate 0 -> 0 mappings to remove first.
127
342k
                assert((A == B || A.first != B.first) &&
128
342k
                       "ContinuousRangeMap::Builder given non-unique keys");
129
342k
                return A == B;
130
342k
              }),
131
80.1k
          Self.Rep.end());
132
80.1k
    }
133
134
349k
    void insert(const value_type &Val) {
135
349k
      Self.Rep.push_back(Val);
136
349k
    }
137
  };
138
139
  friend class Builder;
140
};
141
142
} // namespace clang
143
144
#endif // LLVM_CLANG_SERIALIZATION_CONTINUOUSRANGEMAP_H