Beispiel #1
0
        private static async Task <(DiagnosticAnalysisResult loadDiagnostics, ImmutableHashSet <Document>?failedDocuments)> GetDocumentLoadFailuresAsync(Project project, VersionStamp version, CancellationToken cancellationToken)
        {
            ImmutableHashSet <Document> .Builder?failedDocuments = null;
            ImmutableDictionary <DocumentId, ImmutableArray <DiagnosticData> > .Builder?lazyLoadDiagnostics = null;

            foreach (var document in project.Documents)
            {
                var loadDiagnostic = await document.State.GetLoadDiagnosticAsync(cancellationToken).ConfigureAwait(false);

                if (loadDiagnostic != null)
                {
                    lazyLoadDiagnostics ??= ImmutableDictionary.CreateBuilder <DocumentId, ImmutableArray <DiagnosticData> >();
                    lazyLoadDiagnostics.Add(document.Id, ImmutableArray.Create(DiagnosticData.Create(loadDiagnostic, document)));

                    failedDocuments ??= ImmutableHashSet.CreateBuilder <Document>();
                    failedDocuments.Add(document);
                }
            }

            var result = DiagnosticAnalysisResult.Create(
                project,
                version,
                syntaxLocalMap: lazyLoadDiagnostics?.ToImmutable() ?? ImmutableDictionary <DocumentId, ImmutableArray <DiagnosticData> > .Empty,
                semanticLocalMap: ImmutableDictionary <DocumentId, ImmutableArray <DiagnosticData> > .Empty,
                nonLocalMap: ImmutableDictionary <DocumentId, ImmutableArray <DiagnosticData> > .Empty,
                others: ImmutableArray <DiagnosticData> .Empty,
                documentIds: null);

            return(result, failedDocuments?.ToImmutable());
        }
        public static DiagnosticAnalysisResultMap <DiagnosticAnalyzer, DiagnosticAnalysisResult> ReadDiagnosticAnalysisResults(
            ObjectReader reader, IDictionary <string, DiagnosticAnalyzer> analyzerMap, Project project, VersionStamp version, CancellationToken cancellationToken)
        {
            var diagnosticDataSerializer = new DiagnosticDataSerializer(VersionStamp.Default, VersionStamp.Default);

            var analysisMap = ImmutableDictionary.CreateBuilder <DiagnosticAnalyzer, DiagnosticAnalysisResult>();

            var analysisCount = reader.ReadInt32();

            for (var i = 0; i < analysisCount; i++)
            {
                var analyzer = analyzerMap[reader.ReadString()];

                var syntaxLocalMap   = ReadDiagnosticDataMap(reader, diagnosticDataSerializer, project, cancellationToken);
                var semanticLocalMap = ReadDiagnosticDataMap(reader, diagnosticDataSerializer, project, cancellationToken);
                var nonLocalMap      = ReadDiagnosticDataMap(reader, diagnosticDataSerializer, project, cancellationToken);

                var others = diagnosticDataSerializer.ReadDiagnosticData(reader, project, document: null, cancellationToken);

                var analysisResult = DiagnosticAnalysisResult.Create(
                    project,
                    version,
                    syntaxLocalMap,
                    semanticLocalMap,
                    nonLocalMap,
                    others.NullToEmpty(),
                    documentIds: null);

                analysisMap.Add(analyzer, analysisResult);
            }

            var telemetryMap = ImmutableDictionary.CreateBuilder <DiagnosticAnalyzer, AnalyzerTelemetryInfo>();

            var telemetryCount = reader.ReadInt32();

            for (var i = 0; i < telemetryCount; i++)
            {
                var analyzer      = analyzerMap[reader.ReadString()];
                var telemetryInfo = ReadTelemetry(reader, cancellationToken);

                telemetryMap.Add(analyzer, telemetryInfo);
            }

            var exceptionMap = ImmutableDictionary.CreateBuilder <DiagnosticAnalyzer, ImmutableArray <DiagnosticData> >();

            var exceptionCount = reader.ReadInt32();

            for (var i = 0; i < exceptionCount; i++)
            {
                var analyzer = analyzerMap[reader.ReadString()];

                var exceptions = diagnosticDataSerializer.ReadDiagnosticData(reader, project, document: null, cancellationToken);
                if (!exceptions.IsDefault)
                {
                    exceptionMap.Add(analyzer, exceptions);
                }
            }

            return(DiagnosticAnalysisResultMap.Create(analysisMap.ToImmutable(), telemetryMap.ToImmutable(), exceptionMap.ToImmutable()));
        }
 public DiagnosticAnalysisResult ToResult()
 {
     return(DiagnosticAnalysisResult.Create(_project, _version,
                                            _syntaxLocals?.ToImmutable() ?? ImmutableDictionary <DocumentId, ImmutableArray <DiagnosticData> > .Empty,
                                            _semanticLocals?.ToImmutable() ?? ImmutableDictionary <DocumentId, ImmutableArray <DiagnosticData> > .Empty,
                                            _nonLocals?.ToImmutable() ?? ImmutableDictionary <DocumentId, ImmutableArray <DiagnosticData> > .Empty,
                                            _others.NullToEmpty(),
                                            _documentIds));
 }
            > AnalyzeOutOfProcAsync(
            DocumentAnalysisScope?documentAnalysisScope,
            Project project,
            CompilationWithAnalyzers compilationWithAnalyzers,
            RemoteHostClient client,
            bool forceExecuteAllAnalyzers,
            bool logPerformanceInfo,
            bool getTelemetryInfo,
            CancellationToken cancellationToken
            )
        {
            var solution = project.Solution;

            using var pooledObject = SharedPools
                                     .Default <Dictionary <string, DiagnosticAnalyzer> >()
                                     .GetPooledObject();
            var analyzerMap = pooledObject.Object;

            var analyzers =
                documentAnalysisScope?.Analyzers
                ?? compilationWithAnalyzers.Analyzers.Where(
                    a => forceExecuteAllAnalyzers || !a.IsOpenFileOnly(solution.Options)
                    );

            analyzerMap.AppendAnalyzerMap(analyzers);

            if (analyzerMap.Count == 0)
            {
                return(DiagnosticAnalysisResultMap <
                           DiagnosticAnalyzer,
                           DiagnosticAnalysisResult
                           > .Empty);
            }

            var argument = new DiagnosticArguments(
                compilationWithAnalyzers.AnalysisOptions.ReportSuppressedDiagnostics,
                logPerformanceInfo,
                getTelemetryInfo,
                documentAnalysisScope?.TextDocument.Id,
                documentAnalysisScope?.Span,
                documentAnalysisScope?.Kind,
                project.Id,
                analyzerMap.Keys.ToArray()
                );

            var result = await client
                         .TryInvokeAsync <
                IRemoteDiagnosticAnalyzerService,
                SerializableDiagnosticAnalysisResults
                >(
                solution,
                invocation : (service, solutionInfo, cancellationToken) =>
                service.CalculateDiagnosticsAsync(
                    solutionInfo,
                    argument,
                    cancellationToken
                    ),
                cancellationToken
                )
                         .ConfigureAwait(false);

            if (!result.HasValue)
            {
                return(DiagnosticAnalysisResultMap <
                           DiagnosticAnalyzer,
                           DiagnosticAnalysisResult
                           > .Empty);
            }

            // handling of cancellation and exception
            var version = await DiagnosticIncrementalAnalyzer
                          .GetDiagnosticVersionAsync(project, cancellationToken)
                          .ConfigureAwait(false);

            var documentIds =
                (documentAnalysisScope != null)
                    ? ImmutableHashSet.Create(documentAnalysisScope.TextDocument.Id)
                    : null;

            return(new DiagnosticAnalysisResultMap <DiagnosticAnalyzer, DiagnosticAnalysisResult>(
                       result.Value.Diagnostics.ToImmutableDictionary(
                           entry => analyzerMap[entry.analyzerId],
                           entry =>
                           DiagnosticAnalysisResult.Create(
                               project,
                               version,
                               syntaxLocalMap: Hydrate(entry.diagnosticMap.Syntax, project),
                               semanticLocalMap: Hydrate(entry.diagnosticMap.Semantic, project),
                               nonLocalMap: Hydrate(entry.diagnosticMap.NonLocal, project),
                               others: entry.diagnosticMap.Other,
                               documentIds
                               )
                           ),
                       result.Value.Telemetry.ToImmutableDictionary(
                           entry => analyzerMap[entry.analyzerId],
                           entry => entry.telemetry
                           )
                       ));
        }
