1 // RUN: %clang_cc1 -triple x86_64-apple-macosx10.12.0 -fexceptions -faligned-alloc-unavailable -std=c++1z -verify %s
2 // RUN: %clang_cc1 -triple x86_64-apple-macosx10.12.0 -fexceptions -std=c++1z -verify -DNO_ERRORS %s
3 // RUN: %clang_cc1 -triple x86_64-apple-macosx10.12.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 
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 
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 
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.13 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.13 or newer}}}
122 #endif
123 
124 // expected-note@-20 2 {{if you supply your own aligned allocation functions}}
125 #endif
126 
127 // No errors if user-defined aligned allocation functions are available.
128 void *operator new(std::size_t __sz, std::align_val_t) {
129   static char array[256];
130   return &array;
131 }
132 
133 void operator delete(void *p, std::align_val_t) {
134 }
135 
136 void testOveraligned2() {
137   auto p = new ((std::align_val_t)8) OveralignedS;
138   delete p;
139 }
140