summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEmilio Cobos Álvarez <emilio@crisal.io>2018-12-14 13:43:51 +0100
committerGitHub <noreply@github.com>2018-12-14 13:43:51 +0100
commit371e744e4158f23e75adb296433fc684076964ad (patch)
tree9b3bbf2fff95cf96b170420a7346f0775806ca00
parenteb97c1494d5debac81b51847c3cba61496471bfb (diff)
parent9ba6d13c4320bd7d225b086957c801811458f2c7 (diff)
Merge pull request #1467 from emilio/attr-detection-flagv0.45.0
ir: Put function attribute detection under an opt-in flag.
-rw-r--r--Cargo.lock2
-rw-r--r--Cargo.toml2
-rw-r--r--src/ir/function.rs4
-rw-r--r--src/lib.rs20
-rw-r--r--src/options.rs8
-rw-r--r--tests/expectations/tests/attribute_warn_unused_result_no_attribute_detection.rs41
-rw-r--r--tests/headers/attribute_warn_unused_result.hpp2
-rw-r--r--tests/headers/attribute_warn_unused_result_no_attribute_detection.hpp10
8 files changed, 85 insertions, 4 deletions
diff --git a/Cargo.lock b/Cargo.lock
index ded2f0b6..624c8b26 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 f1bafdbe..2a3a4fd5 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 = [
diff --git a/src/ir/function.rs b/src/ir/function.rs
index f851ad72..acbfe707 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 28de5889..2793182b 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 3594be4e..9d37543a 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 00000000..c60b19c6
--- /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::<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;
+}
diff --git a/tests/headers/attribute_warn_unused_result.hpp b/tests/headers/attribute_warn_unused_result.hpp
index 21550307..26fda091 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 00000000..21550307
--- /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);