Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve #962 - implement --no-copy with tests #1099

Merged
merged 2 commits into from
Oct 26, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/ir/analysis/derive_copy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,10 @@ impl<'ctx> MonotoneFramework for CannotDeriveCopy<'ctx> {
}
};

if self.ctx.no_copy_by_name(&item) {
return self.insert(id);
}

if item.is_opaque(self.ctx, &()) {
let layout_can_derive = ty.layout(self.ctx).map_or(true, |l| {
l.opaque().can_trivially_derive_copy()
Expand Down
8 changes: 8 additions & 0 deletions src/ir/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2455,8 +2455,16 @@ impl BindgenContext {
/// Check if `--no-partialeq` flag is enabled for this item.
pub fn no_partialeq_by_name(&self, item: &Item) -> bool {
let name = item.canonical_path(self)[1..].join("::");

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: no need for this new line.

self.options().no_partialeq_types.matches(&name)
}

/// Check if `--no-copy` flag is enabled for this item.
pub fn no_copy_by_name(&self, item: &Item) -> bool {
let name = item.canonical_path(self)[1..].join("::");

self.options().no_copy_types.matches(&name)
}
}

/// A builder struct for configuring item resolution options.
Expand Down
26 changes: 26 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,20 @@ impl Builder {
})
.count();

self.options
.no_copy_types
.get_items()
.iter()
.map(|item| {
output_vector.push("--no-copy".into());
output_vector.push(
item.trim_left_matches("^")
.trim_right_matches("$")
.into(),
);
})
.count();

output_vector
}

Expand Down Expand Up @@ -1158,6 +1172,13 @@ impl Builder {
self.options.no_partialeq_types.insert(arg);
self
}

/// Don't derive `Copy` for a given type. Regular
/// expressions are supported.
pub fn no_copy(mut self, arg: String) -> Self {
self.options.no_copy_types.insert(arg);
self
}
}

/// Configuration options for generated bindings.
Expand Down Expand Up @@ -1345,6 +1366,9 @@ struct BindgenOptions {

/// The set of types that we should not derive `PartialEq` for.
no_partialeq_types: RegexSet,

/// The set of types that we should not derive `Copy` for.
no_copy_types: RegexSet,
}

/// TODO(emilio): This is sort of a lie (see the error message that results from
Expand All @@ -1363,6 +1387,7 @@ impl BindgenOptions {
self.constified_enum_modules.build();
self.rustified_enums.build();
self.no_partialeq_types.build();
self.no_copy_types.build();
}

/// Update rust target version
Expand Down Expand Up @@ -1434,6 +1459,7 @@ impl Default for BindgenOptions {
rustfmt_bindings: true,
rustfmt_configuration_file: None,
no_partialeq_types: Default::default(),
no_copy_types: Default::default(),
}
}
}
Expand Down
13 changes: 13 additions & 0 deletions src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,13 @@ where
.takes_value(true)
.multiple(true)
.number_of_values(1),
Arg::with_name("no-copy")
.long("no-copy")
.help("Avoid deriving Copy for types matching <regex>.")
.value_name("regex")
.takes_value(true)
.multiple(true)
.number_of_values(1),
]) // .args()
.get_matches_from(args);

Expand Down Expand Up @@ -579,6 +586,12 @@ where
}
}

if let Some(no_partialeq) = matches.values_of("no-copy") {
for regex in no_partialeq {
builder = builder.no_copy(String::from(regex));
}
}

let verbose = matches.is_present("verbose");

Ok((builder, output, verbose))
Expand Down
24 changes: 24 additions & 0 deletions tests/expectations/tests/no_copy_opaque.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/* automatically generated by rust-bindgen */


#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]


#[repr(C)]
#[derive(Debug, Default)]
pub struct NoCopy {
pub _bindgen_opaque_blob: u32,
}
#[test]
fn bindgen_test_layout_NoCopy() {
assert_eq!(
::std::mem::size_of::<NoCopy>(),
4usize,
concat!("Size of: ", stringify!(NoCopy))
);
assert_eq!(
::std::mem::align_of::<NoCopy>(),
4usize,
concat!("Alignment of ", stringify!(NoCopy))
);
}
34 changes: 34 additions & 0 deletions tests/expectations/tests/no_copy_whitelisted.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/* automatically generated by rust-bindgen */


#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]


#[repr(C)]
#[derive(Debug, Default)]
pub struct NoCopy {
pub i: ::std::os::raw::c_int,
}
#[test]
fn bindgen_test_layout_NoCopy() {
assert_eq!(
::std::mem::size_of::<NoCopy>(),
4usize,
concat!("Size of: ", stringify!(NoCopy))
);
assert_eq!(
::std::mem::align_of::<NoCopy>(),
4usize,
concat!("Alignment of ", stringify!(NoCopy))
);
assert_eq!(
unsafe { &(*(0 as *const NoCopy)).i as *const _ as usize },
0usize,
concat!(
"Alignment of field: ",
stringify!(NoCopy),
"::",
stringify!(i)
)
);
}
52 changes: 52 additions & 0 deletions tests/expectations/tests/whitelisted_item_references_no_copy.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/* automatically generated by rust-bindgen */


#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]


#[repr(C)]
#[derive(Debug, Default)]
pub struct NoCopy {
pub _address: u8,
}
#[test]
fn bindgen_test_layout_NoCopy() {
assert_eq!(
::std::mem::size_of::<NoCopy>(),
1usize,
concat!("Size of: ", stringify!(NoCopy))
);
assert_eq!(
::std::mem::align_of::<NoCopy>(),
1usize,
concat!("Alignment of ", stringify!(NoCopy))
);
}
#[repr(C)]
#[derive(Debug, Default)]
pub struct WhitelistMe {
pub a: NoCopy,
}
#[test]
fn bindgen_test_layout_WhitelistMe() {
assert_eq!(
::std::mem::size_of::<WhitelistMe>(),
1usize,
concat!("Size of: ", stringify!(WhitelistMe))
);
assert_eq!(
::std::mem::align_of::<WhitelistMe>(),
1usize,
concat!("Alignment of ", stringify!(WhitelistMe))
);
assert_eq!(
unsafe { &(*(0 as *const WhitelistMe)).a as *const _ as usize },
0usize,
concat!(
"Alignment of field: ",
stringify!(WhitelistMe),
"::",
stringify!(a)
)
);
}
5 changes: 5 additions & 0 deletions tests/headers/no_copy_opaque.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// bindgen-flags: --opaque-type "NoCopy" --no-copy "NoCopy"

class NoCopy {
int i;
};
5 changes: 5 additions & 0 deletions tests/headers/no_copy_whitelisted.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// bindgen-flags: --whitelist-type "NoCopy" --no-copy "NoCopy"

class NoCopy {
int i;
};
7 changes: 7 additions & 0 deletions tests/headers/whitelisted_item_references_no_copy.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// bindgen-flags: --whitelist-type "WhitelistMe" --no-copy "NoCopy"

struct NoCopy {};

class WhitelistMe {
NoCopy a;
};