Coverage Report

Created: 2019-02-20 07:29

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/clang/include/clang/AST/CharUnits.h
Line
Count
Source (jump to first uncovered line)
1
//===--- CharUnits.h - Character units for sizes and offsets ----*- 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 CharUnits class
10
//
11
//===----------------------------------------------------------------------===//
12
13
#ifndef LLVM_CLANG_AST_CHARUNITS_H
14
#define LLVM_CLANG_AST_CHARUNITS_H
15
16
#include "llvm/ADT/DenseMapInfo.h"
17
#include "llvm/Support/DataTypes.h"
18
#include "llvm/Support/MathExtras.h"
19
20
namespace clang {
21
22
  /// CharUnits - This is an opaque type for sizes expressed in character units.
23
  /// Instances of this type represent a quantity as a multiple of the size
24
  /// of the standard C type, char, on the target architecture. As an opaque
25
  /// type, CharUnits protects you from accidentally combining operations on
26
  /// quantities in bit units and character units.
27
  ///
28
  /// In both C and C++, an object of type 'char', 'signed char', or 'unsigned
29
  /// char' occupies exactly one byte, so 'character unit' and 'byte' refer to
30
  /// the same quantity of storage. However, we use the term 'character unit'
31
  /// rather than 'byte' to avoid an implication that a character unit is
32
  /// exactly 8 bits.
33
  ///
34
  /// For portability, never assume that a target character is 8 bits wide. Use
35
  /// CharUnit values wherever you calculate sizes, offsets, or alignments
36
  /// in character units.
37
  class CharUnits {
38
    public:
39
      typedef int64_t QuantityType;
40
41
    private:
42
      QuantityType Quantity = 0;
43
44
52.8M
      explicit CharUnits(QuantityType C) : Quantity(C) {}
45
46
    public:
47
48
      /// CharUnits - A default constructor.
49
36.2M
      CharUnits() = default;
50
51
      /// Zero - Construct a CharUnits quantity of zero.
52
1.86M
      static CharUnits Zero() {
53
1.86M
        return CharUnits(0);
54
1.86M
      }
55
56
      /// One - Construct a CharUnits quantity of one.
57
1.08M
      static CharUnits One() {
58
1.08M
        return CharUnits(1);
59
1.08M
      }
60
61
      /// fromQuantity - Construct a CharUnits quantity from a raw integer type.
62
43.4M
      static CharUnits fromQuantity(QuantityType Quantity) {
63
43.4M
        return CharUnits(Quantity);
64
43.4M
      }
65
66
      // Compound assignment.
67
448k
      CharUnits& operator+= (const CharUnits &Other) {
68
448k
        Quantity += Other.Quantity;
69
448k
        return *this;
70
448k
      }
71
243
      CharUnits& operator++ () {
72
243
        ++Quantity;
73
243
        return *this;
74
243
      }
75
313
      CharUnits operator++ (int) {
76
313
        return CharUnits(Quantity++);
77
313
      }
78
1.15k
      CharUnits& operator-= (const CharUnits &Other) {
79
1.15k
        Quantity -= Other.Quantity;
80
1.15k
        return *this;
81
1.15k
      }
82
0
      CharUnits& operator-- () {
83
0
        --Quantity;
84
0
        return *this;
85
0
      }
86
0
      CharUnits operator-- (int) {
87
0
        return CharUnits(Quantity--);
88
0
      }
89
90
      // Comparison operators.
91
2.77M
      bool operator== (const CharUnits &Other) const {
92
2.77M
        return Quantity == Other.Quantity;
93
2.77M
      }
94
697k
      bool operator!= (const CharUnits &Other) const {
95
697k
        return Quantity != Other.Quantity;
96
697k
      }
97
98
      // Relational operators.
99
4.54M
      bool operator<  (const CharUnits &Other) const {
100
4.54M
        return Quantity <  Other.Quantity;
101
4.54M
      }
102
1.10M
      bool operator<= (const CharUnits &Other) const {
103
1.10M
        return Quantity <= Other.Quantity;
104
1.10M
      }
105
2.19M
      bool operator>  (const CharUnits &Other) const {
106
2.19M
        return Quantity >  Other.Quantity;
107
2.19M
      }
108
625k
      bool operator>= (const CharUnits &Other) const {
109
625k
        return Quantity >= Other.Quantity;
110
625k
      }
111
112
      // Other predicates.
113
114
      /// isZero - Test whether the quantity equals zero.
115
1.87M
      bool isZero() const     { return Quantity == 0; }
116
117
      /// isOne - Test whether the quantity equals one.
118
6.71k
      bool isOne() const      { return Quantity == 1; }
119
120
      /// isPositive - Test whether the quantity is greater than zero.
121
1.94k
      bool isPositive() const { return Quantity  > 0; }
122
123
      /// isNegative - Test whether the quantity is less than zero.
124
110k
      bool isNegative() const { return Quantity  < 0; }
125
126
      /// isPowerOfTwo - Test whether the quantity is a power of two.
127
      /// Zero is not a power of two.
128
411
      bool isPowerOfTwo() const {
129
411
        return (Quantity & -Quantity) == Quantity;
130
411
      }
131
132
      /// Test whether this is a multiple of the other value.
133
      ///
134
      /// Among other things, this promises that
135
      /// self.alignTo(N) will just return self.
136
12.1k
      bool isMultipleOf(CharUnits N) const {
137
12.1k
        return (*this % N) == 0;
138
12.1k
      }
139
140
      // Arithmetic operators.
141
297k
      CharUnits operator* (QuantityType N) const {
142
297k
        return CharUnits(Quantity * N);
143
297k
      }
144
4.33k
      CharUnits &operator*= (QuantityType N) {
145
4.33k
        Quantity *= N;
146
4.33k
        return *this;
147
4.33k
      }
148
238
      CharUnits operator/ (QuantityType N) const {
149
238
        return CharUnits(Quantity / N);
150
238
      }
151
60
      CharUnits &operator/= (QuantityType N) {
152
60
        Quantity /= N;
153
60
        return *this;
154
60
      }
155
1.07k
      QuantityType operator/ (const CharUnits &Other) const {
156
1.07k
        return Quantity / Other.Quantity;
157
1.07k
      }
158
103
      CharUnits operator% (QuantityType N) const {
159
103
        return CharUnits(Quantity % N);
160
103
      }
161
744k
      QuantityType operator% (const CharUnits &Other) const {
162
744k
        return Quantity % Other.Quantity;
163
744k
      }
164
3.08M
      CharUnits operator+ (const CharUnits &Other) const {
165
3.08M
        return CharUnits(Quantity + Other.Quantity);
166
3.08M
      }
167
49.8k
      CharUnits operator- (const CharUnits &Other) const {
168
49.8k
        return CharUnits(Quantity - Other.Quantity);
169
49.8k
      }
170
22.4k
      CharUnits operator- () const {
171
22.4k
        return CharUnits(-Quantity);
172
22.4k
      }
173
174
175
      // Conversions.
176
177
      /// getQuantity - Get the raw integer representation of this quantity.
178
38.8M
      QuantityType getQuantity() const { return Quantity; }
179
180
      /// alignTo - Returns the next integer (mod 2**64) that is
181
      /// greater than or equal to this quantity and is a multiple of \p Align.
182
      /// Align must be non-zero.
183
1.88M
      CharUnits alignTo(const CharUnits &Align) const {
184
1.88M
        return CharUnits(llvm::alignTo(Quantity, Align.Quantity));
185
1.88M
      }
186
187
      /// Given that this is a non-zero alignment value, what is the
188
      /// alignment at the given offset?
189
1.04M
      CharUnits alignmentAtOffset(CharUnits offset) const {
190
1.04M
        assert(Quantity != 0 && "offsetting from unknown alignment?");
191
1.04M
        return CharUnits(llvm::MinAlign(Quantity, offset.Quantity));
192
1.04M
      }
193
194
      /// Given that this is the alignment of the first element of an
195
      /// array, return the minimum alignment of any element in the array.
196
122k
      CharUnits alignmentOfArrayElement(CharUnits elementSize) const {
197
122k
        // Since we don't track offsetted alignments, the alignment of
198
122k
        // the second element (or any odd element) will be minimally
199
122k
        // aligned.
200
122k
        return alignmentAtOffset(elementSize);
201
122k
      }
202
203
204
  }; // class CharUnit
205
} // namespace clang
206
207
inline clang::CharUnits operator* (clang::CharUnits::QuantityType Scale,
208
282k
                                   const clang::CharUnits &CU) {
209
282k
  return CU * Scale;
210
282k
}
211
212
namespace llvm {
213
214
template<> struct DenseMapInfo<clang::CharUnits> {
215
348k
  static clang::CharUnits getEmptyKey() {
216
348k
    clang::CharUnits::QuantityType Quantity =
217
348k
      DenseMapInfo<clang::CharUnits::QuantityType>::getEmptyKey();
218
348k
219
348k
    return clang::CharUnits::fromQuantity(Quantity);
220
348k
  }
221
222
250k
  static clang::CharUnits getTombstoneKey() {
223
250k
    clang::CharUnits::QuantityType Quantity =
224
250k
      DenseMapInfo<clang::CharUnits::QuantityType>::getTombstoneKey();
225
250k
226
250k
    return clang::CharUnits::fromQuantity(Quantity);
227
250k
  }
228
229
262k
  static unsigned getHashValue(const clang::CharUnits &CU) {
230
262k
    clang::CharUnits::QuantityType Quantity = CU.getQuantity();
231
262k
    return DenseMapInfo<clang::CharUnits::QuantityType>::getHashValue(Quantity);
232
262k
  }
233
234
  static bool isEqual(const clang::CharUnits &LHS,
235
1.68M
                      const clang::CharUnits &RHS) {
236
1.68M
    return LHS == RHS;
237
1.68M
  }
238
};
239
240
} // end namespace llvm
241
242
#endif // LLVM_CLANG_AST_CHARUNITS_H