Coverage Report

Created: 2018-07-12 09:57

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/include/llvm/ADT/BitmaskEnum.h
Line
Count
Source
1
//===-- llvm/ADT/BitmaskEnum.h ----------------------------------*- 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
#ifndef LLVM_ADT_BITMASKENUM_H
11
#define LLVM_ADT_BITMASKENUM_H
12
13
#include <cassert>
14
#include <type_traits>
15
#include <utility>
16
17
#include "llvm/Support/MathExtras.h"
18
19
/// LLVM_MARK_AS_BITMASK_ENUM lets you opt in an individual enum type so you can
20
/// perform bitwise operations on it without putting static_cast everywhere.
21
///
22
/// \code
23
///   enum MyEnum {
24
///     E1 = 1, E2 = 2, E3 = 4, E4 = 8,
25
///     LLVM_MARK_AS_BITMASK_ENUM(/* LargestValue = */ E4)
26
///   };
27
///
28
///   void Foo() {
29
///     MyEnum A = (E1 | E2) & E3 ^ ~E4; // Look, ma: No static_cast!
30
///   }
31
/// \endcode
32
///
33
/// Normally when you do a bitwise operation on an enum value, you get back an
34
/// instance of the underlying type (e.g. int).  But using this macro, bitwise
35
/// ops on your enum will return you back instances of the enum.  This is
36
/// particularly useful for enums which represent a combination of flags.
37
///
38
/// The parameter to LLVM_MARK_AS_BITMASK_ENUM should be the largest individual
39
/// value in your enum.
40
///
41
/// All of the enum's values must be non-negative.
42
#define LLVM_MARK_AS_BITMASK_ENUM(LargestValue)                                \
43
  LLVM_BITMASK_LARGEST_ENUMERATOR = LargestValue
44
45
/// LLVM_ENABLE_BITMASK_ENUMS_IN_NAMESPACE() pulls the operator overloads used
46
/// by LLVM_MARK_AS_BITMASK_ENUM into the current namespace.
47
///
48
/// Suppose you have an enum foo::bar::MyEnum.  Before using
49
/// LLVM_MARK_AS_BITMASK_ENUM on MyEnum, you must put
50
/// LLVM_ENABLE_BITMASK_ENUMS_IN_NAMESPACE() somewhere inside namespace foo or
51
/// namespace foo::bar.  This allows the relevant operator overloads to be found
52
/// by ADL.
53
///
54
/// You don't need to use this macro in namespace llvm; it's done at the bottom
55
/// of this file.
56
#define LLVM_ENABLE_BITMASK_ENUMS_IN_NAMESPACE()                               \
57
  using ::llvm::BitmaskEnumDetail::operator~;                                  \
58
  using ::llvm::BitmaskEnumDetail::operator|;                                  \
59
  using ::llvm::BitmaskEnumDetail::operator&;                                  \
60
  using ::llvm::BitmaskEnumDetail::operator^;                                  \
61
  using ::llvm::BitmaskEnumDetail::operator|=;                                 \
62
  using ::llvm::BitmaskEnumDetail::operator&=;                                 \
63
  /* Force a semicolon at the end of this macro. */                            \
64
  using ::llvm::BitmaskEnumDetail::operator^=
