/
Extensions.cs
200 lines (172 loc) · 7.53 KB
/
Extensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the MIT license. See License.txt in the project root for license information.
using System;
using System.Collections;
using System.Collections.Immutable;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Options;
namespace Microsoft.CodeAnalysis.Tools.Analyzers
{
public static class Extensions
{
private static Assembly MicrosoftCodeAnalysisFeaturesAssembly { get; }
private static Type IDEDiagnosticIdToOptionMappingHelperType { get; }
private static MethodInfo TryGetMappedOptionsMethod { get; }
static Extensions()
{
MicrosoftCodeAnalysisFeaturesAssembly = Assembly.Load(new AssemblyName("Microsoft.CodeAnalysis.Features"));
IDEDiagnosticIdToOptionMappingHelperType = MicrosoftCodeAnalysisFeaturesAssembly.GetType("Microsoft.CodeAnalysis.Diagnostics.IDEDiagnosticIdToOptionMappingHelper");
TryGetMappedOptionsMethod = IDEDiagnosticIdToOptionMappingHelperType.GetMethod("TryGetMappedOptions", BindingFlags.Static | BindingFlags.Public);
}
public static bool Any(this SolutionChanges solutionChanges)
=> solutionChanges.GetProjectChanges().Any(x => x.GetChangedDocuments().Any());
public static bool TryCreateInstance<T>(this Type type, [NotNullWhen(returnValue: true)] out T? instance) where T : class
{
try
{
var defaultCtor = type.GetConstructor(
BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance,
binder: null,
new Type[] { },
modifiers: null);
instance = defaultCtor != null
? (T)Activator.CreateInstance(
type,
BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance,
binder: null,
args: null,
culture: null)
: null;
return instance is object;
}
catch (Exception ex)
{
throw new InvalidOperationException($"Failed to create instrance of {type.FullName} in {type.AssemblyQualifiedName}.", ex);
}
}
/// <summary>
/// Get the highest possible severity for any formattable document in the project.
/// </summary>
public static async Task<DiagnosticSeverity> GetSeverityAsync(
this DiagnosticAnalyzer analyzer,
Project project,
ImmutableHashSet<string> formattablePaths,
CancellationToken cancellationToken)
{
var severity = DiagnosticSeverity.Hidden;
var compilation = await project.GetCompilationAsync(cancellationToken).ConfigureAwait(false);
if (compilation is null)
{
return severity;
}
foreach (var document in project.Documents)
{
// Is the document formattable?
if (document.FilePath is null || !formattablePaths.Contains(document.FilePath))
{
continue;
}
var options = await document.GetOptionsAsync(cancellationToken).ConfigureAwait(false);
var documentSeverity = analyzer.GetSeverity(document, project.AnalyzerOptions, options, compilation);
if (documentSeverity > severity)
{
severity = documentSeverity;
}
}
return severity;
}
private static DiagnosticSeverity GetSeverity(
this DiagnosticAnalyzer analyzer,
Document document,
AnalyzerOptions? analyzerOptions,
OptionSet options,
Compilation compilation)
{
var severity = DiagnosticSeverity.Hidden;
if (!document.TryGetSyntaxTree(out var tree))
{
return severity;
}
foreach (var descriptor in analyzer.SupportedDiagnostics)
{
if (severity == DiagnosticSeverity.Error)
{
break;
}
if (analyzerOptions.TryGetSeverityFromBulkConfiguration(tree, compilation, descriptor, out var reportDiagnostic))
{
var configuredSeverity = ToSeverity(reportDiagnostic);
if (configuredSeverity > severity)
{
severity = configuredSeverity;
}
continue;
}
if (TryGetSeverityFromCodeStyleOption(descriptor, compilation, options, out var codeStyleSeverity))
{
if (codeStyleSeverity > severity)
{
severity = codeStyleSeverity;
}
continue;
}
if (descriptor.DefaultSeverity > severity)
{
severity = descriptor.DefaultSeverity;
}
}
return severity;
static DiagnosticSeverity ToSeverity(ReportDiagnostic reportDiagnostic)
{
return reportDiagnostic switch
{
ReportDiagnostic.Error => DiagnosticSeverity.Error,
ReportDiagnostic.Warn => DiagnosticSeverity.Warning,
ReportDiagnostic.Info => DiagnosticSeverity.Info,
_ => DiagnosticSeverity.Hidden
};
}
static bool TryGetSeverityFromCodeStyleOption(
DiagnosticDescriptor descriptor,
Compilation compilation,
OptionSet options,
out DiagnosticSeverity severity)
{
severity = DiagnosticSeverity.Hidden;
var parameters = new object?[] { descriptor.Id, compilation.Language, null };
var result = (bool)TryGetMappedOptionsMethod.Invoke(null, parameters);
if (!result)
{
return false;
}
var codeStyleOptions = (IEnumerable)parameters[2]!;
foreach (var codeStyleOptionObj in codeStyleOptions)
{
var codeStyleOption = (IOption)codeStyleOptionObj;
var option = options.GetOption(new OptionKey(codeStyleOption, codeStyleOption.IsPerLanguage ? compilation.Language : null));
if (option is null)
{
continue;
}
var notificationProperty = option.GetType().GetProperty("Notification");
if (notificationProperty is null)
{
continue;
}
var notification = notificationProperty.GetValue(option);
var reportDiagnostic = (ReportDiagnostic)notification.GetType().GetProperty("Severity").GetValue(notification);
var codeStyleSeverity = ToSeverity(reportDiagnostic);
if (codeStyleSeverity > severity)
{
severity = codeStyleSeverity;
}
}
return true;
}
}
}
}