@@ -498,23 +498,21 @@ impl Cursor {
498
498
}
499
499
}
500
500
501
- /// Traverse this curser 's children, sorted by where they appear in source code.
501
+ /// Traverse all of this cursor 's children, sorted by where they appear in source code.
502
502
///
503
503
/// Call the given function on each AST node traversed.
504
504
pub ( crate ) fn visit_sorted < Visitor > (
505
505
& self ,
506
506
ctx : & mut BindgenContext ,
507
507
mut visitor : Visitor ,
508
508
) where
509
- Visitor : FnMut ( & mut BindgenContext , Cursor ) -> CXChildVisitResult ,
509
+ Visitor : FnMut ( & mut BindgenContext , Cursor ) ,
510
510
{
511
511
let mut children = self . collect_children ( ) ;
512
-
513
512
for child in & children {
514
513
if child. kind ( ) == CXCursor_InclusionDirective {
515
514
if let Some ( included_file) = child. get_included_file_name ( ) {
516
515
let location = child. location ( ) ;
517
-
518
516
let ( source_file, _, _, offset) = location. location ( ) ;
519
517
520
518
if let Some ( source_file) = source_file. name ( ) {
@@ -523,19 +521,62 @@ impl Cursor {
523
521
}
524
522
}
525
523
}
526
-
527
- children. sort_by ( |child1, child2| {
528
- child1
529
- . location ( )
530
- . partial_cmp_with_context ( & child2. location ( ) , ctx)
531
- . unwrap_or ( std:: cmp:: Ordering :: Equal )
532
- } ) ;
533
-
524
+ children
525
+ . sort_by ( |child1, child2| child1. cmp_by_source_order ( child2, ctx) ) ;
534
526
for child in children {
535
527
visitor ( ctx, child) ;
536
528
}
537
529
}
538
530
531
+ /// Compare source order of two cursors, considering `#include` directives.
532
+ ///
533
+ /// Built-in items provided by the compiler (which don't have a source file),
534
+ /// are sorted first. Remaining files are sorted by their position in the source file.
535
+ /// If the items' source files differ, they are sorted by the position of the first
536
+ /// `#include` for their source file. If no source files are included, `None` is returned.
537
+ fn cmp_by_source_order (
538
+ & self ,
539
+ other : & Self ,
540
+ ctx : & BindgenContext ,
541
+ ) -> cmp:: Ordering {
542
+ let ( file, _, _, offset) = self . location ( ) . location ( ) ;
543
+ let ( other_file, _, _, other_offset) = other. location ( ) . location ( ) ;
544
+
545
+ let ( file, other_file) = match ( file. name ( ) , other_file. name ( ) ) {
546
+ ( Some ( file) , Some ( other_file) ) => ( file, other_file) ,
547
+ // Built-in definitions should come first.
548
+ ( Some ( _) , None ) => return cmp:: Ordering :: Greater ,
549
+ ( None , Some ( _) ) => return cmp:: Ordering :: Less ,
550
+ ( None , None ) => return cmp:: Ordering :: Equal ,
551
+ } ;
552
+
553
+ if file == other_file {
554
+ // Both items are in the same source file, compare by byte offset.
555
+ return offset. cmp ( & other_offset) ;
556
+ }
557
+
558
+ let include_location = ctx. included_file_location ( & file) ;
559
+ let other_include_location = ctx. included_file_location ( & other_file) ;
560
+ match ( include_location, other_include_location) {
561
+ ( Some ( ( file2, offset2) ) , _) if file2 == other_file => {
562
+ offset2. cmp ( & other_offset)
563
+ }
564
+ ( Some ( _) , None ) => cmp:: Ordering :: Greater ,
565
+ ( _, Some ( ( other_file2, other_offset2) ) ) if file == other_file2 => {
566
+ offset. cmp ( & other_offset2)
567
+ }
568
+ ( None , Some ( _) ) => cmp:: Ordering :: Less ,
569
+ ( Some ( ( file2, offset2) ) , Some ( ( other_file2, other_offset2) ) ) => {
570
+ if file2 == other_file2 {
571
+ offset2. cmp ( & other_offset2)
572
+ } else {
573
+ cmp:: Ordering :: Equal
574
+ }
575
+ }
576
+ ( None , None ) => cmp:: Ordering :: Equal ,
577
+ }
578
+ }
579
+
539
580
/// Collect all of this cursor's children into a vec and return them.
540
581
pub ( crate ) fn collect_children ( & self ) -> Vec < Cursor > {
541
582
let mut children = vec ! [ ] ;
@@ -1564,63 +1605,6 @@ impl SourceLocation {
1564
1605
}
1565
1606
}
1566
1607
1567
- impl SourceLocation {
1568
- /// Compare source locations, also considering `#include` directives.
1569
- ///
1570
- /// Built-in items provided by the compiler (which don't have a source file),
1571
- /// are sorted first. Remaining files are sorted by their position in the source file.
1572
- /// If the items' source files differ, they are sorted by the position of the first
1573
- /// `#include` for their source file. If no source files are included, `None` is returned.
1574
- pub ( crate ) fn partial_cmp_with_context (
1575
- & self ,
1576
- other : & Self ,
1577
- ctx : & BindgenContext ,
1578
- ) -> Option < cmp:: Ordering > {
1579
- let ( file, _, _, offset) = self . location ( ) ;
1580
- let ( other_file, _, _, other_offset) = other. location ( ) ;
1581
-
1582
- match ( file. name ( ) , other_file. name ( ) ) {
1583
- ( Some ( file) , Some ( other_file) ) => {
1584
- if file == other_file {
1585
- return offset. partial_cmp ( & other_offset) ;
1586
- }
1587
-
1588
- let include_location = ctx. included_file_location ( & file) ;
1589
- let other_include_location =
1590
- ctx. included_file_location ( & other_file) ;
1591
-
1592
- match ( include_location, other_include_location) {
1593
- ( Some ( ( file2, offset2) ) , _) if file2 == other_file => {
1594
- offset2. partial_cmp ( & other_offset)
1595
- }
1596
- ( Some ( _) , None ) => Some ( cmp:: Ordering :: Greater ) ,
1597
- ( _, Some ( ( other_file2, other_offset2) ) )
1598
- if file == other_file2 =>
1599
- {
1600
- offset. partial_cmp ( & other_offset2)
1601
- }
1602
- ( None , Some ( _) ) => Some ( cmp:: Ordering :: Less ) ,
1603
- (
1604
- Some ( ( file2, offset2) ) ,
1605
- Some ( ( other_file2, other_offset2) ) ,
1606
- ) => {
1607
- if file2 == other_file2 {
1608
- offset2. partial_cmp ( & other_offset2)
1609
- } else {
1610
- None
1611
- }
1612
- }
1613
- ( None , None ) => Some ( cmp:: Ordering :: Equal ) ,
1614
- }
1615
- }
1616
- // Built-in definitions should come first.
1617
- ( Some ( _) , None ) => Some ( cmp:: Ordering :: Greater ) ,
1618
- ( None , Some ( _) ) => Some ( cmp:: Ordering :: Less ) ,
1619
- ( None , None ) => Some ( cmp:: Ordering :: Equal ) ,
1620
- }
1621
- }
1622
- }
1623
-
1624
1608
impl fmt:: Display for SourceLocation {
1625
1609
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
1626
1610
let ( file, line, col, _) = self . location ( ) ;
0 commit comments