65
66
namespace llvm {
67
68
/// Traits class to determine whether an enum has a
69
/// LLVM_BITMASK_LARGEST_ENUMERATOR enumerator.
70
template <typename E, typename Enable = void>
71
struct is_bitmask_enum : std::false_type {};
72
73
template <typename E>
74
struct is_bitmask_enum<
75
    E, typename std::enable_if<sizeof(E::LLVM_BITMASK_LARGEST_ENUMERATOR) >=
76
                               0>::type> : std::true_type {};
77
namespace BitmaskEnumDetail {
78
79
/// Get a bitmask with 1s in all places up to the high-order bit of E's largest
80
/// value.
81
34.8k
template <typename E> typename std::underlying_type<E>::type Mask() {
82
34.8k
  // On overflow, NextPowerOf2 returns zero with the type uint64_t, so
83
34.8k
  // subtracting 1 gives us the mask with all bits set, like we want.
84
34.8k
  return NextPowerOf2(static_cast<typename std::underlying_type<E>::type>(
85
34.8k
             E::LLVM_BITMASK_LARGEST_ENUMERATOR)) -
86
34.8k
         1;
87
34.8k
}
std::__1::underlying_type<llvm::MachineMemOperand::Flags>::type llvm::BitmaskEnumDetail::Mask<llvm::MachineMemOperand::Flags>()
Line
Count
Source
81
25.0k
template <typename E> typename std::underlying_type<E>::type Mask() {
82
25.0k
  // On overflow, NextPowerOf2 returns zero with the type uint64_t, so
83
25.0k
  // subtracting 1 gives us the mask with all bits set, like we want.
84
25.0k
  return NextPowerOf2(static_cast<typename std::underlying_type<E>::type>(
85
25.0k
             E::LLVM_BITMASK_LARGEST_ENUMERATOR)) -
86
25.0k
         1;
87
25.0k
}
std::__1::underlying_type<llvm::DINode::DIFlags>::type llvm::BitmaskEnumDetail::Mask<llvm::DINode::DIFlags>()
Line
Count
Source
81
9.73k
template <typename E> typename std::underlying_type<E>::type Mask() {
82
9.73k
  // On overflow, NextPowerOf2 returns zero with the type uint64_t, so
83
9.73k
  // subtracting 1 gives us the mask with all bits set, like we want.
84
9.73k
  return NextPowerOf2(static_cast<typename std::underlying_type<E>::type>(
85
9.73k
             E::LLVM_BITMASK_LARGEST_ENUMERATOR)) -
86
9.73k
         1;
87
9.73k
}
88
89
/// Check that Val is in range for E, and return Val cast to E's underlying
90
/// type.
91
305M
template <typename E> typename std::underlying_type<E>::type Underlying(E Val) {
92
305M
  auto U = static_cast<typename std::underlying_type<E>::type>(Val);
93
305M
  assert(U >= 0 && "Negative enum values are not allowed.");
94
305M
  assert(U <= Mask<E>() && "Enum value too large (or largest val too small?)");
95
305M
  return U;
96
305M
}
std::__1::underlying_type<llvm::MachineMemOperand::Flags>::type llvm::BitmaskEnumDetail::Underlying<llvm::MachineMemOperand::Flags>(llvm::MachineMemOperand::Flags)
Line
Count
Source
91
303M
template <typename E> typename std::underlying_type<E>::type Underlying(E Val) {
92
303M
  auto U = static_cast<typename std::underlying_type<E>::type>(Val);
93
303M
  assert(U >= 0 && "Negative enum values are not allowed.");
94
303M
  assert(U <= Mask<E>() && "Enum value too large (or largest val too small?)");
95
303M
  return U;
96
303M
}
std::__1::underlying_type<llvm::BinaryStreamFlags>::type llvm::BitmaskEnumDetail::Underlying<llvm::BinaryStreamFlags>(llvm::BinaryStreamFlags)
Line
Count
Source
91
386k
template <typename E> typename std::underlying_type<E>::type Underlying(E Val) {
92
386k
  auto U = static_cast<typename std::underlying_type<E>::type>(Val);
93
386k
  assert(U >= 0 && "Negative enum values are not allowed.");
94
386k
  assert(U <= Mask<E>() && "Enum value too large (or largest val too small?)");
95
386k
  return U;
96
386k
}
std::__1::underlying_type<llvm::DINode::DIFlags>::type llvm::BitmaskEnumDetail::Underlying<llvm::DINode::DIFlags>(llvm::DINode::DIFlags)
Line
Count
Source
91
855k
template <typename E> typename std::underlying_type<E>::type Underlying(E Val) {
92
855k
  auto U = static_cast<typename std::underlying_type<E>::type>(Val);
93
855k
  assert(U >= 0 && "Negative enum values are not allowed.");
94
855k
  assert(U <= Mask<E>() && "Enum value too large (or largest val too small?)");
95
855k
  return U;
96
855k
}
SIMemoryLegalizer.cpp:std::__1::underlying_type<(anonymous namespace)::SIAtomicAddrSpace>::type llvm::BitmaskEnumDetail::Underlying<(anonymous namespace)::SIAtomicAddrSpace>((anonymous namespace)::SIAtomicAddrSpace)
Line
Count
Source
91
115k
template <typename E> typename std::underlying_type<E>::type Underlying(E Val) {
92
115k
  auto U = static_cast<typename std::underlying_type<E>::type>(Val);
93
115k
  assert(U >= 0 && "Negative enum values are not allowed.");
94
115k
  assert(U <= Mask<E>() && "Enum value too large (or largest val too small?)");
95
115k
  return U;
96
115k
}
SIMemoryLegalizer.cpp:std::__1::underlying_type<(anonymous namespace)::SIMemOp>::type llvm::BitmaskEnumDetail::Underlying<(anonymous namespace)::SIMemOp>((anonymous namespace)::SIMemOp)
Line
Count
Source
91
2.68k
template <typename E> typename std::underlying_type<E>::type Underlying(E Val) {
92
2.68k
  auto U = static_cast<typename std::underlying_type<E>::type>(Val);
93
2.68k
  assert(U >= 0 && "Negative enum values are not allowed.");
94
2.68k
  assert(U <= Mask<E>() && "Enum value too large (or largest val too small?)");
95
2.68k
  return U;
96
2.68k
}
std::__1::underlying_type<clang::Parser::AngleBracketTracker::Priority>::type llvm::BitmaskEnumDetail::Underlying<clang::Parser::AngleBracketTracker::Priority>(clang::Parser::AngleBracketTracker::Priority)
Line
Count
Source
91
445k
template <typename E> typename std::underlying_type<E>::type Underlying(E Val) {
92
445k
  auto U = static_cast<typename std::underlying_type<E>::type>(Val);
93
445k
  assert(U >= 0 && "Negative enum values are not allowed.");
94
445k
  assert(U <= Mask<E>() && "Enum value too large (or largest val too small?)");
95
445k
  return U;
96
445k
}
std::__1::underlying_type<llvm::pdb::PdbRaw_Features>::type llvm::BitmaskEnumDetail::Underlying<llvm::pdb::PdbRaw_Features>(llvm::pdb::PdbRaw_Features)
Line
Count
Source
91
310
template <typename E> typename std::underlying_type<E>::type Underlying(E Val) {
92
310
  auto U = static_cast<typename std::underlying_type<E>::type>(Val);
93
310
  assert(U >= 0 && "Negative enum values are not allowed.");
94
310
  assert(U <= Mask<E>() && "Enum value too large (or largest val too small?)");
95
310
  return U;
96
310
}
97
98
template <typename E,
99
          typename = typename std::enable_if<is_bitmask_enum<E>::value>::type>
100
34.8k
E operator~(E Val) {
101
34.8k
  return static_cast<E>(~Underlying(Val) & Mask<E>());
102
34.8k
}
llvm::MachineMemOperand::Flags llvm::BitmaskEnumDetail::operator~<llvm::MachineMemOperand::Flags, void>(llvm::MachineMemOperand::Flags)
Line
Count
Source
100
25.0k
E operator~(E Val) {
101
25.0k
  return static_cast<E>(~Underlying(Val) & Mask<E>());
102
25.0k
}
llvm::DINode::DIFlags llvm::BitmaskEnumDetail::operator~<llvm::DINode::DIFlags, void>(llvm::DINode::DIFlags)
Line
Count
Source
100
9.73k
E operator~(E Val) {
101
9.73k
  return static_cast<E>(~Underlying(Val) & Mask<E>());
102
9.73k
}
103
104
template <typename E,
105
          typename = typename std::enable_if<is_bitmask_enum<E>::value>::type>
106
8.09M
E operator|(E LHS, E RHS) {
107
8.09M
  return static_cast<E>(Underlying(LHS) | Underlying(RHS));
108
8.09M
}
llvm::MachineMemOperand::Flags llvm::BitmaskEnumDetail::operator|<llvm::MachineMemOperand::Flags, void>(llvm::MachineMemOperand::Flags, llvm::MachineMemOperand::Flags)
Line
Count
Source
106
7.66M
E operator|(E LHS, E RHS) {
107
7.66M
  return static_cast<E>(Underlying(LHS) | Underlying(RHS));
108
7.66M
}
llvm::BinaryStreamFlags llvm::BitmaskEnumDetail::operator|<llvm::BinaryStreamFlags, void>(llvm::BinaryStreamFlags, llvm::BinaryStreamFlags)
Line
Count
Source
106
36.3k
E operator|(E LHS, E RHS) {
107
36.3k
  return static_cast<E>(Underlying(LHS) | Underlying(RHS));
108
36.3k
}
SIMemoryLegalizer.cpp:(anonymous namespace)::SIAtomicAddrSpace llvm::BitmaskEnumDetail::operator|<(anonymous namespace)::SIAtomicAddrSpace, void>((anonymous namespace)::SIAtomicAddrSpace, (anonymous namespace)::SIAtomicAddrSpace)
Line
Count
Source
106
45.8k
E operator|(E LHS, E RHS) {
107
45.8k
  return static_cast<E>(Underlying(LHS) | Underlying(RHS));
108
45.8k
}
SIMemoryLegalizer.cpp:(anonymous namespace)::SIMemOp llvm::BitmaskEnumDetail::operator|<(anonymous namespace)::SIMemOp, void>((anonymous namespace)::SIMemOp, (anonymous namespace)::SIMemOp)
Line
Count
Source
106
1.34k
E operator|(E LHS, E RHS) {
107
1.34k
  return static_cast<E>(Underlying(LHS) | Underlying(RHS));
108
1.34k
}
llvm::DINode::DIFlags llvm::BitmaskEnumDetail::operator|<llvm::DINode::DIFlags, void>(llvm::DINode::DIFlags, llvm::DINode::DIFlags)
Line
Count
Source
106
124k
E operator|(E LHS, E RHS) {
107
124k
  return static_cast<E>(Underlying(LHS) | Underlying(RHS));
108
124k
}
clang::Parser::AngleBracketTracker::Priority llvm::BitmaskEnumDetail::operator|<clang::Parser::AngleBracketTracker::Priority, void>(clang::Parser::AngleBracketTracker::Priority, clang::Parser::AngleBracketTracker::Priority)
Line
Count
Source
106
222k
E operator|(E LHS, E RHS) {
107
222k
  return static_cast<E>(Underlying(LHS) | Underlying(RHS));
108
222k
}
llvm::pdb::PdbRaw_Features llvm::BitmaskEnumDetail::operator|<llvm::pdb::PdbRaw_Features, void>(llvm::pdb::PdbRaw_Features, llvm::pdb::PdbRaw_Features)
Line
Count
Source
106
63
E operator|(E LHS, E RHS) {
107
63
  return static_cast<E>(Underlying(LHS) | Underlying(RHS));
108
63
}
109
110
template <typename E,
111
          typename = typename std::enable_if<is_bitmask_enum<E>::value>::type>
112
144M
E operator&(E LHS, E RHS) {
113
144M
  return static_cast<E>(Underlying(LHS) & Underlying(RHS));
114
144M
}
llvm::MachineMemOperand::Flags llvm::BitmaskEnumDetail::operator&<llvm::MachineMemOperand::Flags, void>(llvm::MachineMemOperand::Flags, llvm::MachineMemOperand::Flags)
Line
Count
Source
112
144M
E operator&(E LHS, E RHS) {
113
144M
  return static_cast<E>(Underlying(LHS) & Underlying(RHS));
114
144M
}
llvm::BinaryStreamFlags llvm::BitmaskEnumDetail::operator&<llvm::BinaryStreamFlags, void>(llvm::BinaryStreamFlags, llvm::BinaryStreamFlags)
Line
Count
Source
112
156k
E operator&(E LHS, E RHS) {
113
156k
  return static_cast<E>(Underlying(LHS) & Underlying(RHS));
114
156k
}
llvm::DINode::DIFlags llvm::BitmaskEnumDetail::operator&<llvm::DINode::DIFlags, void>(llvm::DINode::DIFlags, llvm::DINode::DIFlags)
Line
Count
Source
112
298k
E operator&(E LHS, E RHS) {
113
298k
  return static_cast<E>(Underlying(LHS) & Underlying(RHS));
114
298k
}
SIMemoryLegalizer.cpp:(anonymous namespace)::SIAtomicAddrSpace llvm::BitmaskEnumDetail::operator&<(anonymous namespace)::SIAtomicAddrSpace, void>((anonymous namespace)::SIAtomicAddrSpace, (anonymous namespace)::SIAtomicAddrSpace)
Line
Count
Source
112
11.9k
E operator&(E LHS, E RHS) {
113
11.9k
  return static_cast<E>(Underlying(LHS) & Underlying(RHS));
114
11.9k
}
llvm::pdb::PdbRaw_Features llvm::BitmaskEnumDetail::operator&<llvm::pdb::PdbRaw_Features, void>(llvm::pdb::PdbRaw_Features, llvm::pdb::PdbRaw_Features)
Line
Count
Source
112
92
E operator&(E LHS, E RHS) {
113
92
  return static_cast<E>(Underlying(LHS) & Underlying(RHS));
114
92
}
115
116
template <typename E,
117
          typename = typename std::enable_if<is_bitmask_enum<E>::value>::type>
118
E operator^(E LHS, E RHS) {
119
  return static_cast<E>(Underlying(LHS) ^ Underlying(RHS));
120
}
121
122
// |=, &=, and ^= return a reference to LHS, to match the behavior of the
123
// operators on builtin types.
124
125
template <typename E,
126
          typename = typename std::enable_if<is_bitmask_enum<E>::value>::type>
127
7.61M
E &operator|=(E &LHS, E RHS) {
128
7.61M
  LHS = LHS | RHS;
129
7.61M
  return LHS;
130
7.61M
}
llvm::MachineMemOperand::Flags& llvm::BitmaskEnumDetail::operator|=<llvm::MachineMemOperand::Flags, void>(llvm::MachineMemOperand::Flags&, llvm::MachineMemOperand::Flags)
Line
Count
Source
127
7.44M
E &operator|=(E &LHS, E RHS) {
128
7.44M
  LHS = LHS | RHS;
129
7.44M
  return LHS;
130
7.44M
}
SIMemoryLegalizer.cpp:(anonymous namespace)::SIAtomicAddrSpace& llvm::BitmaskEnumDetail::operator|=<(anonymous namespace)::SIAtomicAddrSpace, void>((anonymous namespace)::SIAtomicAddrSpace&, (anonymous namespace)::SIAtomicAddrSpace)
Line
Count
Source
127
45.8k
E &operator|=(E &LHS, E RHS) {
128
45.8k
  LHS = LHS | RHS;
129
45.8k
  return LHS;
130
45.8k
}
llvm::DINode::DIFlags& llvm::BitmaskEnumDetail::operator|=<llvm::DINode::DIFlags, void>(llvm::DINode::DIFlags&, llvm::DINode::DIFlags)
Line
Count
Source
127
120k
E &operator|=(E &LHS, E RHS) {
128
120k
  LHS = LHS | RHS;
129
120k
  return LHS;
130
120k
}
llvm::pdb::PdbRaw_Features& llvm::BitmaskEnumDetail::operator|=<llvm::pdb::PdbRaw_Features, void>(llvm::pdb::PdbRaw_Features&, llvm::pdb::PdbRaw_Features)
Line
Count
Source
127
63
E &operator|=(E &LHS, E RHS) {
128
63
  LHS = LHS | RHS;
129
63
  return LHS;
130
63
}
131
132
template <typename E,
133
          typename = typename std::enable_if<is_bitmask_enum<E>::value>::type>
134
9.90k
E &operator&=(E &LHS, E RHS) {
135
9.90k
  LHS = LHS & RHS;
136
9.90k
  return LHS;
137
9.90k
}
llvm::DINode::DIFlags& llvm::BitmaskEnumDetail::operator&=<llvm::DINode::DIFlags, void>(llvm::DINode::DIFlags&, llvm::DINode::DIFlags)
Line
Count
Source
134
9.73k
E &operator&=(E &LHS, E RHS) {
135
9.73k
  LHS = LHS & RHS;
136
9.73k
  return LHS;
137
9.73k
}
llvm::MachineMemOperand::Flags& llvm::BitmaskEnumDetail::operator&=<llvm::MachineMemOperand::Flags, void>(llvm::MachineMemOperand::Flags&, llvm::MachineMemOperand::Flags)
Line
Count
Source
134
171
E &operator&=(E &LHS, E RHS) {
135
171
  LHS = LHS & RHS;
136
171
  return LHS;
137
171
}
138
139
template <typename E,
140
          typename = typename std::enable_if<is_bitmask_enum<E>::value>::type>
141
E &operator^=(E &LHS, E RHS) {
142
  LHS = LHS ^ RHS;
143
  return LHS;
144
}
145
146
} // namespace BitmaskEnumDetail
147
148
// Enable bitmask enums in namespace ::llvm and all nested namespaces.
149
LLVM_ENABLE_BITMASK_ENUMS_IN_NAMESPACE();
150
151
} // namespace llvm
152
153
#endif