Skip to content

Commit 1917cb2

Browse files
committed
Add --no-partialeq <regex> flag
Related to #965. - [x] Add a new RegexSet member to bindgen::Builder (similar to the whitelisted_types set). - [x] A Builder method to add strings to that RegexSet. - [x] Plumbing in src/options.rs to convert --no-partialeq <regex> CLI flags into invocations of the builder method. - [x] Make the MonotoneFramework::constrain function in src/ir/analysis/derive_partialeq.rs check if the given item is explicitly marked not to be Partialeq, and if so, insert it into the self.cannot_derive_partialeq set via return self.insert(id). - [] Tests! - [] When the no-partialeq type is transitively referenced by a whitelisted item - [] When the no-partialeq type is explicitly whitelisted - [] When the no-partialeq type is marked opaque
1 parent da942c4 commit 1917cb2

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

src/ir/analysis/derive_partial_eq.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use ir::comp::FieldMethods;
77
use ir::context::{BindgenContext, ItemId};
88
use ir::derive::CanTriviallyDerivePartialEq;
99
use ir::item::IsOpaque;
10+
use ir::item::ItemCanonicalName;
1011
use ir::traversal::EdgeKind;
1112
use ir::ty::RUST_DERIVE_IN_ARRAY_LIMIT;
1213
use ir::ty::TypeKind;
@@ -144,6 +145,11 @@ impl<'ctx> MonotoneFramework for CannotDerivePartialEq<'ctx> {
144145
};
145146
}
146147

148+
let name = item.canonical_name(self.ctx);
149+
if self.ctx.options().no_partialeq_types.matches(&name) {
150+
return self.insert(id)
151+
}
152+
147153
if ty.layout(self.ctx).map_or(false, |l| {
148154
l.align > RUST_DERIVE_IN_ARRAY_LIMIT
149155
})

src/lib.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,20 @@ impl Builder {
496496
output_vector.push(path.into());
497497
}
498498

499+
self.options
500+
.no_partialeq_types
501+
.get_items()
502+
.iter()
503+
.map(|item| {
504+
output_vector.push("--no-partialeq".into());
505+
output_vector.push(
506+
item.trim_left_matches("^")
507+
.trim_right_matches("$")
508+
.into(),
509+
);
510+
})
511+
.count();
512+
499513
output_vector
500514
}
501515

@@ -1095,6 +1109,13 @@ impl Builder {
10951109
))
10961110
}
10971111
}
1112+
1113+
/// Don't derive `PartialEq` for a given type. Regular
1114+
/// expressions are supported.
1115+
pub fn no_partialeq(mut self, arg: String) -> Builder {
1116+
self.options.no_partialeq_types.insert(arg);
1117+
self
1118+
}
10981119
}
10991120

11001121
/// Configuration options for generated bindings.
@@ -1265,7 +1286,11 @@ struct BindgenOptions {
12651286

12661287
/// The absolute path to the rustfmt configuration file, if None, the standard rustfmt
12671288
/// options are used.
1289+
12681290
rustfmt_configuration_file: Option<PathBuf>,
1291+
1292+
/// The set of types that we should not derive `PartialEq` for.
1293+
no_partialeq_types: RegexSet,
12691294
}
12701295

12711296
/// TODO(emilio): This is sort of a lie (see the error message that results from
@@ -1350,6 +1375,7 @@ impl Default for BindgenOptions {
13501375
time_phases: false,
13511376
rustfmt_bindings: false,
13521377
rustfmt_configuration_file: None,
1378+
no_partialeq_types: Default::default(),
13531379
}
13541380
}
13551381
}

src/options.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,13 @@ where
262262
.takes_value(true)
263263
.multiple(false)
264264
.number_of_values(1),
265+
Arg::with_name("no-partialeq")
266+
.long("no-partialeq")
267+
.help("Avoid deriving PartialEq for types matching <regex>.")
268+
.value_name("regex")
269+
.takes_value(true)
270+
.multiple(true)
271+
.number_of_values(1),
265272
]) // .args()
266273
.get_matches_from(args);
267274

@@ -530,6 +537,12 @@ where
530537
builder = builder.rustfmt_configuration_file(Some(path));
531538
}
532539

540+
if let Some(no_partialeq) = matches.values_of("no-partialeq") {
541+
for regex in no_partialeq {
542+
builder = builder.no_partialeq(String::from(regex));
543+
}
544+
}
545+
533546
let verbose = matches.is_present("verbose");
534547

535548
Ok((builder, output, verbose))

0 commit comments

Comments
 (0)