[clang] Implement lifetime analysis for lifetime_capture_by(X) (#115921)
[llvm-project.git] / clang / test / Modules / placement-new-reachable.cpp
blob2440294704742bce871f0e9cfc09208654fc4d0b
1 // RUN: rm -rf %t
2 // RUN: mkdir -p %t
3 // RUN: split-file %s %t
4 //
5 // RUN: %clang_cc1 -std=c++20 %t/A.cppm -emit-module-interface -o %t/A.pcm
6 // RUN: %clang_cc1 -std=c++20 %t/Use.cpp -fprebuilt-module-path=%t -fsyntax-only -verify
8 // RUN: %clang_cc1 -std=c++20 %t/A.cppm -emit-reduced-module-interface -o %t/A.pcm
9 // RUN: %clang_cc1 -std=c++20 %t/Use.cpp -fprebuilt-module-path=%t -fsyntax-only -verify
11 //--- placement.h
12 namespace std {
13 using size_t = decltype(sizeof(0));
15 void *operator new(std::size_t, void *p) { return p; }
17 //--- A.cppm
18 module;
19 #include "placement.h"
20 export module A;
21 export template<class T>
22 struct A {
23 A(void *p) : ptr(new (p) T(43)) {}
24 private:
25 void *ptr;
28 export struct B {
29 B(void *p) : ptr(new (p) int(43)) {}
30 private:
31 void *ptr;
34 // The use of operator new in the current module unit is only in the non-inline
35 // function definitions. So it may be optimized out.
36 using ::operator new;
38 //--- Use.cpp
39 // expected-no-diagnostics
40 import A;
41 void bar(int *);
42 void foo(void *ptr) {
43 A<int> a(nullptr); // Good. It should be OK to construct A.
44 B b(nullptr);
45 void *p = ::operator new(sizeof(int), ptr); // Bad. The placement allocation in module A is not visible.
46 void *q = new (ptr) int(43); // Good. We don't call the placement allocation function directly.