-
Notifications
You must be signed in to change notification settings - Fork 4k
/
DiagnosticDescriptor.cs
244 lines (220 loc) · 11.9 KB
/
DiagnosticDescriptor.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
// 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 Microsoft.CodeAnalysis.Diagnostics;
using Roslyn.Utilities;
namespace Microsoft.CodeAnalysis
{
/// <summary>
/// Provides a description about a <see cref="Diagnostic"/>
/// </summary>
public sealed class DiagnosticDescriptor : IEquatable<DiagnosticDescriptor>
{
/// <summary>
/// An unique identifier for the diagnostic.
/// </summary>
public string Id { get; }
/// <summary>
/// A short localizable title describing the diagnostic.
/// </summary>
public LocalizableString Title { get; }
/// <summary>
/// An optional longer localizable description for the diagnostic.
/// </summary>
public LocalizableString Description { get; }
/// <summary>
/// An optional hyperlink that provides more detailed information regarding the diagnostic.
/// </summary>
public string HelpLinkUri { get; }
/// <summary>
/// A localizable format message string, which can be passed as the first argument to <see cref="String.Format(string, object[])"/> when creating the diagnostic message with this descriptor.
/// </summary>
/// <returns></returns>
public LocalizableString MessageFormat { get; }
/// <summary>
/// The category of the diagnostic (like Design, Naming etc.)
/// </summary>
public string Category { get; }
/// <summary>
/// The default severity of the diagnostic.
/// </summary>
public DiagnosticSeverity DefaultSeverity { get; }
/// <summary>
/// Returns true if the diagnostic is enabled by default.
/// </summary>
public bool IsEnabledByDefault { get; }
/// <summary>
/// Custom tags for the diagnostic.
/// </summary>
public IEnumerable<string> CustomTags { get; }
/// <summary>
/// Create a DiagnosticDescriptor, which provides description about a <see cref="Diagnostic"/>.
/// NOTE: For localizable <paramref name="title"/>, <paramref name="description"/> and/or <paramref name="messageFormat"/>,
/// use constructor overload <see cref="DiagnosticDescriptor(string, LocalizableString, LocalizableString, string, DiagnosticSeverity, bool, LocalizableString, string, string[])"/>.
/// </summary>
/// <param name="id">A unique identifier for the diagnostic. For example, code analysis diagnostic ID "CA1001".</param>
/// <param name="title">A short title describing the diagnostic. For example, for CA1001: "Types that own disposable fields should be disposable".</param>
/// <param name="messageFormat">A format message string, which can be passed as the first argument to <see cref="String.Format(string, object[])"/> when creating the diagnostic message with this descriptor.
/// For example, for CA1001: "Implement IDisposable on '{0}' because it creates members of the following IDisposable types: '{1}'."</param>
/// <param name="category">The category of the diagnostic (like Design, Naming etc.). For example, for CA1001: "Microsoft.Design".</param>
/// <param name="defaultSeverity">Default severity of the diagnostic.</param>
/// <param name="isEnabledByDefault">True if the diagnostic is enabled by default.</param>
/// <param name="description">An optional longer description of the diagnostic.</param>
/// <param name="helpLinkUri">An optional hyperlink that provides a more detailed description regarding the diagnostic.</param>
/// <param name="customTags">Optional custom tags for the diagnostic. See <see cref="WellKnownDiagnosticTags"/> for some well known tags.</param>
public DiagnosticDescriptor(
string id,
string title,
string messageFormat,
string category,
DiagnosticSeverity defaultSeverity,
bool isEnabledByDefault,
string description = null,
string helpLinkUri = null,
params string[] customTags)
: this(id, title, messageFormat, category, defaultSeverity, isEnabledByDefault, description, helpLinkUri, customTags.AsImmutableOrEmpty())
{
}
/// <summary>
/// Create a DiagnosticDescriptor, which provides description about a <see cref="Diagnostic"/>.
/// </summary>
/// <param name="id">A unique identifier for the diagnostic. For example, code analysis diagnostic ID "CA1001".</param>
/// <param name="title">A short localizable title describing the diagnostic. For example, for CA1001: "Types that own disposable fields should be disposable".</param>
/// <param name="messageFormat">A localizable format message string, which can be passed as the first argument to <see cref="String.Format(string, object[])"/> when creating the diagnostic message with this descriptor.
/// For example, for CA1001: "Implement IDisposable on '{0}' because it creates members of the following IDisposable types: '{1}'."</param>
/// <param name="category">The category of the diagnostic (like Design, Naming etc.). For example, for CA1001: "Microsoft.Design".</param>
/// <param name="defaultSeverity">Default severity of the diagnostic.</param>
/// <param name="isEnabledByDefault">True if the diagnostic is enabled by default.</param>
/// <param name="description">An optional longer localizable description of the diagnostic.</param>
/// <param name="helpLinkUri">An optional hyperlink that provides a more detailed description regarding the diagnostic.</param>
/// <param name="customTags">Optional custom tags for the diagnostic. See <see cref="WellKnownDiagnosticTags"/> for some well known tags.</param>
/// <remarks>Example descriptor for rule CA1001:
/// internal static DiagnosticDescriptor Rule = new DiagnosticDescriptor(RuleId,
/// new LocalizableResourceString(nameof(FxCopRulesResources.TypesThatOwnDisposableFieldsShouldBeDisposable), FxCopRulesResources.ResourceManager, typeof(FxCopRulesResources)),
/// new LocalizableResourceString(nameof(FxCopRulesResources.TypeOwnsDisposableFieldButIsNotDisposable), FxCopRulesResources.ResourceManager, typeof(FxCopRulesResources)),
/// FxCopDiagnosticCategory.Design,
/// DiagnosticSeverity.Warning,
/// isEnabledByDefault: true,
/// helpLinkUri: "http://msdn.microsoft.com/library/ms182172.aspx",
/// customTags: DiagnosticCustomTags.Microsoft);
/// </remarks>
public DiagnosticDescriptor(
string id,
LocalizableString title,
LocalizableString messageFormat,
string category,
DiagnosticSeverity defaultSeverity,
bool isEnabledByDefault,
LocalizableString description = null,
string helpLinkUri = null,
params string[] customTags)
: this(id, title, messageFormat, category, defaultSeverity, isEnabledByDefault, description, helpLinkUri, customTags.AsImmutableOrEmpty())
{
}
internal DiagnosticDescriptor(
string id,
LocalizableString title,
LocalizableString messageFormat,
string category,
DiagnosticSeverity defaultSeverity,
bool isEnabledByDefault,
LocalizableString description,
string helpLinkUri,
ImmutableArray<string> customTags)
{
if (string.IsNullOrWhiteSpace(id))
{
throw new ArgumentException(CodeAnalysisResources.DiagnosticIdCantBeNullOrWhitespace, nameof(id));
}
if (messageFormat == null)
{
throw new ArgumentNullException(nameof(messageFormat));
}
if (category == null)
{
throw new ArgumentNullException(nameof(category));
}
if (title == null)
{
throw new ArgumentNullException(nameof(title));
}
this.Id = id;
this.Title = title;
this.Category = category;
this.MessageFormat = messageFormat;
this.DefaultSeverity = defaultSeverity;
this.IsEnabledByDefault = isEnabledByDefault;
this.Description = description ?? string.Empty;
this.HelpLinkUri = helpLinkUri ?? string.Empty;
this.CustomTags = customTags;
}
public bool Equals(DiagnosticDescriptor other)
{
return
other != null &&
this.Category == other.Category &&
this.DefaultSeverity == other.DefaultSeverity &&
this.Description.Equals(other.Description) &&
this.HelpLinkUri == other.HelpLinkUri &&
this.Id == other.Id &&
this.IsEnabledByDefault == other.IsEnabledByDefault &&
this.MessageFormat.Equals(other.MessageFormat) &&
this.Title.Equals(other.Title);
}
public override bool Equals(object obj)
{
return Equals(obj as DiagnosticDescriptor);
}
public override int GetHashCode()
{
return Hash.Combine(this.Category.GetHashCode(),
Hash.Combine(this.DefaultSeverity.GetHashCode(),
Hash.Combine(this.Description.GetHashCode(),
Hash.Combine(this.HelpLinkUri.GetHashCode(),
Hash.Combine(this.Id.GetHashCode(),
Hash.Combine(this.IsEnabledByDefault.GetHashCode(),
Hash.Combine(this.MessageFormat.GetHashCode(),
this.Title.GetHashCode())))))));
}
/// <summary>
/// Gets the effective severity of diagnostics created based on this descriptor and the given <see cref="CompilationOptions"/>.
/// </summary>
/// <param name="compilationOptions">Compilation options</param>
public ReportDiagnostic GetEffectiveSeverity(CompilationOptions compilationOptions)
{
if (compilationOptions == null)
{
throw new ArgumentNullException(nameof(compilationOptions));
}
// Create a dummy diagnostic to compute the effective diagnostic severity for given compilation options
// TODO: Once https://github.com/dotnet/roslyn/issues/3650 is fixed, we can avoid creating a no-location diagnostic here.
var effectiveDiagnostic = compilationOptions.FilterDiagnostic(Diagnostic.Create(this, Location.None));
return effectiveDiagnostic != null ? MapSeverityToReport(effectiveDiagnostic.Severity) : ReportDiagnostic.Suppress;
}
private static ReportDiagnostic MapSeverityToReport(DiagnosticSeverity severity)
{
switch (severity)
{
case DiagnosticSeverity.Hidden:
return ReportDiagnostic.Hidden;
case DiagnosticSeverity.Info:
return ReportDiagnostic.Info;
case DiagnosticSeverity.Warning:
return ReportDiagnostic.Warn;
case DiagnosticSeverity.Error:
return ReportDiagnostic.Error;
default:
throw ExceptionUtilities.UnexpectedValue(severity);
}
}
/// <summary>
/// Returns true if diagnostic descriptor is not configurable, i.e. cannot be suppressed or filtered or have its severity changed.
/// For example, compiler errors are always non-configurable.
/// </summary>
internal bool IsNotConfigurable()
{
return AnalyzerManager.HasNotConfigurableTag(this.CustomTags);
}
}
}