Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 63 additions & 25 deletions src/Features/Core/Diagnostics/AnalyzerManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,60 +102,66 @@ public ImmutableArray<DiagnosticDescriptor> GetDiagnosticDescriptors(DiagnosticA
}

/// <summary>
/// Get <see cref="AnalyzerReference"/> identity and <see cref="DiagnosticDescriptor"/>s map
/// Get <see cref="AnalyzerReference"/> identity and <see cref="DiagnosticAnalyzer"/>s map for given <paramref name="language"/>
/// </summary>
public ImmutableDictionary<string, ImmutableArray<DiagnosticAnalyzer>> GetHostDiagnosticAnalyzersPerReference(string language)
{
return _hostDiagnosticAnalyzersPerLanguageMap.GetOrAdd(language, CreateHostDiagnosticAnalyzers);
}

/// <summary>
/// Create <see cref="AnalyzerReference"/> identity and <see cref="DiagnosticDescriptor"/>s map
/// </summary>
public ImmutableDictionary<string, ImmutableArray<DiagnosticDescriptor>> GetHostDiagnosticDescriptorsPerReference()
{
return GetDiagnosticDescriptorsPerReference(_lazyHostDiagnosticAnalyzersPerReferenceMap.Value);
return CreateDiagnosticDescriptorsPerReference(_lazyHostDiagnosticAnalyzersPerReferenceMap.Value);
}

/// <summary>
/// Get <see cref="AnalyzerReference"/> identity and <see cref="DiagnosticDescriptor"/>s map for given <paramref name="project"/>
/// Create <see cref="AnalyzerReference"/> identity and <see cref="DiagnosticDescriptor"/>s map for given <paramref name="project"/>
/// </summary>
public ImmutableDictionary<string, ImmutableArray<DiagnosticDescriptor>> GetDiagnosticDescriptorsPerReference(Project project)
public ImmutableDictionary<string, ImmutableArray<DiagnosticDescriptor>> CreateDiagnosticDescriptorsPerReference(Project project)
{
return CreateDiagnosticDescriptorsPerReference(CreateDiagnosticAnalyzersPerReference(project));
}

/// <summary>
/// Create <see cref="AnalyzerReference"/> identity and <see cref="DiagnosticAnalyzer"/>s map for given <paramref name="project"/>
/// </summary>
public ImmutableDictionary<string, ImmutableArray<DiagnosticAnalyzer>> CreateDiagnosticAnalyzersPerReference(Project project)
{
var hostAnalyzerReferences = GetHostDiagnosticAnalyzersPerReference(project.Language);
var projectAnalyzerReferences = CreateDiagnosticAnalyzersPerReferenceMap(CreateAnalyzerReferencesMap(project.AnalyzerReferences), project.Language);
var projectAnalyzerReferences = CreateDiagnosticAnalyzersPerReferenceMap(CreateAnalyzerReferencesMap(project.AnalyzerReferences.Where(CheckAnalyzerReferenceIdentity)), project.Language);

return GetDiagnosticDescriptorsPerReference(hostAnalyzerReferences.Concat(projectAnalyzerReferences));
return MergeDiagnosticAnalyzerMap(hostAnalyzerReferences, projectAnalyzerReferences);
}

/// <summary>
/// Get <see cref="AnalyzerReference"/> identity and <see cref="DiagnosticAnalyzer"/>s map for given <paramref name="language"/>
/// Create <see cref="DiagnosticAnalyzer"/>s collection for given <paramref name="project"/>
/// </summary>
public ImmutableDictionary<string, ImmutableArray<DiagnosticAnalyzer>> GetHostDiagnosticAnalyzersPerReference(string language)
public ImmutableArray<DiagnosticAnalyzer> CreateDiagnosticAnalyzers(Project project)
{
return _hostDiagnosticAnalyzersPerLanguageMap.GetOrAdd(language, CreateHostDiagnosticAnalyzers);
var analyzersPerReferences = CreateDiagnosticAnalyzersPerReference(project);
return analyzersPerReferences.SelectMany(kv => kv.Value).ToImmutableArray();
}

