Skip to content

Commit c5a34be

Browse files
committed
Cleanup parse config propagation.
1 parent 0bcd54f commit c5a34be

File tree

2 files changed

+43
-80
lines changed

2 files changed

+43
-80
lines changed

src/bindgen/builder.rs

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -322,27 +322,13 @@ impl Builder {
322322
result.extend_with(&parser::parse_lib(
323323
cargo,
324324
&self.config.macro_expansion,
325-
self.config.parse.parse_deps,
326-
&self.config.parse.include,
327-
&self.config.parse.exclude,
328-
&self.config.parse.expand.crates,
329-
self.config.parse.expand.all_features,
330-
self.config.parse.expand.default_features,
331-
&self.config.parse.expand.features,
332-
self.config.parse.top_level_items_outside_of_binding_crate,
325+
&self.config.parse,
333326
)?);
334327
} else if let Some(cargo) = self.lib_cargo.clone() {
335328
result.extend_with(&parser::parse_lib(
336329
cargo,
337330
&self.config.macro_expansion,
338-
self.config.parse.parse_deps,
339-
&self.config.parse.include,
340-
&self.config.parse.exclude,
341-
&self.config.parse.expand.crates,
342-
self.config.parse.expand.all_features,
343-
self.config.parse.expand.default_features,
344-
&self.config.parse.expand.features,
345-
self.config.parse.top_level_items_outside_of_binding_crate,
331+
&self.config.parse,
346332
)?);
347333
}
348334

src/bindgen/parser.rs

Lines changed: 41 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use syn;
1111

