| 1 |
/* { dg-options "--coverage -fcondition-coverage -std=c++11" } */ |
| 2 |
/* { dg-do run { target native } } */ |
| 3 |
/* { dg-skip-if "requires hosted libstdc++ for vector" { ! hostedlib } } */ |
| 4 |
|
| 5 |
#include <vector> |
| 6 |
#include <stdexcept> |
| 7 |
|
| 8 |
class nontrivial_destructor |
| 9 |
{ |
| 10 |
public: |
| 11 |
explicit nontrivial_destructor (int v) : val (v) {} |
| 12 |
~nontrivial_destructor () {} |
| 13 |
|
| 14 |
explicit operator bool() const { return bool(val); } |
| 15 |
|
| 16 |
int val; |
| 17 |
}; |
| 18 |
|
| 19 |
int identity (int x) { return x; } |
| 20 |
int throws (int) { throw std::runtime_error("exception"); } |
| 21 |
|
| 22 |
int |
| 23 |
throw_if (int x) |
| 24 |
{ |
| 25 |
if (x) /* conditions(1/2) true(0) */ |
| 26 |
/* conditions(end) */ |
| 27 |
throw std::runtime_error("exception"); |
| 28 |
return x; |
| 29 |
} |
| 30 |
|
| 31 |
/* Used for side effects to insert nodes in conditional bodies etc. */ |
| 32 |
int x = 0; |
| 33 |
|
| 34 |
/* Conditionals work in the presence of non-trivial destructors. */ |
| 35 |
void |
| 36 |
mcdc001a (int a) |
| 37 |
{ |
| 38 |
nontrivial_destructor v (a); |
| 39 |
|
| 40 |
if (v.val > 0) /* conditions(2/2) */ |
| 41 |
x = v.val; |
| 42 |
else |
| 43 |
x = -v.val; |
| 44 |
} |
| 45 |
|
| 46 |
/* Non-trivial destructor in-loop temporary. */ |
| 47 |
nontrivial_destructor |
| 48 |
mcdc002a (int a, int b) |
| 49 |
{ |
| 50 |
for (int i = 0; i < a; i++) /* conditions(2/2) */ |
| 51 |
{ |
| 52 |
nontrivial_destructor tmp (a); |
| 53 |
if (tmp.val % b) /* conditions(2/2) */ |
| 54 |
return nontrivial_destructor (0); |
| 55 |
x += i; |
| 56 |
} /* conditions(suppress) */ |
| 57 |
/* conditions(end) */ |
| 58 |
|
| 59 |
return nontrivial_destructor (a * b); |
| 60 |
} |
| 61 |
|
| 62 |
/* Conditional in constructor. */ |
| 63 |
void |
| 64 |
mcdc003a (int a) |
| 65 |
{ |
| 66 |
class C |
| 67 |
{ |
| 68 |
public: |
| 69 |
explicit C (int e) : v (e) |
| 70 |
{ |
| 71 |
if (e) /* conditions(1/2) false(0) */ |
| 72 |
v = x - e; |
| 73 |
} |
| 74 |
int v; |
| 75 |
}; |
| 76 |
|
| 77 |
C c (a); |
| 78 |
if (c.v > 2) /* conditions(1/2) true(0) */ |
| 79 |
/* conditions(end) */ |
| 80 |
x = c.v + a; |
| 81 |
} |
| 82 |
|
| 83 |
/* Conditional in destructor. */ |
| 84 |
void |
| 85 |
mcdc004a (int a) |
| 86 |
{ |
| 87 |
class C |
| 88 |
{ |
| 89 |
public: |
| 90 |
explicit C (int e) : v (e) {} |
| 91 |
~C () |
| 92 |
{ |
| 93 |
if (v) /* conditions(2/2) */ |
| 94 |
x = 2 * v; |
| 95 |
} |
| 96 |
int v; |
| 97 |
}; |
| 98 |
|
| 99 |
C c (a); |
| 100 |
x = 1; // arbitrary action between ctor+dtor |
| 101 |
} |
| 102 |
|
| 103 |
/* Conditional in try. */ |
| 104 |
void |
| 105 |
mcdc005a (int a) |
| 106 |
{ |
| 107 |
try |
| 108 |
{ |
| 109 |
if (a) /* conditions(1/2) true(0) */ |
| 110 |
/* conditions(end) */ |
| 111 |
x = 2 * identity (a); |
| 112 |
else |
| 113 |
x = 1; |
| 114 |
} |
| 115 |
catch (...) |
| 116 |
{ |
| 117 |
x = 0; |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
/* Conditional in catch. */ |
| 122 |
void |
| 123 |
mcdc006a (int a) { |
| 124 |
try |
| 125 |
{ |
| 126 |
throws (a); |
| 127 |
} |
| 128 |
catch (std::exception&) |
| 129 |
{ |
| 130 |
if (a) /* conditions(1/2) false(0) */ |
| 131 |
/* conditions(end) */ |
| 132 |
x = identity (a); |
| 133 |
else |
| 134 |
x = 0; |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
void |
| 139 |
mcdc006b (int a) |
| 140 |
{ |
| 141 |
if (a) /* conditions(1/2) true(0) */ |
| 142 |
/* conditions(end) */ |
| 143 |
throws (a); |
| 144 |
else |
| 145 |
x = 1; |
| 146 |
} |
| 147 |
|
| 148 |
void |
| 149 |
mcdc006c (int a) try |
| 150 |
{ |
| 151 |
throws (a); |
| 152 |
} |
| 153 |
catch (...) { |
| 154 |
if (a) /* conditions(2/2) */ |
| 155 |
x = 5; |
| 156 |
} |
| 157 |
|
| 158 |
/* Temporary with destructor as term. */ |
| 159 |
void |
| 160 |
mcdc007a (int a, int b) |
| 161 |
{ |
| 162 |
x = a && nontrivial_destructor (b); /* conditions(3/4) false(1) destructor() */ |
| 163 |
} |
| 164 |
|
| 165 |
void |
| 166 |
mcdc007b (int a, int b) |
| 167 |
{ |
| 168 |
if (a || throw_if (b)) /* conditions(3/4) true(1) destructor() */ |
| 169 |
x = -1; |
| 170 |
else |
| 171 |
x = 1; |
| 172 |
} |
| 173 |
|
| 174 |
void |
| 175 |
mcdc007c (int a, int b) |
| 176 |
{ |
| 177 |
if (throw_if (a) || throw_if (b)) /* conditions(2/4) true(0 1) destructor() */ |
| 178 |
x = -1; |
| 179 |
else |
| 180 |
x = 1; |
| 181 |
} |
| 182 |
|
| 183 |
/* Destructor with delete. */ |
| 184 |
void |
| 185 |
mcdc008a (int a) |
| 186 |
{ |
| 187 |
class C |
| 188 |
{ |
| 189 |
public: |
| 190 |
int size = 5; |
| 191 |
int* ptr = nullptr; |
| 192 |
|
| 193 |
explicit C (int v) : size (v + 5), ptr (new int[size]) /* conditions(suppress) */ |
| 194 |
/* conditions(end) */ |
| 195 |
{ |
| 196 |
for (int i = 0; i < size; i++) /* conditions(2/2) */ |
| 197 |
ptr[i] = i + 1; |
| 198 |
} |
| 199 |
~C() |
| 200 |
{ |
| 201 |
// delete with implicit nullptr check |
| 202 |
delete ptr; /* conditions(1/2) false(0) */ |
| 203 |
/* conditions(end) */ |
| 204 |
} |
| 205 |
}; |
| 206 |
|
| 207 |
C c (a); |
| 208 |
if (c.ptr[a + 1]) /* conditions(1/2) false(0) */ |
| 209 |
x = a; |
| 210 |
} |
| 211 |
|
| 212 |
/* Templates. */ |
| 213 |
template <typename T> |
| 214 |
void |
| 215 |
mcdc009a (T a) |
| 216 |
{ |
| 217 |
if (a > 0) /* conditions(1/2) false(0) */ |
| 218 |
/* conditions(end) */ |
| 219 |
x += 2; |
| 220 |
} |
| 221 |
|
| 222 |
/* constexpr. */ |
| 223 |
|
| 224 |
/* Compile-time evaluated branches do not contribute to coverage. */ |
| 225 |
constexpr int |
| 226 |
mcdc010a (int a, int b) |
| 227 |
{ |
| 228 |
return a > b ? 1 : 2; /* conditions(1/2) true(0) */ |
| 229 |
/* conditions(end) */ |
| 230 |
} |
| 231 |
|
| 232 |
/* Compile-time only evaluated functions do not show up in the compiled program |
| 233 |
and gets no coverage at all. If this would generate output unexpectedly it |
| 234 |
would trigger a test failure ("unexpected output"). */ |
| 235 |
constexpr int |
| 236 |
mcdc010b (int a, int b) |
| 237 |
{ |
| 238 |
return a > b ? 1 : 2; |
| 239 |
} |
| 240 |
|
| 241 |
int |
| 242 |
main (void) |
| 243 |
{ |
| 244 |
mcdc001a (0); |
| 245 |
mcdc001a (1); |
| 246 |
|
| 247 |
mcdc002a (1, 1); |
| 248 |
mcdc002a (1, 2); |
| 249 |
|
| 250 |
mcdc003a (1); |
| 251 |
|
| 252 |
mcdc004a (0); |
| 253 |
mcdc004a (1); |
| 254 |
|
| 255 |
mcdc005a (0); |
| 256 |
|
| 257 |
mcdc006a (1); |
| 258 |
|
| 259 |
mcdc006b (0); |
| 260 |
|
| 261 |
mcdc006c (0); |
| 262 |
mcdc006c (1); |
| 263 |
|
| 264 |
mcdc007a (0, 0); |
| 265 |
mcdc007a (1, 1); |
| 266 |
|
| 267 |
mcdc007b (0, 0); |
| 268 |
mcdc007b (1, 0); |
| 269 |
|
| 270 |
mcdc007c (0, 0); |
| 271 |
|
| 272 |
mcdc008a (1); |
| 273 |
|
| 274 |
mcdc009a (1); |
| 275 |
|
| 276 |
/* Use identity () so that this is not constexpr eval'd. */ |
| 277 |
int v1 = mcdc010a (identity (2), 4); |
| 278 |
constexpr int v2 = mcdc010a (4, 2); |
| 279 |
|
| 280 |
constexpr int v3 = mcdc010b (2, 4); |
| 281 |
} |
| 282 |
|
| 283 |
/* { dg-final { run-gcov conditions { --conditions gcov-18.C } } } */ |