Coverage Report

Created: 2019-07-24 05:18

/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
55.1M
      explicit CharUnits(QuantityType C) : Quantity(C) {}
45
46
    public:
47
48
      /// CharUnits - A default constructor.
49
38.3M
      CharUnits() = default;
50
51
      /// Zero - Construct a CharUnits quantity of zero.
52
2.15M
      static CharUnits Zero() {
53
2.15M
        return CharUnits(0);
54
2.15M
      }
55
56
      /// One - Construct a CharUnits quantity of one.
57
1.13M
      static CharUnits One() {
58
1.13M
        return CharUnits(1);
59
1.13M
      }
60
61
      /// fromQuantity - Construct a CharUnits quantity from a raw integer type.
62
44.7M
      static CharUnits fromQuantity(QuantityType Quantity) {
63
44.7M
        return CharUnits(Quantity);
64
44.7M
      }
65
66
      // Compound assignment.
67
470k
      CharUnits& operator+= (const CharUnits &Other) {
68
470k
        Quantity += Other.Quantity;
69
470k
        return *this;
70
470k
      }
71
1.18k
      CharUnits& operator++ () {
72
1.18k
        ++Quantity;
73
1.18k
        return *this;
74
1.18k
      }
75
349
      CharUnits operator++ (int) {
76
349
        return CharUnits(Quantity++);
77
349
      }
78
1.26k
      CharUnits& operator-= (const CharUnits &Other) {
79
1.26k
        Quantity -= Other.Quantity;
80
1.26k
        return *this;
81
1.26k
      }
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.86M
      bool operator== (const CharUnits &Other) const {
92
2.86M
        return Quantity == Other.Quantity;
93
2.86M
      }
94
771k
      bool operator!= (const CharUnits &Other) const {
95
771k
        return Quantity != Other.Quantity;
96
771k
      }
97
98
      // Relational operators.
99
5.21M
      bool operator<  (const CharUnits &Other) const {
100
5.21M
        return Quantity <  Other.Quantity;
101
5.21M
      }
102
1.10M
      bool operator<= (const CharUnits &Other) const {
103
1.10M
        return Quantity <= Other.Quantity;
104
1.10M
      }
105
2.23M
      bool operator>  (const CharUnits &Other) const {
106
2.23M
        return Quantity >  Other.Quantity;
107
2.23M
      }
108
698k
      bool operator>= (const CharUnits &Other) const {
109
698k
        return Quantity >= Other.Quantity;
110
698k
      }
111
112
      // Other predicates.
113
114
      /// isZero - Test whether the quantity equals zero.
115
1.92M
      bool isZero() const     { return Quantity == 0; }
116
117
      /// isOne - Test whether the quantity equals one.
118
6.84k
      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
113k
      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.3k
      bool isMultipleOf(CharUnits N) const {
137
12.3k
        return (*this % N) == 0;
138
12.3k
      }
139
140
      // Arithmetic operators.
141
301k
      CharUnits operator* (QuantityType N) const {
142
301k
        return CharUnits(Quantity * N);
143
301k
      }
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.14k
      QuantityType operator/ (const CharUnits &Other) const {
156
1.14k
        return Quantity / Other.Quantity;
157
1.14k
      }
158
120
      CharUnits operator% (QuantityType N) const {
159
120
        return CharUnits(Quantity % N);
160
120
      }
161
751k
      QuantityType operator% (const CharUnits &Other) const {
162
751k
        return Quantity % Other.Quantity;
163
751k
      }
164
3.76M
      CharUnits operator+ (const CharUnits &Other) const {
165
3.76M
        return CharUnits(Quantity + Other.Quantity);
166
3.76M
      }
167
51.2k
      CharUnits operator- (const CharUnits &Other) const {
168
51.2k
        return CharUnits(Quantity - Other.Quantity);
169
51.2k
      }
170
22.6k
      CharUnits operator- () const {
171
22.6k
        return CharUnits(-Quantity);
172
22.6k
      }
173
174
175
      // Conversions.
176
177
      /// getQuantity - Get the raw integer representation of this quantity.
178
39.9M
      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.97M
      CharUnits alignTo(const CharUnits &Align) const {
184
1.97M
        return CharUnits(llvm::alignTo(Quantity, Align.Quantity));
185
1.97M
      }
186
187
      /// Given that this is a non-zero alignment value, what is the
188
      /// alignment at the given offset?
189
1.06M
      CharUnits alignmentAtOffset(CharUnits offset) const {
190
1.06M
        assert(Quantity != 0 && "offsetting from unknown alignment?");
191
1.06M
        return CharUnits(llvm::MinAlign(Quantity, offset.Quantity));
192
1.06M
      }
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
123k
      CharUnits alignmentOfArrayElement(CharUnits elementSize) const {
197
123k
        // Since we don't track offsetted alignments, the alignment of
198
123k
        // the second element (or any odd element) will be minimally
199
123k
        // aligned.
200
123k
        return alignmentAtOffset(elementSize);
201
123k
      }
202
203
204
  }; // class CharUnit
205
} // namespace clang
206
207
inline clang::CharUnits operator* (clang::CharUnits::QuantityType Scale,
208
286k
                                   const clang::CharUnits &CU) {
209
286k
  return CU * Scale;
210
286k
}
211
212
namespace llvm {
213
214
template<> struct DenseMapInfo<clang::CharUnits> {
215
356k
  static clang::CharUnits getEmptyKey() {
216
356k
    clang::CharUnits::QuantityType Quantity =
217
356k
      DenseMapInfo<clang::CharUnits::QuantityType>::getEmptyKey();
218
356k
219
356k
    return clang::CharUnits::fromQuantity(Quantity);
220
356k
  }
221
222
255k
  static clang::CharUnits getTombstoneKey() {
223
255k
    clang::CharUnits::QuantityType Quantity =
224
255k
      DenseMapInfo<clang::CharUnits::QuantityType>::getTombstoneKey();
225
255k
226
255k
    return clang::CharUnits::fromQuantity(Quantity);
227
255k
  }
228
229
267k
  static unsigned getHashValue(const clang::CharUnits &CU) {
230
267k
    clang::CharUnits::QuantityType Quantity = CU.getQuantity();
231
267k
    return DenseMapInfo<clang::CharUnits::QuantityType>::getHashValue(Quantity);
232
267k
  }
233
234
  static bool isEqual(const clang::CharUnits &LHS,
235
1.76M
                      const clang::CharUnits &RHS) {
236
1.76M
    return LHS == RHS;
237
1.76M
  }
238
};
239
240
} // end namespace llvm
241
242
#endif // LLVM_CLANG_AST_CHARUNITS_H