Skip to content

Commit 710f7e4

Browse files
authored
Merge pull request #62225 from apple/egorzhdan/cxx-operator-new-irgen
[cxx-interop] Emit IR for custom `operator new`
2 parents c225fc4 + c307ccc commit 710f7e4

File tree

4 files changed

+38
-0
lines changed

4 files changed

+38
-0
lines changed

lib/IRGen/GenClangDecl.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,11 @@ class ClangDeclFinder
8282
return true;
8383
}
8484

85+
bool VisitCXXNewExpr(clang::CXXNewExpr *NE) {
86+
callback(NE->getOperatorNew());
87+
return true;
88+
}
89+
8590
// Do not traverse unevaluated expressions. Doing to might result in compile
8691
// errors if we try to instantiate an un-instantiatable template.
8792

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#ifndef TEST_INTEROP_CXX_CLASS_CUSTOM_NEW_OPERATOR_H
2+
#define TEST_INTEROP_CXX_CLASS_CUSTOM_NEW_OPERATOR_H
3+
4+
#include <cstddef>
5+
#include <cstdint>
6+
7+
struct container_new_t {};
8+
9+
inline void *operator new(size_t, void *p, container_new_t) { return p; }
10+
11+
struct MakeMe {
12+
int x;
13+
};
14+
15+
inline MakeMe *callsCustomNew() {
16+
char buffer[8];
17+
return new (buffer, container_new_t()) MakeMe;
18+
}
19+
20+
#endif // TEST_INTEROP_CXX_CLASS_CUSTOM_NEW_OPERATOR_H

test/Interop/Cxx/class/Inputs/module.modulemap

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ module ConstructorsObjC {
1818
requires cplusplus
1919
}
2020

21+
module CustomNewOperator {
22+
header "custom-new-operator.h"
23+
requires cplusplus
24+
}
25+
2126
module Destructors {
2227
header "destructors.h"
2328
requires cplusplus
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// RUN: %target-swiftxx-frontend -I %S/Inputs %s -emit-ir | %FileCheck %s
2+
3+
import CustomNewOperator
4+
5+
var x = callsCustomNew()
6+
7+
// Make sure the definition of `operator new` is emitted.
8+
// CHECK: define {{.*}} @{{_ZnwmPv15container_new_t|"\?\?2@YAPEAX_KPEAXUcontainer_new_t@@@Z"}}

0 commit comments

Comments
 (0)