@@ -86,6 +86,7 @@ use std::{env, iter};
86
86
// Some convenient typedefs for a fast hash map and hash set.
87
87
type HashMap < K , V > = :: rustc_hash:: FxHashMap < K , V > ;
88
88
type HashSet < K > = :: rustc_hash:: FxHashSet < K > ;
89
+ use quote:: ToTokens ;
89
90
pub ( crate ) use std:: collections:: hash_map:: Entry ;
90
91
91
92
/// Default prefix for the anon fields.
@@ -587,6 +588,10 @@ impl Builder {
587
588
output_vector. push ( "--vtable-generation" . into ( ) ) ;
588
589
}
589
590
591
+ if self . options . sort_semantically {
592
+ output_vector. push ( "--sort-semantically" . into ( ) ) ;
593
+ }
594
+
590
595
// Add clang arguments
591
596
592
597
output_vector. push ( "--" . into ( ) ) ;
@@ -1476,6 +1481,14 @@ impl Builder {
1476
1481
self
1477
1482
}
1478
1483
1484
+ /// If true, enables the sorting of the output in a predefined manner
1485
+ ///
1486
+ /// TODO: Perhaps move the sorting order out into a config
1487
+ pub fn sort_semantically ( mut self , doit : bool ) -> Self {
1488
+ self . options . sort_semantically = doit;
1489
+ self
1490
+ }
1491
+
1479
1492
/// Generate the Rust bindings using the options built up thus far.
1480
1493
pub fn generate ( mut self ) -> Result < Bindings , BindgenError > {
1481
1494
// Add any extra arguments from the environment to the clang command line.
@@ -2005,6 +2018,9 @@ struct BindgenOptions {
2005
2018
2006
2019
/// Emit vtable functions.
2007
2020
vtable_generation : bool ,
2021
+
2022
+ /// Sort the code generation
2023
+ sort_semantically : bool ,
2008
2024
}
2009
2025
2010
2026
/// TODO(emilio): This is sort of a lie (see the error message that results from
@@ -2153,6 +2169,7 @@ impl Default for BindgenOptions {
2153
2169
c_naming : false ,
2154
2170
force_explicit_padding : false ,
2155
2171
vtable_generation : false ,
2172
+ sort_semantically : false ,
2156
2173
}
2157
2174
}
2158
2175
}
@@ -2437,6 +2454,49 @@ impl Bindings {
2437
2454
2438
2455
let ( items, options) = codegen:: codegen ( context) ;
2439
2456
2457
+ if options. sort_semantically {
2458
+ let module_wrapped_tokens =
2459
+ quote ! ( mod wrapper_for_sorting_hack { #( #items ) * } ) ;
2460
+ let mut syn_parsed_items =
2461
+ syn:: parse2 :: < syn:: ItemMod > ( module_wrapped_tokens)
2462
+ . unwrap ( )
2463
+ . content
2464
+ . unwrap ( )
2465
+ . 1 ;
2466
+
2467
+ syn_parsed_items. sort_by_key ( |item| match item {
2468
+ syn:: Item :: Type ( _) => 0 ,
2469
+ syn:: Item :: Struct ( _) => 1 ,
2470
+ syn:: Item :: Const ( _) => 2 ,
2471
+ syn:: Item :: Fn ( _) => 3 ,
2472
+ syn:: Item :: Enum ( _) => 4 ,
2473
+ syn:: Item :: Union ( _) => 5 ,
2474
+ syn:: Item :: Static ( _) => 6 ,
2475
+ syn:: Item :: Trait ( _) => 7 ,
2476
+ syn:: Item :: TraitAlias ( _) => 8 ,
2477
+ syn:: Item :: Impl ( _) => 9 ,
2478
+ syn:: Item :: Mod ( _) => 10 ,
2479
+ syn:: Item :: Use ( _) => 11 ,
2480
+ syn:: Item :: Verbatim ( _) => 12 ,
2481
+ syn:: Item :: ExternCrate ( _) => 13 ,
2482
+ syn:: Item :: ForeignMod ( _) => 14 ,
2483
+ syn:: Item :: Macro ( _) => 15 ,
2484
+ syn:: Item :: Macro2 ( _) => 16 ,
2485
+ _ => 18 ,
2486
+ } ) ;
2487
+
2488
+ let items = syn_parsed_items
2489
+ . into_iter ( )
2490
+ . map ( |item| item. into_token_stream ( ) ) ;
2491
+
2492
+ return Ok ( Bindings {
2493
+ options,
2494
+ module : quote ! {
2495
+ #( #items ) *
2496
+ } ,
2497
+ } ) ;
2498
+ }
2499
+
2440
2500
Ok ( Bindings {
2441
2501
options,
2442
2502
module : quote ! {
0 commit comments