Skip to content

Commit 9833b67

Browse files
KarelPeetersemilio
authored andcommitted
Implement must_use_type commandline flag and builder option.
1 parent d1d2eb6 commit 9833b67

File tree

6 files changed

+59
-0
lines changed

6 files changed

+59
-0
lines changed

src/codegen/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2023,6 +2023,10 @@ impl CodeGenerator for CompInfo {
20232023
attributes.push(attributes::derives(&derives))
20242024
}
20252025

2026+
if ctx.must_use_type_by_name(item) {
2027+
attributes.push(attributes::must_use());
2028+
}
2029+
20262030
let mut tokens = if is_union && struct_layout.is_rust_union() {
20272031
quote! {
20282032
#( #attributes )*

src/ir/context.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2663,6 +2663,12 @@ If you encounter an error missing from this list, please file an issue or a PR!"
26632663
let name = item.path_for_allowlisting(self)[1..].join("::");
26642664
self.options().no_hash_types.matches(&name)
26652665
}
2666+
2667+
/// Check if `--must-use-type` flag is enabled for this item.
2668+
pub fn must_use_type_by_name(&self, item: &Item) -> bool {
2669+
let name = item.path_for_allowlisting(self)[1..].join("::");
2670+
self.options().must_use_types.matches(&name)
2671+
}
26662672
}
26672673

26682674
/// A builder struct for configuring item resolution options.

src/lib.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,7 @@ impl Builder {
317317
(&self.options.no_debug_types, "--no-debug"),
318318
(&self.options.no_default_types, "--no-default"),
319319
(&self.options.no_hash_types, "--no-hash"),
320+
(&self.options.must_use_types, "--must-use-type"),
320321
];
321322

322323
for (set, flag) in regex_sets {
@@ -1588,6 +1589,13 @@ impl Builder {
15881589
self
15891590
}
15901591

1592+
/// Add `#[must_use]` for the given type. Regular
1593+
/// expressions are supported.
1594+
pub fn must_use_type<T: Into<String>>(mut self, arg: T) -> Builder {
1595+
self.options.must_use_types.insert(arg.into());
1596+
self
1597+
}
1598+
15911599
/// Set whether `arr[size]` should be treated as `*mut T` or `*mut [T; size]` (same for mut)
15921600
pub fn array_pointers_in_arguments(mut self, doit: bool) -> Self {
15931601
self.options.array_pointers_in_arguments = doit;
@@ -1928,6 +1936,9 @@ struct BindgenOptions {
19281936
/// The set of types that we should not derive `Hash` for.
19291937
no_hash_types: RegexSet,
19301938

1939+
/// The set of types that we should be annotated with `#[must_use]`.
1940+
must_use_types: RegexSet,
1941+
19311942
/// Decide if C arrays should be regular pointers in rust or array pointers
19321943
array_pointers_in_arguments: bool,
19331944

@@ -1986,6 +1997,7 @@ impl BindgenOptions {
19861997
&mut self.no_debug_types,
19871998
&mut self.no_default_types,
19881999
&mut self.no_hash_types,
2000+
&mut self.must_use_types,
19892001
];
19902002
let record_matches = self.record_matches;
19912003
for regex_set in &mut regex_sets {
@@ -2090,6 +2102,7 @@ impl Default for BindgenOptions {
20902102
no_debug_types: Default::default(),
20912103
no_default_types: Default::default(),
20922104
no_hash_types: Default::default(),
2105+
must_use_types: Default::default(),
20932106
array_pointers_in_arguments: false,
20942107
wasm_import_module_name: None,
20952108
dynamic_library_name: None,

src/options.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,13 @@ where
486486
.takes_value(true)
487487
.multiple(true)
488488
.number_of_values(1),
489+
Arg::with_name("must-use-type")
490+
.long("must-use-type")
491+
.help("Add #[must_use] annotation to types matching <regex>.")
492+
.value_name("regex")
493+
.takes_value(true)
494+
.multiple(true)
495+
.number_of_values(1),
489496
Arg::with_name("enable-function-attribute-detection")
490497
.long("enable-function-attribute-detection")
491498
.help(
@@ -942,6 +949,12 @@ where
942949
builder = builder.no_hash(regex);
943950
}
944951
}
952+
953+
if let Some(must_use_type) = matches.values_of("must-use-type") {
954+
for regex in must_use_type {
955+
builder = builder.must_use_type(regex);
956+
}
957+
}
945958

946959
if let Some(dynamic_library_name) = matches.value_of("dynamic-loading") {
947960
builder = builder.dynamic_library_name(dynamic_library_name);
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#![allow(
2+
dead_code,
3+
non_snake_case,
4+
non_camel_case_types,
5+
non_upper_case_globals
6+
)]
7+
8+
#[repr(C)]
9+
#[derive(Debug, Copy, Clone)]
10+
#[must_use]
11+
pub struct MyType {
12+
_unused: [u8; 0],
13+
}
14+
#[repr(C)]
15+
#[derive(Debug, Copy, Clone)]
16+
pub struct OtherType {
17+
_unused: [u8; 0],
18+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// bindgen-flags: --must-use-type MyType
2+
3+
struct MyType;
4+
5+
struct OtherType;

0 commit comments

Comments
 (0)