Beispiel #5
0
        public static DiagnosticAnalysisResultMap <DiagnosticAnalyzer, DiagnosticAnalysisResult> ReadDiagnosticAnalysisResults(
            ObjectReader reader,
            IDictionary <string, DiagnosticAnalyzer> analyzerMap,
            DocumentAnalysisScope?documentAnalysisScope,
            Project project,
            VersionStamp version,
            CancellationToken cancellationToken)
        {
            var diagnosticDataSerializer = new DiagnosticDataSerializer(VersionStamp.Default, VersionStamp.Default);

            var analysisMap = ImmutableDictionary.CreateBuilder <DiagnosticAnalyzer, DiagnosticAnalysisResult>();
            var documentIds = documentAnalysisScope != null?ImmutableHashSet.Create(documentAnalysisScope.TextDocument.Id) : null;

            var analysisCount = reader.ReadInt32();

            for (var i = 0; i < analysisCount; i++)
            {
                var analyzer = analyzerMap[reader.ReadString()];

                DiagnosticAnalysisResult analysisResult;
                if (documentAnalysisScope != null)
                {
                    ImmutableDictionary <DocumentId, ImmutableArray <DiagnosticData> > syntaxLocalMap, semanticLocalMap;
                    if (documentAnalysisScope.Kind == AnalysisKind.Syntax)
                    {
                        syntaxLocalMap   = ReadDiagnosticDataMap(reader, diagnosticDataSerializer, project, cancellationToken);
                        semanticLocalMap = ImmutableDictionary <DocumentId, ImmutableArray <DiagnosticData> > .Empty;
                    }
                    else
                    {
                        Debug.Assert(documentAnalysisScope.Kind == AnalysisKind.Semantic);
                        semanticLocalMap = ReadDiagnosticDataMap(reader, diagnosticDataSerializer, project, cancellationToken);
                        syntaxLocalMap   = ImmutableDictionary <DocumentId, ImmutableArray <DiagnosticData> > .Empty;
                    }

                    analysisResult = DiagnosticAnalysisResult.Create(
                        project,
                        version,
                        syntaxLocalMap,
                        semanticLocalMap,
                        nonLocalMap: ImmutableDictionary <DocumentId, ImmutableArray <DiagnosticData> > .Empty,
                        others: ImmutableArray <DiagnosticData> .Empty,
                        documentIds);
                }
                else
                {
                    var syntaxLocalMap   = ReadDiagnosticDataMap(reader, diagnosticDataSerializer, project, cancellationToken);
                    var semanticLocalMap = ReadDiagnosticDataMap(reader, diagnosticDataSerializer, project, cancellationToken);
                    var nonLocalMap      = ReadDiagnosticDataMap(reader, diagnosticDataSerializer, project, cancellationToken);

                    var others = diagnosticDataSerializer.ReadDiagnosticData(reader, project, document: null, cancellationToken);

                    analysisResult = DiagnosticAnalysisResult.Create(
                        project,
                        version,
                        syntaxLocalMap,
                        semanticLocalMap,
                        nonLocalMap,
                        others.NullToEmpty(),
                        documentIds: null);
                }

                analysisMap.Add(analyzer, analysisResult);
            }

            var telemetryMap = ImmutableDictionary.CreateBuilder <DiagnosticAnalyzer, AnalyzerTelemetryInfo>();

            var telemetryCount = reader.ReadInt32();

            for (var i = 0; i < telemetryCount; i++)
            {
                var analyzer      = analyzerMap[reader.ReadString()];
                var telemetryInfo = ReadTelemetry(reader, cancellationToken);

                telemetryMap.Add(analyzer, telemetryInfo);
            }

            return(DiagnosticAnalysisResultMap.Create(analysisMap.ToImmutable(), telemetryMap.ToImmutable()));
        }
        public static bool TryReadDiagnosticAnalysisResults(
            ObjectReader reader,
            IDictionary <string, DiagnosticAnalyzer> analyzerMap,
            DocumentAnalysisScope?documentAnalysisScope,
            Project project,
            VersionStamp version,
            CancellationToken cancellationToken,
            [NotNullWhen(returnValue: true)] out DiagnosticAnalysisResultMap <DiagnosticAnalyzer, DiagnosticAnalysisResult>?result)
        {
            result = null;

            try
            {
                var diagnosticDataSerializer = new DiagnosticDataSerializer(VersionStamp.Default, VersionStamp.Default);

                var analysisMap = ImmutableDictionary.CreateBuilder <DiagnosticAnalyzer, DiagnosticAnalysisResult>();
                var documentIds = documentAnalysisScope != null?ImmutableHashSet.Create(documentAnalysisScope.TextDocument.Id) : null;

                var analysisCount = reader.ReadInt32();
                for (var i = 0; i < analysisCount; i++)
                {
                    var analyzer = analyzerMap[reader.ReadString()];

                    DiagnosticAnalysisResult analysisResult;
                    if (documentAnalysisScope != null)
                    {
                        ImmutableDictionary <DocumentId, ImmutableArray <DiagnosticData> >?syntaxLocalMap, semanticLocalMap;
                        if (documentAnalysisScope.Kind == AnalysisKind.Syntax)
                        {
                            if (!TryReadDiagnosticDataMap(reader, diagnosticDataSerializer, project, cancellationToken, out syntaxLocalMap))
                            {
                                return(false);
                            }

                            semanticLocalMap = ImmutableDictionary <DocumentId, ImmutableArray <DiagnosticData> > .Empty;
                        }
                        else
                        {
                            Debug.Assert(documentAnalysisScope.Kind == AnalysisKind.Semantic);
                            if (!TryReadDiagnosticDataMap(reader, diagnosticDataSerializer, project, cancellationToken, out semanticLocalMap))
                            {
                                return(false);
                            }

                            syntaxLocalMap = ImmutableDictionary <DocumentId, ImmutableArray <DiagnosticData> > .Empty;
                        }

                        analysisResult = DiagnosticAnalysisResult.Create(
                            project,
                            version,
                            syntaxLocalMap,
                            semanticLocalMap,
                            nonLocalMap: ImmutableDictionary <DocumentId, ImmutableArray <DiagnosticData> > .Empty,
                            others: ImmutableArray <DiagnosticData> .Empty,
                            documentIds);
                    }
                    else
                    {
                        if (!TryReadDiagnosticDataMap(reader, diagnosticDataSerializer, project, cancellationToken, out var syntaxLocalMap) ||
                            !TryReadDiagnosticDataMap(reader, diagnosticDataSerializer, project, cancellationToken, out var semanticLocalMap) ||
                            !TryReadDiagnosticDataMap(reader, diagnosticDataSerializer, project, cancellationToken, out var nonLocalMap) ||
                            !diagnosticDataSerializer.TryReadDiagnosticData(reader, project, document: null, cancellationToken, out var others))
                        {
                            return(false);
                        }

                        analysisResult = DiagnosticAnalysisResult.Create(
                            project,
                            version,
                            syntaxLocalMap,
                            semanticLocalMap,
                            nonLocalMap,
                            others.NullToEmpty(),
                            documentIds: null);
                    }

                    analysisMap.Add(analyzer, analysisResult);
                }

                var telemetryMap = ImmutableDictionary.CreateBuilder <DiagnosticAnalyzer, AnalyzerTelemetryInfo>();

                var telemetryCount = reader.ReadInt32();
                for (var i = 0; i < telemetryCount; i++)
                {
                    var analyzer      = analyzerMap[reader.ReadString()];
                    var telemetryInfo = ReadTelemetry(reader, cancellationToken);

                    telemetryMap.Add(analyzer, telemetryInfo);
                }

                result = DiagnosticAnalysisResultMap.Create(analysisMap.ToImmutable(), telemetryMap.ToImmutable());
                return(true);
            }
            catch (Exception ex) when(FatalError.ReportWithoutCrashUnlessCanceled(ex))
            {
                return(false);
            }
        }