Skip to content

Commit 2d46b3f

Browse files
CopilotEgorBo
andauthored
Fix extern safety code fix ordering
Co-authored-by: EgorBo <[email protected]>
1 parent eec38c7 commit 2d46b3f

3 files changed

Lines changed: 66 additions & 24 deletions

File tree

‎src/tools/illink/src/ILLink.CodeFix/AddUnsafeToExternCodeFixProvider.cs‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using ILLink.CodeFixProvider;
99
using Microsoft.CodeAnalysis;
1010
using Microsoft.CodeAnalysis.CodeFixes;
11+
using Microsoft.CodeAnalysis.CSharp;
1112
using Microsoft.CodeAnalysis.CSharp.Syntax;
1213

1314
namespace ILLink.CodeFix
@@ -41,7 +42,8 @@ or PropertyDeclarationSyntax
4142
or IndexerDeclarationSyntax
4243
or EventDeclarationSyntax
4344
or EventFieldDeclarationSyntax
44-
or LocalFunctionStatementSyntax);
45+
or LocalFunctionStatementSyntax,
46+
insertAfterModifier: SyntaxKind.ExternKeyword);
4547
}
4648
}
4749
#endif

‎src/tools/illink/src/ILLink.CodeFix/UnsafeModifierCodeFixHelpers.cs‎

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ internal static class UnsafeModifierCodeFixHelpers
3333
internal static async Task RegisterAddUnsafeCodeFixAsync(
3434
CodeFixContext context,
3535
LocalizableString codeFixTitle,
36-
Func<SyntaxNode, bool> isSupportedDeclaration)
36+
Func<SyntaxNode, bool> isSupportedDeclaration,
37+
SyntaxKind insertAfterModifier = SyntaxKind.None)
3738
{
3839
var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);
3940
if (root is null)
@@ -68,7 +69,12 @@ internal static async Task RegisterAddUnsafeCodeFixAsync(
6869
context.RegisterCodeFix(
6970
CodeAction.Create(
7071
displayTitle,
71-
cancellationToken => AddModifierAsync(context.Document, declaration, modifier, cancellationToken),
72+
cancellationToken => AddModifierAsync(
73+
context.Document,
74+
declaration,
75+
modifier,
76+
cancellationToken,
77+
insertAfterModifier),
7278
title),
7379
context.Diagnostics[0]);
7480
}
@@ -102,10 +108,11 @@ internal static async Task<Document> AddModifierAsync(
102108
Document document,
103109
SyntaxNode declaration,
104110
SyntaxKind modifier,
105-
CancellationToken cancellationToken)
111+
CancellationToken cancellationToken,
112+
SyntaxKind insertAfterModifier = SyntaxKind.None)
106113
{
107114
var editor = await DocumentEditor.CreateAsync(document, cancellationToken).ConfigureAwait(false);
108-
editor.ReplaceNode(declaration, AddModifier(declaration, modifier));
115+
editor.ReplaceNode(declaration, AddModifier(declaration, modifier, insertAfterModifier));
109116
return editor.GetChangedDocument();
110117
}
111118

