private async Task <ImmutableArray <DiagnosticData> > GetDiagnosticsAsync(
                TextDocument document, AnalysisKind kind, CancellationToken cancellationToken)
            {
                var loadDiagnostic = await document.State.GetLoadDiagnosticAsync(cancellationToken).ConfigureAwait(false);

                if (loadDiagnostic != null)
                {
                    return(ImmutableArray.Create(DiagnosticData.Create(loadDiagnostic, document)));
                }

                var project   = document.Project;
                var analyzers = GetAnalyzers(project.Solution.State.Analyzers, project);

                if (analyzers.IsEmpty)
                {
                    return(ImmutableArray <DiagnosticData> .Empty);
                }

                var crashOnAnalyzerException = _service._globalOptions.GetOption(InternalDiagnosticsOptions.CrashOnAnalyzerException);
                var compilationWithAnalyzers = await DocumentAnalysisExecutor.CreateCompilationWithAnalyzersAsync(
                    project, analyzers, includeSuppressedDiagnostics : false, crashOnAnalyzerException, cancellationToken).ConfigureAwait(false);

                var analysisScope = new DocumentAnalysisScope(document, span: null, analyzers, kind);
                var executor      = new DocumentAnalysisExecutor(analysisScope, compilationWithAnalyzers, _diagnosticAnalyzerRunner, logPerformanceInfo: true);

                using var _ = ArrayBuilder <DiagnosticData> .GetInstance(out var builder);

                foreach (var analyzer in analyzers)
                {
                    builder.AddRange(await executor.ComputeDiagnosticsAsync(analyzer, cancellationToken).ConfigureAwait(false));
                }

                return(builder.ToImmutable());
            }
            /// <summary>
            /// Get diagnostics for the given document.
            ///
            /// This is a simple API to get all diagnostics for the given document.
            ///
            /// The intended audience for this API is for ones that pefer simplicity over performance such as document that belong to misc project.
            /// this doesn't cache nor use cache for anything. it will re-caculate new diagnostics every time for the given document.
            /// it will not persist any data on disk nor use OOP to calcuate the data.
            ///
            /// This should never be used when performance is a big concern. for such context, use much complex API from IDiagnosticAnalyzerService
            /// that provide all kinds of knobs/cache/persistency/OOP to get better perf over simplicity.
            /// </summary>
            private async Task <ImmutableArray <DiagnosticData> > GetDiagnosticsAsync(
                TextDocument document, AnalysisKind kind, CancellationToken cancellationToken)
            {
                var loadDiagnostic = await document.State.GetLoadDiagnosticAsync(cancellationToken).ConfigureAwait(false);

                if (loadDiagnostic != null)
                {
                    return(ImmutableArray.Create(DiagnosticData.Create(loadDiagnostic, document)));
                }

                var project   = document.Project;
                var analyzers = GetAnalyzers(project.Solution.State.Analyzers, project);

                if (analyzers.IsEmpty)
                {
                    return(ImmutableArray <DiagnosticData> .Empty);
                }

                var compilationWithAnalyzers = await AnalyzerHelper.CreateCompilationWithAnalyzersAsync(
                    project, analyzers, includeSuppressedDiagnostics : false, cancellationToken).ConfigureAwait(false);

                var analysisScope = new DocumentAnalysisScope(document, span: null, analyzers, kind);
                var executor      = new DocumentAnalysisExecutor(analysisScope, compilationWithAnalyzers, _service._analyzerInfoCache);

                var builder = ArrayBuilder <DiagnosticData> .GetInstance();

                foreach (var analyzer in analyzers)
                {
                    builder.AddRange(await executor.ComputeDiagnosticsAsync(analyzer, cancellationToken).ConfigureAwait(false));
                }

                return(builder.ToImmutableAndFree());
            }
示例#3
0
            > GetAnalysisResultAsync(
            DocumentAnalysisScope analysisScope,
            CancellationToken cancellationToken
            )
        {
            RoslynDebug.Assert(_compilationWithAnalyzers != null);

            try
            {
                var resultAndTelemetry = await _diagnosticAnalyzerRunner
                                         .AnalyzeDocumentAsync(
                    analysisScope,
                    _compilationWithAnalyzers,
                    _logPerformanceInfo,
                    getTelemetryInfo : false,
                    cancellationToken
                    )
                                         .ConfigureAwait(false);

                return(resultAndTelemetry.AnalysisResult);
            }
            catch
            {
                _onAnalysisException?.Invoke();
                throw;
            }
        }
示例#4
0
 public Task <DiagnosticAnalysisResultMap <DiagnosticAnalyzer, DiagnosticAnalysisResult> > AnalyzeDocumentAsync(
     DocumentAnalysisScope documentAnalysisScope,
     CompilationWithAnalyzers compilationWithAnalyzers,
     bool logPerformanceInfo,
     bool getTelemetryInfo,
     CancellationToken cancellationToken)
 => AnalyzeAsync(documentAnalysisScope, documentAnalysisScope.TextDocument.Project, compilationWithAnalyzers,
                 forceExecuteAllAnalyzers: false, logPerformanceInfo, getTelemetryInfo, cancellationToken);
示例#5
0
        public DocumentAnalysisExecutor(
            DocumentAnalysisScope analysisScope,
            CompilationWithAnalyzers?compilationWithAnalyzers,
            DiagnosticAnalyzerInfoCache analyzerInfoCache)
        {
            AnalysisScope             = analysisScope;
            _compilationWithAnalyzers = compilationWithAnalyzers;
            _analyzerInfoCache        = analyzerInfoCache;

            var compilationBasedAnalyzers = compilationWithAnalyzers?.Analyzers.ToImmutableHashSet();

            _compilationBasedAnalyzersInAnalysisScope = compilationBasedAnalyzers != null
                ? analysisScope.Analyzers.WhereAsArray(compilationBasedAnalyzers.Contains)
                : ImmutableArray <DiagnosticAnalyzer> .Empty;
        }
示例#6
0
        public DocumentAnalysisExecutor(
            DocumentAnalysisScope analysisScope,
            CompilationWithAnalyzers?compilationWithAnalyzers,
            InProcOrRemoteHostAnalyzerRunner diagnosticAnalyzerRunner,
            bool logPerformanceInfo)
        {
            AnalysisScope             = analysisScope;
            _compilationWithAnalyzers = compilationWithAnalyzers;
            _diagnosticAnalyzerRunner = diagnosticAnalyzerRunner;
            _logPerformanceInfo       = logPerformanceInfo;

            var compilationBasedAnalyzers = compilationWithAnalyzers?.Analyzers.ToImmutableHashSet();

            _compilationBasedAnalyzersInAnalysisScope = compilationBasedAnalyzers != null
                ? analysisScope.Analyzers.WhereAsArray(compilationBasedAnalyzers.Contains)
                : ImmutableArray <DiagnosticAnalyzer> .Empty;
        }
示例#7
0
        private async Task <ImmutableDictionary <DiagnosticAnalyzer, DiagnosticAnalysisResult> > GetAnalysisResultAsync(DocumentAnalysisScope analysisScope, CancellationToken cancellationToken)
        {
            RoslynDebug.Assert(_compilationWithAnalyzers != null);

            var resultAndTelemetry = await _diagnosticAnalyzerRunner.AnalyzeDocumentAsync(analysisScope, _compilationWithAnalyzers,
                                                                                          _logPerformanceInfo, getTelemetryInfo : false, cancellationToken).ConfigureAwait(false);

            return(resultAndTelemetry.AnalysisResult);
        }