Skip to content

Commit 91e0f3a

Browse files
committed
move user input fields to BindgenOptions
1 parent 5e9435f commit 91e0f3a

File tree

7 files changed

+443
-278
lines changed

7 files changed

+443
-278
lines changed

src/codegen/mod.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ use self::dyngen::DynamicItems;
1515
use self::helpers::attributes;
1616
use self::struct_layout::StructLayoutTracker;
1717

18-
use super::BindgenState;
19-
2018
use crate::ir::analysis::{HasVtable, Sizedness};
2119
use crate::ir::annotations::FieldAccessorKind;
2220
use crate::ir::comment;
@@ -47,7 +45,7 @@ use crate::ir::var::Var;
4745
use proc_macro2::{self, Ident, Span};
4846
use quote::TokenStreamExt;
4947

50-
use crate::{Entry, HashMap, HashSet};
48+
use crate::{BindgenOptions, Entry, HashMap, HashSet};
5149
use std::borrow::Cow;
5250
use std::cell::Cell;
5351
use std::collections::VecDeque;
@@ -890,11 +888,11 @@ impl CodeGenerator for Type {
890888
quote! {}
891889
};
892890

893-
let alias_style = if ctx.options().type_alias.matches(&name) {
891+
let alias_style = if ctx.state().type_alias.matches(&name) {
894892
AliasVariation::TypeAlias
895-
} else if ctx.options().new_type_alias.matches(&name) {
893+
} else if ctx.state().new_type_alias.matches(&name) {
896894
AliasVariation::NewType
897-
} else if ctx.options().new_type_alias_deref.matches(&name) {
895+
} else if ctx.state().new_type_alias_deref.matches(&name) {
898896
AliasVariation::NewTypeDeref
899897
} else {
900898
ctx.options().default_alias_style
@@ -2073,7 +2071,7 @@ impl CodeGenerator for CompInfo {
20732071
// The custom derives callback may return a list of derive attributes;
20742072
// add them to the end of the list.
20752073
let custom_derives;
2076-
if let Some(cb) = &ctx.options().parse_callbacks {
2074+
if let Some(cb) = &ctx.state().parse_callbacks {
20772075
custom_derives = cb.add_derives(&canonical_name);
20782076
// In most cases this will be a no-op, since custom_derives will be empty.
20792077
derives.extend(custom_derives.iter().map(|s| s.as_str()));
@@ -3089,7 +3087,7 @@ impl CodeGenerator for Enum {
30893087
// The custom derives callback may return a list of derive attributes;
30903088
// add them to the end of the list.
30913089
let custom_derives;
3092-
if let Some(cb) = &ctx.options().parse_callbacks {
3090+
if let Some(cb) = &ctx.state().parse_callbacks {
30933091
custom_derives = cb.add_derives(&name);
30943092
// In most cases this will be a no-op, since custom_derives will be empty.
30953093
derives.extend(custom_derives.iter().map(|s| s.as_str()));
@@ -4330,7 +4328,7 @@ impl CodeGenerator for ObjCInterface {
43304328

43314329
pub(crate) fn codegen(
43324330
context: BindgenContext,
4333-
) -> (Vec<proc_macro2::TokenStream>, BindgenState, Vec<String>) {
4331+
) -> (Vec<proc_macro2::TokenStream>, BindgenOptions, Vec<String>) {
43344332
context.gen(|context| {
43354333
let _t = context.timer("codegen");
43364334
let counter = Cell::new(0);

src/deps.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/// Generating build depfiles from parsed bindings.
22
use std::{collections::BTreeSet, path::PathBuf};
33

4-
#[derive(Debug)]
4+
#[derive(Debug, Clone)]
55
pub(crate) struct DepfileSpec {
66
pub output_module: String,
77
pub depfile_path: PathBuf,

src/ir/context.rs

Lines changed: 42 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use super::ty::{FloatKind, Type, TypeKind};
2222
use crate::callbacks::ParseCallbacks;
2323
use crate::clang::{self, Cursor};
2424
use crate::parse::ClangItemParser;
25-
use crate::BindgenState;
25+
use crate::{BindgenOptions, BindgenState};
2626
use crate::{Entry, HashMap, HashSet};
2727
use cexpr;
2828
use clang_sys;
@@ -371,7 +371,9 @@ pub struct BindgenContext {
371371
target_info: clang::TargetInfo,
372372

373373
/// The options given by the user via cli or other medium.
374-
options: BindgenState,
374+
options: BindgenOptions,
375+
376+
state: BindgenState,
375377

376378
/// Whether a bindgen complex was generated
377379
generated_bindgen_complex: Cell<bool>,
@@ -505,7 +507,7 @@ impl<'ctx> AllowlistedItemsTraversal<'ctx> {
505507

506508
impl BindgenContext {
507509
/// Construct the context for the given `options`.
508-
pub(crate) fn new(options: BindgenState) -> Self {
510+
pub(crate) fn new(state: BindgenState, options: BindgenOptions) -> Self {
509511
// TODO(emilio): Use the CXTargetInfo here when available.
510512
//
511513
// see: https://reviews.llvm.org/D32389
@@ -522,7 +524,7 @@ impl BindgenContext {
522524
&index,
523525
"",
524526
&options.clang_args,
525-
&options.input_unsaved_files,
527+
&state.input_unsaved_files,
526528
parse_options,
527529
).expect("libclang error; possible causes include:
528530
- Invalid flag syntax
@@ -561,6 +563,7 @@ If you encounter an error missing from this list, please file an issue or a PR!"
561563
translation_unit,
562564
target_info,
563565
options,
566+
state,
564567
generated_bindgen_complex: Cell::new(false),
565568
allowlisted: None,
566569
blocklisted_types_implement_traits: Default::default(),
@@ -622,7 +625,7 @@ If you encounter an error missing from this list, please file an issue or a PR!"
622625

623626
/// Get the user-provided callbacks by reference, if any.
624627
pub fn parse_callbacks(&self) -> Option<&dyn ParseCallbacks> {
625-
self.options().parse_callbacks.as_deref()
628+
self.state().parse_callbacks.as_deref()
626629
}
627630

628631
/// Add another path to the set of included files.
@@ -1136,7 +1139,7 @@ If you encounter an error missing from this list, please file an issue or a PR!"
11361139
pub(crate) fn gen<F, Out>(
11371140
mut self,
11381141
cb: F,
1139-
) -> (Out, BindgenState, Vec<String>)
1142+
) -> (Out, BindgenOptions, Vec<String>)
11401143
where
11411144
F: FnOnce(&Self) -> Out,
11421145
{
@@ -2077,14 +2080,18 @@ If you encounter an error missing from this list, please file an issue or a PR!"
20772080
self.in_codegen_phase(),
20782081
"You're not supposed to call this yet"
20792082
);
2080-
self.options.opaque_types.matches(&path[1..].join("::"))
2083+
self.state.opaque_types.matches(&path[1..].join("::"))
20812084
}
20822085

20832086
/// Get the options used to configure this bindgen context.
2084-
pub(crate) fn options(&self) -> &BindgenState {
2087+
pub(crate) fn options(&self) -> &BindgenOptions {
20852088
&self.options
20862089
}
20872090

2091+
pub(crate) fn state(&self) -> &BindgenState {
2092+
&self.state
2093+
}
2094+
20882095
/// Tokenizes a namespace cursor in order to get the name and kind of the
20892096
/// namespace.
20902097
fn tokenize_namespace(
@@ -2242,7 +2249,7 @@ If you encounter an error missing from this list, please file an issue or a PR!"
22422249
.or_insert_with(|| {
22432250
item.expect_type()
22442251
.name()
2245-
.and_then(|name| match self.options.parse_callbacks {
2252+
.and_then(|name| match self.state.parse_callbacks {
22462253
Some(ref cb) => cb.blocklisted_type_implements_trait(
22472254
name,
22482255
derive_trait,
@@ -2311,7 +2318,7 @@ If you encounter an error missing from this list, please file an issue or a PR!"
23112318
let (file, _, _, _) = location.location();
23122319
if let Some(filename) = file.name() {
23132320
if self
2314-
.options()
2321+
.state()
23152322
.allowlisted_files
23162323
.matches(&filename)
23172324
{
@@ -2326,13 +2333,13 @@ If you encounter an error missing from this list, please file an issue or a PR!"
23262333
match *item.kind() {
23272334
ItemKind::Module(..) => true,
23282335
ItemKind::Function(_) => {
2329-
self.options().allowlisted_functions.matches(&name)
2336+
self.state().allowlisted_functions.matches(&name)
23302337
}
23312338
ItemKind::Var(_) => {
2332-
self.options().allowlisted_vars.matches(&name)
2339+
self.state().allowlisted_vars.matches(&name)
23332340
}
23342341
ItemKind::Type(ref ty) => {
2335-
if self.options().allowlisted_types.matches(&name) {
2342+
if self.state().allowlisted_types.matches(&name) {
23362343
return true;
23372344
}
23382345

@@ -2385,7 +2392,7 @@ If you encounter an error missing from this list, please file an issue or a PR!"
23852392
);
23862393
let name = prefix_path[1..].join("::");
23872394
prefix_path.pop().unwrap();
2388-
self.options().allowlisted_vars.matches(&name)
2395+
self.state().allowlisted_vars.matches(&name)
23892396
})
23902397
}
23912398
}
@@ -2434,16 +2441,28 @@ If you encounter an error missing from this list, please file an issue or a PR!"
24342441

24352442
let mut warnings = Vec::new();
24362443

2437-
for item in self.options().allowlisted_functions.unmatched_items() {
2444+
for item in self
2445+
.state()
2446+
.allowlisted_functions
2447+
.unmatched_items(&self.options().allowlisted_functions)
2448+
{
24382449
warnings
24392450
.push(format!("unused option: --allowlist-function {}", item));
24402451
}
24412452

2442-
for item in self.options().allowlisted_vars.unmatched_items() {
2453+
for item in self
2454+
.state()
2455+
.allowlisted_vars
2456+
.unmatched_items(&self.options().allowlisted_vars)
2457+
{
24432458
warnings.push(format!("unused option: --allowlist-var {}", item));
24442459
}
24452460

2446-
for item in self.options().allowlisted_types.unmatched_items() {
2461+
for item in self
2462+
.state()
2463+
.allowlisted_types
2464+
.unmatched_items(&self.options().allowlisted_types)
2465+
{
24472466
warnings.push(format!("unused option: --allowlist-type {}", item));
24482467
}
24492468

@@ -2667,37 +2686,37 @@ If you encounter an error missing from this list, please file an issue or a PR!"
26672686
/// Check if `--no-partialeq` flag is enabled for this item.
26682687
pub fn no_partialeq_by_name(&self, item: &Item) -> bool {
26692688
let name = item.path_for_allowlisting(self)[1..].join("::");
2670-
self.options().no_partialeq_types.matches(&name)
2689+
self.state().no_partialeq_types.matches(&name)
26712690
}
26722691

26732692
/// Check if `--no-copy` flag is enabled for this item.
26742693
pub fn no_copy_by_name(&self, item: &Item) -> bool {
26752694
let name = item.path_for_allowlisting(self)[1..].join("::");
2676-
self.options().no_copy_types.matches(&name)
2695+
self.state().no_copy_types.matches(&name)
26772696
}
26782697

26792698
/// Check if `--no-debug` flag is enabled for this item.
26802699
pub fn no_debug_by_name(&self, item: &Item) -> bool {
26812700
let name = item.path_for_allowlisting(self)[1..].join("::");
2682-
self.options().no_debug_types.matches(&name)
2701+
self.state().no_debug_types.matches(&name)
26832702
}
26842703

26852704
/// Check if `--no-default` flag is enabled for this item.
26862705
pub fn no_default_by_name(&self, item: &Item) -> bool {
26872706
let name = item.path_for_allowlisting(self)[1..].join("::");
2688-
self.options().no_default_types.matches(&name)
2707+
self.state().no_default_types.matches(&name)
26892708
}
26902709

26912710
/// Check if `--no-hash` flag is enabled for this item.
26922711
pub fn no_hash_by_name(&self, item: &Item) -> bool {
26932712
let name = item.path_for_allowlisting(self)[1..].join("::");
2694-
self.options().no_hash_types.matches(&name)
2713+
self.state().no_hash_types.matches(&name)
26952714
}
26962715

26972716
/// Check if `--must-use-type` flag is enabled for this item.
26982717
pub fn must_use_type_by_name(&self, item: &Item) -> bool {
26992718
let name = item.path_for_allowlisting(self)[1..].join("::");
2700-
self.options().must_use_types.matches(&name)
2719+
self.state().must_use_types.matches(&name)
27012720
}
27022721
}
27032722

src/ir/enum_ty.rs

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -178,38 +178,31 @@ impl Enum {
178178
// problems with overlapping match patterns.
179179
if self.is_matching_enum(
180180
ctx,
181-
&ctx.options().constified_enum_modules,
181+
&ctx.state().constified_enum_modules,
182182
item,
183183
) {
184184
EnumVariation::ModuleConsts
185-
} else if self.is_matching_enum(
186-
ctx,
187-
&ctx.options().bitfield_enums,
188-
item,
189-
) {
190-
EnumVariation::NewType { is_bitfield: true }
191-
} else if self.is_matching_enum(ctx, &ctx.options().newtype_enums, item)
185+
} else if self.is_matching_enum(ctx, &ctx.state().bitfield_enums, item)
192186
{
187+
EnumVariation::NewType { is_bitfield: true }
188+
} else if self.is_matching_enum(ctx, &ctx.state().newtype_enums, item) {
193189
EnumVariation::NewType { is_bitfield: false }
194-
} else if self.is_matching_enum(
195-
ctx,
196-
&ctx.options().rustified_enums,
197-
item,
198-
) {
190+
} else if self.is_matching_enum(ctx, &ctx.state().rustified_enums, item)
191+
{
199192
EnumVariation::Rust {
200193
non_exhaustive: false,
201194
}
202195
} else if self.is_matching_enum(
203196
ctx,
204-
&ctx.options().rustified_non_exhaustive_enums,
197+
&ctx.state().rustified_non_exhaustive_enums,
205198
item,
206199
) {
207200
EnumVariation::Rust {
208201
non_exhaustive: true,
209202
}
210203
} else if self.is_matching_enum(
211204
ctx,
212-
&ctx.options().constified_enums,
205+
&ctx.state().constified_enums,
213206
item,
214207
) {
215208
EnumVariation::Consts

src/ir/item.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -650,7 +650,7 @@ impl Item {
650650
if let Some(location) = &self.location {
651651
let (file, _, _, _) = location.location();
652652
if let Some(filename) = file.name() {
653-
if ctx.options().blocklisted_files.matches(&filename) {
653+
if ctx.state().blocklisted_files.matches(&filename) {
654654
return true;
655655
}
656656
}
@@ -659,14 +659,14 @@ impl Item {
659659

660660
let path = self.path_for_allowlisting(ctx);
661661
let name = path[1..].join("::");
662-
ctx.options().blocklisted_items.matches(&name) ||
662+
ctx.state().blocklisted_items.matches(&name) ||
663663
match self.kind {
664664
ItemKind::Type(..) => {
665-
ctx.options().blocklisted_types.matches(&name) ||
665+
ctx.state().blocklisted_types.matches(&name) ||
666666
ctx.is_replaced_type(path, self.id)
667667
}
668668
ItemKind::Function(..) => {
669-
ctx.options().blocklisted_functions.matches(&name)
669+
ctx.state().blocklisted_functions.matches(&name)
670670
}
671671
// TODO: Add constant / namespace blocklisting?
672672
ItemKind::Var(..) | ItemKind::Module(..) => false,
@@ -887,7 +887,9 @@ impl Item {
887887
true
888888
});
889889

890-
let ids: Vec<_> = if ctx.options().disable_nested_struct_naming {
890+
let ids: Vec<_> = if ctx.options().enable_nested_struct_naming {
891+
ids_iter.collect()
892+
} else {
891893
let mut ids = Vec::new();
892894

893895
// If target is anonymous we need find its first named ancestor.
@@ -902,8 +904,6 @@ impl Item {
902904
}
903905

904906
ids
905-
} else {
906-
ids_iter.collect()
907907
};
908908

909909
// Concatenate this item's ancestors' names together.
@@ -1924,7 +1924,7 @@ impl ItemCanonicalName for Item {
19241924
self.canonical_name
19251925
.borrow_with(|| {
19261926
let in_namespace = ctx.options().enable_cxx_namespaces ||
1927-
ctx.options().disable_name_namespacing;
1927+
!ctx.options().enable_name_namespacing;
19281928

19291929
if in_namespace {
19301930
self.name(ctx).within_namespaces().get()
@@ -1946,7 +1946,7 @@ impl ItemCanonicalPath for Item {
19461946
// ASSUMPTION: (disable_name_namespacing && cxx_namespaces)
19471947
// is equivalent to
19481948
// disable_name_namespacing
1949-
if ctx.options().disable_name_namespacing {
1949+
if !ctx.options().enable_name_namespacing {
19501950
// Only keep the last item in path
19511951
let split_idx = path.len() - 1;
19521952
path = path.split_off(split_idx);

0 commit comments

Comments
 (0)