|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using System.Collections.Concurrent; |
| 5 | +using System.Collections.Immutable; |
| 6 | +using Microsoft.CodeAnalysis; |
| 7 | +using Microsoft.CodeAnalysis.FindSymbols; |
| 8 | +using Microsoft.Extensions.Logging; |
| 9 | + |
| 10 | +namespace Silk.NET.SilkTouch.Mods.LocationTransformation; |
| 11 | + |
| 12 | +/// <summary> |
| 13 | +/// Utilities for transforming <see cref="Location"/>s. |
| 14 | +/// </summary> |
| 15 | +public static class LocationTransformationUtils |
| 16 | +{ |
| 17 | + /// <summary> |
| 18 | + /// Finds all references to the specified symbols and applies the specified transformations to them. |
| 19 | + /// Transformations will be done in order. |
| 20 | + /// </summary> |
| 21 | + public static async Task ModifyAllReferencesAsync( |
| 22 | + IModContext ctx, |
| 23 | + ILogger logger, |
| 24 | + IEnumerable<ISymbol> symbols, |
| 25 | + IEnumerable<LocationTransformer> transformers, |
| 26 | + CancellationToken ct = default) |
| 27 | + { |
| 28 | + // Convert to lists |
| 29 | + // The parameters being IEnumerables is for convenience |
| 30 | + var symbolList = symbols.ToList(); |
| 31 | + var transformersList = transformers.ToList(); |
| 32 | + |
| 33 | + var project = ctx.SourceProject; |
| 34 | + if (project == null) |
| 35 | + { |
| 36 | + return; |
| 37 | + } |
| 38 | + |
| 39 | + var compilation = await project.GetCompilationAsync(ct); |
| 40 | + if (compilation == null) |
| 41 | + { |
| 42 | + return; |
| 43 | + } |
| 44 | + |
| 45 | + var locations = new ConcurrentBag<(Location Location, LocationTransformerContext Context)>(); |
| 46 | + |
| 47 | + // Add the locations of the declaration identifiers for each symbol |
| 48 | + foreach (var symbol in symbolList) |
| 49 | + { |
| 50 | + foreach (var declaringSyntaxReference in symbol.DeclaringSyntaxReferences) |
| 51 | + { |
| 52 | + locations.Add((declaringSyntaxReference.GetSyntax().GetLocation(), |
| 53 | + new LocationTransformerContext(symbol, true, false))); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + // Find all locations where the symbols are referenced |
| 58 | + // TODO this needs parallelisation config & be sensitive to the environment (future src generator form factor?) |
| 59 | + var documents = project.Documents.ToImmutableHashSet(); |
| 60 | + await Parallel.ForEachAsync( |
| 61 | + symbolList, |
| 62 | + ct, |
| 63 | + async (symbol, _) => { |
| 64 | + var references = await SymbolFinder.FindReferencesAsync(symbol, project.Solution, documents, ct); |
| 65 | + foreach (var reference in references) |
| 66 | + { |
| 67 | + foreach (var location in reference.Locations) |
| 68 | + { |
| 69 | + locations.Add((location.Location, |
| 70 | + new LocationTransformerContext(symbol, false, |
| 71 | + location.IsCandidateLocation || location.IsImplicit))); |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + ); |
| 76 | + |
| 77 | + // Group the locations by source tree. This will be used to prevent accidentally overwriting changes. |
| 78 | + var locationsBySourcetree = locations.GroupBy(l => l.Location.SourceTree); |
| 79 | + foreach (var group in locationsBySourcetree) |
| 80 | + { |
| 81 | + var syntaxTree = group.Key; |
| 82 | + if (syntaxTree == null) |
| 83 | + { |
| 84 | + continue; |
| 85 | + } |
| 86 | + |
| 87 | + var document = project.GetDocument(syntaxTree); |
| 88 | + if (document == null) |
| 89 | + { |
| 90 | + continue; |
| 91 | + } |
| 92 | + |
| 93 | + var syntaxRoot = await syntaxTree.GetRootAsync(ct); |
| 94 | + |
| 95 | + // Modify each location |
| 96 | + // We order the locations so that we modify starting from the end of the file |
| 97 | + // This way we prevent changes from being accidentally overwriting changes |
| 98 | + foreach (var (location, context) in group.OrderByDescending(l => l.Location.SourceSpan.Start)) |
| 99 | + { |
| 100 | + foreach (var transformer in transformersList) |
| 101 | + { |
| 102 | + var syntaxNode = syntaxRoot.FindNode(location.SourceSpan); |
| 103 | + var nodeToModify = transformer.GetNodeToModify(syntaxNode, context); |
| 104 | + if (nodeToModify == null) |
| 105 | + { |
| 106 | + continue; |
| 107 | + } |
| 108 | + |
| 109 | + var newNode = transformer.Visit(nodeToModify); |
| 110 | + syntaxRoot = syntaxRoot.ReplaceNode(nodeToModify, newNode); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + // Commit the changes to the project |
| 115 | + var newDocument = document.WithSyntaxRoot(syntaxRoot); |
| 116 | + project = newDocument.Project; |
| 117 | + } |
| 118 | + |
| 119 | + ctx.SourceProject = project; |
| 120 | + } |
| 121 | +} |
0 commit comments