private ImmutableDictionary<string, ImmutableArray<DiagnosticDescriptor>> GetDiagnosticDescriptorsPerReference(
IEnumerable<KeyValuePair<string, ImmutableArray<DiagnosticAnalyzer>>> analyzersMap)
private ImmutableDictionary<string, ImmutableArray<DiagnosticDescriptor>> CreateDiagnosticDescriptorsPerReference(
ImmutableDictionary<string, ImmutableArray<DiagnosticAnalyzer>> analyzersMap)
{
var seen = new HashSet<DiagnosticAnalyzer>();
var builder = ImmutableDictionary.CreateBuilder<string, ImmutableArray<DiagnosticDescriptor>>();
foreach (var kv in analyzersMap)
{
var referenceId = kv.Key;
var analyzers = kv.Value;

// this can happen if same analyzer exist in both host and projects.
if (builder.ContainsKey(referenceId))
{
continue;
}

var descriptors = ImmutableArray.CreateBuilder<DiagnosticDescriptor>();
foreach (var analyzer in analyzers)
{
// don't put duplicated analyzers
if (analyzer == null || !seen.Add(analyzer))
{
continue;
}

// given map should be in good shape. no duplication. no null and etc
descriptors.AddRange(GetDiagnosticDescriptors(analyzer));
}

// there can't be duplication since _hostAnalyzerReferenceMap is already de-duplicated.
builder.Add(referenceId, descriptors.ToImmutable());
}

Expand Down Expand Up @@ -190,6 +196,16 @@ private static string GetAnalyzerReferenceId(AnalyzerReference reference)
return reference.Display ?? FeaturesResources.Unknown;
}

private bool CheckAnalyzerReferenceIdentity(AnalyzerReference reference)
{
if (reference == null)
{
return false;
}

return !_hostAnalyzerReferencesMap.ContainsKey(GetAnalyzerReferenceId(reference));
}

private static ImmutableDictionary<string, ImmutableArray<DiagnosticAnalyzer>> CreateDiagnosticAnalyzersPerReferenceMap(
IDictionary<string, AnalyzerReference> analyzerReferencesMap, string languageOpt = null)
{
Expand All @@ -204,7 +220,7 @@ private static ImmutableDictionary<string, ImmutableArray<DiagnosticAnalyzer>> C
}

// input "analyzerReferencesMap" is a dictionary, so there will be no duplication here.
builder.Add(reference.Key, analyzers);
builder.Add(reference.Key, analyzers.WhereNotNull().ToImmutableArray());
}

return builder.ToImmutable();
Expand Down Expand Up @@ -248,5 +264,27 @@ private static ImmutableArray<AnalyzerReference> CreateAnalyzerReferencesFromAss

return builder.ToImmutable();
}

private static ImmutableDictionary<string, ImmutableArray<DiagnosticAnalyzer>> MergeDiagnosticAnalyzerMap(
ImmutableDictionary<string, ImmutableArray<DiagnosticAnalyzer>> map1, ImmutableDictionary<string, ImmutableArray<DiagnosticAnalyzer>> map2)
{
var current = map1;
var seen = new HashSet<DiagnosticAnalyzer>(map1.Values.SelectMany(v => v));

foreach (var kv in map2)
{
var referenceIdentity = kv.Key;
var analyzers = kv.Value;

if (map1.ContainsKey(referenceIdentity))
{
continue;
}

current = current.Add(referenceIdentity, analyzers.Where(a => seen.Add(a)).ToImmutableArray());
}

return current;
}
}
}
2 changes: 1 addition & 1 deletion src/Features/Core/Diagnostics/DiagnosticAnalyzerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public ImmutableDictionary<string, ImmutableArray<DiagnosticDescriptor>> GetDiag
return _analyzerManager.GetHostDiagnosticDescriptorsPerReference();
}

return _analyzerManager.GetDiagnosticDescriptorsPerReference(projectOpt);
return _analyzerManager.CreateDiagnosticDescriptorsPerReference(projectOpt);
}

public ImmutableArray<DiagnosticDescriptor> GetDiagnosticDescriptors(DiagnosticAnalyzer analyzer)
Expand Down
134 changes: 118 additions & 16 deletions src/Features/Core/Diagnostics/EngineV2/DiagnosticIncrementalAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Text;
Expand Down Expand Up @@ -30,9 +31,11 @@ public override Task AnalyzeDocumentAsync(Document document, SyntaxNode bodyOpt,
return SpecializedTasks.EmptyTask;
}

public override Task AnalyzeProjectAsync(Project project, bool semanticsChanged, CancellationToken cancellationToken)
public override async Task AnalyzeProjectAsync(Project project, bool semanticsChanged, CancellationToken cancellationToken)
{
return SpecializedTasks.EmptyTask;
var diagnostics = await GetDiagnosticsAsync(project.Solution, project.Id, null, cancellationToken).ConfigureAwait(false);

RaiseEvents(project, diagnostics);
}

