xref: /aosp_15_r20/external/clang/test/SemaCXX/builtin-assume-aligned-tmpl.cpp (revision 67e74705e28f6214e480b399dd47ea732279e315)
1*67e74705SXin Li // RUN: %clang_cc1 -fsyntax-only -verify %s
2*67e74705SXin Li 
3*67e74705SXin Li template<int z>
test9(int * a)4*67e74705SXin Li int test9(int *a) {
5*67e74705SXin Li   a = (int *) __builtin_assume_aligned(a, z + 1); // expected-error {{requested alignment is not a power of 2}}
6*67e74705SXin Li   return a[0];
7*67e74705SXin Li }
8*67e74705SXin Li 
test9i(int * a)9*67e74705SXin Li void test9i(int *a) {
10*67e74705SXin Li   test9<42>(a); // expected-note {{in instantiation of function template specialization 'test9<42>' requested here}}
11*67e74705SXin Li }
12*67e74705SXin Li 
13*67e74705SXin Li template<typename T>
test10(int * a,T z)14*67e74705SXin Li int test10(int *a, T z) {
15*67e74705SXin Li   a = (int *) __builtin_assume_aligned(a, z + 1); // expected-error {{must be a constant integer}}
16*67e74705SXin Li   return a[0];
17*67e74705SXin Li }
18*67e74705SXin Li 
test10i(int * a)19*67e74705SXin Li int test10i(int *a) {
20*67e74705SXin Li   return test10(a, 42); // expected-note {{in instantiation of function template specialization 'test10<int>' requested here}}
21*67e74705SXin Li }
22*67e74705SXin Li 
23*67e74705SXin Li template <int q>
24*67e74705SXin Li void *atest() __attribute__((assume_aligned(q))); // expected-error {{requested alignment is not a power of 2}}
25*67e74705SXin Li 
26*67e74705SXin Li template <int q, int o>
27*67e74705SXin Li void *atest2() __attribute__((assume_aligned(q, o))); // expected-error {{requested alignment is not a power of 2}}
28*67e74705SXin Li 
test20()29*67e74705SXin Li void test20() {
30*67e74705SXin Li   atest<31>(); // expected-note {{in instantiation of function template specialization 'atest<31>' requested here}}
31*67e74705SXin Li   atest<32>();
32*67e74705SXin Li 
33*67e74705SXin Li   atest2<31, 5>(); // expected-note {{in instantiation of function template specialization 'atest2<31, 5>' requested here}}
34*67e74705SXin Li   atest2<32, 4>();
35*67e74705SXin Li }
36*67e74705SXin Li 
37*67e74705SXin Li // expected-error@+1 {{invalid application of 'sizeof' to a function type}}
38*67e74705SXin Li template<typename T> __attribute__((assume_aligned(sizeof(int(T()))))) T *f();
test21()39*67e74705SXin Li void test21() {
40*67e74705SXin Li   void *p = f<void>(); // expected-note {{in instantiation of function template specialization 'f<void>' requested here}}
41*67e74705SXin Li }
42*67e74705SXin Li 
43*67e74705SXin Li // expected-error@+1 {{functional-style cast from 'void' to 'int' is not allowed}}
44*67e74705SXin Li template<typename T> __attribute__((assume_aligned(sizeof((int(T())))))) T *g();
test23()45*67e74705SXin Li void test23() {
46*67e74705SXin Li   void *p = g<void>(); // expected-note {{in instantiation of function template specialization 'g<void>' requested here}}
47*67e74705SXin Li }
48*67e74705SXin Li 
49*67e74705SXin Li template <typename T, int o>
50*67e74705SXin Li T *atest3() __attribute__((assume_aligned(31, o))); // expected-error {{requested alignment is not a power of 2}}
51*67e74705SXin Li 
52*67e74705SXin Li template <typename T, int o>
53*67e74705SXin Li T *atest4() __attribute__((assume_aligned(32, o)));
54*67e74705SXin Li 
test22()55*67e74705SXin Li void test22() {
56*67e74705SXin Li   atest3<int, 5>();
57*67e74705SXin Li   atest4<int, 5>();
58*67e74705SXin Li }
59*67e74705SXin Li 
60*67e74705SXin Li // expected-warning@+1 {{'assume_aligned' attribute only applies to functions and methods}}
61*67e74705SXin Li class __attribute__((assume_aligned(32))) x {
62*67e74705SXin Li   int y;
63*67e74705SXin Li };
64*67e74705SXin Li 
65*67e74705SXin Li // expected-warning@+1 {{'assume_aligned' attribute only applies to return values that are pointers or references}}
66*67e74705SXin Li x foo() __attribute__((assume_aligned(32)));
67*67e74705SXin Li 
68*67e74705SXin Li struct s1 {
69*67e74705SXin Li   static const int x = 32;
70*67e74705SXin Li };
71*67e74705SXin Li 
72*67e74705SXin Li struct s2 {
73*67e74705SXin Li   static const int x = 64;
74*67e74705SXin Li };
75*67e74705SXin Li 
76*67e74705SXin Li struct s3 {
77*67e74705SXin Li   static const int x = 63;
78*67e74705SXin Li };
79*67e74705SXin Li 
80*67e74705SXin Li template <typename X>
81*67e74705SXin Li void *atest5() __attribute__((assume_aligned(X::x))); // expected-error {{requested alignment is not a power of 2}}
test24()82*67e74705SXin Li void test24() {
83*67e74705SXin Li   atest5<s1>();
84*67e74705SXin Li   atest5<s2>();
85*67e74705SXin Li   atest5<s3>(); // expected-note {{in instantiation of function template specialization 'atest5<s3>' requested here}}
86*67e74705SXin Li }
87*67e74705SXin Li 
88