@@ -11,7 +11,7 @@ use syn;
11
11
12
12
use bindgen:: bitflags;
13
13
use bindgen:: cargo:: { Cargo , PackageRef } ;
14
- use bindgen:: config:: MacroExpansionConfig ;
14
+ use bindgen:: config:: { MacroExpansionConfig , ParseConfig } ;
15
15
use bindgen:: error:: Error ;
16
16
use bindgen:: ir:: {
17
17
AnnotationSet , Cfg , Constant , Documentation , Enum , Function , GenericParams , ItemMap ,
@@ -36,19 +36,16 @@ pub fn parse_src(
36
36
macro_expansion_config : & MacroExpansionConfig ,
37
37
) -> ParseResult {
38
38
let mod_name = src_file. file_stem ( ) . unwrap ( ) . to_str ( ) . unwrap ( ) ;
39
+ let parse_config = ParseConfig {
40
+ parse_deps : true ,
41
+ ..ParseConfig :: default ( )
42
+ } ;
39
43
40
44
let mut context = Parser {
41
45
binding_crate_name : mod_name. to_owned ( ) ,
42
46
macro_expansion_config,
47
+ parse_config : & parse_config,
43
48
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 ,
52
49
parsed_crates : HashSet :: new ( ) ,
53
50
cache_src : HashMap :: new ( ) ,
54
51
cache_expanded_crate : HashMap :: new ( ) ,
@@ -73,27 +70,13 @@ pub fn parse_src(
73
70
pub ( crate ) fn parse_lib (
74
71
lib : Cargo ,
75
72
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 ,
84
74
) -> ParseResult {
85
75
let mut context = Parser {
86
76
binding_crate_name : lib. binding_crate_name ( ) . to_owned ( ) ,
87
77
macro_expansion_config,
78
+ parse_config,
88
79
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 ( ) ,
97
80
parsed_crates : HashSet :: new ( ) ,
98
81
cache_src : HashMap :: new ( ) ,
99
82
cache_expanded_crate : HashMap :: new ( ) ,
@@ -109,17 +92,9 @@ pub(crate) fn parse_lib(
109
92
#[ derive( Debug , Clone ) ]
110
93
struct Parser < ' a > {
111
94
binding_crate_name : String ,
112
- macro_expansion_config : & ' a MacroExpansionConfig ,
113
95
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 ,
123
98
124
99
parsed_crates : HashSet < String > ,
125
100
cache_src : HashMap < FilePathBuf , Vec < syn:: Item > > ,
@@ -136,32 +111,33 @@ impl<'a> Parser<'a> {
136
111
return false ;
137
112
}
138
113
139
- if !self . parse_deps {
114
+ if !self . parse_config . parse_deps {
140
115
return false ;
141
116
}
142
117
143
118
// Skip any whitelist or blacklist for expand
144
- if self . expand . contains ( & pkg_name) {
119
+ if self . parse_config . expand . crates . contains ( & pkg_name) {
145
120
return true ;
146
121
}
147
122
148
123
// 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 {
150
125
if !include. contains ( & pkg_name) {
151
126
return false ;
152
127
}
153
128
}
154
129
155
130
// 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) ;
157
133
}
158
134
159
135
fn parse_crate ( & mut self , pkg : & PackageRef ) -> Result < ( ) , Error > {
160
136
assert ! ( self . lib. is_some( ) ) ;
161
137
self . parsed_crates . insert ( pkg. name . clone ( ) ) ;
162
138
163
139
// 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 ) {
165
141
return self . parse_expand_crate ( pkg) ;
166
142
}
167
143
@@ -211,9 +187,9 @@ impl<'a> Parser<'a> {
211
187
. unwrap ( )
212
188
. expand_crate (
213
189
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 ,
217
193
)
218
194
. map_err ( |x| Error :: CargoExpand ( pkg. name . clone ( ) , x) ) ?;
219
195
let i = syn:: parse_file ( & s) . map_err ( |x| Error :: ParseSyntaxError {
@@ -233,9 +209,9 @@ impl<'a> Parser<'a> {
233
209
fn process_expanded_mod ( & mut self , pkg : & PackageRef , items : & [ syn:: Item ] ) -> Result < ( ) , Error > {
234
210
self . out . load_syn_crate_mod (
235
211
& self . macro_expansion_config ,
212
+ & self . parse_config ,
236
213
& self . binding_crate_name ,
237
214
& pkg. name ,
238
- self . top_level_items_outside_of_binding_crate ,
239
215
Cfg :: join ( & self . cfg_stack ) . as_ref ( ) ,
240
216
items,
241
217
) ;
@@ -309,9 +285,9 @@ impl<'a> Parser<'a> {
309
285
) -> Result < ( ) , Error > {
310
286
self . out . load_syn_crate_mod (
311
287
& self . macro_expansion_config ,
288
+ & self . parse_config ,
312
289
& self . binding_crate_name ,
313
290
& pkg. name ,
314
- self . top_level_items_outside_of_binding_crate ,
315
291
Cfg :: join ( & self . cfg_stack ) . as_ref ( ) ,
316
292
items,
317
293
) ;
@@ -452,9 +428,9 @@ impl Parse {
452
428
pub fn load_syn_crate_mod (
453
429
& mut self ,
454
430
macro_expansion_config : & MacroExpansionConfig ,
431
+ parse_config : & ParseConfig ,
455
432
binding_crate_name : & str ,
456
433
crate_name : & str ,
457
- top_level_items_outside_of_binding_crate : bool ,
458
434
mod_cfg : Option < & Cfg > ,
459
435
items : & [ syn:: Item ] ,
460
436
) {
@@ -467,36 +443,30 @@ impl Parse {
467
443
match item {
468
444
syn:: Item :: ForeignMod ( ref item) => {
469
445
self . load_syn_foreign_mod (
446
+ parse_config,
470
447
binding_crate_name,
471
448
crate_name,
472
- top_level_items_outside_of_binding_crate,
473
449
mod_cfg,
474
450
item,
475
451
) ;
476
452
}
477
453
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) ;
485
455
}
486
456
syn:: Item :: Const ( ref item) => {
487
457
self . load_syn_const (
458
+ parse_config,
488
459
binding_crate_name,
489
460
crate_name,
490
- top_level_items_outside_of_binding_crate,
491
461
mod_cfg,
492
462
item,
493
463
) ;
494
464
}
495
465
syn:: Item :: Static ( ref item) => {
496
466
self . load_syn_static (
467
+ parse_config,
497
468
binding_crate_name,
498
469
crate_name,
499
- top_level_items_outside_of_binding_crate,
500
470
mod_cfg,
501
471
item,
502
472
) ;
@@ -555,9 +525,9 @@ impl Parse {
555
525
/// Enters a `extern "C" { }` declaration and loads function declarations.
556
526
fn load_syn_foreign_mod (
557
527
& mut self ,
528
+ parse_config : & ParseConfig ,
558
529
binding_crate_name : & str ,
559
530
crate_name : & str ,
560
- top_level_items_outside_of_binding_crate : bool ,
561
531
mod_cfg : Option < & Cfg > ,
562
532
item : & syn:: ItemForeignMod ,
563
533
) {
@@ -569,7 +539,8 @@ impl Parse {
569
539
for foreign_item in & item. items {
570
540
match * foreign_item {
571
541
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
573
544
{
574
545
info ! (
575
546
"Skip {}::{} - (fn's outside of the binding crate are not used)." ,
@@ -600,13 +571,15 @@ impl Parse {
600
571
/// Loads a `fn` declaration
601
572
fn load_syn_fn (
602
573
& mut self ,
574
+ parse_config : & ParseConfig ,
603
575
binding_crate_name : & str ,
604
576
crate_name : & str ,
605
- top_level_items_outside_of_binding_crate : bool ,
606
577
mod_cfg : Option < & Cfg > ,
607
578
item : & syn:: ItemFn ,
608
579
) {
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
+ {
610
583
info ! (
611
584
"Skip {}::{} - (fn's outside of the binding crate are not used)." ,
612
585
crate_name, & item. ident
@@ -715,13 +688,15 @@ impl Parse {
715
688
/// Loads a `const` declaration
716
689
fn load_syn_const (
717
690
& mut self ,
691
+ parse_config : & ParseConfig ,
718
692
binding_crate_name : & str ,
719
693
crate_name : & str ,
720
- top_level_items_outside_of_binding_crate : bool ,
721
694
mod_cfg : Option < & Cfg > ,
722
695
item : & syn:: ItemConst ,
723
696
) {
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
+ {
725
700
info ! (
726
701
"Skip {}::{} - (const's outside of the binding crate are not used)." ,
727
702
crate_name, & item. ident
@@ -754,13 +729,15 @@ impl Parse {
754
729
/// Loads a `static` declaration
755
730
fn load_syn_static (
756
731
& mut self ,
732
+ parse_config : & ParseConfig ,
757
733
binding_crate_name : & str ,
758
734
crate_name : & str ,
759
- top_level_items_outside_of_binding_crate : bool ,
760
735
mod_cfg : Option < & Cfg > ,
761
736
item : & syn:: ItemStatic ,
762
737
) {
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
+ {
764
741
info ! (
765
742
"Skip {}::{} - (static's outside of the binding crate are not used)." ,
766
743
crate_name, & item. ident
0 commit comments