提交 6675b8d1 编写于 作者: J jmarolf

Provide a static method for getting diagnostics given a set of analyzers and a compilation.

***NO_CI***
 (changeset 1367702)
上级 bd0015ef
......@@ -11,6 +11,7 @@
using System.Reflection.Metadata;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CodeGen;
using Microsoft.CodeAnalysis.Collections;
using Microsoft.CodeAnalysis.Diagnostics;
......@@ -861,6 +862,35 @@ public INamedTypeSymbol GetTypeByMetadataName(string fullyQualifiedMetadataName)
/// </summary>
public abstract ImmutableArray<Diagnostic> GetDiagnostics(CancellationToken cancellationToken = default(CancellationToken));
/// <summary>
/// Returns all diagnostics computed by the analyzers for the compilation.
/// </summary>
/// <param name="analyzers">The set of analyzers to include in the analysis</param>
/// <param name="cancellationToken">A cancellation token that can be used to abort analysis.</param>
public Task<ImmutableArray<Diagnostic>> GetDiagnosticsAsync(ImmutableArray<DiagnosticAnalyzer> analyzers, CancellationToken cancellationToken = default(CancellationToken))
{
return GetDiagnosticsAsync(analyzers, null, cancellationToken);
}
/// <summary>
/// Returns all diagnostics computed by the analyzers for the compilation.
/// </summary>
/// <param name="analyzers">The set of analyzers to include in the analysis</param>
/// <param name="options">Options that are passed to analyzers</param>
/// <param name="cancellationToken">A cancellation token that can be used to abort analysis.</param>
public Task<ImmutableArray<Diagnostic>> GetDiagnosticsAsync(ImmutableArray<DiagnosticAnalyzer> analyzers, AnalyzerOptions options, CancellationToken cancellationToken = default(CancellationToken))
{
options = options ?? AnalyzerOptions.Empty;
Compilation newCompilation = null;
var analyzerDriver = AnalyzerDriver.Create(this, analyzers, options, out newCompilation, cancellationToken);
// We need to generate compiler events in order for the event queue to be populated and the analyzer driver to return diagnostics.
// So we'll call GetDiagnostics which will generate all events except for those on emit.
newCompilation.GetDiagnostics(cancellationToken);
return analyzerDriver.GetDiagnosticsAsync();
}
internal abstract CommonMessageProvider MessageProvider { get; }
/// <param name="accumulator">Bag to which filtered diagnostics will be added.</param>
......
......@@ -64,10 +64,7 @@ protected static Diagnostic[] GetSortedDiagnosticsFromDocuments(DiagnosticAnalyz
{
var compilation = project.GetCompilationAsync().GetAwaiter().GetResult();
var driver = AnalyzerDriver.Create(compilation, ImmutableArray.Create(analyzer), null, out compilation, CancellationToken.None);
var discarded = compilation.GetDiagnostics();
var diags = driver.GetDiagnosticsAsync().GetAwaiter().GetResult();
var diags = compilation.GetDiagnosticsAsync(ImmutableArray.Create(analyzer)).GetAwaiter().GetResult();
foreach (var diag in diags)
{
if (diag.Location == Location.None || diag.Location.IsInMetadata)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册