Skip to content

Commit 371e744

Browse files
authored
Merge pull request #1467 from emilio/attr-detection-flag
ir: Put function attribute detection under an opt-in flag.
2 parents eb97c14 + 9ba6d13 commit 371e744

8 files changed

+85
-4
lines changed

Cargo.lock

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ readme = "README.md"
1414
repository = "https://github.com/rust-lang/rust-bindgen"
1515
documentation = "https://docs.rs/bindgen"
1616
homepage = "https://rust-lang.github.io/rust-bindgen/"
17-
version = "0.44.0"
17+
version = "0.45.0"
1818
build = "build.rs"
1919

2020
include = [

src/ir/function.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,9 @@ impl FunctionSig {
394394
}
395395
};
396396

397-
let must_use = cursor.has_simple_attr("warn_unused_result");
397+
let must_use =
398+
ctx.options().enable_function_attribute_detection &&
399+
cursor.has_simple_attr("warn_unused_result");
398400
let is_method = cursor.kind() == CXCursor_CXXMethod;
399401
let is_constructor = cursor.kind() == CXCursor_Constructor;
400402
let is_destructor = cursor.kind() == CXCursor_Destructor;

src/lib.rs

+20
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,9 @@ impl Builder {
418418
if self.options.enable_cxx_namespaces {
419419
output_vector.push("--enable-cxx-namespaces".into());
420420
}
421+
if self.options.enable_function_attribute_detection {
422+
output_vector.push("--enable-function-attribute-detection".into());
423+
}
421424
if self.options.disable_name_namespacing {
422425
output_vector.push("--disable-name-namespacing".into());
423426
}
@@ -1057,6 +1060,18 @@ impl Builder {
10571060
self
10581061
}
10591062

1063+
/// Enable detecting must_use attributes on C functions.
1064+
///
1065+
/// This is quite slow in some cases (see #1465), so it's disabled by
1066+
/// default.
1067+
///
1068+
/// Note that for this to do something meaningful for now at least, the rust
1069+
/// target version has to have support for `#[must_use]`.
1070+
pub fn enable_function_attribute_detection(mut self) -> Self {
1071+
self.options.enable_function_attribute_detection = true;
1072+
self
1073+
}
1074+
10601075
/// Disable name auto-namespacing.
10611076
///
10621077
/// By default, bindgen mangles names like `foo::bar::Baz` to look like
@@ -1391,6 +1406,10 @@ struct BindgenOptions {
13911406
/// generated bindings.
13921407
enable_cxx_namespaces: bool,
13931408

1409+
/// True if we should try to find unexposed attributes in functions, in
1410+
/// order to be able to generate #[must_use] attributes in Rust.
1411+
enable_function_attribute_detection: bool,
1412+
13941413
/// True if we should avoid mangling names with namespaces.
13951414
disable_name_namespacing: bool,
13961415

@@ -1618,6 +1637,7 @@ impl Default for BindgenOptions {
16181637
derive_partialeq: false,
16191638
derive_eq: false,
16201639
enable_cxx_namespaces: false,
1640+
enable_function_attribute_detection: false,
16211641
disable_name_namespacing: false,
16221642
use_core: false,
16231643
ctypes_prefix: None,

src/options.rs

+8
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,10 @@ where
315315
.takes_value(true)
316316
.multiple(true)
317317
.number_of_values(1),
318+
Arg::with_name("enable-function-attribute-detection")
319+
.long("enable-function-attribute-detection")
320+
.help("Enables detecting unexposed attributes in functions (slow).
321+
Used to generate #[must_use] annotations."),
318322
]) // .args()
319323
.get_matches_from(args);
320324

@@ -484,6 +488,10 @@ where
484488
builder = builder.enable_cxx_namespaces();
485489
}
486490

491+
if matches.is_present("enable-function-attribute-detection") {
492+
builder = builder.enable_function_attribute_detection();
493+
}
494+
487495
if matches.is_present("disable-name-namespacing") {
488496
builder = builder.disable_name_namespacing();
489497
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/* automatically generated by rust-bindgen */
2+
3+
#![allow(
4+
dead_code,
5+
non_snake_case,
6+
non_camel_case_types,
7+
non_upper_case_globals
8+
)]
9+
10+
#[repr(C)]
11+
#[derive(Debug, Default, Copy, Clone)]
12+
pub struct Foo {
13+
pub _address: u8,
14+
}
15+
#[test]
16+
fn bindgen_test_layout_Foo() {
17+
assert_eq!(
18+
::std::mem::size_of::<Foo>(),
19+
1usize,
20+
concat!("Size of: ", stringify!(Foo))
21+
);
22+
assert_eq!(
23+
::std::mem::align_of::<Foo>(),
24+
1usize,
25+
concat!("Alignment of ", stringify!(Foo))
26+
);
27+
}
28+
extern "C" {
29+
#[link_name = "\u{1}_ZN3Foo3fooEi"]
30+
pub fn Foo_foo(this: *mut Foo, arg1: ::std::os::raw::c_int) -> ::std::os::raw::c_int;
31+
}
32+
impl Foo {
33+
#[inline]
34+
pub unsafe fn foo(&mut self, arg1: ::std::os::raw::c_int) -> ::std::os::raw::c_int {
35+
Foo_foo(self, arg1)
36+
}
37+
}
38+
extern "C" {
39+
#[link_name = "\u{1}_Z3fooi"]
40+
pub fn foo(arg1: ::std::os::raw::c_int) -> ::std::os::raw::c_int;
41+
}

tests/headers/attribute_warn_unused_result.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// bindgen-flags: --rust-target 1.27
1+
// bindgen-flags: --rust-target 1.27 --enable-function-attribute-detection
22

33
class Foo {
44
public:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// bindgen-flags: --rust-target 1.27
2+
3+
class Foo {
4+
public:
5+
__attribute__((warn_unused_result))
6+
int foo(int);
7+
};
8+
9+
__attribute__((warn_unused_result))
10+
int foo(int);

0 commit comments

Comments
 (0)