forked from dotnet/roslyn-analyzers
-
Notifications
You must be signed in to change notification settings - Fork 1
/
DiagnosticExtensions.cs
255 lines (227 loc) · 8.99 KB
/
DiagnosticExtensions.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Reflection;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Diagnostics;
namespace Analyzer.Utilities.Extensions
{
internal static class DiagnosticExtensions
{
public static IEnumerable<Diagnostic> CreateDiagnostics(
this IEnumerable<SyntaxNode> nodes,
DiagnosticDescriptor rule,
params object[] args)
{
foreach (SyntaxNode node in nodes)
{
yield return node.CreateDiagnostic(rule, args);
}
}
public static Diagnostic CreateDiagnostic(
this SyntaxNode node,
DiagnosticDescriptor rule,
params object[] args)
{
return node.GetLocation().CreateDiagnostic(rule, args);
}
public static Diagnostic CreateDiagnostic(
this SyntaxNode node,
DiagnosticDescriptor rule,
ImmutableDictionary<string, string> properties,
params object[] args)
=> node
.GetLocation()
.CreateDiagnostic(
rule: rule,
properties: properties,
args: args);
public static Diagnostic CreateDiagnostic(
this IOperation operation,
DiagnosticDescriptor rule,
params object[] args)
{
return operation.Syntax.CreateDiagnostic(rule, args);
}
public static IEnumerable<Diagnostic> CreateDiagnostics(
this IEnumerable<SyntaxToken> tokens,
DiagnosticDescriptor rule,
params object[] args)
{
foreach (SyntaxToken token in tokens)
{
yield return token.CreateDiagnostic(rule, args);
}
}
public static Diagnostic CreateDiagnostic(
this SyntaxToken token,
DiagnosticDescriptor rule,
params object[] args)
{
return token.GetLocation().CreateDiagnostic(rule, args);
}
public static IEnumerable<Diagnostic> CreateDiagnostics(
this IEnumerable<SyntaxNodeOrToken> nodesOrTokens,
DiagnosticDescriptor rule,
params object[] args)
{
foreach (SyntaxNodeOrToken nodeOrToken in nodesOrTokens)
{
yield return nodeOrToken.CreateDiagnostic(rule, args);
}
}
public static Diagnostic CreateDiagnostic(
this SyntaxNodeOrToken nodeOrToken,
DiagnosticDescriptor rule,
params object[] args)
{
return nodeOrToken.GetLocation().CreateDiagnostic(rule, args);
}
public static IEnumerable<Diagnostic> CreateDiagnostics(
this IEnumerable<ISymbol> symbols,
DiagnosticDescriptor rule,
params object[] args)
{
foreach (ISymbol symbol in symbols)
{
yield return symbol.CreateDiagnostic(rule, args);
}
}
public static Diagnostic CreateDiagnostic(
this ISymbol symbol,
DiagnosticDescriptor rule,
params object[] args)
{
return symbol.Locations.CreateDiagnostic(rule, args);
}
public static Diagnostic CreateDiagnostic(
this Location location,
DiagnosticDescriptor rule,
params object[] args)
=> location
.CreateDiagnostic(
rule: rule,
properties: ImmutableDictionary<string, string>.Empty,
args: args);
public static Diagnostic CreateDiagnostic(
this Location location,
DiagnosticDescriptor rule,
ImmutableDictionary<string, string> properties,
params object[] args)
{
if (!location.IsInSource)
{
location = Location.None;
}
return Diagnostic.Create(
descriptor: rule,
location: location,
properties: properties,
messageArgs: args);
}
public static IEnumerable<Diagnostic> CreateDiagnostics(
this IEnumerable<IEnumerable<Location>> setOfLocations,
DiagnosticDescriptor rule,
params object[] args)
{
foreach (IEnumerable<Location> locations in setOfLocations)
{
yield return locations.CreateDiagnostic(rule, args);
}
}
public static Diagnostic CreateDiagnostic(
this IEnumerable<Location> locations,
DiagnosticDescriptor rule,
params object[] args)
{
return locations.CreateDiagnostic(rule, null, args);
}
public static Diagnostic CreateDiagnostic(
this IEnumerable<Location> locations,
DiagnosticDescriptor rule,
ImmutableDictionary<string, string>? properties,
params object[] args)
{
IEnumerable<Location> inSource = locations.Where(l => l.IsInSource);
if (!inSource.Any())
{
return Diagnostic.Create(rule, null, args);
}
return Diagnostic.Create(rule,
location: inSource.First(),
additionalLocations: inSource.Skip(1),
properties: properties,
messageArgs: args);
}
/// <summary>
/// TODO: Revert this reflection based workaround once we move to Microsoft.CodeAnalysis version 3.0
/// </summary>
private static readonly PropertyInfo s_syntaxTreeDiagnosticOptionsProperty =
typeof(SyntaxTree).GetTypeInfo().GetDeclaredProperty("DiagnosticOptions");
public static void ReportNoLocationDiagnostic(
this CompilationAnalysisContext context,
DiagnosticDescriptor rule,
params object[] args)
=> context.Compilation.ReportNoLocationDiagnostic(rule, context.ReportDiagnostic, properties: null, args);
public static void ReportNoLocationDiagnostic(
this Compilation compilation,
DiagnosticDescriptor rule,
Action<Diagnostic> addDiagnostic,
params object[] args)
=> compilation.ReportNoLocationDiagnostic(rule, addDiagnostic, properties: null, args);
public static void ReportNoLocationDiagnostic(
this Compilation compilation,
DiagnosticDescriptor rule,
Action<Diagnostic> addDiagnostic,
ImmutableDictionary<string, string>? properties,
params object[] args)
{
var effectiveSeverity = GetEffectiveSeverity();
if (!effectiveSeverity.HasValue)
{
// Disabled rule
return;
}
if (effectiveSeverity.Value != rule.DefaultSeverity)
{
rule = new DiagnosticDescriptor(rule.Id, rule.Title, rule.MessageFormat, rule.Category,
effectiveSeverity.Value, rule.IsEnabledByDefault, rule.Description, rule.HelpLinkUri, rule.CustomTags.ToArray());
}
var diagnostic = Diagnostic.Create(rule, Location.None, properties, args);
addDiagnostic(diagnostic);
return;
DiagnosticSeverity? GetEffectiveSeverity()
{
if (s_syntaxTreeDiagnosticOptionsProperty == null)
{
return rule.DefaultSeverity;
}
ReportDiagnostic? overriddenSeverity = null;
foreach (var tree in compilation.SyntaxTrees)
{
var options = (ImmutableDictionary<string, ReportDiagnostic>)s_syntaxTreeDiagnosticOptionsProperty.GetValue(tree);
if (options.TryGetValue(rule.Id, out var configuredValue))
{
if (configuredValue == ReportDiagnostic.Suppress)
{
// Any suppression entry always wins.
return null;
}
if (overriddenSeverity == null)
{
overriddenSeverity = configuredValue;
}
else if (overriddenSeverity.Value.IsLessSevereThan(configuredValue))
{
// Choose the most severe value for conflicts.
overriddenSeverity = configuredValue;
}
}
}
return overriddenSeverity.HasValue ? overriddenSeverity.Value.ToDiagnosticSeverity() : rule.DefaultSeverity;
}
}
}
}