xref: /aosp_15_r20/external/clang/test/SemaCXX/builtin-assume-aligned.cpp (revision 67e74705e28f6214e480b399dd47ea732279e315)
1*67e74705SXin Li // RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 -triple x86_64-linux-gnu %s
2*67e74705SXin Li 
3*67e74705SXin Li int n;
4*67e74705SXin Li constexpr int *p = 0;
5*67e74705SXin Li // expected-error@+1 {{must be initialized by a constant expression}}
6*67e74705SXin Li constexpr int *k = (int *) __builtin_assume_aligned(p, 16, n = 5);
7*67e74705SXin Li 
8*67e74705SXin Li constexpr void *l = __builtin_assume_aligned(p, 16);
9*67e74705SXin Li 
10*67e74705SXin Li // expected-error@+2 {{must be initialized by a constant expression}}
11*67e74705SXin Li // expected-note@+1 {{cast from 'void *' is not allowed in a constant expression}}
12*67e74705SXin Li constexpr int *c = (int *) __builtin_assume_aligned(p, 16);
13*67e74705SXin Li 
14*67e74705SXin Li // expected-error@+2 {{must be initialized by a constant expression}}
15*67e74705SXin Li // expected-note@+1 {{alignment of the base pointee object (4 bytes) is less than the asserted 16 bytes}}
16*67e74705SXin Li constexpr void *m = __builtin_assume_aligned(&n, 16);
17*67e74705SXin Li 
18*67e74705SXin Li // expected-error@+2 {{must be initialized by a constant expression}}
19*67e74705SXin Li // expected-note@+1 {{offset of the aligned pointer from the base pointee object (-2 bytes) is not a multiple of the asserted 4 bytes}}
20*67e74705SXin Li constexpr void *q1 = __builtin_assume_aligned(&n, 4, 2);
21*67e74705SXin Li // expected-error@+2 {{must be initialized by a constant expression}}
22*67e74705SXin Li // expected-note@+1 {{offset of the aligned pointer from the base pointee object (2 bytes) is not a multiple of the asserted 4 bytes}}
23*67e74705SXin Li constexpr void *q2 = __builtin_assume_aligned(&n, 4, -2);
24*67e74705SXin Li constexpr void *q3 = __builtin_assume_aligned(&n, 4, 4);
25*67e74705SXin Li constexpr void *q4 = __builtin_assume_aligned(&n, 4, -4);
26*67e74705SXin Li 
27*67e74705SXin Li static char ar1[6];
28*67e74705SXin Li // expected-error@+2 {{must be initialized by a constant expression}}
29*67e74705SXin Li // expected-note@+1 {{alignment of the base pointee object (1 byte) is less than the asserted 16 bytes}}
30*67e74705SXin Li constexpr void *r1 = __builtin_assume_aligned(&ar1[2], 16);
31*67e74705SXin Li 
32*67e74705SXin Li static char ar2[6] __attribute__((aligned(32)));
33*67e74705SXin Li // expected-error@+2 {{must be initialized by a constant expression}}
34*67e74705SXin Li // expected-note@+1 {{offset of the aligned pointer from the base pointee object (2 bytes) is not a multiple of the asserted 16 bytes}}
35*67e74705SXin Li constexpr void *r2 = __builtin_assume_aligned(&ar2[2], 16);
36*67e74705SXin Li constexpr void *r3 = __builtin_assume_aligned(&ar2[2], 16, 2);
37*67e74705SXin Li // expected-error@+2 {{must be initialized by a constant expression}}
38*67e74705SXin Li // expected-note@+1 {{offset of the aligned pointer from the base pointee object (1 byte) is not a multiple of the asserted 16 bytes}}
39*67e74705SXin Li constexpr void *r4 = __builtin_assume_aligned(&ar2[2], 16, 1);
40*67e74705SXin Li 
41*67e74705SXin Li constexpr int* x = __builtin_constant_p((int*)0xFF) ? (int*)0xFF : (int*)0xFF;
42*67e74705SXin Li // expected-error@+2 {{must be initialized by a constant expression}}
43*67e74705SXin Li // expected-note@+1 {{value of the aligned pointer (255) is not a multiple of the asserted 32 bytes}}
44*67e74705SXin Li constexpr void *s1 = __builtin_assume_aligned(x, 32);
45*67e74705SXin Li // expected-error@+2 {{must be initialized by a constant expression}}
46*67e74705SXin Li // expected-note@+1 {{value of the aligned pointer (250) is not a multiple of the asserted 32 bytes}}
47*67e74705SXin Li constexpr void *s2 = __builtin_assume_aligned(x, 32, 5);
48*67e74705SXin Li constexpr void *s3 = __builtin_assume_aligned(x, 32, -1);
49*67e74705SXin Li 
50