Coverage Report

Created: 2018-07-19 20:53

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