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