summaryrefslogtreecommitdiffstats
path: root/test/SemaCXX/unavailable_aligned_allocation.cpp
blob: 2f0f8fe7a4b5043769074be14bfa7c2f5b20b33c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// RUN: %clang_cc1 -triple x86_64-apple-macosx10.13.0 -fexceptions -faligned-alloc-unavailable -std=c++1z -verify %s
// RUN: %clang_cc1 -triple x86_64-apple-macosx10.13.0 -fexceptions -std=c++1z -verify -DNO_ERRORS %s
// RUN: %clang_cc1 -triple x86_64-apple-macosx10.13.0 -fexceptions -faligned-allocation -faligned-alloc-unavailable -std=c++14 -verify %s
// RUN: %clang_cc1 -triple arm64-apple-ios10.0.0 -fexceptions -faligned-alloc-unavailable -std=c++1z -verify -DIOS %s
// RUN: %clang_cc1 -triple arm64-apple-ios10.0.0 -fexceptions -std=c++1z -verify -DNO_ERRORS %s
// RUN: %clang_cc1 -triple arm64-apple-tvos10.0.0 -fexceptions -faligned-alloc-unavailable -std=c++1z -verify -DTVOS %s
// RUN: %clang_cc1 -triple arm64-apple-tvos10.0.0 -fexceptions -std=c++1z -verify -DNO_ERRORS %s
// RUN: %clang_cc1 -triple armv7k-apple-watchos3.0.0 -fexceptions -faligned-alloc-unavailable -std=c++1z -verify -DWATCHOS %s
// RUN: %clang_cc1 -triple armv7k-apple-watchos3.0.0 -fexceptions -std=c++1z -verify -DNO_ERRORS %s

namespace std {
  typedef decltype(sizeof(0)) size_t;
  enum class align_val_t : std::size_t {};
  struct nothrow_t {};
  nothrow_t nothrow;
}

void *operator new(std::size_t __sz, const std::nothrow_t&) noexcept;
void *operator new[](std::size_t __sz, const std::nothrow_t&) noexcept;

void *operator new(std::size_t __sz, std::align_val_t, const std::nothrow_t&) noexcept;
void *operator new[](std::size_t __sz, std::align_val_t, const std::nothrow_t&) noexcept;
void operator delete(void *, std::align_val_t, const std::nothrow_t&);
void operator delete[](void *, std::align_val_t, const std::nothrow_t&);
void operator delete(void*, std::size_t, std::align_val_t) noexcept;
void operator delete[](void*, std::size_t, std::align_val_t) noexcept;

void *operator new(std::size_t, std::align_val_t, long long);

struct alignas(256) OveralignedS {
  int x[16];
};

struct S {
  int x[16];
};

void test() {
  auto *p = new S;
  delete p;
  p = new (std::nothrow) S;

  auto *pa = new S[4];
  delete[] pa;
  pa = new (std::nothrow) S[4];
}

void testOveraligned() {
  auto *p = new OveralignedS;
  p = new ((std::align_val_t)8) OveralignedS;
  delete p;
  p = new (std::nothrow) OveralignedS;

  auto *pa = new OveralignedS[4];
  pa = new ((std::align_val_t)8) OveralignedS[4];
  delete[] pa;
  pa = new (std::nothrow) OveralignedS[4];
  // No error here since it is not calling a replaceable allocation function.
  p = new ((std::align_val_t)8, 10LL) OveralignedS;
}

#ifdef NO_ERRORS
// expected-no-diagnostics
#else
// expected-error@-16 {{aligned allocation function of type 'void *(unsigned long, enum std::align_val_t)' is only available on}}
// expected-note@-17 {{if you supply your own aligned allocation functions}}
// expected-error@-18 {{aligned deallocation function of type 'void (void *, enum std::align_val_t) noexcept' is only available on}}
// expected-note@-19 {{if you supply your own aligned allocation functions}}

// expected-error@-20 {{aligned allocation function of type 'void *(unsigned long, enum std::align_val_t)' is only available on}}
// expected-note@-21 {{if you supply your own aligned allocation functions}}
// expected-error@-22 {{aligned deallocation function of type 'void (void *, enum std::align_val_t) noexcept' is only available on}}
// expected-note@-23 {{if you supply your own aligned allocation functions}}

// expected-error@-24 {{aligned deallocation function of type 'void (void *, enum std::align_val_t) noexcept' is only available on}}
// expected-note@-25 {{if you supply your own aligned allocation functions}}

// 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}}
// expected-note@-27 {{if you supply your own aligned allocation functions}}
// expected-error@-28 {{aligned deallocation function of type 'void (void *, std::align_val_t, const std::nothrow_t &) noexcept' is only available on}}
// expected-note@-29 {{if you supply your own aligned allocation functions}}

// expected-error@-29 {{aligned allocation function of type 'void *(unsigned long, enum std::align_val_t)' is only available on}}
// expected-note@-30 {{if you supply your own aligned allocation functions}}
// expected-error@-31 {{aligned deallocation function of type 'void (void *, enum std::align_val_t) noexcept' is only available on}}
// expected-note@-32 {{if you supply your own aligned allocation functions}}

// expected-error@-33 {{aligned allocation function of type 'void *(unsigned long, enum std::align_val_t)' is only available on}}
// expected-note@-34 {{if you supply your own aligned allocation functions}}
// expected-error@-35 {{aligned deallocation function of type 'void (void *, enum std::align_val_t) noexcept' is only available on}}
// expected-note@-36 {{if you supply your own aligned allocation functions}}

