Skip to content

Commit

Permalink
Generate impl UniquePtr for abstract types too
Browse files Browse the repository at this point in the history
Similarly to non-abstract types, if one `include_cpp!` section
`generate!`s an abstract type but never uses `unique_ptr` for that type,
but another one does, we need the explicit `impl UniquePtr` so that cxx
allows it.
  • Loading branch information
bsilver8192 committed Jul 26, 2022
1 parent b830b9d commit ec7a454
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 7 deletions.
16 changes: 13 additions & 3 deletions engine/src/conversion/codegen_rs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -644,9 +644,7 @@ impl<'a> RsCodeGenerator<'a> {
false,
)
}
Api::ForwardDeclaration { .. }
| Api::ConcreteType { .. }
| Api::OpaqueTypedef { .. } => self.generate_type(
Api::ConcreteType { .. } => self.generate_type(
&name,
id,
TypeKind::Abstract,
Expand All @@ -657,6 +655,17 @@ impl<'a> RsCodeGenerator<'a> {
None,
false,
),
Api::ForwardDeclaration { .. } | Api::OpaqueTypedef { .. } => self.generate_type(
&name,
id,
TypeKind::Abstract,
false, // these types can't be kept in a Vector
false, // these types can't be put in a smart pointer
|| None,
associated_methods,
None,
false,
),
Api::CType { .. } => RsCodegenResult {
extern_c_mod_items: vec![ForeignItem::Verbatim(quote! {
type #id = autocxx::#id;
Expand Down Expand Up @@ -1042,6 +1051,7 @@ impl<'a> RsCodeGenerator<'a> {
extern_c_mod_items: vec![
self.generate_cxxbridge_type(name, false, doc_attrs)
],
bridge_items: create_impl_items(&id, movable, destroyable, self.config),
bindgen_mod_items,
materializations,
..Default::default()
Expand Down
15 changes: 13 additions & 2 deletions integration-tests/tests/builder_modifiers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,19 @@ pub(crate) fn make_cpp17_adder() -> Option<BuilderModifier> {
make_clang_arg_adder(&["-std=c++17"])
}

struct ClangArgAdder(Vec<String>);
struct ClangArgAdder(Vec<String>, Vec<String>);

pub(crate) fn make_clang_arg_adder(args: &[&str]) -> Option<BuilderModifier> {
make_clang_optional_arg_adder(args, &[])
}

pub(crate) fn make_clang_optional_arg_adder(
args: &[&str],
optional_args: &[&str],
) -> Option<BuilderModifier> {
let args: Vec<_> = args.iter().map(|a| a.to_string()).collect();
Some(Box::new(ClangArgAdder(args)))
let optional_args: Vec<_> = optional_args.iter().map(|a| a.to_string()).collect();
Some(Box::new(ClangArgAdder(args, optional_args)))
}

impl BuilderModifierFns for ClangArgAdder {
Expand All @@ -34,6 +42,9 @@ impl BuilderModifierFns for ClangArgAdder {
for f in &self.0 {
builder = builder.flag(f);
}
for f in &self.1 {
builder = builder.flag_if_supported(f);
}
builder
}
}
Expand Down
52 changes: 50 additions & 2 deletions integration-tests/tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@

use crate::{
builder_modifiers::{
make_clang_arg_adder, make_cpp17_adder, EnableAutodiscover, SetSuppressSystemHeaders,
make_clang_arg_adder, make_clang_optional_arg_adder, make_cpp17_adder, EnableAutodiscover,
SetSuppressSystemHeaders,
},
code_checkers::{
make_error_finder, make_rust_code_finder, make_string_finder, CppMatcher,
Expand Down Expand Up @@ -5756,7 +5757,17 @@ fn test_issue_506() {
} // namespace spanner
"};
let rs = quote! {};
run_test("", hdr, rs, &["spanner::Database", "spanner::Row"], &[]);
run_test_ex(
"",
hdr,
rs,
directives_from_lists(&["spanner::Database", "spanner::Row"], &[], None),
// This is normally a valid warning for generating bindings for this code, but we're doing
// it on purpose as a regression test on minimized code so we'll just ignore it.
make_clang_optional_arg_adder(&[], &["-Wno-delete-abstract-non-virtual-dtor"]),
None,
None,
);
}

#[test]
Expand Down Expand Up @@ -9429,6 +9440,43 @@ fn test_abstract_up() {
run_test("", hdr, rs, &["A", "get_a"], &[]);
}

#[test]
fn test_abstract_up_multiple_bridge() {
let hdr = indoc! {"
#include <memory>
class A {
public:
virtual void foo() const = 0;
virtual ~A() {}
};
class B : public A {
public:
void foo() const {}
};
inline std::unique_ptr<A> get_a() { return std::make_unique<B>(); }
"};
let hexathorpe = Token![#](Span::call_site());
let rs = quote! {
autocxx::include_cpp! {
#hexathorpe include "input.h"
safety!(unsafe_ffi)
generate!("A")
}
autocxx::include_cpp! {
#hexathorpe include "input.h"
safety!(unsafe_ffi)
name!(ffi2)
extern_cpp_type!("A", crate::ffi::A)
generate!("get_a")
}
fn main() {
let a = ffi2::get_a();
a.foo();
}
};
do_run_test_manual("", hdr, rs, None, None).unwrap();
}

#[test]
fn test_abstract_private() {
let hdr = indoc! {"
Expand Down

0 comments on commit ec7a454

Please sign in to comment.