xref: /aosp_15_r20/external/clang/test/Sema/address_spaces.c (revision 67e74705e28f6214e480b399dd47ea732279e315)
1*67e74705SXin Li // RUN: %clang_cc1 %s -fsyntax-only -verify
2*67e74705SXin Li 
3*67e74705SXin Li #define _AS1 __attribute__((address_space(1)))
4*67e74705SXin Li #define _AS2 __attribute__((address_space(2)))
5*67e74705SXin Li #define _AS3 __attribute__((address_space(3)))
6*67e74705SXin Li 
7*67e74705SXin Li void bar(_AS2 int a); // expected-error {{parameter may not be qualified with an address space}}
8*67e74705SXin Li 
foo(_AS3 float * a,_AS1 float b)9*67e74705SXin Li void foo(_AS3 float *a,
10*67e74705SXin Li          _AS1 float b) // expected-error {{parameter may not be qualified with an address space}}
11*67e74705SXin Li {
12*67e74705SXin Li   _AS2 *x;// expected-warning {{type specifier missing, defaults to 'int'}}
13*67e74705SXin Li   _AS1 float * _AS2 *B;
14*67e74705SXin Li 
15*67e74705SXin Li   int _AS1 _AS2 *Y;   // expected-error {{multiple address spaces specified for type}}
16*67e74705SXin Li   int *_AS1 _AS2 *Z;  // expected-error {{multiple address spaces specified for type}}
17*67e74705SXin Li 
18*67e74705SXin Li   _AS1 int local;     // expected-error {{automatic variable qualified with an address space}}
19*67e74705SXin Li   _AS1 int array[5];  // expected-error {{automatic variable qualified with an address space}}
20*67e74705SXin Li   _AS1 int arrarr[5][5]; // expected-error {{automatic variable qualified with an address space}}
21*67e74705SXin Li 
22*67e74705SXin Li   __attribute__((address_space(-1))) int *_boundsA; // expected-error {{address space is negative}}
23*67e74705SXin Li   __attribute__((address_space(0x7FFFFF))) int *_boundsB;
24*67e74705SXin Li   __attribute__((address_space(0x1000000))) int *_boundsC; // expected-error {{address space is larger than the maximum supported}}
25*67e74705SXin Li   // chosen specifically to overflow 32 bits and come out reasonable
26*67e74705SXin Li   __attribute__((address_space(4294967500))) int *_boundsD; // expected-error {{address space is larger than the maximum supported}}
27*67e74705SXin Li 
28*67e74705SXin Li   *a = 5.0f + b;
29*67e74705SXin Li }
30*67e74705SXin Li 
31*67e74705SXin Li struct _st {
32*67e74705SXin Li  int x, y;
33*67e74705SXin Li } s __attribute ((address_space(1))) = {1, 1};
34*67e74705SXin Li 
35*67e74705SXin Li 
36*67e74705SXin Li // rdar://6774906
37*67e74705SXin Li __attribute__((address_space(256))) void * * const base = 0;
get_0(void)38*67e74705SXin Li void * get_0(void) {
39*67e74705SXin Li   return base[0];  // expected-error {{returning '__attribute__((address_space(256))) void *' from a function with result type 'void *' changes address space of pointer}}
40*67e74705SXin Li }
41*67e74705SXin Li 
42*67e74705SXin Li __attribute__((address_space(1))) char test3_array[10];
test3(void)43*67e74705SXin Li void test3(void) {
44*67e74705SXin Li   extern void test3_helper(char *p); // expected-note {{passing argument to parameter 'p' here}}
45*67e74705SXin Li   test3_helper(test3_array); // expected-error {{changes address space of pointer}}
46*67e74705SXin Li }
47*67e74705SXin Li 
48*67e74705SXin Li typedef void ft(void);
49*67e74705SXin Li _AS1 ft qf; // expected-error {{function type may not be qualified with an address space}}
50*67e74705SXin Li typedef _AS1 ft qft; // expected-error {{function type may not be qualified with an address space}}
51*67e74705SXin Li 
52*67e74705SXin Li 
53*67e74705SXin Li typedef _AS2 int AS2Int;
54*67e74705SXin Li 
55*67e74705SXin Li struct HasASFields
56*67e74705SXin Li {
57*67e74705SXin Li   _AS2 int as_field; // expected-error {{field may not be qualified with an address space}}
58*67e74705SXin Li    AS2Int typedef_as_field; // expected-error {{field may not be qualified with an address space}}
59*67e74705SXin Li };
60*67e74705SXin Li 
61*67e74705SXin Li // Assertion failure was when the field was accessed
access_as_field()62*67e74705SXin Li void access_as_field()
63*67e74705SXin Li {
64*67e74705SXin Li     struct HasASFields x;
65*67e74705SXin Li     (void) bar.as_field;
66*67e74705SXin Li }
67*67e74705SXin Li 
68*67e74705SXin Li typedef int PR4997 __attribute__((address_space(Foobar))); // expected-error {{use of undeclared identifier 'Foobar'}}
69*67e74705SXin Li __attribute__((address_space("12"))) int *i; // expected-error {{'address_space' attribute requires an integer constant}}
70*67e74705SXin Li 
71*67e74705SXin Li // Clang extension doesn't forbid operations on pointers to different address spaces.
cmp(_AS1 char * x,_AS2 char * y)72*67e74705SXin Li char* cmp(_AS1 char *x,  _AS2 char *y) {
73*67e74705SXin Li   return x < y ? x : y; // expected-warning {{pointer type mismatch ('__attribute__((address_space(1))) char *' and '__attribute__((address_space(2))) char *')}}
74*67e74705SXin Li }
75