public override Task AnalyzeSyntaxAsync(Document document, CancellationToken cancellationToken)
Expand All @@ -57,51 +60,150 @@ public override Task NewSolutionSnapshotAsync(Solution solution, CancellationTok

public override void RemoveDocument(DocumentId documentId)
{
_owner.RaiseDiagnosticsUpdated(
this, new DiagnosticsUpdatedArgs(ValueTuple.Create(this, documentId), _workspace, null, null, null, ImmutableArray<DiagnosticData>.Empty));
}

public override void RemoveProject(ProjectId projectId)
{
_owner.RaiseDiagnosticsUpdated(
this, new DiagnosticsUpdatedArgs(ValueTuple.Create(this, projectId), _workspace, null, null, null, ImmutableArray<DiagnosticData>.Empty));
}
#endregion

public override Task<ImmutableArray<DiagnosticData>> GetCachedDiagnosticsAsync(Solution solution, ProjectId projectId = null, DocumentId documentId = null, CancellationToken cancellationToken = default(CancellationToken))
{
return SpecializedTasks.EmptyImmutableArray<DiagnosticData>();
return GetDiagnosticsAsync(solution, projectId, documentId, cancellationToken);
}

public override Task<ImmutableArray<DiagnosticData>> GetSpecificCachedDiagnosticsAsync(Solution solution, object id, CancellationToken cancellationToken)
{
return SpecializedTasks.EmptyImmutableArray<DiagnosticData>();
return GetSpecificDiagnosticsAsync(solution, id, cancellationToken);
}

public override Task<ImmutableArray<DiagnosticData>> GetDiagnosticsAsync(Solution solution, ProjectId projectId = null, DocumentId documentId = null, CancellationToken cancellationToken = default(CancellationToken))
public override async Task<ImmutableArray<DiagnosticData>> GetDiagnosticsAsync(Solution solution, ProjectId projectId = null, DocumentId documentId = null, CancellationToken cancellationToken = default(CancellationToken))
{
return SpecializedTasks.EmptyImmutableArray<DiagnosticData>();
if (documentId != null)
{
var diagnostics = await GetProjectDiagnosticsAsync(solution.GetProject(projectId), cancellationToken).ConfigureAwait(false);
return diagnostics.Where(d => d.DocumentId == documentId).ToImmutableArrayOrEmpty();
}

if (projectId != null)
{
return await GetProjectDiagnosticsAsync(solution.GetProject(projectId), cancellationToken).ConfigureAwait(false);
}

var builder = ImmutableArray.CreateBuilder<DiagnosticData>();
foreach (var project in solution.Projects)
{
builder.AddRange(await GetProjectDiagnosticsAsync(project, cancellationToken).ConfigureAwait(false));
}

return builder.ToImmutable();
}

public override Task<ImmutableArray<DiagnosticData>> GetSpecificDiagnosticsAsync(Solution solution, object id, CancellationToken cancellationToken)
public override async Task<ImmutableArray<DiagnosticData>> GetSpecificDiagnosticsAsync(Solution solution, object id, CancellationToken cancellationToken)
{
return SpecializedTasks.EmptyImmutableArray<DiagnosticData>();
if (id is ValueTuple<DiagnosticIncrementalAnalyzer, DocumentId>)
{
var key = (ValueTuple<DiagnosticIncrementalAnalyzer, DocumentId>)id;
return await GetDiagnosticsAsync(solution, key.Item2.ProjectId, key.Item2, cancellationToken).ConfigureAwait(false);
}

if (id is ValueTuple<DiagnosticIncrementalAnalyzer, ProjectId>)
{
var key = (ValueTuple<DiagnosticIncrementalAnalyzer, ProjectId>)id;
var diagnostics = await GetDiagnosticsAsync(solution, key.Item2, null, cancellationToken).ConfigureAwait(false);
return diagnostics.Where(d => d.DocumentId == null).ToImmutableArray();
}

return ImmutableArray<DiagnosticData>.Empty;
}

public override Task<ImmutableArray<DiagnosticData>> GetDiagnosticsForIdsAsync(Solution solution, ProjectId projectId = null, DocumentId documentId = null, ImmutableHashSet<string> diagnosticIds = null, CancellationToken cancellationToken = default(CancellationToken))
public override async Task<ImmutableArray<DiagnosticData>> GetDiagnosticsForIdsAsync(Solution solution, ProjectId projectId = null, DocumentId documentId = null, ImmutableHashSet<string> diagnosticIds = null, CancellationToken cancellationToken = default(CancellationToken))
{
return SpecializedTasks.EmptyImmutableArray<DiagnosticData>();
var diagnostics = await GetDiagnosticsAsync(solution, projectId, documentId, cancellationToken).ConfigureAwait(false);
return diagnostics.Where(d => diagnosticIds.Contains(d.Id)).ToImmutableArrayOrEmpty();
}

public override Task<ImmutableArray<DiagnosticData>> GetProjectDiagnosticsForIdsAsync(Solution solution, ProjectId projectId = null, ImmutableHashSet<string> diagnosticIds = null, CancellationToken cancellationToken = default(CancellationToken))
public override async Task<ImmutableArray<DiagnosticData>> GetProjectDiagnosticsForIdsAsync(Solution solution, ProjectId projectId = null, ImmutableHashSet<string> diagnosticIds = null, CancellationToken cancellationToken = default(CancellationToken))
{
return SpecializedTasks.EmptyImmutableArray<DiagnosticData>();
var diagnostics = await GetDiagnosticsForIdsAsync(solution, projectId, null, diagnosticIds, cancellationToken).ConfigureAwait(false);
return diagnostics.Where(d => d.DocumentId == null).ToImmutableArray();
}

public override Task<bool> TryAppendDiagnosticsForSpanAsync(Document document, TextSpan range, List<DiagnosticData> diagnostics, CancellationToken cancellationToken)
public override async Task<bool> TryAppendDiagnosticsForSpanAsync(Document document, TextSpan range, List<DiagnosticData> result, CancellationToken cancellationToken)
{
return SpecializedTasks.False;
result.AddRange(await GetDiagnosticsForSpanAsync(document, range, cancellationToken).ConfigureAwait(false));
return true;
}

public override Task<IEnumerable<DiagnosticData>> GetDiagnosticsForSpanAsync(Document document, TextSpan range, CancellationToken cancellationToken)
public override async Task<IEnumerable<DiagnosticData>> GetDiagnosticsForSpanAsync(Document document, TextSpan range, CancellationToken cancellationToken)
{
return SpecializedTasks.EmptyEnumerable<DiagnosticData>();
var diagnostics = await GetDiagnosticsAsync(document.Project.Solution, document.Project.Id, document.Id, cancellationToken).ConfigureAwait(false);
return diagnostics.Where(d => range.IntersectsWith(d.TextSpan));
}

private async Task<ImmutableArray<DiagnosticData>> GetProjectDiagnosticsAsync(Project project, CancellationToken cancellationToken)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So basically we come in here for any client that needs any sort of diagnostics right, except cached one? If we ignore perf then I think you fixed lot of bugs in current driver :)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ha ha. this one is very slow :)

