File: std-align-val-t-in-operator-new.cpp

package info (click to toggle)
llvm-toolchain-13 1%3A13.0.1-11
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,418,840 kB
  • sloc: cpp: 5,290,826; ansic: 996,570; asm: 544,593; python: 188,212; objc: 72,027; lisp: 30,291; f90: 25,395; sh: 24,898; javascript: 9,780; pascal: 9,398; perl: 7,484; ml: 5,432; awk: 3,523; makefile: 2,913; xml: 953; cs: 573; fortran: 539
file content (83 lines) | stat: -rw-r--r-- 3,373 bytes parent folder | download | duplicates (18)
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
// RUN: %clang_cc1 -std=c++11                      -fsyntax-only -verify %s
// RUN: %clang_cc1 -std=c++11 -faligned-allocation -fsyntax-only -verify %s
// RUN: %clang_cc1 -std=c++14                      -fsyntax-only -verify %s
// RUN: %clang_cc1 -std=c++14 -faligned-allocation -fsyntax-only -verify %s
// RUN: %clang_cc1 -std=c++17                      -fsyntax-only -verify %s
// RUN: %clang_cc1 -std=c++17 -faligned-allocation -fsyntax-only -verify %s

namespace std {
typedef __SIZE_TYPE__ size_t;
struct nothrow_t {};
#if __cplusplus >= 201103L
enum class align_val_t : size_t {};
#else
enum align_val_t {
// We can't force an underlying type when targeting windows.
#ifndef _WIN32
  __zero = 0,
  __max = (size_t)-1
#endif
};
#endif
} // namespace std

void *operator new(std::size_t count, std::align_val_t al) __attribute__((alloc_align(2))); // #1

#define OVERALIGNED alignas(__STDCPP_DEFAULT_NEW_ALIGNMENT__ * 2)

struct OVERALIGNED A {
  A();
  int n[128];
};

void *ptr_variable(int align) { return new (std::align_val_t(align)) A; }
void *ptr_align16() { return new (std::align_val_t(16)) A; }
void *ptr_align15() { return new (std::align_val_t(15)) A; } // expected-warning {{requested alignment is not a power of 2}}

struct alignas(128) S {
  S() {}
};

void *alloc_overaligned_struct() {
  return new S;
}

void *alloc_overaligned_struct_with_extra_variable_alignment(int align) {
  return new (std::align_val_t(align)) S;
}
void *alloc_overaligned_struct_with_extra_256_alignment(int align) {
  return new (std::align_val_t(256)) S;
}
void *alloc_overaligned_struct_with_extra_255_alignment(int align) {
  return new (std::align_val_t(255)) S; // expected-warning {{requested alignment is not a power of 2}}
}

std::align_val_t align_variable(int align) { return std::align_val_t(align); }
std::align_val_t align_align16() { return std::align_val_t(16); }
std::align_val_t align_align15() { return std::align_val_t(15); }

struct X {};
void *operator new(std::size_t, X); // #2
void *operator new(std::size_t, std::align_val_t, X); // #3
// FIXME: Consider improving notes 1 and 3 here to say that these are aligned
// allocation functions and the type is not over-aligned.
X *p = new (123) X; // expected-error {{no matching function}}
// expected-note@#1 {{no known conversion from 'int' to 'std::align_val_t' for 2nd argument}}
// expected-note@#2 {{no known conversion from 'int' to 'X' for 2nd argument}}
// expected-note@#3 {{requires 3 arguments}}
// expected-note@* {{requires 1 argument, but 2 were provided}} (builtin)

#ifdef __cpp_aligned_new
struct alignas(__STDCPP_DEFAULT_NEW_ALIGNMENT__ * 2) Y {};
Y *q = new (123) Y; // expected-error {{no matching function}}
// expected-note@#1 {{requires 2 arguments, but 3 were provided}}
// expected-note@#2 {{no known conversion from 'int' to 'X' for 2nd argument}}
// expected-note@#3 {{no known conversion from 'int' to 'X' for 3rd argument}}
// expected-note@* {{requires 1 argument, but 2 were provided}} (builtin)
#endif

X *r = new (std::align_val_t(32), 123) X; // expected-error {{no matching function}}
// expected-note@#1 {{requires 2 arguments, but 3 were provided}}
// expected-note@#2 {{requires 2 arguments, but 3 were provided}}
// expected-note@#3 {{no known conversion from 'int' to 'X' for 3rd argument}}
// expected-note@* {{requires 1 argument, but 3 were provided}} (builtin)