1 // RUN: %clang_cc1 -triple x86_64-apple-macosx10.13.0 -fexceptions -faligned-alloc-unavailable -std=c++1z -verify -DMACOS %s
2 // RUN: %clang_cc1 -triple x86_64-apple-macosx10.13.0 -fexceptions -std=c++1z -verify -DNO_ERRORS %s
3 // RUN: %clang_cc1 -triple x86_64-apple-macosx10.13.0 -fexceptions -faligned-allocation -faligned-alloc-unavailable -std=c++14 -verify -DMACOS %s
4 // RUN: %clang_cc1 -triple arm64-apple-ios10.0.0 -fexceptions -faligned-alloc-unavailable -std=c++1z -verify -DIOS %s
5 // RUN: %clang_cc1 -triple arm64-apple-ios10.0.0 -fexceptions -std=c++1z -verify -DNO_ERRORS %s
6 // RUN: %clang_cc1 -triple arm64-apple-tvos10.0.0 -fexceptions -faligned-alloc-unavailable -std=c++1z -verify -DTVOS %s
7 // RUN: %clang_cc1 -triple arm64-apple-tvos10.0.0 -fexceptions -std=c++1z -verify -DNO_ERRORS %s
8 // RUN: %clang_cc1 -triple armv7k-apple-watchos3.0.0 -fexceptions -faligned-alloc-unavailable -std=c++1z -verify -DWATCHOS %s
9 // RUN: %clang_cc1 -triple armv7k-apple-watchos3.0.0 -fexceptions -std=c++1z -verify -DNO_ERRORS %s
10 // RUN: %clang_cc1 -triple s390x-none-zos -fexceptions -faligned-alloc-unavailable -std=c++1z -verify -DZOS %s
11 // RUN: %clang_cc1 -triple s390x-none-zos -fexceptions -std=c++1z -verify -DNO_ERRORS %s
12 // RUN: %clang_cc1 -triple s390x-none-zos -fexceptions -faligned-allocation -faligned-alloc-unavailable -std=c++14 -verify -DZOS %s
13
14 namespace std {
15 typedef decltype(sizeof(0)) size_t;
16 enum class align_val_t : std::size_t {};
17 struct nothrow_t {};
18 nothrow_t nothrow;
19 }
20
21 void *operator new(std::size_t __sz, const std::nothrow_t&) noexcept;
22 void *operator new[](std::size_t __sz, const std::nothrow_t&) noexcept;
23
24 void *operator new(std::size_t __sz, std::align_val_t, const std::nothrow_t&) noexcept;
25 void *operator new[](std::size_t __sz, std::align_val_t, const std::nothrow_t&) noexcept;
26 void operator delete(void *, std::align_val_t, const std::nothrow_t&);
27 void operator delete[](void *, std::align_val_t, const std::nothrow_t&);
28 void operator delete(void*, std::size_t, std::align_val_t) noexcept;
29 void operator delete[](void*, std::size_t, std::align_val_t) noexcept;
30
31 void *operator new(std::size_t, std::align_val_t, long long);
32
33 struct alignas(256) OveralignedS {
34 int x[16];
35 };
36
37 struct S {
38 int x[16];
39 };
40
test()41 void test() {
42 auto *p = new S;
43 delete p;
44 p = new (std::nothrow) S;
45
46 auto *pa = new S[4];
47 delete[] pa;
48 pa = new (std::nothrow) S[4];
49 }
50
testOveraligned()51 void testOveraligned() {
52 auto *p = new OveralignedS;
53 p = new ((std::align_val_t)8) OveralignedS;
54 delete p;
55 p = new (std::nothrow) OveralignedS;
56
57 auto *pa = new OveralignedS[4];
58 pa = new ((std::align_val_t)8) OveralignedS[4];
59 delete[] pa;
60 pa = new (std::nothrow) OveralignedS[4];
61 // No error here since it is not calling a replaceable allocation function.
62 p = new ((std::align_val_t)8, 10LL) OveralignedS;
63 }
64
65 #ifdef NO_ERRORS
66 // expected-no-diagnostics
67 #else
68 // expected-error-re@-16 {{aligned allocation function of type 'void *(unsigned long, enum std::align_val_t)' is {{only|not}} available on}}
69 // expected-note@-17 {{if you supply your own aligned allocation functions}}
70 // expected-error-re@-18 {{aligned deallocation function of type 'void (void *, enum std::align_val_t) noexcept' is {{only|not}} available on}}
71 // expected-note@-19 {{if you supply your own aligned allocation functions}}
72
73 // expected-error-re@-20 {{aligned allocation function of type 'void *(unsigned long, enum std::align_val_t)' is {{only|not}} available on}}
74 // expected-note@-21 {{if you supply your own aligned allocation functions}}
75 // expected-error-re@-22 {{aligned deallocation function of type 'void (void *, enum std::align_val_t) noexcept' is {{only|not}} available on}}
76 // expected-note@-23 {{if you supply your own aligned allocation functions}}
77
78 // expected-error-re@-24 {{aligned deallocation function of type 'void (void *, enum std::align_val_t) noexcept' is {{only|not}} available on}}
79 // expected-note@-25 {{if you supply your own aligned allocation functions}}
80
81 // expected-error-re@-26 {{aligned allocation function of type 'void *(std::size_t, std::align_val_t, const std::nothrow_t &) noexcept' is {{only|not}} available on}}
82 // expected-note@-27 {{if you supply your own aligned allocation functions}}
83 // expected-error-re@-28 {{aligned deallocation function of type 'void (void *, std::align_val_t, const std::nothrow_t &) noexcept' is {{only|not}} available on}}
84 // expected-note@-29 {{if you supply your own aligned allocation functions}}
85
86 // expected-error-re@-29 {{aligned allocation function of type 'void *(unsigned long, enum std::align_val_t)' is {{only|not}} available on}}
87 // expected-note@-30 {{if you supply your own aligned allocation functions}}
88 // expected-error-re@-31 {{aligned deallocation function of type 'void (void *, enum std::align_val_t) noexcept' is {{only|not}} available on}}
89 // expected-note@-32 {{if you supply your own aligned allocation functions}}
90
91 // expected-error-re@-33 {{aligned allocation function of type 'void *(unsigned long, enum std::align_val_t)' is {{only|not}} available on}}
92 // expected-note@-34 {{if you supply your own aligned allocation functions}}
93 // expected-error-re@-35 {{aligned deallocation function of type 'void (void *, enum std::align_val_t) noexcept' is {{only|not}} available on}}
94 // expected-note@-36 {{if you supply your own aligned allocation functions}}
95
96 // expected-error-re@-37 {{aligned deallocation function of type 'void (void *, enum std::align_val_t) noexcept' is {{only|not}} available on}}
97 // expected-note@-38 {{if you supply your own aligned allocation functions}}
98
99 // expected-error-re@-39 {{aligned allocation function of type 'void *(std::size_t, std::align_val_t, const std::nothrow_t &) noexcept' is {{only|not}} available on}}
100 // expected-note@-40 {{if you supply your own aligned allocation functions}}
101 // expected-error-re@-41 {{aligned deallocation function of type 'void (void *, std::align_val_t, const std::nothrow_t &) noexcept' is {{only|not}} available on}}
102 // expected-note@-42 {{if you supply your own aligned allocation functions}}
103
104 #endif
105
testOveralignedCheckOS()106 void testOveralignedCheckOS() {
107 auto *p = new OveralignedS;
108 }
109
110 #ifdef NO_ERRORS
111 // expected-no-diagnostics
112 #else
113 #if defined(IOS)
114 // expected-error@-7 {{aligned allocation function of type 'void *(unsigned long, enum std::align_val_t)' is only available on iOS 11 or newer}}
115 // expected-error@-8 {{aligned deallocation function of type 'void (void *, enum std::align_val_t) noexcept' is only available on iOS 11 or newer}}}
116 #elif defined(TVOS)
117 // expected-error@-10 {{aligned allocation function of type 'void *(unsigned long, enum std::align_val_t)' is only available on tvOS 11 or newer}}}
118 // expected-error@-11 {{aligned deallocation function of type 'void (void *, enum std::align_val_t) noexcept' is only available on tvOS 11 or newer}}}
119 #elif defined(WATCHOS)
120 // expected-error@-13 {{aligned allocation function of type 'void *(unsigned long, enum std::align_val_t)' is only available on watchOS 4 or newer}}}
121 // expected-error@-14 {{aligned deallocation function of type 'void (void *, enum std::align_val_t) noexcept' is only available on watchOS 4 or newer}}}
122 #elif defined(MACOS)
123 // expected-error@-16 {{aligned allocation function of type 'void *(unsigned long, enum std::align_val_t)' is only available on macOS 10.14 or newer}}}
124 // expected-error@-17 {{aligned deallocation function of type 'void (void *, enum std::align_val_t) noexcept' is only available on macOS 10.14 or newer}}}
125 #elif defined(ZOS)
126 // expected-error@-19 {{aligned allocation function of type 'void *(unsigned long, enum std::align_val_t)' is not available on z/OS}}}
127 // expected-error@-20 {{aligned deallocation function of type 'void (void *, enum std::align_val_t) noexcept' is not available on z/OS}}}
128 #endif
129
130 // expected-note@-23 2 {{if you supply your own aligned allocation functions}}
131 #endif
132
133 // Test that diagnostics are produced when an unavailable aligned deallocation
134 // function is called from a deleting destructor.
135 struct alignas(256) OveralignedS2 {
136 int a[4];
137 virtual ~OveralignedS2();
138 };
139
~OveralignedS2()140 OveralignedS2::~OveralignedS2() {}
141
142 #ifdef NO_ERRORS
143 // expected-no-diagnostics
144 #else
145 #if defined(IOS)
146 // expected-error@-6 {{aligned deallocation function of type 'void (void *, enum std::align_val_t) noexcept' is only available on iOS 11 or newer}}}
147 // expected-note@-7 {{if you supply your own aligned allocation functions}}
148 #elif defined(TVOS)
149 // expected-error@-9 {{aligned deallocation function of type 'void (void *, enum std::align_val_t) noexcept' is only available on tvOS 11 or newer}}}
150 // expected-note@-10 {{if you supply your own aligned allocation functions}}
151 #elif defined(WATCHOS)
152 // expected-error@-12 {{aligned deallocation function of type 'void (void *, enum std::align_val_t) noexcept' is only available on watchOS 4 or newer}}}
153 // expected-note@-13 {{if you supply your own aligned allocation functions}}
154 #elif defined(MACOS)
155 // expected-error@-15 {{aligned deallocation function of type 'void (void *, enum std::align_val_t) noexcept' is only available on macOS 10.14 or newer}}}
156 // expected-note@-16 {{if you supply your own aligned allocation functions}}
157 #elif defined(ZOS)
158 // expected-error@-18 {{aligned deallocation function of type 'void (void *, enum std::align_val_t) noexcept' is not available on z/OS}}}
159 // expected-note@-19 {{if you supply your own aligned allocation functions}}
160 #endif
161 #endif
162
testExplicitOperatorNewDelete()163 void testExplicitOperatorNewDelete() {
164 void *p = operator new(128);
165 operator delete(p);
166 p = operator new[](128);
167 operator delete[](p);
168 p = __builtin_operator_new(128);
169 __builtin_operator_delete(p);
170 }
171
testExplicitOperatorNewDeleteOveraligned()172 void testExplicitOperatorNewDeleteOveraligned() {
173 void *p = operator new(128, (std::align_val_t)64);
174 operator delete(p, (std::align_val_t)64);
175 p = operator new[](128, (std::align_val_t)64);
176 operator delete[](p, (std::align_val_t)64);
177 p = __builtin_operator_new(128, (std::align_val_t)64);
178 __builtin_operator_delete(p, (std::align_val_t)64);
179 }
180
181 #ifdef NO_ERRORS
182 // expected-no-diagnostics
183 #else
184 // expected-error-re@-11 {{aligned allocation function of type 'void *(unsigned long, enum std::align_val_t)' is {{only|not}} available on}}
185 // expected-note@-12 {{if you supply your own aligned allocation functions}}
186
187 // expected-error-re@-13 {{aligned deallocation function of type 'void (void *, enum std::align_val_t) noexcept' is {{only|not}} available on}}
188 // expected-note@-14 {{if you supply your own aligned allocation functions}}
189
190 // expected-error-re@-15 {{aligned allocation function of type 'void *(unsigned long, enum std::align_val_t)' is {{only|not}} available on}}
191 // expected-note@-16 {{if you supply your own aligned allocation functions}}
192
193 // expected-error-re@-17 {{aligned deallocation function of type 'void (void *, enum std::align_val_t) noexcept' is {{only|not}} available on}}
194 // expected-note@-18 {{if you supply your own aligned allocation functions}}
195
196 // expected-error-re@-19 {{aligned allocation function of type 'void *(unsigned long, enum std::align_val_t)' is {{only|not}} available on}}
197 // expected-note@-20 {{if you supply your own aligned allocation functions}}
198
199 // expected-error-re@-21 {{aligned deallocation function of type 'void (void *, enum std::align_val_t) noexcept' is {{only|not}} available on}}
200 // expected-note@-22 {{if you supply your own aligned allocation functions}}
201 #endif
202
203 // No errors if user-defined aligned allocation functions are available.
operator new(std::size_t __sz,std::align_val_t)204 void *operator new(std::size_t __sz, std::align_val_t) {
205 static char array[256];
206 return &array;
207 }
208
operator delete(void * p,std::align_val_t)209 void operator delete(void *p, std::align_val_t) {
210 }
211
testOveraligned2()212 void testOveraligned2() {
213 auto p = new ((std::align_val_t)8) OveralignedS;
214 delete p;
215 }
216