-
Notifications
You must be signed in to change notification settings - Fork 4k
/
AnalysisResult.cs
147 lines (128 loc) · 6.46 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
// 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.
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<AdditionalText, ImmutableDictionary<DiagnosticAnalyzer, ImmutableArray<Diagnostic>>> localAdditionalFileDiagnostics,
ImmutableDictionary<DiagnosticAnalyzer, ImmutableArray<Diagnostic>> nonLocalDiagnostics,
ImmutableDictionary<DiagnosticAnalyzer, AnalyzerTelemetryInfo> analyzerTelemetryInfo)
{
Analyzers = analyzers;
SyntaxDiagnostics = localSyntaxDiagnostics;
SemanticDiagnostics = localSemanticDiagnostics;
AdditionalFileDiagnostics = localAdditionalFileDiagnostics;
CompilationDiagnostics = nonLocalDiagnostics;
AnalyzerTelemetryInfo = analyzerTelemetryInfo;
}
/// <summary>
/// Analyzers corresponding to this analysis result.
/// </summary>
public ImmutableArray<DiagnosticAnalyzer> Analyzers { get; }
/// <summary>
/// Syntax diagnostics reported by the <see cref="Analyzers"/>.
/// </summary>
public ImmutableDictionary<SyntaxTree, ImmutableDictionary<DiagnosticAnalyzer, ImmutableArray<Diagnostic>>> SyntaxDiagnostics { get; }
/// <summary>
/// Semantic diagnostics reported by the <see cref="Analyzers"/>.
/// </summary>
public ImmutableDictionary<SyntaxTree, ImmutableDictionary<DiagnosticAnalyzer, ImmutableArray<Diagnostic>>> SemanticDiagnostics { get; }
/// <summary>
/// Diagnostics in additional files reported by the <see cref="Analyzers"/>.
/// </summary>
public ImmutableDictionary<AdditionalText, ImmutableDictionary<DiagnosticAnalyzer, ImmutableArray<Diagnostic>>> AdditionalFileDiagnostics { get; }
/// <summary>
/// Compilation diagnostics reported by the <see cref="Analyzers"/>.
/// </summary>
public ImmutableDictionary<DiagnosticAnalyzer, ImmutableArray<Diagnostic>> CompilationDiagnostics { get; }
/// <summary>
/// Analyzer telemetry info (register action counts and execution times).
/// </summary>
public ImmutableDictionary<DiagnosticAnalyzer, AnalyzerTelemetryInfo> AnalyzerTelemetryInfo { get; }
/// <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));
}
/// <summary>
/// Gets all the diagnostics reported by all the <see cref="Analyzers"/>.
/// </summary>
public ImmutableArray<Diagnostic> GetAllDiagnostics()
{
return GetDiagnostics(Analyzers);
}
private ImmutableArray<Diagnostic> GetDiagnostics(IEnumerable<DiagnosticAnalyzer> analyzers)
{
var excludedAnalyzers = Analyzers.Except(analyzers);
var excludedAnalyzersSet = excludedAnalyzers.Any() ? excludedAnalyzers.ToImmutableHashSet() : ImmutableHashSet<DiagnosticAnalyzer>.Empty;
return GetDiagnostics(excludedAnalyzersSet);
}
private ImmutableArray<Diagnostic> GetDiagnostics(ImmutableHashSet<DiagnosticAnalyzer> excludedAnalyzers)
{
if (SyntaxDiagnostics.Count > 0 || SemanticDiagnostics.Count > 0 || AdditionalFileDiagnostics.Count > 0 || CompilationDiagnostics.Count > 0)
{
var builder = ImmutableArray.CreateBuilder<Diagnostic>();
AddLocalDiagnostics(SyntaxDiagnostics, excludedAnalyzers, builder);
AddLocalDiagnostics(SemanticDiagnostics, excludedAnalyzers, builder);
AddLocalDiagnostics(AdditionalFileDiagnostics, excludedAnalyzers, builder);
AddNonLocalDiagnostics(CompilationDiagnostics, excludedAnalyzers, builder);
return builder.ToImmutable();
}
return ImmutableArray<Diagnostic>.Empty;
}
private static void AddLocalDiagnostics<T>(
ImmutableDictionary<T, ImmutableDictionary<DiagnosticAnalyzer, ImmutableArray<Diagnostic>>> localDiagnostics,
ImmutableHashSet<DiagnosticAnalyzer> excludedAnalyzers,
ImmutableArray<Diagnostic>.Builder builder)
where T : notnull
{
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);
}
}
}
}