-
Notifications
You must be signed in to change notification settings - Fork 4.3k
add bare minimum implementation of diagnostic analyzer that uses compiler analyzer driver #477
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
ec03d31
3989c3c
a3e0ca9
28bd12b
dfaa0f5
2f07a38
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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; | ||
|
|
@@ -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) | ||
|
|
@@ -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) | ||
| { | ||
| if (project == null) | ||
| { | ||
| return ImmutableArray<DiagnosticData>.Empty; | ||
| } | ||
|
|
||
| var compilation = await project.GetCompilationAsync(cancellationToken).ConfigureAwait(false); | ||
|
|
||
| var analyzers = _analyzerManager.CreateDiagnosticAnalyzers(project); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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())); | ||
| } | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
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 :)
There was a problem hiding this comment.
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 :)