Skip to content

Commit 08bb77c

Browse files
authored
Skip explicit template instantiation declarations (#481)
1 parent 8a23240 commit 08bb77c

5 files changed

Lines changed: 83 additions & 1 deletion

File tree

‎cpp2rust/converter/converter.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,10 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {
503503
return true;
504504
}
505505

506+
bool TraverseExplicitInstantiationDecl(clang::ExplicitInstantiationDecl *) {
507+
return true;
508+
}
509+
506510
virtual bool
507511
VisitCXXStdInitializerListExpr(clang::CXXStdInitializerListExpr *expr);
508512

‎cpp2rust/converter/converter_lib.cpp‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,15 @@ bool IsCodeUnitStringLiteral(const clang::StringLiteral *expr) {
265265
bool IsUserDefinedDecl(const clang::Decl *decl) {
266266
const auto &ctx = decl->getASTContext();
267267
const auto &src_mgr = ctx.getSourceManager();
268-
const auto src_loc = decl->getLocation();
268+
auto src_loc = decl->getLocation();
269+
if (auto *spec =
270+
clang::dyn_cast<clang::ClassTemplateSpecializationDecl>(decl);
271+
spec && (spec->getSpecializationKind() ==
272+
clang::TSK_ExplicitInstantiationDeclaration ||
273+
spec->getSpecializationKind() ==
274+
clang::TSK_ExplicitInstantiationDefinition)) {
275+
src_loc = spec->getSpecializedTemplate()->getLocation();
276+
}
269277
return !decl->getBeginLoc().isInvalid() && !decl->isImplicit() &&
270278
!src_mgr.isInSystemHeader(src_loc) &&
271279
!src_mgr.isInSystemMacro(src_loc);

‎tests/unit/class_templates.cpp‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,20 @@ template <typename T> struct Outer {
3939
Inner<int> with(int n) const { return Inner<int>{v, n}; }
4040
};
4141

42+
struct Elem {
43+
int i;
44+
};
45+
46+
template class std::vector<Elem>;
47+
48+
template struct Boxed<short>;
49+
4250
int main() {
51+
std::vector<Elem> ev;
52+
ev.push_back(Elem{2});
53+
assert(ev[0].i == 2);
54+
assert(Boxed<short>::twice(2) == 4);
55+
4356
Outer<int> oi{3};
4457
assert(oi.with(4).sum() == 7);
4558
Outer<long> ol{5};

‎tests/unit/out/refcount/class_templates.rs‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,17 @@ pub struct MyContainer_float_ {
2828
vec_: Value<Vec<f32>>,
2929
}
3030
#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)]
31+
#[byte_size(2)]
32+
pub struct Boxed_short_ {
33+
#[offset(0)]
34+
pub value: i16,
35+
}
36+
impl Boxed_short_ {
37+
pub fn twice(mut v: i16) -> i16 {
38+
return (((v as i32) + (v as i32)) as i16);
39+
}
40+
}
41+
#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)]
3142
#[byte_size(4)]
3243
pub struct Boxed_int_ {
3344
#[offset(0)]
@@ -85,11 +96,24 @@ pub struct Outer_long_ {
8596
#[offset(0)]
8697
pub v: i64,
8798
}
99+
#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)]
100+
#[byte_size(4)]
101+
pub struct Elem {
102+
#[offset(0)]
103+
pub i: i32,
104+
}
88105
pub fn main() {
89106
__cpp2rust_init_globals();
90107
std::process::exit(main_0());
91108
}
92109
fn main_0() -> i32 {
110+
let mut ev: Vec<Elem> = Vec::new();
111+
{
112+
let __a1 = Elem { i: 2 };
113+
ev.push(__a1)
114+
};
115+
assert!(({ ev[0_usize].i } == 2));
116+
assert!(((({ Boxed_short_::twice(2_i16,) }) as i32) == 4));
93117
let oi: Value<Outer_int_> = Rc::new(RefCell::new(Outer_int_ { v: 3 }));
94118
assert!(
95119
(({
@@ -181,6 +205,14 @@ impl Boxed_long_Impl for Ptr<Boxed_long_> {
181205
return ((*self).with(|__s| __s.value) + other);
182206
}
183207
}
208+
pub trait Boxed_short_Impl {
209+
fn plus(&self, other: i16) -> i16;
210+
}
211+
impl Boxed_short_Impl for Ptr<Boxed_short_> {
212+
fn plus(&self, mut other: i16) -> i16 {
213+
return ((((*self).with(|__s| __s.value) as i32) + (other as i32)) as i16);
214+
}
215+
}
184216
pub trait MyContainer_char_Impl {
185217
fn empty(&self) -> bool;
186218
fn size(&self) -> usize;

‎tests/unit/out/unsafe/class_templates.rs‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,19 @@ impl MyContainer_float_ {
8686
}
8787
#[repr(C)]
8888
#[derive(Copy, Clone, VaArg, FnPtrArg, Default)]
89+
pub struct Boxed_short_ {
90+
pub value: i16,
91+
}
92+
impl Boxed_short_ {
93+
pub unsafe fn twice(mut v: i16) -> i16 {
94+
return (((v as i32) + (v as i32)) as i16);
95+
}
96+
pub unsafe fn plus(&self, mut other: i16) -> i16 {
97+
return (((self.value as i32) + (other as i32)) as i16);
98+
}
99+
}
100+
#[repr(C)]
101+
#[derive(Copy, Clone, VaArg, FnPtrArg, Default)]
89102
pub struct Boxed_int_ {
90103
pub value: i32,
91104
}
@@ -163,13 +176,25 @@ impl Outer_long_ {
163176
return Outer_long__Inner_int_ { t: self.v, u: n };
164177
}
165178
}
179+
#[repr(C)]
180+
#[derive(Copy, Clone, VaArg, FnPtrArg, Default)]
181+
pub struct Elem {
182+
pub i: i32,
183+
}
166184
pub fn main() {
167185
unsafe {
168186
__cpp2rust_init_globals();
169187
std::process::exit(main_0() as i32);
170188
}
171189
}
172190
unsafe fn main_0() -> i32 {
191+
let mut ev: Vec<Elem> = Vec::new();
192+
{
193+
let __a1 = Elem { i: 2 };
194+
ev.push(__a1)
195+
};
196+
assert!(((ev[(0_usize)].i) == (2)));
197+
assert!((((unsafe { Boxed_short_::twice(2_i16,) }) as i32) == (4)));
173198
let mut oi: Outer_int_ = Outer_int_ { v: 3 };
174199
assert!(
175200
((unsafe { Outer_int__Inner_int_::sum(&(unsafe { Outer_int_::with(&oi, 4,) }),) }) == (7))

0 commit comments

Comments
 (0)