@@ -3,6 +3,7 @@ use proc_macro2::Span;
3
3
use quote:: ToTokens ;
4
4
use syn:: spanned:: Spanned ;
5
5
6
+ use crate :: cdecl:: Constness ;
6
7
use crate :: ffi_items:: FfiItems ;
7
8
use crate :: translator:: { TranslationErrorKind , Translator } ;
8
9
use crate :: {
@@ -86,7 +87,7 @@ impl TestTemplate {
86
87
& mut self ,
87
88
helper : & TranslateHelper ,
88
89
) -> Result < ( ) , TranslationError > {
89
- for constant in helper. ffi_items . constants ( ) {
90
+ for constant in helper. filtered_ffi_items . constants ( ) {
90
91
if let syn:: Type :: Ptr ( ptr) = & constant. ty
91
92
&& let syn:: Type :: Path ( path) = & * ptr. elem
92
93
&& path. path . segments . last ( ) . unwrap ( ) . ident == "c_char"
@@ -124,7 +125,7 @@ impl TestTemplate {
124
125
& mut self ,
125
126
helper : & TranslateHelper ,
126
127
) -> Result < ( ) , TranslationError > {
127
- for alias in helper. ffi_items . aliases ( ) {
128
+ for alias in helper. filtered_ffi_items . aliases ( ) {
128
129
let item = TestSizeAlign {
129
130
test_name : size_align_test_ident ( alias. ident ( ) ) ,
130
131
id : alias. ident ( ) . into ( ) ,
@@ -134,7 +135,7 @@ impl TestTemplate {
134
135
self . size_align_tests . push ( item. clone ( ) ) ;
135
136
self . test_idents . push ( item. test_name ) ;
136
137
}
137
- for struct_ in helper. ffi_items . structs ( ) {
138
+ for struct_ in helper. filtered_ffi_items . structs ( ) {
138
139
let item = TestSizeAlign {
139
140
test_name : size_align_test_ident ( struct_. ident ( ) ) ,
140
141
id : struct_. ident ( ) . into ( ) ,
@@ -144,7 +145,7 @@ impl TestTemplate {
144
145
self . size_align_tests . push ( item. clone ( ) ) ;
145
146
self . test_idents . push ( item. test_name ) ;
146
147
}
147
- for union_ in helper. ffi_items . unions ( ) {
148
+ for union_ in helper. filtered_ffi_items . unions ( ) {
148
149
let item = TestSizeAlign {
149
150
test_name : size_align_test_ident ( union_. ident ( ) ) ,
150
151
id : union_. ident ( ) . into ( ) ,
@@ -165,7 +166,7 @@ impl TestTemplate {
165
166
& mut self ,
166
167
helper : & TranslateHelper ,
167
168
) -> Result < ( ) , TranslationError > {
168
- for alias in helper. ffi_items . aliases ( ) {
169
+ for alias in helper. filtered_ffi_items . aliases ( ) {
169
170
let should_skip_signededness_test = helper
170
171
. generator
171
172
. skip_signededness
@@ -197,7 +198,7 @@ impl TestTemplate {
197
198
let should_skip = |map_input| helper. generator . skips . iter ( ) . any ( |f| f ( & map_input) ) ;
198
199
199
200
let struct_fields = helper
200
- . ffi_items
201
+ . filtered_ffi_items
201
202
. structs ( )
202
203
. iter ( )
203
204
. flat_map ( |struct_| struct_. fields . iter ( ) . map ( move |field| ( struct_, field) ) )
@@ -213,7 +214,7 @@ impl TestTemplate {
213
214
)
214
215
} ) ;
215
216
let union_fields = helper
216
- . ffi_items
217
+ . filtered_ffi_items
217
218
. unions ( )
218
219
. iter ( )
219
220
. flat_map ( |union_| union_. fields . iter ( ) . map ( move |field| ( union_, field) ) )
@@ -251,15 +252,15 @@ impl TestTemplate {
251
252
& mut self ,
252
253
helper : & TranslateHelper ,
253
254
) -> Result < ( ) , TranslationError > {
254
- for alias in helper. ffi_items . aliases ( ) {
255
+ for alias in helper. filtered_ffi_items . aliases ( ) {
255
256
let c_ty = helper. c_type ( alias) ?;
256
257
self . add_roundtrip_test ( helper, alias. ident ( ) , & [ ] , & c_ty, true ) ;
257
258
}
258
- for struct_ in helper. ffi_items . structs ( ) {
259
+ for struct_ in helper. filtered_ffi_items . structs ( ) {
259
260
let c_ty = helper. c_type ( struct_) ?;
260
261
self . add_roundtrip_test ( helper, struct_. ident ( ) , & struct_. fields , & c_ty, false ) ;
261
262
}
262
- for union_ in helper. ffi_items . unions ( ) {
263
+ for union_ in helper. filtered_ffi_items . unions ( ) {
263
264
let c_ty = helper. c_type ( union_) ?;
264
265
self . add_roundtrip_test ( helper, union_. ident ( ) , & union_. fields , & c_ty, false ) ;
265
266
}
@@ -303,14 +304,14 @@ impl TestTemplate {
303
304
let should_skip = |map_input| helper. generator . skips . iter ( ) . any ( |f| f ( & map_input) ) ;
304
305
305
306
let struct_fields = helper
306
- . ffi_items
307
+ . filtered_ffi_items
307
308
. structs ( )
308
309
. iter ( )
309
310
. flat_map ( |s| s. fields . iter ( ) . map ( move |f| ( s, f) ) )
310
311
. filter ( |( s, f) | {
311
- !should_skip ( MapInput :: StructField ( s, f) )
312
- && ! should_skip ( MapInput :: StructFieldType ( s, f) )
313
- && f. public
312
+ !( should_skip ( MapInput :: StructField ( s, f) )
313
+ || should_skip ( MapInput :: StructFieldType ( s, f) )
314
+ || ! f. public )
314
315
} )
315
316
. map ( |( s, f) | {
316
317
(
@@ -332,14 +333,14 @@ impl TestTemplate {
332
333
)
333
334
} ) ;
334
335
let union_fields = helper
335
- . ffi_items
336
+ . filtered_ffi_items
336
337
. unions ( )
337
338
. iter ( )
338
339
. flat_map ( |u| u. fields . iter ( ) . map ( move |f| ( u, f) ) )
339
340
. filter ( |( u, f) | {
340
- !should_skip ( MapInput :: UnionField ( u, f) )
341
- && ! should_skip ( MapInput :: UnionFieldType ( u, f) )
342
- && f. public
341
+ !( should_skip ( MapInput :: UnionField ( u, f) )
342
+ || should_skip ( MapInput :: UnionFieldType ( u, f) )
343
+ || ! f. public )
343
344
} )
344
345
. map ( |( u, f) | {
345
346
(
@@ -532,6 +533,7 @@ fn foreign_fn_test_ident(ident: &str) -> BoxStr {
532
533
533
534
/// Wrap methods that depend on both ffi items and the generator.
534
535
pub ( crate ) struct TranslateHelper < ' a > {
536
+ filtered_ffi_items : FfiItems ,
535
537
ffi_items : & ' a FfiItems ,
536
538
generator : & ' a TestGenerator ,
537
539
translator : Translator < ' a > ,
@@ -540,11 +542,49 @@ pub(crate) struct TranslateHelper<'a> {
540
542
impl < ' a > TranslateHelper < ' a > {
541
543
/// Create a new translation helper.
542
544
pub ( crate ) fn new ( ffi_items : & ' a FfiItems , generator : & ' a TestGenerator ) -> Self {
543
- Self {
545
+ let filtered_ffi_items = ffi_items. clone ( ) ;
546
+ let mut helper = Self {
547
+ filtered_ffi_items,
544
548
ffi_items,
545
549
generator,
546
550
translator : Translator :: new ( ffi_items, generator) ,
551
+ } ;
552
+ helper. filter_ffi_items ( ) ;
553
+
554
+ helper
555
+ }
556
+
557
+ /// Skips entire items such as structs, constants, and aliases from being tested.
558
+ ///
559
+ /// Does not skip specific tests or specific fields. If `skip_private` is true,
560
+ /// it will skip tests for all private items.
561
+ fn filter_ffi_items ( & mut self ) {
562
+ let verbose = self . generator . verbose_skip ;
563
+
564
+ macro_rules! filter {
565
+ ( $field: ident, $variant: ident, $label: literal) => { {
566
+ let skipped = self . filtered_ffi_items. $field. extract_if( .., |item| {
567
+ ( self . generator. skip_private && !item. public)
568
+ || self
569
+ . generator
570
+ . skips
571
+ . iter( )
572
+ . any( |f| f( & MapInput :: $variant( item) ) )
573
+ } ) ;
574
+ for item in skipped {
575
+ if verbose {
576
+ eprintln!( "Skipping {} \" {}\" " , $label, item. ident( ) )
577
+ }
578
+ }
579
+ } } ;
547
580
}
581
+
582
+ filter ! ( aliases, Alias , "alias" ) ;
583
+ filter ! ( constants, Const , "const" ) ;
584
+ filter ! ( structs, Struct , "struct" ) ;
585
+ filter ! ( unions, Union , "union" ) ;
586
+ filter ! ( foreign_functions, Fn , "fn" ) ;
587
+ filter ! ( foreign_statics, Static , "static" ) ;
548
588
}
549
589
550
590
/// Returns the equivalent C/Cpp identifier of the Rust item.
@@ -565,9 +605,9 @@ impl<'a> TranslateHelper<'a> {
565
605
// inside of `Fn` when parsed.
566
606
MapInput :: Fn ( _) => unimplemented ! ( ) ,
567
607
// For structs/unions/aliases, their type is the same as their identifier.
568
- MapInput :: Alias ( a) => ( a. ident ( ) , cdecl:: named ( a. ident ( ) , cdecl :: Constness :: Mut ) ) ,
569
- MapInput :: Struct ( s) => ( s. ident ( ) , cdecl:: named ( s. ident ( ) , cdecl :: Constness :: Mut ) ) ,
570
- MapInput :: Union ( u) => ( u. ident ( ) , cdecl:: named ( u. ident ( ) , cdecl :: Constness :: Mut ) ) ,
608
+ MapInput :: Alias ( a) => ( a. ident ( ) , cdecl:: named ( a. ident ( ) , Constness :: Mut ) ) ,
609
+ MapInput :: Struct ( s) => ( s. ident ( ) , cdecl:: named ( s. ident ( ) , Constness :: Mut ) ) ,
610
+ MapInput :: Union ( u) => ( u. ident ( ) , cdecl:: named ( u. ident ( ) , Constness :: Mut ) ) ,
571
611
572
612
MapInput :: StructType ( _) => panic ! ( "MapInput::StructType is not allowed!" ) ,
573
613
MapInput :: UnionType ( _) => panic ! ( "MapInput::UnionType is not allowed!" ) ,
@@ -584,7 +624,7 @@ impl<'a> TranslateHelper<'a> {
584
624
)
585
625
} ) ?;
586
626
587
- let item = if self . ffi_items . contains_struct ( ident ) {
627
+ let item = if self . ffi_items . contains_struct ( & ty ) {
588
628
MapInput :: StructType ( & ty)
589
629
} else if self . ffi_items . contains_union ( ident) {
590
630
MapInput :: UnionType ( & ty)
0 commit comments