@@ -615,8 +615,7 @@ public async Task ReducePointerDimensionAsync(List<INamedTypeSymbol> symbols)
615615 locations . AddRange ( references . SelectMany ( r => r . Locations ) . Select ( rl => rl . Location ) ) ;
616616 }
617617
618- // Reduce the pointer dimension of all reference locations
619- // Also, do modifications in groups to prevent overwriting changes
618+ // Group the locations by source tree. This will be used to prevent accidentally overwriting changes.
620619 var locationsBySourcetree = locations . GroupBy ( l => l . SourceTree ) ;
621620 foreach ( var group in locationsBySourcetree )
622621 {
@@ -635,6 +634,8 @@ public async Task ReducePointerDimensionAsync(List<INamedTypeSymbol> symbols)
635634 var syntaxRoot = await syntaxTree . GetRootAsync ( ct ) ;
636635
637636 // Modify each location
637+ // We order the locations so that we modify starting from the end of the file
638+ // This way we prevent changes from being accidentally overwriting changes
638639 foreach ( var location in group . OrderByDescending ( l => l . SourceSpan . Start ) )
639640 {
640641 var syntaxNode = syntaxRoot . FindNode ( location . SourceSpan ) ;
@@ -669,91 +670,4 @@ public async Task ReducePointerDimensionAsync(List<INamedTypeSymbol> symbols)
669670
670671 public override SyntaxNode ? VisitPointerType ( PointerTypeSyntax node ) => node . ElementType ;
671672 }
672-
673- private class OldPointerDimensionReducer ( Dictionary < string , Dictionary < string , string > > handles ) : CSharpSyntaxRewriter
674- {
675- /// <summary>
676- /// The current scope i.e. fully qualified type name.
677- /// </summary>
678- private string _currentScope = string . Empty ;
679-
680- private bool _isPointerType ;
681- private bool _derefPtr ;
682-
683- // We restrict the allowed parents to hopefully avoid mistaking references to e.g. variable names as type
684- // references.
685- private static bool SkipTypeNode ( SyntaxNode node ) =>
686- node . Parent
687- is not (
688- TypeSyntax
689- or BaseParameterSyntax
690- or BaseMethodDeclarationSyntax
691- or VariableDeclarationSyntax
692- )
693- or QualifiedNameSyntax ;
694-
695- private SyntaxNode ? VisitType < T > ( T type , SyntaxToken identifier , Func < T , SyntaxNode ? > @base )
696- where T : SyntaxNode
697- {
698- var before = _currentScope ;
699- _currentScope = string . IsNullOrWhiteSpace ( _currentScope )
700- ? $ "{ type . NamespaceFromSyntaxNode ( ) } .{ identifier } "
701- : $ "{ _currentScope } .{ identifier } ";
702- var ret = @base ( type ) ;
703- _currentScope = before ;
704- return ret ;
705- }
706-
707- public override SyntaxNode ? VisitPointerType ( PointerTypeSyntax node )
708- {
709- if ( SkipTypeNode ( node ) )
710- {
711- return node ;
712- }
713- var before = _isPointerType ;
714- _isPointerType = true ;
715- var ret = base . VisitPointerType ( node ) ;
716- _isPointerType = before ;
717- if ( _derefPtr && ret is PointerTypeSyntax ptr )
718- {
719- ret = ptr . ElementType ;
720- }
721-
722- _derefPtr = false ;
723- return ret ;
724- }
725-
726- public override SyntaxNode VisitGenericName ( GenericNameSyntax node ) => node ;
727-
728- public override SyntaxNode VisitIdentifierName ( IdentifierNameSyntax node )
729- {
730- if ( SkipTypeNode ( node ) )
731- {
732- return node ;
733- }
734-
735- _derefPtr =
736- handles . TryGetValue ( node . Identifier . ToString ( ) , out var applicableScopes )
737- && applicableScopes . ContainsKey ( _currentScope ) ;
738- return _derefPtr ? node . WithIdentifier ( Identifier ( $ "{ node . Identifier } Handle") ) : node ;
739- }
740-
741- public override SyntaxNode ? VisitStructDeclaration ( StructDeclarationSyntax node ) =>
742- VisitType ( node , node . Identifier , base . VisitStructDeclaration ) ;
743-
744- public override SyntaxNode ? VisitClassDeclaration ( ClassDeclarationSyntax node ) =>
745- VisitType ( node , node . Identifier , base . VisitClassDeclaration ) ;
746-
747- public override SyntaxNode ? VisitRecordDeclaration ( RecordDeclarationSyntax node ) =>
748- VisitType ( node , node . Identifier , base . VisitRecordDeclaration ) ;
749-
750- public override SyntaxNode ? VisitEnumDeclaration ( EnumDeclarationSyntax node ) =>
751- VisitType ( node , node . Identifier , base . VisitEnumDeclaration ) ;
752-
753- public override SyntaxNode ? VisitDelegateDeclaration ( DelegateDeclarationSyntax node ) =>
754- VisitType ( node , node . Identifier , base . VisitDelegateDeclaration ) ;
755-
756- public override SyntaxNode ? VisitInterfaceDeclaration ( InterfaceDeclarationSyntax node ) =>
757- VisitType ( node , node . Identifier , base . VisitInterfaceDeclaration ) ;
758- }
759673}
0 commit comments