@@ -138,14 +145,17 @@ internal static async Task<Document> ReplaceUnsafeWithSafeAsync(
138145
/// <summary>
139146
/// Returns <paramref name="declaration"/> with a safety modifier added.
140147
/// </summary>
141-
internal static SyntaxNode AddModifier(SyntaxNode declaration, SyntaxKind modifier)
148+
internal static SyntaxNode AddModifier(
149+
SyntaxNode declaration,
150+
SyntaxKind modifier,
151+
SyntaxKind insertAfterModifier = SyntaxKind.None)
142152
{
143153
if (declaration is AccessorDeclarationSyntax accessor)
144154
return AddModifier(accessor, modifier);
145155

146156
SyntaxTokenList modifiers = UnsafeMigrationSyntaxHelpers.GetModifiers(declaration);
147157
if (modifiers.Count > 0)
148-
return WithModifiers(declaration, AddModifier(modifiers, modifier));
158+
return WithModifiers(declaration, AddModifier(modifiers, modifier, insertAfterModifier));
149159

150160
// With no existing modifiers the declaration's leading trivia belongs to the token the new modifier
151161
// displaces, so it has to move with it. Attribute lists keep their own leading trivia.
@@ -206,7 +216,7 @@ private static AccessorDeclarationSyntax AddModifier(AccessorDeclarationSyntax a
206216
{
207217
// SyntaxGenerator does not yet model unsafe property accessors, so edit their tokens directly.
208218
if (accessor.Modifiers.Count > 0)
209-
return accessor.WithModifiers(AddModifier(accessor.Modifiers, modifier));
219+
return accessor.WithModifiers(AddModifier(accessor.Modifiers, modifier, SyntaxKind.None));
210220

211221
SyntaxToken modifierToken = SyntaxFactory.Token(modifier)
212222
.WithLeadingTrivia(accessor.Keyword.LeadingTrivia)
@@ -243,12 +253,26 @@ private static AccessorDeclarationSyntax RemoveUnsafeModifier(AccessorDeclaratio
243253
return accessor.WithModifiers(modifiers);
244254
}
245255

246-
private static SyntaxTokenList AddModifier(SyntaxTokenList modifiers, SyntaxKind modifier)
256+
private static SyntaxTokenList AddModifier(
257+
SyntaxTokenList modifiers,
258+
SyntaxKind modifier,
259+
SyntaxKind insertAfterModifier)
247260
{
248-
// 'extern' and 'partial' conventionally sit closest to the return type, so the safety modifier goes
249-
// before them. Otherwise it is appended, which matches the repo's preferred modifier order where
250-
// 'unsafe' follows 'virtual', 'abstract', 'sealed' and 'override'.
251-
int insertionIndex = GetFirstModifierIndex(modifiers, SyntaxKind.ExternKeyword, SyntaxKind.PartialKeyword);
261+
int insertionIndex = insertAfterModifier == SyntaxKind.None
262+
? -1
263+
: modifiers.IndexOf(insertAfterModifier);
264+
if (insertionIndex >= 0)
265+
{
266+
insertionIndex++;
267+
}
268+
else
269+
{
270+
// 'extern' and 'partial' conventionally sit closest to the return type, so the safety modifier goes
271+
// before them. Otherwise it is appended, which matches the repo's preferred modifier order where
272+
// 'unsafe' follows 'virtual', 'abstract', 'sealed' and 'override'.
273+
insertionIndex = GetFirstModifierIndex(modifiers, SyntaxKind.ExternKeyword, SyntaxKind.PartialKeyword);
274+
}
275+
252276
if (insertionIndex < 0)
253277
insertionIndex = modifiers.Count;
254278

‎src/tools/illink/test/ILLink.RoslynAnalyzer.Tests/AddUnsafeToExternCodeFixTests.cs‎

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class C
2626
"""
2727
class C
2828
{
29-
public unsafe extern void M();
29+
public extern unsafe void M();
3030
}
3131
"""
3232
},
@@ -40,7 +40,7 @@ class C
4040
"""
4141
class C
4242
{
43-
public unsafe extern int P { get; set; }
43+
public extern unsafe int P { get; set; }
4444
}
4545
"""
4646
},
@@ -54,7 +54,7 @@ class C
5454
"""
5555
class C
5656
{
57-
public unsafe extern int this[int index] { get; set; }
57+
public extern unsafe int this[int index] { get; set; }
5858
}
5959
"""
6060
},
@@ -72,7 +72,7 @@ class C
7272
7373
class C
7474
{
75-
public static unsafe extern event Action E;
75+
public static extern unsafe event Action E;
7676
}
7777
"""
7878
},
@@ -86,7 +86,7 @@ class C
8686
"""
8787
class C
8888
{
89-
public unsafe extern C(int value);
89+
public extern unsafe C(int value);
9090
}
9191
"""
9292
},
@@ -100,7 +100,7 @@ class C
100100
"""
101101
class C
102102
{
103-
public static unsafe extern C operator +(C left, C right);
103+
public static extern unsafe C operator +(C left, C right);
104104
}
105105
"""
106106
},
@@ -114,7 +114,7 @@ class C
114114
"""
115115
class C
116116
{
117-
public static unsafe extern explicit operator int(C value);
117+
public static extern unsafe explicit operator int(C value);
118118
}
119119
"""
120120
},
@@ -128,7 +128,7 @@ class C
128128
"""
129129
class C
130130
{
131-
unsafe extern ~C();
131+
extern unsafe ~C();
132132
}
133133
"""
134134
},
@@ -147,7 +147,7 @@ class C
147147
{
148148
void M()
149149
{
150-
static unsafe extern void Local();
150+
static extern unsafe void Local();
151151
}
152152
}
153153
"""
@@ -162,7 +162,23 @@ class C
162162
"""
163163
class C
164164
{
165-
required public unsafe extern int P { get; set; }
165+
required public extern unsafe int P { get; set; }
166+
}
167+
"""
168+
},
169+
{
170+
"""
171+
class C
172+
{
173+
/// <safety>Audited interop boundary.</safety>
174+
{|CS9389:extern|} void M();
175+
}
176+
""",
177+
"""
178+
class C
179+
{
180+
/// <safety>Audited interop boundary.</safety>
181+
extern safe void M();
166182
}
167183
"""
168184
},
@@ -178,7 +194,7 @@ class C
178194
class C
179195
{
180196
/// <summary>Invokes native code.</summary>
181-
unsafe extern void M();
197+
extern unsafe void M();
182198
}
183199
"""
184200
},

0 commit comments

Comments
 (0)