From a4d9ffbd8530735a87b22b278dc33f32692b228e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emilio=20Cobos=20=C3=81lvarez?= Date: Fri, 14 Dec 2018 12:24:14 +0100 Subject: [PATCH 1/2] ir: Put function attribute detection under an opt-in flag. Given it was a considerable performance hit under some workloads. Closes #1465. --- src/ir/function.rs | 4 +- src/lib.rs | 20 +++++++++ src/options.rs | 8 ++++ ...rn_unused_result_no_attribute_detection.rs | 41 +++++++++++++++++++ .../headers/attribute_warn_unused_result.hpp | 2 +- ...n_unused_result_no_attribute_detection.hpp | 10 +++++ 6 files changed, 83 insertions(+), 2 deletions(-) create mode 100644 tests/expectations/tests/attribute_warn_unused_result_no_attribute_detection.rs create mode 100644 tests/headers/attribute_warn_unused_result_no_attribute_detection.hpp diff --git a/src/ir/function.rs b/src/ir/function.rs index f851ad7224..acbfe707ce 100644 --- a/src/ir/function.rs +++ b/src/ir/function.rs @@ -394,7 +394,9 @@ impl FunctionSig { } }; - let must_use = cursor.has_simple_attr("warn_unused_result"); + let must_use = + ctx.options().enable_function_attribute_detection && + cursor.has_simple_attr("warn_unused_result"); let is_method = cursor.kind() == CXCursor_CXXMethod; let is_constructor = cursor.kind() == CXCursor_Constructor; let is_destructor = cursor.kind() == CXCursor_Destructor; diff --git a/src/lib.rs b/src/lib.rs index 28de5889f5..2793182bbe 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -418,6 +418,9 @@ impl Builder { if self.options.enable_cxx_namespaces { output_vector.push("--enable-cxx-namespaces".into()); } + if self.options.enable_function_attribute_detection { + output_vector.push("--enable-function-attribute-detection".into()); + } if self.options.disable_name_namespacing { output_vector.push("--disable-name-namespacing".into()); } @@ -1057,6 +1060,18 @@ impl Builder { self } + /// Enable detecting must_use attributes on C functions. + /// + /// This is quite slow in some cases (see #1465), so it's disabled by + /// default. + /// + /// Note that for this to do something meaningful for now at least, the rust + /// target version has to have support for `#[must_use]`. + pub fn enable_function_attribute_detection(mut self) -> Self { + self.options.enable_function_attribute_detection = true; + self + } + /// Disable name auto-namespacing. /// /// By default, bindgen mangles names like `foo::bar::Baz` to look like @@ -1391,6 +1406,10 @@ struct BindgenOptions { /// generated bindings. enable_cxx_namespaces: bool, + /// True if we should try to find unexposed attributes in functions, in + /// order to be able to generate #[must_use] attributes in Rust. + enable_function_attribute_detection: bool, + /// True if we should avoid mangling names with namespaces. disable_name_namespacing: bool, @@ -1618,6 +1637,7 @@ impl Default for BindgenOptions { derive_partialeq: false, derive_eq: false, enable_cxx_namespaces: false, + enable_function_attribute_detection: false, disable_name_namespacing: false, use_core: false, ctypes_prefix: None, diff --git a/src/options.rs b/src/options.rs index 3594be4e24..9d37543ac3 100644 --- a/src/options.rs +++ b/src/options.rs @@ -315,6 +315,10 @@ where .takes_value(true) .multiple(true) .number_of_values(1), + Arg::with_name("enable-function-attribute-detection") + .long("enable-function-attribute-detection") + .help("Enables detecting unexposed attributes in functions (slow). + Used to generate #[must_use] annotations."), ]) // .args() .get_matches_from(args); @@ -484,6 +488,10 @@ where builder = builder.enable_cxx_namespaces(); } + if matches.is_present("enable-function-attribute-detection") { + builder = builder.enable_function_attribute_detection(); + } + if matches.is_present("disable-name-namespacing") { builder = builder.disable_name_namespacing(); } diff --git a/tests/expectations/tests/attribute_warn_unused_result_no_attribute_detection.rs b/tests/expectations/tests/attribute_warn_unused_result_no_attribute_detection.rs new file mode 100644 index 0000000000..c60b19c6b0 --- /dev/null +++ b/tests/expectations/tests/attribute_warn_unused_result_no_attribute_detection.rs @@ -0,0 +1,41 @@ +/* automatically generated by rust-bindgen */ + +#![allow( + dead_code, + non_snake_case, + non_camel_case_types, + non_upper_case_globals +)] + +#[repr(C)] +#[derive(Debug, Default, Copy, Clone)] +pub struct Foo { + pub _address: u8, +} +#[test] +fn bindgen_test_layout_Foo() { + assert_eq!( + ::std::mem::size_of::(), + 1usize, + concat!("Size of: ", stringify!(Foo)) + ); + assert_eq!( + ::std::mem::align_of::(), + 1usize, + concat!("Alignment of ", stringify!(Foo)) + ); +} +extern "C" { + #[link_name = "\u{1}_ZN3Foo3fooEi"] + pub fn Foo_foo(this: *mut Foo, arg1: ::std::os::raw::c_int) -> ::std::os::raw::c_int; +} +impl Foo { + #[inline] + pub unsafe fn foo(&mut self, arg1: ::std::os::raw::c_int) -> ::std::os::raw::c_int { + Foo_foo(self, arg1) + } +} +extern "C" { + #[link_name = "\u{1}_Z3fooi"] + pub fn foo(arg1: ::std::os::raw::c_int) -> ::std::os::raw::c_int; +} diff --git a/tests/headers/attribute_warn_unused_result.hpp b/tests/headers/attribute_warn_unused_result.hpp index 2155030711..26fda0910c 100644 --- a/tests/headers/attribute_warn_unused_result.hpp +++ b/tests/headers/attribute_warn_unused_result.hpp @@ -1,4 +1,4 @@ -// bindgen-flags: --rust-target 1.27 +// bindgen-flags: --rust-target 1.27 --enable-function-attribute-detection class Foo { public: diff --git a/tests/headers/attribute_warn_unused_result_no_attribute_detection.hpp b/tests/headers/attribute_warn_unused_result_no_attribute_detection.hpp new file mode 100644 index 0000000000..2155030711 --- /dev/null +++ b/tests/headers/attribute_warn_unused_result_no_attribute_detection.hpp @@ -0,0 +1,10 @@ +// bindgen-flags: --rust-target 1.27 + +class Foo { +public: + __attribute__((warn_unused_result)) + int foo(int); +}; + +__attribute__((warn_unused_result)) +int foo(int); From 9ba6d13c4320bd7d225b086957c801811458f2c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emilio=20Cobos=20=C3=81lvarez?= Date: Fri, 14 Dec 2018 12:27:22 +0100 Subject: [PATCH 2/2] Version bump. --- Cargo.lock | 2 +- Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ded2f0b6b5..624c8b26ed 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -47,7 +47,7 @@ dependencies = [ [[package]] name = "bindgen" -version = "0.44.0" +version = "0.45.0" dependencies = [ "bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)", "cexpr 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/Cargo.toml b/Cargo.toml index f1bafdbee5..2a3a4fd5f9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,7 +14,7 @@ readme = "README.md" repository = "https://github.com/rust-lang/rust-bindgen" documentation = "https://docs.rs/bindgen" homepage = "https://rust-lang.github.io/rust-bindgen/" -version = "0.44.0" +version = "0.45.0" build = "build.rs" include = [