// expected-error@-37 {{aligned deallocation function of type 'void (void *, enum std::align_val_t) noexcept' is only available on}}
// expected-note@-38 {{if you supply your own aligned allocation functions}}

// 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}}
// expected-note@-40 {{if you supply your own aligned allocation functions}}
// expected-error@-41 {{aligned deallocation function of type 'void (void *, std::align_val_t, const std::nothrow_t &) noexcept' is only available on}}
// expected-note@-42 {{if you supply your own aligned allocation functions}}

#endif

void testOveralignedCheckOS() {
  auto *p = new OveralignedS;
}

#ifdef NO_ERRORS
// expected-no-diagnostics
#else
#if defined(IOS)
// expected-error@-7 {{aligned allocation function of type 'void *(unsigned long, enum std::align_val_t)' is only available on iOS 11 or newer}}
// expected-error@-8 {{aligned deallocation function of type 'void (void *, enum std::align_val_t) noexcept' is only available on iOS 11 or newer}}}
#elif defined(TVOS)
// expected-error@-10 {{aligned allocation function of type 'void *(unsigned long, enum std::align_val_t)' is only available on tvOS 11 or newer}}}
// expected-error@-11 {{aligned deallocation function of type 'void (void *, enum std::align_val_t) noexcept' is only available on tvOS 11 or newer}}}
#elif defined(WATCHOS)
// expected-error@-13 {{aligned allocation function of type 'void *(unsigned long, enum std::align_val_t)' is only available on watchOS 4 or newer}}}
// expected-error@-14 {{aligned deallocation function of type 'void (void *, enum std::align_val_t) noexcept' is only available on watchOS 4 or newer}}}
#else
// 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}}}
// 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}}}
#endif

// expected-note@-20 2 {{if you supply your own aligned allocation functions}}
#endif

// Test that diagnostics are produced when an unavailable aligned deallocation
// function is called from a deleting destructor.
struct alignas(256) OveralignedS2 {
  int a[4];
  virtual ~OveralignedS2();
};

OveralignedS2::~OveralignedS2() {}

#ifdef NO_ERRORS
// expected-no-diagnostics
#else
#if defined(IOS)
// expected-error@-6 {{aligned deallocation function of type 'void (void *, enum std::align_val_t) noexcept' is only available on iOS 11 or newer}}}
// expected-note@-7 {{if you supply your own aligned allocation functions}}
#elif defined(TVOS)
// expected-error@-9 {{aligned deallocation function of type 'void (void *, enum std::align_val_t) noexcept' is only available on tvOS 11 or newer}}}
// expected-note@-10 {{if you supply your own aligned allocation functions}}
#elif defined(WATCHOS)
// expected-error@-12 {{aligned deallocation function of type 'void (void *, enum std::align_val_t) noexcept' is only available on watchOS 4 or newer}}}
// expected-note@-13 {{if you supply your own aligned allocation functions}}
#else
// 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}}}
// expected-note@-16 {{if you supply your own aligned allocation functions}}
#endif
#endif

void testExplicitOperatorNewDelete() {
  void *p = operator new(128);
  operator delete(p);
  p = operator new[](128);
  operator delete[](p);
  p = __builtin_operator_new(128);
  __builtin_operator_delete(p);
}

void testExplicitOperatorNewDeleteOveraligned() {
  void *p = operator new(128, (std::align_val_t)64);
  operator delete(p, (std::align_val_t)64);
  p = operator new[](128, (std::align_val_t)64);
  operator delete[](p, (std::align_val_t)64);
  p = __builtin_operator_new(128, (std::align_val_t)64);
  __builtin_operator_delete(p, (std::align_val_t)64);
}

#ifdef NO_ERRORS
// expected-no-diagnostics
#else
// expected-error@-11 {{aligned allocation function of type 'void *(unsigned long, enum std::align_val_t)' is only available on}}
// expected-note@-12 {{if you supply your own aligned allocation functions}}

// expected-error@-13 {{aligned deallocation function of type 'void (void *, enum std::align_val_t) noexcept' is only available on}}
// expected-note@-14 {{if you supply your own aligned allocation functions}}

// expected-error@-15 {{aligned allocation function of type 'void *(unsigned long, enum std::align_val_t)' is only available on}}
// expected-note@-16 {{if you supply your own aligned allocation functions}}

// expected-error@-17 {{aligned deallocation function of type 'void (void *, enum std::align_val_t) noexcept' is only available on}}
// expected-note@-18 {{if you supply your own aligned allocation functions}}

// expected-error@-19 {{aligned allocation function of type 'void *(unsigned long, enum std::align_val_t)' is only available on}}
// expected-note@-20 {{if you supply your own aligned allocation functions}}

// expected-error@-21 {{aligned deallocation function of type 'void (void *, enum std::align_val_t) noexcept' is only available on}}
// expected-note@-22 {{if you supply your own aligned allocation functions}}
#endif

// No errors if user-defined aligned allocation functions are available.
void *operator new(std::size_t __sz, std::align_val_t) {
  static char array[256];
  return &array;
}

void operator delete(void *p, std::align_val_t) {
}

void testOveraligned2() {
  auto p = new ((std::align_val_t)8) OveralignedS;
  delete p;
}