| 1 |
// P1774R8 - Portable assumptions |
| 2 |
// { dg-do compile { target c++11 } } |
| 3 |
|
| 4 |
[[assume (true)]] void f1 (); // { dg-error "'assume' attribute ignored" } |
| 5 |
typedef int intx [[assume (true)]]; // { dg-error "'assume' attribute ignored" } |
| 6 |
[[assume (true)]]; // { dg-warning "attribute ignored" } |
| 7 |
|
| 8 |
void |
| 9 |
foo () |
| 10 |
{ |
| 11 |
int i; |
| 12 |
[[assume]]; // { dg-error "wrong number of arguments specified for 'assume' attribute" } |
| 13 |
// { dg-message "expected 1, found 0" "" { target *-*-* } .-1 } |
| 14 |
[[assume ()]]; // { dg-error "parentheses must be omitted if 'assume' attribute argument list is empty" } |
| 15 |
// { dg-error "wrong number of arguments specified for 'assume' attribute" "" { target *-*-* } .-1 } |
| 16 |
// { dg-message "expected 1, found 0" "" { target *-*-* } .-2 } |
| 17 |
[[assume (true, true)]]; // { dg-error "wrong number of arguments specified for 'assume' attribute" } |
| 18 |
// { dg-message "expected 1, found 2" "" { target *-*-* } .-1 } |
| 19 |
[[assume (true)]] i = 1; // { dg-warning "'assume' attribute not followed by ';'" } |
| 20 |
[[assume (throw 1)]]; // { dg-error "expected primary-expression before 'throw'" } |
| 21 |
[[assume (i = 1)]]; // { dg-error "expected '\\\)' before '=' token" } |
| 22 |
} |
| 23 |
|
| 24 |
constexpr int |
| 25 |
f2 (int x) |
| 26 |
{ |
| 27 |
#if __cpp_constexpr >= 201304L |
| 28 |
[[assume (x == 42)]]; // { dg-error "failed 'assume' attribute assumption" "" { target c++14 } } |
| 29 |
#endif // { dg-message "the comparison reduces to '\\\(44 == 42\\\)'" "" { target c++14 } .-1 } |
| 30 |
return x; |
| 31 |
} |
| 32 |
|
| 33 |
constexpr int a = f2 (44); |
| 34 |
|
| 35 |
int |
| 36 |
f3 (int x) |
| 37 |
{ |
| 38 |
__asm ("" : "+r" (x)); |
| 39 |
return x; |
| 40 |
} |
| 41 |
|
| 42 |
constexpr int |
| 43 |
f4 (int x) |
| 44 |
{ |
| 45 |
#if __cpp_constexpr >= 201304L |
| 46 |
[[assume (f3 (42) == 42)]]; |
| 47 |
#endif |
| 48 |
return x; |
| 49 |
} |
| 50 |
|
| 51 |
static_assert (f4 (42) == 42, ""); |
| 52 |
|
| 53 |
struct S {}; |
| 54 |
|
| 55 |
int |
| 56 |
f5 () |
| 57 |
{ |
| 58 |
S s; |
| 59 |
[[assume (s)]]; // { dg-error "could not convert 's' from 'S' to 'bool'" } |
| 60 |
return 0; |
| 61 |
} |
| 62 |
|
| 63 |
template <typename T> |
| 64 |
int |
| 65 |
f6 () |
| 66 |
{ |
| 67 |
T t; |
| 68 |
[[assume (t)]]; // { dg-error "could not convert 't' from 'S' to 'bool'" } |
| 69 |
return 0; |
| 70 |
} |
| 71 |
|
| 72 |
int z = f6 <S> (); |
| 73 |
|
| 74 |
constexpr int |
| 75 |
f7 (int x, int y, int z, int w) |
| 76 |
{ |
| 77 |
#if __cpp_constexpr >= 201304L |
| 78 |
[[assume (x == 42 && y == 43 && z == 44 && w == 45)]]; // { dg-error "failed 'assume' attribute assumption" "" { target c++14 } } |
| 79 |
#endif // { dg-message "the comparison reduces to '\\\(45 == 44\\\)'" "" { target c++14 } .-1 } |
| 80 |
return x; |
| 81 |
} |
| 82 |
|
| 83 |
constexpr int w = f7 (42, 43, 45, 44); |