1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
|
// RUN: %check_clang_tidy -std=c++14 %s bugprone-unhandled-exception-at-new %t -- -- -fexceptions
// FIXME: Fix the checker to work in C++17 or later mode.
namespace std {
typedef __typeof__(sizeof(0)) size_t;
enum class align_val_t : std::size_t {};
class exception {};
class bad_alloc : public exception {};
class bad_array_new_length : public bad_alloc {};
struct nothrow_t {};
extern const nothrow_t nothrow;
} // namespace std
void *operator new(std::size_t, const std::nothrow_t &) noexcept;
void *operator new(std::size_t, std::align_val_t, const std::nothrow_t &) noexcept;
void *operator new(std::size_t, void *) noexcept;
class A {};
typedef std::bad_alloc badalloc1;
using badalloc2 = std::bad_alloc;
using badalloc3 = std::bad_alloc &;
void *operator new(std::size_t, int, int);
void *operator new(std::size_t, int, int, int) noexcept;
struct ClassSpecificNew {
void *operator new(std::size_t);
void *operator new(std::size_t, std::align_val_t);
void *operator new(std::size_t, int, int) noexcept;
void *operator new(std::size_t, int, int, int);
};
void f1() noexcept {
int *I1 = new int;
// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: missing exception handler for allocation failure at 'new'
try {
int *I2 = new int;
try {
int *I3 = new int;
} catch (A) {
}
} catch (std::bad_alloc) {
}
try {
int *I = new int;
} catch (std::bad_alloc &) {
}
try {
int *I = new int;
} catch (const std::bad_alloc &) {
}
try {
int *I = new int;
} catch (badalloc1) {
}
try {
int *I = new int;
} catch (badalloc1 &) {
}
try {
int *I = new int;
} catch (const badalloc1 &) {
}
try {
int *I = new int;
} catch (badalloc2) {
}
try {
int *I = new int;
} catch (badalloc3) {
}
try {
int *I = new int;
// CHECK-MESSAGES: :[[@LINE-1]]:14: warning: missing exception handler for allocation failure at 'new'
} catch (std::bad_alloc *) {
}
try {
int *I = new int;
// CHECK-MESSAGES: :[[@LINE-1]]:14: warning: missing exception handler for allocation failure at 'new'
} catch (A) {
}
}
void f2() noexcept {
try {
int *I = new int;
} catch (A) {
} catch (std::bad_alloc) {
}
try {
int *I = new int;
} catch (...) {
}
try {
int *I = new int;
} catch (const std::exception &) {
}
try {
int *I = new int;
// CHECK-MESSAGES: :[[@LINE-1]]:14: warning: missing exception handler for allocation failure at 'new'
} catch (const std::bad_array_new_length &) {
}
}
void f_new_nothrow() noexcept {
int *I1 = new (std::nothrow) int;
int *I2 = new (static_cast<std::align_val_t>(1), std::nothrow) int;
}
void f_new_placement() noexcept {
char buf[100];
int *I = new (buf) int;
}
void f_new_user_defined() noexcept {
int *I1 = new (1, 2) int;
// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: missing exception handler for allocation failure at 'new'
int *I2 = new (1, 2, 3) int;
}
void f_class_specific() noexcept {
ClassSpecificNew *N1 = new ClassSpecificNew;
// CHECK-MESSAGES: :[[@LINE-1]]:26: warning: missing exception handler for allocation failure at 'new'
ClassSpecificNew *N2 = new (static_cast<std::align_val_t>(1)) ClassSpecificNew;
// CHECK-MESSAGES: :[[@LINE-1]]:26: warning: missing exception handler for allocation failure at 'new'
ClassSpecificNew *N3 = new (1, 2) ClassSpecificNew;
ClassSpecificNew *N4 = new (1, 2, 3) ClassSpecificNew;
// CHECK-MESSAGES: :[[@LINE-1]]:26: warning: missing exception handler for allocation failure at 'new'
}
void f_est_none() {
int *I = new int;
}
void f_est_noexcept_false() noexcept(false) {
int *I = new int;
}
void f_est_noexcept_true() noexcept(true) {
int *I = new int;
// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: missing exception handler for allocation failure at 'new'
}
void f_est_dynamic_none() throw() {
int *I = new int;
// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: missing exception handler for allocation failure at 'new'
}
void f_est_dynamic_1() throw(std::bad_alloc) {
int *I = new int;
}
void f_est_dynamic_2() throw(A) {
// the exception specification list is not checked
int *I = new int;
}
void f_try() noexcept try {
int *I = new int;
} catch (const std::bad_alloc &) {
}
void f_try_bad() noexcept try {
int *I = new int;
// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: missing exception handler for allocation failure at 'new'
} catch (const A &) {
}
void f_embedded_try() noexcept {
try {
try {
int *I = new int;
} catch (const A &) {
}
} catch (const std::bad_alloc &) {
}
}
template <bool P>
void f_est_noexcept_dependent_unused() noexcept(P) {
int *I = new int;
}
template <bool P>
void f_est_noexcept_dependent_used() noexcept(P) {
int *I = new int;
// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: missing exception handler for allocation failure at 'new'
}
template <class T>
void f_dependent_new() {
T *X = new T;
}
void f_1() {
f_est_noexcept_dependent_used<true>();
}
|