/
AnalysisResult.cs
161 lines (138 loc) · 7.15 KB
/
AnalysisResult.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
#nullable enable
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using Microsoft.CodeAnalysis.Diagnostics.Telemetry;
using Roslyn.Utilities;
namespace Microsoft.CodeAnalysis.Diagnostics
{
/// <summary>
/// Stores the results of analyzer execution:
/// 1. Local and non-local diagnostics, per-analyzer.
/// 2. Analyzer execution times, if requested.
/// </summary>
public class AnalysisResult
{
internal AnalysisResult(
ImmutableArray<DiagnosticAnalyzer> analyzers,
ImmutableDictionary<SyntaxTree, ImmutableDictionary<DiagnosticAnalyzer, ImmutableArray<Diagnostic>>> localSyntaxDiagnostics,
ImmutableDictionary<SyntaxTree, ImmutableDictionary<DiagnosticAnalyzer, ImmutableArray<Diagnostic>>> localSemanticDiagnostics,
ImmutableDictionary<DiagnosticAnalyzer, ImmutableArray<Diagnostic>> nonLocalDiagnostics,
ImmutableDictionary<DiagnosticAnalyzer, AnalyzerTelemetryInfo> analyzerTelemetryInfo)
{
Analyzers = analyzers;
SyntaxDiagnostics = localSyntaxDiagnostics;
SemanticDiagnostics = localSemanticDiagnostics;
CompilationDiagnostics = nonLocalDiagnostics;
AnalyzerTelemetryInfo = analyzerTelemetryInfo;
}
/// <summary>
/// Analyzers corresponding to this analysis result.
/// </summary>
public ImmutableArray<DiagnosticAnalyzer> Analyzers { get; private set; }
/// <summary>
/// Syntax diagnostics reported by the <see cref="Analyzers"/>.
/// </summary>
public ImmutableDictionary<SyntaxTree, ImmutableDictionary<DiagnosticAnalyzer, ImmutableArray<Diagnostic>>> SyntaxDiagnostics { get; private set; }
/// <summary>
/// Semantic diagnostics reported by the <see cref="Analyzers"/>.
/// </summary>
public ImmutableDictionary<SyntaxTree, ImmutableDictionary<DiagnosticAnalyzer, ImmutableArray<Diagnostic>>> SemanticDiagnostics { get; private set; }
/// <summary>
/// Compilation diagnostics reported by the <see cref="Analyzers"/>.
/// </summary>
public ImmutableDictionary<DiagnosticAnalyzer, ImmutableArray<Diagnostic>> CompilationDiagnostics { get; private set; }
/// <summary>
/// Analyzer telemetry info (register action counts and execution times).
/// </summary>
public ImmutableDictionary<DiagnosticAnalyzer, AnalyzerTelemetryInfo> AnalyzerTelemetryInfo { get; private set; }
/// <summary>
/// Gets all the diagnostics reported by the given <paramref name="analyzer"/>.
/// </summary>
public ImmutableArray<Diagnostic> GetAllDiagnostics(DiagnosticAnalyzer analyzer)
{
if (!Analyzers.Contains(analyzer))
{
throw new ArgumentException(CodeAnalysisResources.UnsupportedAnalyzerInstance, nameof(analyzer));
}
return GetDiagnostics(SpecializedCollections.SingletonEnumerable(analyzer), getLocalSyntaxDiagnostics: true, getLocalSemanticDiagnostics: true, getNonLocalDiagnostics: true);
}
/// <summary>
/// Gets all the diagnostics reported by all the <see cref="Analyzers"/>.
/// </summary>
public ImmutableArray<Diagnostic> GetAllDiagnostics()
{
return GetDiagnostics(Analyzers, getLocalSyntaxDiagnostics: true, getLocalSemanticDiagnostics: true, getNonLocalDiagnostics: true);
}
internal ImmutableArray<Diagnostic> GetSyntaxDiagnostics(ImmutableArray<DiagnosticAnalyzer> analyzers)
{
return GetDiagnostics(analyzers, getLocalSyntaxDiagnostics: true, getLocalSemanticDiagnostics: false, getNonLocalDiagnostics: false);
}
internal ImmutableArray<Diagnostic> GetSemanticDiagnostics(ImmutableArray<DiagnosticAnalyzer> analyzers)
{
return GetDiagnostics(analyzers, getLocalSyntaxDiagnostics: false, getLocalSemanticDiagnostics: true, getNonLocalDiagnostics: false);
}
internal ImmutableArray<Diagnostic> GetDiagnostics(IEnumerable<DiagnosticAnalyzer> analyzers, bool getLocalSyntaxDiagnostics, bool getLocalSemanticDiagnostics, bool getNonLocalDiagnostics)
{
var excludedAnalyzers = Analyzers.Except(analyzers);
var excludedAnalyzersSet = excludedAnalyzers.Any() ? excludedAnalyzers.ToImmutableHashSet() : ImmutableHashSet<DiagnosticAnalyzer>.Empty;
return GetDiagnostics(excludedAnalyzersSet, getLocalSyntaxDiagnostics, getLocalSemanticDiagnostics, getNonLocalDiagnostics);
}
private ImmutableArray<Diagnostic> GetDiagnostics(ImmutableHashSet<DiagnosticAnalyzer> excludedAnalyzers, bool getLocalSyntaxDiagnostics, bool getLocalSemanticDiagnostics, bool getNonLocalDiagnostics)
{
if (SyntaxDiagnostics.Count > 0 || SemanticDiagnostics.Count > 0 || CompilationDiagnostics.Count > 0)
{
var builder = ImmutableArray.CreateBuilder<Diagnostic>();
if (getLocalSyntaxDiagnostics)
{
AddLocalDiagnostics(SyntaxDiagnostics, excludedAnalyzers, builder);
}
if (getLocalSemanticDiagnostics)
{
AddLocalDiagnostics(SemanticDiagnostics, excludedAnalyzers, builder);
}
if (getNonLocalDiagnostics)
{
AddNonLocalDiagnostics(CompilationDiagnostics, excludedAnalyzers, builder);
}
return builder.ToImmutable();
}
return ImmutableArray<Diagnostic>.Empty;
}
private static void AddLocalDiagnostics(
ImmutableDictionary<SyntaxTree, ImmutableDictionary<DiagnosticAnalyzer, ImmutableArray<Diagnostic>>> localDiagnostics,
ImmutableHashSet<DiagnosticAnalyzer> excludedAnalyzers,
ImmutableArray<Diagnostic>.Builder builder)
{
foreach (var diagnosticsByTree in localDiagnostics)
{
foreach (var diagnosticsByAnalyzer in diagnosticsByTree.Value)
{
if (excludedAnalyzers.Contains(diagnosticsByAnalyzer.Key))
{
continue;
}
builder.AddRange(diagnosticsByAnalyzer.Value);
}
}
}
private static void AddNonLocalDiagnostics(
ImmutableDictionary<DiagnosticAnalyzer, ImmutableArray<Diagnostic>> nonLocalDiagnostics,
ImmutableHashSet<DiagnosticAnalyzer> excludedAnalyzers,
ImmutableArray<Diagnostic>.Builder builder)
{
foreach (var diagnosticsByAnalyzer in nonLocalDiagnostics)
{
if (excludedAnalyzers.Contains(diagnosticsByAnalyzer.Key))
{
continue;
}
builder.AddRange(diagnosticsByAnalyzer.Value);
}
}
}
}