-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Diagnostic.cs
564 lines (506 loc) · 26.7 KB
/
Diagnostic.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
// 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.Diagnostics;
using System.Globalization;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Text;
using Roslyn.Utilities;
namespace Microsoft.CodeAnalysis
{
/// <summary>
/// Represents a diagnostic, such as a compiler error or a warning, along with the location where it occurred.
/// </summary>
[DebuggerDisplay("{GetDebuggerDisplay(), nq}")]
public abstract partial class Diagnostic : IEquatable<Diagnostic>, IFormattable
{
internal const string CompilerDiagnosticCategory = "Compiler";
/// <summary>
/// Highest valid warning level for non-error diagnostics.
/// </summary>
internal const int HighestValidWarningLevel = 4;
/// <summary>
/// Creates a <see cref="Diagnostic"/> instance.
/// </summary>
/// <param name="descriptor">A <see cref="DiagnosticDescriptor"/> describing the diagnostic</param>
/// <param name="location">An optional primary location of the diagnostic. If null, <see cref="Location"/> will return <see cref="Location.None"/>.</param>
/// <param name="messageArgs">Arguments to the message of the diagnostic</param>
/// <returns>The <see cref="Diagnostic"/> instance.</returns>
public static Diagnostic Create(
DiagnosticDescriptor descriptor,
Location location,
params object[] messageArgs)
{
return Create(descriptor, location, null, null, messageArgs);
}
/// <summary>
/// Creates a <see cref="Diagnostic"/> instance.
/// </summary>
/// <param name="descriptor">A <see cref="DiagnosticDescriptor"/> describing the diagnostic.</param>
/// <param name="location">An optional primary location of the diagnostic. If null, <see cref="Location"/> will return <see cref="Location.None"/>.</param>
/// <param name="properties">
/// An optional set of name-value pairs by means of which the analyzer that creates the diagnostic
/// can convey more detailed information to the fixer. If null, <see cref="Properties"/> will return
/// <see cref="ImmutableDictionary{TKey, TValue}.Empty"/>.
/// </param>
/// <param name="messageArgs">Arguments to the message of the diagnostic.</param>
/// <returns>The <see cref="Diagnostic"/> instance.</returns>
public static Diagnostic Create(
DiagnosticDescriptor descriptor,
Location location,
ImmutableDictionary<string, string> properties,
params object[] messageArgs)
{
return Create(descriptor, location, null, properties, messageArgs);
}
/// <summary>
/// Creates a <see cref="Diagnostic"/> instance.
/// </summary>
/// <param name="descriptor">A <see cref="DiagnosticDescriptor"/> describing the diagnostic.</param>
/// <param name="location">An optional primary location of the diagnostic. If null, <see cref="Location"/> will return <see cref="Location.None"/>.</param>
/// <param name="additionalLocations">
/// An optional set of additional locations related to the diagnostic.
/// Typically, these are locations of other items referenced in the message.
/// If null, <see cref="AdditionalLocations"/> will return an empty list.
/// </param>
/// <param name="messageArgs">Arguments to the message of the diagnostic.</param>
/// <returns>The <see cref="Diagnostic"/> instance.</returns>
public static Diagnostic Create(
DiagnosticDescriptor descriptor,
Location location,
IEnumerable<Location> additionalLocations,
params object[] messageArgs)
{
return Create(descriptor, location, additionalLocations, properties: null, messageArgs: messageArgs);
}
/// <summary>
/// Creates a <see cref="Diagnostic"/> instance.
/// </summary>
/// <param name="descriptor">A <see cref="DiagnosticDescriptor"/> describing the diagnostic.</param>
/// <param name="location">An optional primary location of the diagnostic. If null, <see cref="Location"/> will return <see cref="Location.None"/>.</param>
/// <param name="additionalLocations">
/// An optional set of additional locations related to the diagnostic.
/// Typically, these are locations of other items referenced in the message.
/// If null, <see cref="AdditionalLocations"/> will return an empty list.
/// </param>
/// <param name="properties">
/// An optional set of name-value pairs by means of which the analyzer that creates the diagnostic
/// can convey more detailed information to the fixer. If null, <see cref="Properties"/> will return
/// <see cref="ImmutableDictionary{TKey, TValue}.Empty"/>.
/// </param>
/// <param name="messageArgs">Arguments to the message of the diagnostic.</param>
/// <returns>The <see cref="Diagnostic"/> instance.</returns>
public static Diagnostic Create(
DiagnosticDescriptor descriptor,
Location location,
IEnumerable<Location> additionalLocations,
ImmutableDictionary<string, string> properties,
params object[] messageArgs)
{
return Create(descriptor, location, effectiveSeverity: descriptor.DefaultSeverity, additionalLocations, properties, messageArgs);
}
/// <summary>
/// Creates a <see cref="Diagnostic"/> instance.
/// </summary>
/// <param name="descriptor">A <see cref="DiagnosticDescriptor"/> describing the diagnostic.</param>
/// <param name="location">An optional primary location of the diagnostic. If null, <see cref="Location"/> will return <see cref="Location.None"/>.</param>
/// <param name="effectiveSeverity">Effective severity of the diagnostic.</param>
/// <param name="additionalLocations">
/// An optional set of additional locations related to the diagnostic.
/// Typically, these are locations of other items referenced in the message.
/// If null, <see cref="AdditionalLocations"/> will return an empty list.
/// </param>
/// <param name="properties">
/// An optional set of name-value pairs by means of which the analyzer that creates the diagnostic
/// can convey more detailed information to the fixer. If null, <see cref="Properties"/> will return
/// <see cref="ImmutableDictionary{TKey, TValue}.Empty"/>.
/// </param>
/// <param name="messageArgs">Arguments to the message of the diagnostic.</param>
/// <returns>The <see cref="Diagnostic"/> instance.</returns>
public static Diagnostic Create(
DiagnosticDescriptor descriptor,
Location location,
DiagnosticSeverity effectiveSeverity,
IEnumerable<Location> additionalLocations,
ImmutableDictionary<string, string> properties,
params object[] messageArgs)
{
if (descriptor == null)
{
throw new ArgumentNullException(nameof(descriptor));
}
var warningLevel = GetDefaultWarningLevel(effectiveSeverity);
return SimpleDiagnostic.Create(
descriptor,
severity: effectiveSeverity,
warningLevel: warningLevel,
location: location ?? Location.None,
additionalLocations: additionalLocations,
messageArgs: messageArgs,
properties: properties);
}
/// <summary>
/// Creates a <see cref="Diagnostic"/> instance which is localizable.
/// </summary>
/// <param name="id">An identifier for the diagnostic. For diagnostics generated by the compiler, this will be a numeric code with a prefix such as "CS1001".</param>
/// <param name="category">The category of the diagnostic. For diagnostics generated by the compiler, the category will be "Compiler".</param>
/// <param name="message">The diagnostic message text.</param>
/// <param name="severity">The diagnostic's effective severity.</param>
/// <param name="defaultSeverity">The diagnostic's default severity.</param>
/// <param name="isEnabledByDefault">True if the diagnostic is enabled by default</param>
/// <param name="warningLevel">The warning level, between 1 and 4 if severity is <see cref="DiagnosticSeverity.Warning"/>; otherwise 0.</param>
/// <param name="title">An optional short localizable title describing the diagnostic.</param>
/// <param name="description">An optional longer localizable description for the diagnostic.</param>
/// <param name="helpLink">An optional hyperlink that provides more detailed information regarding the diagnostic.</param>
/// <param name="location">An optional primary location of the diagnostic. If null, <see cref="Location"/> will return <see cref="Location.None"/>.</param>
/// <param name="additionalLocations">
/// An optional set of additional locations related to the diagnostic.
/// Typically, these are locations of other items referenced in the message.
/// If null, <see cref="AdditionalLocations"/> will return an empty list.
/// </param>
/// <param name="customTags">
/// An optional set of custom tags for the diagnostic. See <see cref="WellKnownDiagnosticTags"/> for some well known tags.
/// If null, <see cref="CustomTags"/> will return an empty list.
/// </param>
/// <param name="properties">
/// An optional set of name-value pairs by means of which the analyzer that creates the diagnostic
/// can convey more detailed information to the fixer. If null, <see cref="Properties"/> will return
/// <see cref="ImmutableDictionary{TKey, TValue}.Empty"/>.
/// </param>
/// <returns>The <see cref="Diagnostic"/> instance.</returns>
public static Diagnostic Create(
string id,
string category,
LocalizableString message,
DiagnosticSeverity severity,
DiagnosticSeverity defaultSeverity,
bool isEnabledByDefault,
int warningLevel,
LocalizableString title = null,
LocalizableString description = null,
string helpLink = null,
Location location = null,
IEnumerable<Location> additionalLocations = null,
IEnumerable<string> customTags = null,
ImmutableDictionary<string, string> properties = null)
{
return Create(id, category, message, severity, defaultSeverity, isEnabledByDefault, warningLevel, false,
title, description, helpLink, location, additionalLocations, customTags, properties);
}
/// <summary>
/// Creates a <see cref="Diagnostic"/> instance which is localizable.
/// </summary>
/// <param name="id">An identifier for the diagnostic. For diagnostics generated by the compiler, this will be a numeric code with a prefix such as "CS1001".</param>
/// <param name="category">The category of the diagnostic. For diagnostics generated by the compiler, the category will be "Compiler".</param>
/// <param name="message">The diagnostic message text.</param>
/// <param name="severity">The diagnostic's effective severity.</param>
/// <param name="defaultSeverity">The diagnostic's default severity.</param>
/// <param name="isEnabledByDefault">True if the diagnostic is enabled by default</param>
/// <param name="warningLevel">The warning level, between 1 and 4 if severity is <see cref="DiagnosticSeverity.Warning"/>; otherwise 0.</param>
/// <param name="isSuppressed">Flag indicating whether the diagnostic is suppressed by a source suppression.</param>
/// <param name="title">An optional short localizable title describing the diagnostic.</param>
/// <param name="description">An optional longer localizable description for the diagnostic.</param>
/// <param name="helpLink">An optional hyperlink that provides more detailed information regarding the diagnostic.</param>
/// <param name="location">An optional primary location of the diagnostic. If null, <see cref="Location"/> will return <see cref="Location.None"/>.</param>
/// <param name="additionalLocations">
/// An optional set of additional locations related to the diagnostic.
/// Typically, these are locations of other items referenced in the message.
/// If null, <see cref="AdditionalLocations"/> will return an empty list.
/// </param>
/// <param name="customTags">
/// An optional set of custom tags for the diagnostic. See <see cref="WellKnownDiagnosticTags"/> for some well known tags.
/// If null, <see cref="CustomTags"/> will return an empty list.
/// </param>
/// <param name="properties">
/// An optional set of name-value pairs by means of which the analyzer that creates the diagnostic
/// can convey more detailed information to the fixer. If null, <see cref="Properties"/> will return
/// <see cref="ImmutableDictionary{TKey, TValue}.Empty"/>.
/// </param>
/// <returns>The <see cref="Diagnostic"/> instance.</returns>
public static Diagnostic Create(
string id,
string category,
LocalizableString message,
DiagnosticSeverity severity,
DiagnosticSeverity defaultSeverity,
bool isEnabledByDefault,
int warningLevel,
bool isSuppressed,
LocalizableString title = null,
LocalizableString description = null,
string helpLink = null,
Location location = null,
IEnumerable<Location> additionalLocations = null,
IEnumerable<string> customTags = null,
ImmutableDictionary<string, string> properties = null)
{
if (id == null)
{
throw new ArgumentNullException(nameof(id));
}
if (category == null)
{
throw new ArgumentNullException(nameof(category));
}
if (message == null)
{
throw new ArgumentNullException(nameof(message));
}
return SimpleDiagnostic.Create(id, title ?? string.Empty, category, message, description ?? string.Empty, helpLink ?? string.Empty,
severity, defaultSeverity, isEnabledByDefault, warningLevel, location ?? Location.None, additionalLocations, customTags, properties, isSuppressed);
}
internal static Diagnostic Create(CommonMessageProvider messageProvider, int errorCode)
{
return Create(new DiagnosticInfo(messageProvider, errorCode));
}
internal static Diagnostic Create(CommonMessageProvider messageProvider, int errorCode, params object[] arguments)
{
return Create(new DiagnosticInfo(messageProvider, errorCode, arguments));
}
internal static Diagnostic Create(DiagnosticInfo info)
{
return new DiagnosticWithInfo(info, Location.None);
}
/// <summary>
/// Gets the diagnostic descriptor, which provides a description about a <see cref="Diagnostic"/>.
/// </summary>
public abstract DiagnosticDescriptor Descriptor { get; }
/// <summary>
/// Gets the diagnostic identifier. For diagnostics generated by the compiler, this will be a numeric code with a prefix such as "CS1001".
/// </summary>
public abstract string Id { get; }
/// <summary>
/// Gets the category of diagnostic. For diagnostics generated by the compiler, the category will be "Compiler".
/// </summary>
internal virtual string Category { get { return this.Descriptor.Category; } }
/// <summary>
/// Get the culture specific text of the message.
/// </summary>
public abstract string GetMessage(IFormatProvider formatProvider = null);
/// <summary>
/// Gets the default <see cref="DiagnosticSeverity"/> of the diagnostic's <see cref="DiagnosticDescriptor"/>.
/// </summary>
/// <remarks>
/// To get the effective severity of the diagnostic, use <see cref="Severity"/>.
/// </remarks>
public virtual DiagnosticSeverity DefaultSeverity { get { return this.Descriptor.DefaultSeverity; } }
/// <summary>
/// Gets the effective <see cref="DiagnosticSeverity"/> of the diagnostic.
/// </summary>
/// <remarks>
/// To get the default severity of diagnostic's <see cref="DiagnosticDescriptor"/>, use <see cref="DefaultSeverity"/>.
/// To determine if this is a warning treated as an error, use <see cref="IsWarningAsError"/>.
/// </remarks>
public abstract DiagnosticSeverity Severity { get; }
/// <summary>
/// Gets the warning level. This is 0 for diagnostics with severity <see cref="DiagnosticSeverity.Error"/>,
/// otherwise an integer between 1 and 4.
/// </summary>
public abstract int WarningLevel { get; }
/// <summary>
/// Returns true if the diagnostic has a source suppression, i.e. an attribute or a pragma suppression.
/// </summary>
public abstract bool IsSuppressed { get; }
/// <summary>
/// Gets the <see cref="SuppressionInfo"/> for suppressed diagnostics, i.e. <see cref="IsSuppressed"/> = true.
/// Otherwise, returns null.
/// </summary>
public SuppressionInfo GetSuppressionInfo(Compilation compilation)
{
if (!IsSuppressed)
{
return null;
}
AttributeData attribute;
var suppressMessageState = new SuppressMessageAttributeState(compilation);
if (!suppressMessageState.IsDiagnosticSuppressed(this, out attribute))
{
attribute = null;
}
return new SuppressionInfo(this.Id, attribute);
}
/// <summary>
/// Returns true if this diagnostic is enabled by default by the author of the diagnostic.
/// </summary>
internal virtual bool IsEnabledByDefault { get { return this.Descriptor.IsEnabledByDefault; } }
/// <summary>
/// Returns true if this is a warning treated as an error; otherwise false.
/// </summary>
/// <remarks>
/// True implies <see cref="DefaultSeverity"/> = <see cref="DiagnosticSeverity.Warning"/>
/// and <see cref="Severity"/> = <see cref="DiagnosticSeverity.Error"/>.
/// </remarks>
public bool IsWarningAsError
{
get
{
return this.DefaultSeverity == DiagnosticSeverity.Warning &&
this.Severity == DiagnosticSeverity.Error;
}
}
/// <summary>
/// Gets the primary location of the diagnostic, or <see cref="Location.None"/> if no primary location.
/// </summary>
public abstract Location Location { get; }
/// <summary>
/// Gets an array of additional locations related to the diagnostic.
/// Typically these are the locations of other items referenced in the message.
/// </summary>
public abstract IReadOnlyList<Location> AdditionalLocations { get; }
/// <summary>
/// Gets custom tags for the diagnostic.
/// </summary>
internal virtual IReadOnlyList<string> CustomTags { get { return (IReadOnlyList<string>)this.Descriptor.CustomTags; } }
/// <summary>
/// Gets property bag for the diagnostic. it will return <see cref="ImmutableDictionary{TKey, TValue}.Empty"/>
/// if there is no entry. This can be used to put diagnostic specific information you want
/// to pass around. for example, to corresponding fixer.
/// </summary>
public virtual ImmutableDictionary<string, string> Properties
=> ImmutableDictionary<string, string>.Empty;
string IFormattable.ToString(string ignored, IFormatProvider formatProvider)
{
return DiagnosticFormatter.Instance.Format(this, formatProvider);
}
public override string ToString()
{
return DiagnosticFormatter.Instance.Format(this, CultureInfo.CurrentUICulture);
}
public abstract override bool Equals(object obj);
public abstract override int GetHashCode();
public abstract bool Equals(Diagnostic obj);
private string GetDebuggerDisplay()
{
switch (this.Severity)
{
case InternalDiagnosticSeverity.Unknown:
// If we called ToString before the diagnostic was resolved,
// we would risk infinite recursion (e.g. if we were still computing
// member lists).
return "Unresolved diagnostic at " + this.Location;
case InternalDiagnosticSeverity.Void:
// If we called ToString on a void diagnostic, the MessageProvider
// would complain about the code.
return "Void diagnostic at " + this.Location;
default:
return ToString();
}
}
/// <summary>
/// Create a new instance of this diagnostic with the Location property changed.
/// </summary>
internal abstract Diagnostic WithLocation(Location location);
/// <summary>
/// Create a new instance of this diagnostic with the Severity property changed.
/// </summary>
internal abstract Diagnostic WithSeverity(DiagnosticSeverity severity);
/// <summary>
/// Create a new instance of this diagnostic with the suppression info changed.
/// </summary>
internal abstract Diagnostic WithIsSuppressed(bool isSuppressed);
// compatibility
internal virtual int Code { get { return 0; } }
internal virtual IReadOnlyList<object> Arguments
{
get { return SpecializedCollections.EmptyReadOnlyList<object>(); }
}
/// <summary>
/// Returns true if the diagnostic location (or any additional location) is within the given tree and intersects with the filterSpanWithinTree, if non-null.
/// </summary>
internal bool HasIntersectingLocation(SyntaxTree tree, TextSpan? filterSpanWithinTree = null)
{
var locations = this.GetDiagnosticLocationsWithinTree(tree);
foreach (var location in locations)
{
if (!filterSpanWithinTree.HasValue || filterSpanWithinTree.Value.IntersectsWith(location.SourceSpan))
{
return true;
}
}
return false;
}
private IEnumerable<Location> GetDiagnosticLocationsWithinTree(SyntaxTree tree)
{
if (this.Location.SourceTree == tree)
{
yield return this.Location;
}
if (this.AdditionalLocations != null)
{
foreach (var additionalLocation in this.AdditionalLocations)
{
if (additionalLocation.SourceTree == tree)
{
yield return additionalLocation;
}
}
}
}
internal Diagnostic WithReportDiagnostic(ReportDiagnostic reportAction)
{
switch (reportAction)
{
case ReportDiagnostic.Suppress:
// Suppressed diagnostic.
return null;
case ReportDiagnostic.Error:
return this.WithSeverity(DiagnosticSeverity.Error);
case ReportDiagnostic.Default:
return this;
case ReportDiagnostic.Warn:
return this.WithSeverity(DiagnosticSeverity.Warning);
case ReportDiagnostic.Info:
return this.WithSeverity(DiagnosticSeverity.Info);
case ReportDiagnostic.Hidden:
return this.WithSeverity(DiagnosticSeverity.Hidden);
default:
throw ExceptionUtilities.UnexpectedValue(reportAction);
}
}
/// <summary>
/// Gets the default warning level for a diagnostic severity. Warning levels are used with the <c>/warn:N</c>
/// command line option to suppress diagnostics over a severity of interest. When N is 0, only error severity
/// messages are produced by the compiler. Values greater than 0 indicated that warnings up to and including
/// level N should also be included.
/// </summary>
/// <remarks>
/// <see cref="DiagnosticSeverity.Info"/> and <see cref="DiagnosticSeverity.Hidden"/> are treated as warning
/// level 1. In other words, these diagnostics which typically interact with editor features are enabled unless
/// the special <c>/warn:0</c> option is set.
/// </remarks>
/// <param name="severity">A <see cref="DiagnosticSeverity"/> value.</param>
/// <returns>The default compiler warning level for <paramref name="severity"/>.</returns>
internal static int GetDefaultWarningLevel(DiagnosticSeverity severity)
{
switch (severity)
{
case DiagnosticSeverity.Error:
return 0;
case DiagnosticSeverity.Warning:
default:
return 1;
}
}
/// <summary>
/// Returns true if a diagnostic 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 virtual bool IsNotConfigurable()
{
return AnalyzerManager.HasNotConfigurableTag(this.CustomTags);
}
}
/// <summary>
/// This type is attached to diagnostics for required language version and should only be used
/// on such diagnostics, as they are recognized by <see cref="Compilation.GetRequiredLanguageVersion"/>.
/// </summary>
internal abstract class RequiredLanguageVersion : IFormattable
{
public abstract override string ToString();
string IFormattable.ToString(string format, IFormatProvider formatProvider)
{
return ToString();
}
}
}