{
if (project == null)
{
return ImmutableArray<DiagnosticData>.Empty;
}

var compilation = await project.GetCompilationAsync(cancellationToken).ConfigureAwait(false);

var analyzers = _analyzerManager.CreateDiagnosticAnalyzers(project);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be instead renamed to "GetDiagnosticAnalyzers" as it isn't creating them for each invocation?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure what you meant. GetProjectDiagnosticsAsync generate project wide diagnostics.


var compilationWithAnalyzer = compilation.WithAnalyzers(analyzers, project.AnalyzerOptions, cancellationToken);

// REVIEW: this API is a bit strange.
// if getting diagnostic is cancelled, it has to create new compilation and do everything from scretch again?
return GetDiagnosticData(project, await compilationWithAnalyzer.GetAnalyzerDiagnosticsAsync().ConfigureAwait(false)).ToImmutableArrayOrEmpty();
}

private IEnumerable<DiagnosticData> GetDiagnosticData(Project project, ImmutableArray<Diagnostic> diagnostics)
{
foreach (var diagnostic in diagnostics)
{
if (diagnostic.Location == Location.None)
{
yield return DiagnosticData.Create(project, diagnostic);
continue;
}

var document = project.GetDocument(diagnostic.Location.SourceTree);
if (document == null)
{
continue;
}

yield return DiagnosticData.Create(document, diagnostic);
}
}

private void RaiseEvents(Project project, ImmutableArray<DiagnosticData> diagnostics)
{
var groups = diagnostics.GroupBy(d => d.DocumentId);

var solution = project.Solution;
var workspace = solution.Workspace;

foreach (var kv in groups)
{
if (kv.Key == null)
{
_owner.RaiseDiagnosticsUpdated(
this, new DiagnosticsUpdatedArgs(
ValueTuple.Create(this, project.Id), workspace, solution, project.Id, null, kv.ToImmutableArrayOrEmpty()));
continue;
}

_owner.RaiseDiagnosticsUpdated(
this, new DiagnosticsUpdatedArgs(
ValueTuple.Create(this, kv.Key), workspace, solution, project.Id, kv.Key, kv.ToImmutableArrayOrEmpty()));
}
}
}
}