1 // RUN: %clang_cc1 -std=c++11                      -fsyntax-only -verify %s
2 // RUN: %clang_cc1 -std=c++11 -faligned-allocation -fsyntax-only -verify %s
3 // RUN: %clang_cc1 -std=c++14                      -fsyntax-only -verify %s
4 // RUN: %clang_cc1 -std=c++14 -faligned-allocation -fsyntax-only -verify %s
5 // RUN: %clang_cc1 -std=c++17                      -fsyntax-only -verify %s
6 // RUN: %clang_cc1 -std=c++17 -faligned-allocation -fsyntax-only -verify %s
7 
8 namespace std {
9 typedef __SIZE_TYPE__ size_t;
10 struct nothrow_t {};
11 #if __cplusplus >= 201103L
12 enum class align_val_t : size_t {};
13 #else
14 enum align_val_t {
15 // We can't force an underlying type when targeting windows.
16 #ifndef _WIN32
17   __zero = 0,
18   __max = (size_t)-1
19 #endif
20 };
21 #endif
22 } // namespace std
23 
24 void *operator new(std::size_t count, std::align_val_t al) __attribute__((alloc_align(2))); // #1
25 
26 #define OVERALIGNED alignas(__STDCPP_DEFAULT_NEW_ALIGNMENT__ * 2)
27 
28 struct OVERALIGNED A {
29   A();
30   int n[128];
31 };
32 
ptr_variable(int align)33 void *ptr_variable(int align) { return new (std::align_val_t(align)) A; }
ptr_align16()34 void *ptr_align16() { return new (std::align_val_t(16)) A; }
ptr_align15()35 void *ptr_align15() { return new (std::align_val_t(15)) A; } // expected-warning {{requested alignment is not a power of 2}}
36 
37 struct alignas(128) S {
SS38   S() {}
39 };
40 
alloc_overaligned_struct()41 void *alloc_overaligned_struct() {
42   return new S;
43 }
44 
alloc_overaligned_struct_with_extra_variable_alignment(int align)45 void *alloc_overaligned_struct_with_extra_variable_alignment(int align) {
46   return new (std::align_val_t(align)) S;
47 }
alloc_overaligned_struct_with_extra_256_alignment(int align)48 void *alloc_overaligned_struct_with_extra_256_alignment(int align) {
49   return new (std::align_val_t(256)) S;
50 }
alloc_overaligned_struct_with_extra_255_alignment(int align)51 void *alloc_overaligned_struct_with_extra_255_alignment(int align) {
52   return new (std::align_val_t(255)) S; // expected-warning {{requested alignment is not a power of 2}}
53 }
54 
align_variable(int align)55 std::align_val_t align_variable(int align) { return std::align_val_t(align); }
align_align16()56 std::align_val_t align_align16() { return std::align_val_t(16); }
align_align15()57 std::align_val_t align_align15() { return std::align_val_t(15); }
58 
59 struct X {};
60 void *operator new(std::size_t, X); // #2
61 void *operator new(std::size_t, std::align_val_t, X); // #3
62 // FIXME: Consider improving notes 1 and 3 here to say that these are aligned
63 // allocation functions and the type is not over-aligned.
64 X *p = new (123) X; // expected-error {{no matching function}}
65 // expected-note@#1 {{no known conversion from 'int' to 'std::align_val_t' for 2nd argument}}
66 // expected-note@#2 {{no known conversion from 'int' to 'X' for 2nd argument}}
67 // expected-note@#3 {{requires 3 arguments}}
68 // expected-note@* {{requires 1 argument, but 2 were provided}} (builtin)
69 
70 #ifdef __cpp_aligned_new
71 struct alignas(__STDCPP_DEFAULT_NEW_ALIGNMENT__ * 2) Y {};
72 Y *q = new (123) Y; // expected-error {{no matching function}}
73 // expected-note@#1 {{requires 2 arguments, but 3 were provided}}
74 // expected-note@#2 {{no known conversion from 'int' to 'X' for 2nd argument}}
75 // expected-note@#3 {{no known conversion from 'int' to 'X' for 3rd argument}}
76 // expected-note@* {{requires 1 argument, but 2 were provided}} (builtin)
77 #endif
78 
79 X *r = new (std::align_val_t(32), 123) X; // expected-error {{no matching function}}
80 // expected-note@#1 {{requires 2 arguments, but 3 were provided}}
81 // expected-note@#2 {{requires 2 arguments, but 3 were provided}}
82 // expected-note@#3 {{no known conversion from 'int' to 'X' for 3rd argument}}
83 // expected-note@* {{requires 1 argument, but 3 were provided}} (builtin)
84