Skip to content

Commit 18497e1

Browse files
authored
Use runtime formatting for ILLink (#116941)
This change was made by deleting the ILLink .editorconfig and running `dotnet format` over the various ILLink projects. Some of the .editorconfig settings were then added back because they were not fixed automatically (and are enforced during build).
1 parent 9ec2190 commit 18497e1

File tree

1,659 files changed

+140892
-135923
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,659 files changed

+140892
-135923
lines changed

src/tools/illink/.editorconfig

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,4 @@
11
[*.cs]
2-
indent_style = tab
3-
indent_size = 4
4-
csharp_new_line_before_open_brace = types,methods
5-
csharp_new_line_before_else = false
6-
csharp_new_line_before_catch = false
7-
csharp_new_line_before_finally = false
8-
csharp_new_line_before_members_in_object_initializers = true
9-
csharp_new_line_before_members_in_anonymous_types = true
10-
csharp_new_line_between_query_expression_clauses = true
11-
12-
csharp_space_after_keywords_in_control_flow_statements = true
13-
csharp_space_between_method_declaration_name_and_open_parenthesis = true
14-
csharp_space_between_method_call_name_and_opening_parenthesis = true
15-
csharp_space_before_open_square_brackets = false
16-
csharp_space_after_cast = true
17-
18-
csharp_indent_switch_labels = false
19-
20-
# Sort using and Import directives with System.* appearing first
21-
dotnet_sort_system_directives_first = true
22-
23-
# Prefer property-like constructs to have an expression-body
24-
csharp_style_expression_bodied_properties = true:none
25-
csharp_style_expression_bodied_indexers = true:none
26-
csharp_style_expression_bodied_accessors = true:none
27-
28-
# Suggest more modern language features when available
29-
csharp_style_pattern_matching_over_is_with_cast_check = true:suggestion
30-
csharp_style_pattern_matching_over_as_with_null_check = true:suggestion
31-
csharp_style_inlined_variable_declaration = true:suggestion
32-
csharp_style_throw_expression = true:suggestion
33-
csharp_style_conditional_delegate_call = true:suggestion
34-
352
# Avoid redundant accessibility modifiers when they're default
363
dotnet_style_require_accessibility_modifiers = omit_if_default:suggestion
374

@@ -42,9 +9,6 @@ file_header_template = Copyright (c) .NET Foundation and contributors. All right
429
# Spacing around keywords
4310
dotnet_diagnostic.SA1000.severity = none
4411

45-
# Closing generic bracket should not be followed by a space
46-
dotnet_diagnostic.SA1015.severity = none
47-
4812
# Access modifier must be declared
4913
dotnet_diagnostic.SA1400.severity = none
5014

src/tools/illink/src/ILLink.CodeFix/BaseAttributeCodeFixProvider.cs

Lines changed: 133 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -16,132 +16,137 @@
1616

1717
namespace ILLink.CodeFix
1818
{
19-
public abstract class BaseAttributeCodeFixProvider : Microsoft.CodeAnalysis.CodeFixes.CodeFixProvider
20-
{
21-
private protected abstract LocalizableString CodeFixTitle { get; }
22-
23-
private protected abstract string FullyQualifiedAttributeName { get; }
24-
25-
private protected abstract AttributeableParentTargets AttributableParentTargets { get; }
26-
27-
public sealed override FixAllProvider GetFixAllProvider ()
28-
{
29-
// See https://github.com/dotnet/roslyn/blob/master/docs/analyzers/FixAllProvider.md for more information on Fix All Providers
30-
return WellKnownFixAllProviders.BatchFixer;
31-
}
32-
33-
protected async Task BaseRegisterCodeFixesAsync (CodeFixContext context)
34-
{
35-
var document = context.Document;
36-
var diagnostic = context.Diagnostics.First ();
37-
var codeFixTitle = CodeFixTitle.ToString ();
38-
39-
if (await document.GetSyntaxRootAsync (context.CancellationToken).ConfigureAwait (false) is not { } root)
40-
return;
41-
42-
SyntaxNode targetNode = root.FindNode (diagnostic.Location.SourceSpan, getInnermostNodeForTie: true);
43-
if (FindAttributableParent (targetNode, AttributableParentTargets) is not SyntaxNode attributableNode)
44-
return;
45-
46-
context.RegisterCodeFix (CodeAction.Create (
47-
title: codeFixTitle,
48-
createChangedDocument: ct => AddAttributeAsync (
49-
document, diagnostic, targetNode, attributableNode, ct),
50-
equivalenceKey: codeFixTitle), diagnostic);
51-
}
52-
53-
private async Task<Document> AddAttributeAsync (
54-
Document document,
55-
Diagnostic diagnostic,
56-
SyntaxNode targetNode,
57-
SyntaxNode attributableNode,
58-
CancellationToken cancellationToken)
59-
{
60-
if (await document.GetSemanticModelAsync (cancellationToken).ConfigureAwait (false) is not { } model)
61-
return document;
62-
if (model.GetSymbolInfo (targetNode, cancellationToken).Symbol is not { } targetSymbol)
63-
return document;
64-
if (model.Compilation.GetBestTypeByMetadataName (FullyQualifiedAttributeName) is not { } attributeSymbol)
65-
return document;
66-
67-
// N.B. May be null for FieldDeclaration, since field declarations can declare multiple variables
68-
var attributableSymbol = model.GetDeclaredSymbol (attributableNode, cancellationToken);
69-
70-
var attributeArguments = GetAttributeArguments (attributableSymbol, targetSymbol, SyntaxGenerator.GetGenerator (document), diagnostic);
71-
72-
var editor = await DocumentEditor.CreateAsync (document, cancellationToken).ConfigureAwait (false);
73-
var generator = editor.Generator;
74-
var attribute = generator.Attribute (
75-
generator.TypeExpression (attributeSymbol), attributeArguments)
76-
.WithAdditionalAnnotations (Simplifier.Annotation, Simplifier.AddImportsAnnotation);
77-
78-
editor.AddAttribute (attributableNode, attribute);
79-
return editor.GetChangedDocument ();
80-
}
81-
82-
[Flags]
83-
protected enum AttributeableParentTargets
84-
{
85-
MethodOrConstructor = 0x0001,
86-
Property = 0x0002,
87-
Field = 0x0004,
88-
Event = 0x0008,
89-
Class = 0x0010,
90-
All = MethodOrConstructor | Property | Field | Event | Class
91-
}
92-
93-
private static CSharpSyntaxNode? FindAttributableParent (SyntaxNode node, AttributeableParentTargets targets)
94-
{
95-
SyntaxNode? parentNode = node.Parent;
96-
while (parentNode is not null) {
97-
switch (parentNode) {
98-
case LambdaExpressionSyntax:
99-
return null;
100-
101-
case PropertyDeclarationSyntax when targets.HasFlag (AttributeableParentTargets.Property):
102-
case EventDeclarationSyntax when targets.HasFlag (AttributeableParentTargets.Event):
103-
return (CSharpSyntaxNode) parentNode;
104-
case PropertyDeclarationSyntax:
105-
case EventDeclarationSyntax:
106-
// If the attribute can be placed on a method but not directly on a property/event, we don't want to keep walking up
107-
// the syntax tree to annotate the class. Instead the correct thing to do is to add accessor methods and annotate those.
108-
// The code fixer doesn't support doing this automatically, so return null to indicate that the attribute can't be added.
109-
if (targets.HasFlag (AttributeableParentTargets.MethodOrConstructor))
110-
return null;
111-
112-
parentNode = parentNode.Parent;
113-
break;
114-
case LocalFunctionStatementSyntax or BaseMethodDeclarationSyntax or AccessorDeclarationSyntax when targets.HasFlag (AttributeableParentTargets.MethodOrConstructor):
115-
case FieldDeclarationSyntax when targets.HasFlag (AttributeableParentTargets.Field):
116-
case ClassDeclarationSyntax when targets.HasFlag (AttributeableParentTargets.Class):
117-
return (CSharpSyntaxNode) parentNode;
118-
119-
default:
120-
parentNode = parentNode.Parent;
121-
break;
122-
}
123-
}
124-
125-
return null;
126-
}
127-
128-
protected abstract SyntaxNode[] GetAttributeArguments (
129-
ISymbol? attributableSymbol,
130-
ISymbol targetSymbol,
131-
SyntaxGenerator syntaxGenerator,
132-
Diagnostic diagnostic);
133-
134-
protected static bool HasPublicAccessibility (ISymbol? m)
135-
{
136-
if (m is not { DeclaredAccessibility: Accessibility.Public or Accessibility.Protected }) {
137-
return false;
138-
}
139-
for (var t = m.ContainingType; t is not null; t = t.ContainingType) {
140-
if (t.DeclaredAccessibility != Accessibility.Public) {
141-
return false;
142-
}
143-
}
144-
return true;
145-
}
146-
}
19+
public abstract class BaseAttributeCodeFixProvider : Microsoft.CodeAnalysis.CodeFixes.CodeFixProvider
20+
{
21+
private protected abstract LocalizableString CodeFixTitle { get; }
22+
23+
private protected abstract string FullyQualifiedAttributeName { get; }
24+
25+
private protected abstract AttributeableParentTargets AttributableParentTargets { get; }
26+
27+
public sealed override FixAllProvider GetFixAllProvider()
28+
{
29+
// See https://github.com/dotnet/roslyn/blob/master/docs/analyzers/FixAllProvider.md for more information on Fix All Providers
30+
return WellKnownFixAllProviders.BatchFixer;
31+
}
32+
33+
protected async Task BaseRegisterCodeFixesAsync(CodeFixContext context)
34+
{
35+
var document = context.Document;
36+
var diagnostic = context.Diagnostics.First();
37+
var codeFixTitle = CodeFixTitle.ToString();
38+
39+
if (await document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false) is not { } root)
40+
return;
41+
42+
SyntaxNode targetNode = root.FindNode(diagnostic.Location.SourceSpan, getInnermostNodeForTie: true);
43+
if (FindAttributableParent(targetNode, AttributableParentTargets) is not SyntaxNode attributableNode)
44+
return;
45+
46+
context.RegisterCodeFix(CodeAction.Create(
47+
title: codeFixTitle,
48+
createChangedDocument: ct => AddAttributeAsync(
49+
document, diagnostic, targetNode, attributableNode, ct),
50+
equivalenceKey: codeFixTitle), diagnostic);
51+
}
52+
53+
private async Task<Document> AddAttributeAsync(
54+
Document document,
55+
Diagnostic diagnostic,
56+
SyntaxNode targetNode,
57+
SyntaxNode attributableNode,
58+
CancellationToken cancellationToken)
59+
{
60+
if (await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false) is not { } model)
61+
return document;
62+
if (model.GetSymbolInfo(targetNode, cancellationToken).Symbol is not { } targetSymbol)
63+
return document;
64+
if (model.Compilation.GetBestTypeByMetadataName(FullyQualifiedAttributeName) is not { } attributeSymbol)
65+
return document;
66+
67+
// N.B. May be null for FieldDeclaration, since field declarations can declare multiple variables
68+
var attributableSymbol = model.GetDeclaredSymbol(attributableNode, cancellationToken);
69+
70+
var attributeArguments = GetAttributeArguments(attributableSymbol, targetSymbol, SyntaxGenerator.GetGenerator(document), diagnostic);
71+
72+
var editor = await DocumentEditor.CreateAsync(document, cancellationToken).ConfigureAwait(false);
73+
var generator = editor.Generator;
74+
var attribute = generator.Attribute(
75+
generator.TypeExpression(attributeSymbol), attributeArguments)
76+
.WithAdditionalAnnotations(Simplifier.Annotation, Simplifier.AddImportsAnnotation);
77+
78+
editor.AddAttribute(attributableNode, attribute);
79+
return editor.GetChangedDocument();
80+
}
81+
82+
[Flags]
83+
protected enum AttributeableParentTargets
84+
{
85+
MethodOrConstructor = 0x0001,
86+
Property = 0x0002,
87+
Field = 0x0004,
88+
Event = 0x0008,
89+
Class = 0x0010,
90+
All = MethodOrConstructor | Property | Field | Event | Class
91+
}
92+
93+
private static CSharpSyntaxNode? FindAttributableParent(SyntaxNode node, AttributeableParentTargets targets)
94+
{
95+
SyntaxNode? parentNode = node.Parent;
96+
while (parentNode is not null)
97+
{
98+
switch (parentNode)
99+
{
100+
case LambdaExpressionSyntax:
101+
return null;
102+
103+
case PropertyDeclarationSyntax when targets.HasFlag(AttributeableParentTargets.Property):
104+
case EventDeclarationSyntax when targets.HasFlag(AttributeableParentTargets.Event):
105+
return (CSharpSyntaxNode)parentNode;
106+
case PropertyDeclarationSyntax:
107+
case EventDeclarationSyntax:
108+
// If the attribute can be placed on a method but not directly on a property/event, we don't want to keep walking up
109+
// the syntax tree to annotate the class. Instead the correct thing to do is to add accessor methods and annotate those.
110+
// The code fixer doesn't support doing this automatically, so return null to indicate that the attribute can't be added.
111+
if (targets.HasFlag(AttributeableParentTargets.MethodOrConstructor))
112+
return null;
113+
114+
parentNode = parentNode.Parent;
115+
break;
116+
case LocalFunctionStatementSyntax or BaseMethodDeclarationSyntax or AccessorDeclarationSyntax when targets.HasFlag(AttributeableParentTargets.MethodOrConstructor):
117+
case FieldDeclarationSyntax when targets.HasFlag(AttributeableParentTargets.Field):
118+
case ClassDeclarationSyntax when targets.HasFlag(AttributeableParentTargets.Class):
119+
return (CSharpSyntaxNode)parentNode;
120+
121+
default:
122+
parentNode = parentNode.Parent;
123+
break;
124+
}
125+
}
126+
127+
return null;
128+
}
129+
130+
protected abstract SyntaxNode[] GetAttributeArguments(
131+
ISymbol? attributableSymbol,
132+
ISymbol targetSymbol,
133+
SyntaxGenerator syntaxGenerator,
134+
Diagnostic diagnostic);
135+
136+
protected static bool HasPublicAccessibility(ISymbol? m)
137+
{
138+
if (m is not { DeclaredAccessibility: Accessibility.Public or Accessibility.Protected })
139+
{
140+
return false;
141+
}
142+
for (var t = m.ContainingType; t is not null; t = t.ContainingType)
143+
{
144+
if (t.DeclaredAccessibility != Accessibility.Public)
145+
{
146+
return false;
147+
}
148+
}
149+
return true;
150+
}
151+
}
147152
}

0 commit comments

Comments
 (0)