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