Skip to content

Commit

Permalink
Merge pull request #1467 from emilio/attr-detection-flag
Browse files Browse the repository at this point in the history
ir: Put function attribute detection under an opt-in flag.
  • Loading branch information
emilio authored Dec 14, 2018
2 parents eb97c14 + 9ba6d13 commit 371e744
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
4 changes: 3 additions & 1 deletion src/ir/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
20 changes: 20 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,

Expand Down Expand Up @@ -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,
Expand Down
8 changes: 8 additions & 0 deletions src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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::<Foo>(),
1usize,
concat!("Size of: ", stringify!(Foo))
);
assert_eq!(
::std::mem::align_of::<Foo>(),
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;
}
2 changes: 1 addition & 1 deletion tests/headers/attribute_warn_unused_result.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// bindgen-flags: --rust-target 1.27
// bindgen-flags: --rust-target 1.27 --enable-function-attribute-detection

class Foo {
public:
Expand Down
Original file line number Diff line number Diff line change
@@ -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);

0 comments on commit 371e744

Please sign in to comment.