Skip to content

Commit cc53cf4

Browse files
committed
Update location transformation code with the Microsoft job changes
1 parent 821a7e3 commit cc53cf4

6 files changed

Lines changed: 98 additions & 33 deletions

File tree

.silktouch/d48a9fc4a502f7c6.stout

0 Bytes
Binary file not shown.

sources/SilkTouch/SilkTouch/Mods/LocationTransformation/IdentifierRenamingTransformer.cs

Lines changed: 65 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ namespace Silk.NET.SilkTouch.Mods.LocationTransformation;
1515
/// <param name="includeCandidateLocations">Should candidate references or implicit references be renamed?</param>
1616
public class IdentifierRenamingTransformer(IEnumerable<(ISymbol Symbol, string NewName)> newNamesBySymbol, bool includeDeclarations = true, bool includeCandidateLocations = false) : LocationTransformer
1717
{
18+
// Identifiers can also be referenced within XML doc, which are trivia nodes.
19+
/// <inheritdoc />
20+
public override bool VisitIntoStructuredTrivia => true;
21+
1822
private LocationTransformerContext _context;
1923
private Dictionary<ISymbol, string> _newNameLookup = newNamesBySymbol.Select(t => new KeyValuePair<ISymbol, string>(t.Symbol, t.NewName)).ToDictionary(SymbolEqualityComparer.Default);
2024

@@ -36,71 +40,116 @@ public class IdentifierRenamingTransformer(IEnumerable<(ISymbol Symbol, string N
3640
return current;
3741
}
3842

39-
private SyntaxToken GetNewName() => Identifier(_newNameLookup[_context.Symbol]);
43+
private SyntaxToken GetNewName(string currentName)
44+
{
45+
var symbolName = _context.Symbol switch
46+
{
47+
// Constructor/destructor symbols have a name of .ctor/.dtor, which isn't what we want
48+
IMethodSymbol { MethodKind: MethodKind.Constructor or MethodKind.Destructor } methodSymbol => methodSymbol.ContainingType.Name,
49+
_ => _context.Symbol.Name,
50+
};
51+
52+
if (currentName != symbolName)
53+
{
54+
return Identifier(currentName);
55+
}
56+
57+
return Identifier(_newNameLookup[_context.Symbol]);
58+
}
4059

4160
/// <inheritdoc />
4261
public override SyntaxNode? VisitIdentifierName(IdentifierNameSyntax node)
43-
=> IdentifierName(GetNewName());
62+
=> IdentifierName(GetNewName(node.Identifier.ValueText))
63+
.WithLeadingTrivia(node.GetLeadingTrivia())
64+
.WithTrailingTrivia(node.GetTrailingTrivia());
4465

4566
// ----- Types -----
4667

4768
/// <inheritdoc />
4869
public override SyntaxNode? VisitClassDeclaration(ClassDeclarationSyntax node)
49-
=> node.WithIdentifier(GetNewName());
70+
=> node.WithIdentifier(GetNewName(node.Identifier.ValueText))
71+
.WithLeadingTrivia(node.GetLeadingTrivia().Select(VisitTrivia))
72+
.WithTrailingTrivia(node.GetTrailingTrivia());
5073

5174
/// <inheritdoc />
5275
public override SyntaxNode? VisitStructDeclaration(StructDeclarationSyntax node)
53-
=> node.WithIdentifier(GetNewName());
76+
=> node.WithIdentifier(GetNewName(node.Identifier.ValueText))
77+
.WithLeadingTrivia(node.GetLeadingTrivia().Select(VisitTrivia))
78+
.WithTrailingTrivia(node.GetTrailingTrivia());
5479

5580
/// <inheritdoc />
5681
public override SyntaxNode? VisitInterfaceDeclaration(InterfaceDeclarationSyntax node)
57-
=> node.WithIdentifier(GetNewName());
82+
=> node.WithIdentifier(GetNewName(node.Identifier.ValueText))
83+
.WithLeadingTrivia(node.GetLeadingTrivia().Select(VisitTrivia))
84+
.WithTrailingTrivia(node.GetTrailingTrivia());
5885

5986
/// <inheritdoc />
6087
public override SyntaxNode? VisitRecordDeclaration(RecordDeclarationSyntax node)
61-
=> node.WithIdentifier(GetNewName());
88+
=> node.WithIdentifier(GetNewName(node.Identifier.ValueText))
89+
.WithLeadingTrivia(node.GetLeadingTrivia().Select(VisitTrivia))
90+
.WithTrailingTrivia(node.GetTrailingTrivia());
6291

6392
/// <inheritdoc />
6493
public override SyntaxNode? VisitDelegateDeclaration(DelegateDeclarationSyntax node)
65-
=> node.WithIdentifier(GetNewName());
94+
=> node.WithIdentifier(GetNewName(node.Identifier.ValueText))
95+
.WithLeadingTrivia(node.GetLeadingTrivia().Select(VisitTrivia))
96+
.WithTrailingTrivia(node.GetTrailingTrivia());
6697

6798
/// <inheritdoc />
6899
public override SyntaxNode? VisitEnumDeclaration(EnumDeclarationSyntax node)
69-
=> node.WithIdentifier(GetNewName());
100+
=> node.WithIdentifier(GetNewName(node.Identifier.ValueText))
101+
.WithLeadingTrivia(node.GetLeadingTrivia().Select(VisitTrivia))
102+
.WithTrailingTrivia(node.GetTrailingTrivia());
70103

71104
// ----- Members -----
72105

73106
/// <inheritdoc />
74107
public override SyntaxNode? VisitEnumMemberDeclaration(EnumMemberDeclarationSyntax node)
75-
=> node.WithIdentifier(GetNewName());
108+
=> node.WithIdentifier(GetNewName(node.Identifier.ValueText))
109+
.WithLeadingTrivia(node.GetLeadingTrivia().Select(VisitTrivia))
110+
.WithTrailingTrivia(node.GetTrailingTrivia());
76111

77112
/// <inheritdoc />
78113
public override SyntaxNode? VisitEventDeclaration(EventDeclarationSyntax node)
79-
=> node.WithIdentifier(GetNewName());
114+
=> node.WithIdentifier(GetNewName(node.Identifier.ValueText))
115+
.WithLeadingTrivia(node.GetLeadingTrivia().Select(VisitTrivia))
116+
.WithTrailingTrivia(node.GetTrailingTrivia());
80117

81118
/// <inheritdoc />
82119
public override SyntaxNode? VisitMethodDeclaration(MethodDeclarationSyntax node)
83-
=> node.WithIdentifier(GetNewName());
120+
=> node.WithIdentifier(GetNewName(node.Identifier.ValueText))
121+
.WithLeadingTrivia(node.GetLeadingTrivia().Select(VisitTrivia))
122+
.WithTrailingTrivia(node.GetTrailingTrivia());
84123

85124
/// <inheritdoc />
86125
public override SyntaxNode? VisitPropertyDeclaration(PropertyDeclarationSyntax node)
87-
=> node.WithIdentifier(GetNewName());
126+
=> node.WithIdentifier(GetNewName(node.Identifier.ValueText))
127+
.WithLeadingTrivia(node.GetLeadingTrivia().Select(VisitTrivia))
128+
.WithTrailingTrivia(node.GetTrailingTrivia());
88129

89130
/// <inheritdoc />
90131
public override SyntaxNode? VisitConstructorDeclaration(ConstructorDeclarationSyntax node)
91-
=> node.WithIdentifier(GetNewName());
132+
=> node.WithIdentifier(GetNewName(node.Identifier.ValueText))
133+
.WithLeadingTrivia(node.GetLeadingTrivia().Select(VisitTrivia))
134+
.WithTrailingTrivia(node.GetTrailingTrivia());
92135

93136
/// <inheritdoc />
94137
public override SyntaxNode? VisitDestructorDeclaration(DestructorDeclarationSyntax node)
95-
=> node.WithIdentifier(GetNewName());
138+
=> node.WithIdentifier(GetNewName(node.Identifier.ValueText))
139+
.WithLeadingTrivia(node.GetLeadingTrivia().Select(VisitTrivia))
140+
.WithTrailingTrivia(node.GetTrailingTrivia());
96141

97142
// ----- Other -----
98143

99144
/// <inheritdoc />
100145
public override SyntaxNode? VisitVariableDeclarator(VariableDeclaratorSyntax node)
101-
=> node.WithIdentifier(GetNewName());
146+
=> node.WithIdentifier(GetNewName(node.Identifier.ValueText))
147+
.WithLeadingTrivia(node.GetLeadingTrivia())
148+
.WithTrailingTrivia(node.GetTrailingTrivia());
102149

103150
/// <inheritdoc />
104151
public override SyntaxNode? VisitTypeParameter(TypeParameterSyntax node)
105-
=> node.WithIdentifier(GetNewName());
152+
=> node.WithIdentifier(GetNewName(node.Identifier.ValueText))
153+
.WithLeadingTrivia(node.GetLeadingTrivia())
154+
.WithTrailingTrivia(node.GetTrailingTrivia());
106155
}

sources/SilkTouch/SilkTouch/Mods/LocationTransformation/LocationTransformationUtils.cs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Collections.Concurrent;
55
using System.Collections.Immutable;
66
using Microsoft.CodeAnalysis;
7+
using Microsoft.CodeAnalysis.CSharp.Syntax;
78
using Microsoft.CodeAnalysis.FindSymbols;
89
using Microsoft.Extensions.Logging;
910
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
@@ -21,23 +22,22 @@ public static class LocationTransformationUtils
2122
/// </summary>
2223
public static async Task ModifyAllReferencesAsync(
2324
IModContext ctx,
24-
ILogger logger,
2525
IEnumerable<ISymbol> symbols,
2626
IEnumerable<LocationTransformer> transformers,
27+
ILogger? logger = null,
2728
CancellationToken ct = default)
2829
{
2930
// Convert to lists
3031
// The parameters being IEnumerables is for convenience
3132
var symbolList = symbols.ToList();
3233
var transformersList = transformers.ToList();
3334

34-
var project = ctx.SourceProject;
35-
if (project == null)
35+
if (ctx.SourceProject == null)
3636
{
3737
return;
3838
}
3939

40-
var compilation = await project.GetCompilationAsync(ct);
40+
var compilation = await ctx.SourceProject.GetCompilationAsync(ct);
4141
if (compilation == null)
4242
{
4343
return;
@@ -57,12 +57,12 @@ public static async Task ModifyAllReferencesAsync(
5757

5858
// Find all locations where the symbols are referenced
5959
// TODO this needs parallelisation config & be sensitive to the environment (future src generator form factor?)
60-
var documents = project.Documents.ToImmutableHashSet();
60+
ImmutableHashSet<Document> documents = [..ctx.SourceProject.Documents, ..ctx.TestProject?.Documents ?? []];
6161
await Parallel.ForEachAsync(
6262
symbolList,
6363
ct,
6464
async (symbol, _) => {
65-
var references = await SymbolFinder.FindReferencesAsync(symbol, project.Solution, documents, ct);
65+
var references = await SymbolFinder.FindReferencesAsync(symbol, ctx.SourceProject.Solution, documents, ct);
6666
foreach (var reference in references)
6767
{
6868
foreach (var location in reference.Locations)
@@ -76,6 +76,7 @@ await Parallel.ForEachAsync(
7676
);
7777

7878
// Group the locations by source tree. This will be used to prevent accidentally overwriting changes.
79+
var solution = ctx.SourceProject.Solution;
7980
var locationsBySourcetree = locations.GroupBy(l => l.Location.SourceTree);
8081
foreach (var group in locationsBySourcetree)
8182
{
@@ -85,7 +86,7 @@ await Parallel.ForEachAsync(
8586
continue;
8687
}
8788

88-
var document = project.GetDocument(syntaxTree);
89+
var document = solution.GetDocument(syntaxTree);
8990
if (document == null)
9091
{
9192
continue;
@@ -123,11 +124,11 @@ await Parallel.ForEachAsync(
123124
}
124125
}
125126

126-
// Commit the changes to the project
127+
// Commit the changes to the solution
127128
var newDocument = document.WithSyntaxRoot(syntaxRoot.NormalizeWhitespace());
128-
project = newDocument.Project;
129+
solution = newDocument.Project.Solution;
129130
}
130131

131-
ctx.SourceProject = project;
132+
ctx.SourceProject = solution.GetProject(ctx.SourceProject.Id);
132133
}
133134
}

sources/SilkTouch/SilkTouch/Mods/PrettifyNames.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,6 @@ await proj.GetCompilationAsync(ct)
275275
);
276276
await NameUtils.RenameAllAsync(
277277
ctx,
278-
logger,
279278
types.SelectMany(x =>
280279
{
281280
var nonFunctionConflicts = x
@@ -315,6 +314,7 @@ z.MethodKind is MethodKind.Constructor or MethodKind.Destructor
315314
]
316315
);
317316
}),
317+
logger,
318318
ct
319319
);
320320
logger.LogDebug(

sources/SilkTouch/SilkTouch/Mods/TransformHandles.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,10 @@ public override async Task ExecuteAsync(IModContext ctx, CancellationToken ct =
101101
// 1. Add -Handle suffix
102102
// 2. Reduce pointer dimensions
103103
ctx.SourceProject = project;
104-
await LocationTransformationUtils.ModifyAllReferencesAsync(ctx, logger, handleTypes, [
104+
await LocationTransformationUtils.ModifyAllReferencesAsync(ctx, handleTypes, [
105105
new IdentifierRenamingTransformer(handleTypes.Select(t => ((ISymbol)t, $"{t.Name}Handle"))),
106106
new PointerDimensionReductionTransformer()
107-
], ct);
107+
], logger, ct);
108108
project = ctx.SourceProject;
109109

110110
// Use document IDs from earlier

sources/SilkTouch/SilkTouch/Naming/NameUtils.cs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,21 @@ private static string MakeFirstLetterUpper(string wordToConvert, CultureInfo cul
320320
private static partial Regex Words();
321321
}
322322

323+
private static Location? IdentifierLocation(SyntaxNode? node) =>
324+
node switch
325+
{
326+
BaseTypeDeclarationSyntax bt => bt.Identifier.GetLocation(),
327+
DelegateDeclarationSyntax d => d.Identifier.GetLocation(),
328+
EnumMemberDeclarationSyntax em => em.Identifier.GetLocation(),
329+
EventDeclarationSyntax e => e.Identifier.GetLocation(),
330+
MethodDeclarationSyntax m => m.Identifier.GetLocation(),
331+
PropertyDeclarationSyntax p => p.Identifier.GetLocation(),
332+
VariableDeclaratorSyntax v => v.Identifier.GetLocation(),
333+
ConstructorDeclarationSyntax c => c.Identifier.GetLocation(),
334+
DestructorDeclarationSyntax d => d.Identifier.GetLocation(),
335+
_ => null,
336+
};
337+
323338
/// <summary>
324339
/// Rename all symbols with the given new names
325340
/// </summary>
@@ -333,16 +348,16 @@ private static string MakeFirstLetterUpper(string wordToConvert, CultureInfo cul
333348
/// <exception cref="ArgumentException"></exception>
334349
public static async Task RenameAllAsync(
335350
IModContext ctx,
336-
ILogger logger,
337351
IEnumerable<(ISymbol Symbol, string NewName)> toRename,
352+
ILogger? logger = null,
338353
CancellationToken ct = default,
339354
bool includeDeclarations = true,
340355
bool includeCandidateLocations = false
341356
)
342357
{
343358
var toRenameList = toRename.ToList();
344-
await LocationTransformationUtils.ModifyAllReferencesAsync(ctx, logger, toRenameList.Select(t => t.Symbol), [
345-
new IdentifierRenamingTransformer(toRenameList)
346-
], ct);
359+
await LocationTransformationUtils.ModifyAllReferencesAsync(ctx, toRenameList.Select(t => t.Symbol), [
360+
new IdentifierRenamingTransformer(toRenameList, includeDeclarations, includeCandidateLocations)
361+
], logger, ct);
347362
}
348363
}

0 commit comments

Comments
 (0)