1212
use bindgen::bitflags;
1313
use bindgen::cargo::{Cargo, PackageRef};
14-
use bindgen::config::MacroExpansionConfig;
14+
use bindgen::config::{MacroExpansionConfig, ParseConfig};
1515
use bindgen::error::Error;
1616
use bindgen::ir::{
1717
AnnotationSet, Cfg, Constant, Documentation, Enum, Function, GenericParams, ItemMap,
@@ -36,19 +36,16 @@ pub fn parse_src(
3636
macro_expansion_config: &MacroExpansionConfig,
3737
) -> ParseResult {
3838
let mod_name = src_file.file_stem().unwrap().to_str().unwrap();
39+
let parse_config = ParseConfig {
40+
parse_deps: true,
41+
..ParseConfig::default()
42+
};
3943

4044
let mut context = Parser {
4145
binding_crate_name: mod_name.to_owned(),
4246
macro_expansion_config,
47+
parse_config: &parse_config,
4348
lib: None,
44-
parse_deps: true,
45-
top_level_items_outside_of_binding_crate: false,
46-
include: None,
47-
exclude: Vec::new(),
48-
expand: Vec::new(),
49-
expand_all_features: true,
50-
expand_default_features: true,
51-
expand_features: None,
5249
parsed_crates: HashSet::new(),
5350
cache_src: HashMap::new(),
5451
cache_expanded_crate: HashMap::new(),
@@ -73,27 +70,13 @@ pub fn parse_src(
7370
pub(crate) fn parse_lib(
7471
lib: Cargo,
7572
macro_expansion_config: &MacroExpansionConfig,
76-
parse_deps: bool,
77-
include: &Option<Vec<String>>,
78-
exclude: &[String],
79-
expand: &[String],
80-
expand_all_features: bool,
81-
expand_default_features: bool,
82-
expand_features: &Option<Vec<String>>,
83-
top_level_items_outside_of_binding_crate: bool,
73+
parse_config: &ParseConfig,
8474
) -> ParseResult {
8575
let mut context = Parser {
8676
binding_crate_name: lib.binding_crate_name().to_owned(),
8777
macro_expansion_config,
78+
parse_config,
8879
lib: Some(lib),
89-
parse_deps,
90-
top_level_items_outside_of_binding_crate,
91-
include: include.clone(),
92-
exclude: exclude.to_owned(),
93-
expand: expand.to_owned(),
94-
expand_all_features,
95-
expand_default_features,
96-
expand_features: expand_features.clone(),
9780
parsed_crates: HashSet::new(),
9881
cache_src: HashMap::new(),
9982
cache_expanded_crate: HashMap::new(),
@@ -109,17 +92,9 @@ pub(crate) fn parse_lib(
10992
#[derive(Debug, Clone)]
11093
struct Parser<'a> {
11194
binding_crate_name: String,
112-
macro_expansion_config: &'a MacroExpansionConfig,
11395
lib: Option<Cargo>,
114-
parse_deps: bool,
115-
top_level_items_outside_of_binding_crate: bool,
116-
117-
include: Option<Vec<String>>,
118-
exclude: Vec<String>,
119-
expand: Vec<String>,
120-
expand_all_features: bool,
121-
expand_default_features: bool,
122-
expand_features: Option<Vec<String>>,
96+
macro_expansion_config: &'a MacroExpansionConfig,
97+
parse_config: &'a ParseConfig,
12398

12499
parsed_crates: HashSet<String>,
125100
cache_src: HashMap<FilePathBuf, Vec<syn::Item>>,
@@ -136,32 +111,33 @@ impl<'a> Parser<'a> {
136111
return false;
137112
}
138113

139-
if !self.parse_deps {
114+
if !self.parse_config.parse_deps {
140115
return false;
141116
}
142117

143118
// Skip any whitelist or blacklist for expand
144-
if self.expand.contains(&pkg_name) {
119+
if self.parse_config.expand.crates.contains(&pkg_name) {
145120
return true;
146121
}
147122

148123
// If we have a whitelist, check it
149-
if let Some(ref include) = self.include {
124+
if let Some(ref include) = self.parse_config.include {
150125
if !include.contains(&pkg_name) {
151126
return false;
152127
}
153128
}
154129

155130
// Check the blacklist
156-
return !STD_CRATES.contains(&pkg_name.as_ref()) && !self.exclude.contains(&pkg_name);
131+
return !STD_CRATES.contains(&pkg_name.as_ref())
132+
&& !self.parse_config.exclude.contains(&pkg_name);
157133
}
158134

159135
fn parse_crate(&mut self, pkg: &PackageRef) -> Result<(), Error> {
160136
assert!(self.lib.is_some());
161137
self.parsed_crates.insert(pkg.name.clone());
162138

163139
// Check if we should use cargo expand for this crate
164-
if self.expand.contains(&pkg.name) {
140+
if self.parse_config.expand.crates.contains(&pkg.name) {
165141
return self.parse_expand_crate(pkg);
166142
}
167143

@@ -211,9 +187,9 @@ impl<'a> Parser<'a> {
211187
.unwrap()
212188
.expand_crate(
213189
pkg,
214-
self.expand_all_features,
215-
self.expand_default_features,
216-
&self.expand_features,
190+
self.parse_config.expand.all_features,
191+
self.parse_config.expand.default_features,
192+
&self.parse_config.expand.features,
217193
)
218194
.map_err(|x| Error::CargoExpand(pkg.name.clone(), x))?;
219195
let i = syn::parse_file(&s).map_err(|x| Error::ParseSyntaxError {
@@ -233,9 +209,9 @@ impl<'a> Parser<'a> {
233209
fn process_expanded_mod(&mut self, pkg: &PackageRef, items: &[syn::Item]) -> Result<(), Error> {
234210
self.out.load_syn_crate_mod(
235211
&self.macro_expansion_config,
212+
&self.parse_config,
236213
&self.binding_crate_name,
237214
&pkg.name,
238-
self.top_level_items_outside_of_binding_crate,
239215
Cfg::join(&self.cfg_stack).as_ref(),
240216
items,
241217
);
@@ -309,9 +285,9 @@ impl<'a> Parser<'a> {
309285
) -> Result<(), Error> {
310286
self.out.load_syn_crate_mod(
311287
&self.macro_expansion_config,
288+
&self.parse_config,
312289
&self.binding_crate_name,
313290
&pkg.name,
314-
self.top_level_items_outside_of_binding_crate,
315291
Cfg::join(&self.cfg_stack).as_ref(),
316292
items,
317293
);
@@ -452,9 +428,9 @@ impl Parse {
452428
pub fn load_syn_crate_mod(
453429
&mut self,
454430
macro_expansion_config: &MacroExpansionConfig,
431+
parse_config: &ParseConfig,
455432
binding_crate_name: &str,
456433
crate_name: &str,
457-
top_level_items_outside_of_binding_crate: bool,
458434
mod_cfg: Option<&Cfg>,
459435
items: &[syn::Item],
460436
) {
@@ -467,36 +443,30 @@ impl Parse {
467443
match item {
468444
syn::Item::ForeignMod(ref item) => {
469445
self.load_syn_foreign_mod(
446+
parse_config,
470447
binding_crate_name,
471448
crate_name,
472-
top_level_items_outside_of_binding_crate,
473449
mod_cfg,
474450
item,
475451
);
476452
}
477453
syn::Item::Fn(ref item) => {
478-
self.load_syn_fn(
479-
binding_crate_name,
480-
crate_name,
481-
top_level_items_outside_of_binding_crate,
482-
mod_cfg,
483-
item,
484-
);
454+
self.load_syn_fn(parse_config, binding_crate_name, crate_name, mod_cfg, item);
485455
}
486456
syn::Item::Const(ref item) => {
487457
self.load_syn_const(
458+
parse_config,
488459
binding_crate_name,
489460
crate_name,
490-
top_level_items_outside_of_binding_crate,
491461
mod_cfg,
492462
item,
493463
);
494464
}
495465
syn::Item::Static(ref item) => {
496466
self.load_syn_static(
467+
parse_config,
497468
binding_crate_name,
498469
crate_name,
499-
top_level_items_outside_of_binding_crate,
500470
mod_cfg,
501471
item,
502472
);
@@ -555,9 +525,9 @@ impl Parse {
555525
/// Enters a `extern "C" { }` declaration and loads function declarations.
556526
fn load_syn_foreign_mod(
557527
&mut self,
528+
parse_config: &ParseConfig,
558529
binding_crate_name: &str,
559530
crate_name: &str,
560-
top_level_items_outside_of_binding_crate: bool,
561531
mod_cfg: Option<&Cfg>,
562532
item: &syn::ItemForeignMod,
563533
) {
@@ -569,7 +539,8 @@ impl Parse {
569539
for foreign_item in &item.items {
570540
match *foreign_item {
571541
syn::ForeignItem::Fn(ref function) => {
572-
if !top_level_items_outside_of_binding_crate && crate_name != binding_crate_name
542+
if !parse_config.top_level_items_outside_of_binding_crate
543+
&& crate_name != binding_crate_name
573544
{
574545
info!(
575546
"Skip {}::{} - (fn's outside of the binding crate are not used).",
@@ -600,13 +571,15 @@ impl Parse {
600571
/// Loads a `fn` declaration
601572
fn load_syn_fn(
602573
&mut self,
574+
parse_config: &ParseConfig,
603575
binding_crate_name: &str,
604576
crate_name: &str,
605-
top_level_items_outside_of_binding_crate: bool,
606577
mod_cfg: Option<&Cfg>,
607578
item: &syn::ItemFn,
608579
) {
609-
if !top_level_items_outside_of_binding_crate && crate_name != binding_crate_name {
580+
if !parse_config.top_level_items_outside_of_binding_crate
581+
&& crate_name != binding_crate_name
582+
{
610583
info!(
611584
"Skip {}::{} - (fn's outside of the binding crate are not used).",
612585
crate_name, &item.ident
@@ -715,13 +688,15 @@ impl Parse {
715688
/// Loads a `const` declaration
716689
fn load_syn_const(
717690
&mut self,
691+
parse_config: &ParseConfig,
718692
binding_crate_name: &str,
719693
crate_name: &str,
720-
top_level_items_outside_of_binding_crate: bool,
721694
mod_cfg: Option<&Cfg>,
722695
item: &syn::ItemConst,
723696
) {
724-
if !top_level_items_outside_of_binding_crate && crate_name != binding_crate_name {
697+
if !parse_config.top_level_items_outside_of_binding_crate
698+
&& crate_name != binding_crate_name
699+
{
725700
info!(
726701
"Skip {}::{} - (const's outside of the binding crate are not used).",
727702
crate_name, &item.ident
@@ -754,13 +729,15 @@ impl Parse {
754729
/// Loads a `static` declaration
755730
fn load_syn_static(
756731
&mut self,
732+
parse_config: &ParseConfig,
757733
binding_crate_name: &str,
758734
crate_name: &str,
759-
top_level_items_outside_of_binding_crate: bool,
760735
mod_cfg: Option<&Cfg>,
761736
item: &syn::ItemStatic,
762737
) {
763-
if !top_level_items_outside_of_binding_crate && crate_name != binding_crate_name {
738+
if !parse_config.top_level_items_outside_of_binding_crate
739+
&& crate_name != binding_crate_name
740+
{
764741
info!(
765742
"Skip {}::{} - (static's outside of the binding crate are not used).",
766743
crate_name, &item.ident

0 commit comments

Comments
 (0)