| 1 |
// RUN: %clang_cc1 -fsyntax-only -verify %s |
| 2 |
|
| 3 |
template<int z> |
| 4 |
int test9(int *a) { |
| 5 |
a = (int *) __builtin_assume_aligned(a, z + 1); // expected-error {{requested alignment is not a power of 2}} |
| 6 |
return a[0]; |
| 7 |
} |
| 8 |
|
| 9 |
void test9i(int *a) { |
| 10 |
test9<42>(a); // expected-note {{in instantiation of function template specialization 'test9<42>' requested here}} |
| 11 |
} |
| 12 |
|
| 13 |
template<typename T> |
| 14 |
int test10(int *a, T z) { |
| 15 |
a = (int *) __builtin_assume_aligned(a, z + 1); // expected-error {{must be a constant integer}} |
| 16 |
return a[0]; |
| 17 |
} |
| 18 |
|
| 19 |
int test10i(int *a) { |
| 20 |
return test10(a, 42); // expected-note {{in instantiation of function template specialization 'test10<int>' requested here}} |
| 21 |
} |
| 22 |
|
| 23 |
template <int q> |
| 24 |
void *atest() __attribute__((assume_aligned(q))); // expected-error {{requested alignment is not a power of 2}} |
| 25 |
|
| 26 |
template <int q, int o> |
| 27 |
void *atest2() __attribute__((assume_aligned(q, o))); // expected-error {{requested alignment is not a power of 2}} |
| 28 |
|
| 29 |
void test20() { |
| 30 |
atest<31>(); // expected-note {{in instantiation of function template specialization 'atest<31>' requested here}} |
| 31 |
atest<32>(); |
| 32 |
|
| 33 |
atest2<31, 5>(); // expected-note {{in instantiation of function template specialization 'atest2<31, 5>' requested here}} |
| 34 |
atest2<32, 4>(); |
| 35 |
} |
| 36 |
|
| 37 |
// expected-error@+1 {{invalid application of 'sizeof' to a function type}} |
| 38 |
template<typename T> __attribute__((assume_aligned(sizeof(int(T()))))) T *f(); |
| 39 |
void test21() { |
| 40 |
void *p = f<void>(); // expected-note {{in instantiation of function template specialization 'f<void>' requested here}} |
| 41 |
} |
| 42 |
|
| 43 |
// expected-error@+1 {{functional-style cast from 'void' to 'int' is not allowed}} |
| 44 |
template<typename T> __attribute__((assume_aligned(sizeof((int(T())))))) T *g(); |
| 45 |
void test23() { |
| 46 |
void *p = g<void>(); // expected-note {{in instantiation of function template specialization 'g<void>' requested here}} |
| 47 |
} |
| 48 |
|
| 49 |
template <typename T, int o> |
| 50 |
T *atest3() __attribute__((assume_aligned(31, o))); // expected-error {{requested alignment is not a power of 2}} |
| 51 |
|
| 52 |
template <typename T, int o> |
| 53 |
T *atest4() __attribute__((assume_aligned(32, o))); |
| 54 |
|
| 55 |
template<typename T> |
| 56 |
T atest5(int) __attribute__((assume_aligned(2))); |
| 57 |
|
| 58 |
// expected-warning@+1 {{'assume_aligned' attribute only applies to return values that are pointers or references}} |
| 59 |
int atest6(int) __attribute__((assume_aligned(2))); |
| 60 |
|
| 61 |
void test22() { |
| 62 |
atest3<int, 5>(); |
| 63 |
atest4<int, 5>(); |
| 64 |
} |
| 65 |
|
| 66 |
// expected-warning@+1 {{'assume_aligned' attribute only applies to Objective-C methods and functions}} |
| 67 |
class __attribute__((assume_aligned(32))) x { |
| 68 |
int y; |
| 69 |
}; |
| 70 |
|
| 71 |
// expected-warning@+1 {{'assume_aligned' attribute only applies to return values that are pointers or references}} |
| 72 |
x foo() __attribute__((assume_aligned(32))); |
| 73 |
|
| 74 |
struct s1 { |
| 75 |
static const int x = 32; |
| 76 |
}; |
| 77 |
|
| 78 |
struct s2 { |
| 79 |
static const int x = 64; |
| 80 |
}; |
| 81 |
|
| 82 |
struct s3 { |
| 83 |
static const int x = 63; |
| 84 |
}; |
| 85 |
|
| 86 |
template <typename X> |
| 87 |
void *atest5() __attribute__((assume_aligned(X::x))); // expected-error {{requested alignment is not a power of 2}} |
| 88 |
void test24() { |
| 89 |
atest5<s1>(); |
| 90 |
atest5<s2>(); |
| 91 |
atest5<s3>(); // expected-note {{in instantiation of function template specialization 'atest5<s3>' requested here}} |
| 92 |
} |
| 93 |
|
| 94 |
namespace GH26612 { |
| 95 |
// This issue was about the align_value attribute, but assume_aligned has the |
| 96 |
// same problematic code pattern, so is being fixed at the same time despite |
| 97 |
// not having the same crashing behavior. |
| 98 |
template <class T> |
| 99 |
__attribute__((assume_aligned(4))) T f(T x); // expected-warning {{'assume_aligned' attribute only applies to return values that are pointers or references}} |
| 100 |
|
| 101 |
void foo() { |
| 102 |
f<int>(0); // expected-note {{in instantiation of function template specialization 'GH26612::f<int>' requested here}} |
| 103 |
} |
| 104 |
} // namespace GH26612 |