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