xref: /llvm-project/clang/test/Modules/implicit-declared-allocation-functions.cppm (revision cb2289f39240a0fdccc9a853a02ae9751578a0fd)
1*cb2289f3SChuanqi Xu// Tests that the implicit declared allocation functions
2*cb2289f3SChuanqi Xu// are attached to the global module fragment.
3*cb2289f3SChuanqi Xu// RUN: rm -rf %t
4*cb2289f3SChuanqi Xu// RUN: mkdir -p %t
5*cb2289f3SChuanqi Xu// RUN: split-file %s %t
6*cb2289f3SChuanqi Xu//
7*cb2289f3SChuanqi Xu// RUN: %clang_cc1 -std=c++20 %t/foo.cppm -fsyntax-only -verify
8*cb2289f3SChuanqi Xu// RUN: %clang_cc1 -std=c++20 %t/foo2.cppm -fsyntax-only -verify
9*cb2289f3SChuanqi Xu
10*cb2289f3SChuanqi Xu//--- foo.cppm
11*cb2289f3SChuanqi Xuexport module foo;
12*cb2289f3SChuanqi Xuexport void alloc_wrapper() {
13*cb2289f3SChuanqi Xu  void *a = ::operator new(32);
14*cb2289f3SChuanqi Xu  // [basic.stc.dynamic.general]Note2
15*cb2289f3SChuanqi Xu  //   The implicit declarations do not introduce the names std, std::size_­t,
16*cb2289f3SChuanqi Xu  //   std::align_­val_­t, ..., However, referring to std or std::size_­t or
17*cb2289f3SChuanqi Xu  //   std::align_­val_­t is ill-formed unless a standard library declaration
18*cb2289f3SChuanqi Xu  //   ([cstddef.syn], [new.syn], [std.modules]) of that name precedes
19*cb2289f3SChuanqi Xu  //   ([basic.lookup.general]) the use of that name.
20*cb2289f3SChuanqi Xu  void *b = ::operator new((std::size_t)32); // expected-error {{use of undeclared identifier 'std'}}
21*cb2289f3SChuanqi Xu  void *c = ::operator new((std::size_t)32, // expected-error {{use of undeclared identifier 'std'}}
22*cb2289f3SChuanqi Xu                           (std::align_val_t)64); // expected-error {{use of undeclared identifier 'std'}}
23*cb2289f3SChuanqi Xu
24*cb2289f3SChuanqi Xu  ::operator delete(a);
25*cb2289f3SChuanqi Xu  ::operator delete(b, (std::size_t)32); // expected-error {{use of undeclared identifier 'std'}}
26*cb2289f3SChuanqi Xu  ::operator delete(c, (std::size_t)32,  // expected-error {{use of undeclared identifier 'std'}}
27*cb2289f3SChuanqi Xu                       (std::align_val_t)64); // expected-error {{use of undeclared identifier 'std'}}
28*cb2289f3SChuanqi Xu}
29*cb2289f3SChuanqi Xu
30*cb2289f3SChuanqi Xu//--- new
31*cb2289f3SChuanqi Xunamespace std {
32*cb2289f3SChuanqi Xu  using size_t = decltype(sizeof(0));
33*cb2289f3SChuanqi Xu  enum class align_val_t : size_t {};
34*cb2289f3SChuanqi Xu}
35*cb2289f3SChuanqi Xu
36*cb2289f3SChuanqi Xu[[nodiscard]] void *operator new(std::size_t);
37*cb2289f3SChuanqi Xu[[nodiscard]] void *operator new(std::size_t, std::align_val_t);
38*cb2289f3SChuanqi Xu[[nodiscard]] void *operator new[](std::size_t);
39*cb2289f3SChuanqi Xu[[nodiscard]] void *operator new[](std::size_t, std::align_val_t);
40*cb2289f3SChuanqi Xuvoid operator delete(void*) noexcept;
41*cb2289f3SChuanqi Xuvoid operator delete(void*, std::size_t) noexcept;
42*cb2289f3SChuanqi Xuvoid operator delete(void*, std::align_val_t) noexcept;
43*cb2289f3SChuanqi Xuvoid operator delete(void*, std::size_t, std::align_val_t) noexcept;
44*cb2289f3SChuanqi Xuvoid operator delete[](void*, std::size_t, std::align_val_t) noexcept;
45*cb2289f3SChuanqi Xuvoid operator delete[](void*, std::size_t) noexcept;
46*cb2289f3SChuanqi Xuvoid operator delete[](void*, std::align_val_t) noexcept;
47*cb2289f3SChuanqi Xuvoid operator delete[](void*, std::size_t, std::align_val_t) noexcept;
48*cb2289f3SChuanqi Xu
49*cb2289f3SChuanqi Xu//--- foo2.cppm
50*cb2289f3SChuanqi Xu// expected-no-diagnostics
51*cb2289f3SChuanqi Xumodule;
52*cb2289f3SChuanqi Xu#include "new"
53*cb2289f3SChuanqi Xuexport module foo2;
54*cb2289f3SChuanqi Xuexport void alloc_wrapper() {
55*cb2289f3SChuanqi Xu  void *a = ::operator new(32);
56*cb2289f3SChuanqi Xu  void *b = ::operator new((std::size_t)32);
57*cb2289f3SChuanqi Xu  void *c = ::operator new((std::size_t)32,
58*cb2289f3SChuanqi Xu                           (std::align_val_t)64);
59*cb2289f3SChuanqi Xu
60*cb2289f3SChuanqi Xu  ::operator delete(a);
61*cb2289f3SChuanqi Xu  ::operator delete(b, (std::size_t)32);
62*cb2289f3SChuanqi Xu  ::operator delete(c, (std::size_t)32,
63*cb2289f3SChuanqi Xu                       (std::align_val_t)64);
64*cb2289f3SChuanqi Xu}
65