60 lines
2.0 KiB
C#
60 lines
2.0 KiB
C#
using System;
|
|
using System.Threading;
|
|
using Microsoft.CodeAnalysis.Text;
|
|
|
|
namespace Microsoft.CodeAnalysis.Diagnostics;
|
|
|
|
public readonly struct SyntaxTreeAnalysisContext
|
|
{
|
|
private readonly SyntaxTree _tree;
|
|
|
|
private readonly Compilation? _compilationOpt;
|
|
|
|
private readonly AnalyzerOptions _options;
|
|
|
|
private readonly Action<Diagnostic> _reportDiagnostic;
|
|
|
|
private readonly Func<Diagnostic, CancellationToken, bool> _isSupportedDiagnostic;
|
|
|
|
private readonly CancellationToken _cancellationToken;
|
|
|
|
public SyntaxTree Tree => _tree;
|
|
|
|
public AnalyzerOptions Options => _options;
|
|
|
|
public TextSpan? FilterSpan { get; }
|
|
|
|
public bool IsGeneratedCode { get; }
|
|
|
|
public CancellationToken CancellationToken => _cancellationToken;
|
|
|
|
internal Compilation? Compilation => _compilationOpt;
|
|
|
|
[Obsolete("Use CompilationWithAnalyzers instead. See https://github.com/dotnet/roslyn/issues/63440 for more details.")]
|
|
public SyntaxTreeAnalysisContext(SyntaxTree tree, AnalyzerOptions options, Action<Diagnostic> reportDiagnostic, Func<Diagnostic, bool> isSupportedDiagnostic, CancellationToken cancellationToken)
|
|
: this(tree, options, reportDiagnostic, (Diagnostic d, CancellationToken _) => isSupportedDiagnostic(d), null, null, isGeneratedCode: false, cancellationToken)
|
|
{
|
|
}
|
|
|
|
internal SyntaxTreeAnalysisContext(SyntaxTree tree, AnalyzerOptions options, Action<Diagnostic> reportDiagnostic, Func<Diagnostic, CancellationToken, bool> isSupportedDiagnostic, Compilation? compilation, TextSpan? filterSpan, bool isGeneratedCode, CancellationToken cancellationToken)
|
|
{
|
|
_tree = tree;
|
|
_options = options;
|
|
_reportDiagnostic = reportDiagnostic;
|
|
_isSupportedDiagnostic = isSupportedDiagnostic;
|
|
_compilationOpt = compilation;
|
|
FilterSpan = filterSpan;
|
|
IsGeneratedCode = isGeneratedCode;
|
|
_cancellationToken = cancellationToken;
|
|
}
|
|
|
|
public void ReportDiagnostic(Diagnostic diagnostic)
|
|
{
|
|
DiagnosticAnalysisContextHelpers.VerifyArguments(diagnostic, _compilationOpt, _isSupportedDiagnostic, _cancellationToken);
|
|
lock (_reportDiagnostic)
|
|
{
|
|
_reportDiagnostic(diagnostic);
|
|
}
|
|
}
|
|
}
|