Пример #1
0
        public async Task BuildErrorZeroLengthSpan()
        {
            var workspaceXml =
                @"<Workspace>
    <Project Language=""C#"" CommonReferences=""true"">
        <Document FilePath = ""Test.cs"" >
            class Test
{
}
        </Document>
    </Project>
</Workspace>";

            using (var workspace = TestWorkspace.Create(workspaceXml))
            {
                var document = workspace.Documents.First();

                var updateArgs = DiagnosticsUpdatedArgs.DiagnosticsCreated(
                    new object(), workspace, workspace.CurrentSolution, document.Project.Id, document.Id,
                    ImmutableArray.Create(
                        _producer.CreateDiagnosticData(workspace, document, new TextSpan(0, 0)),
                        _producer.CreateDiagnosticData(workspace, document, new TextSpan(0, 1))));

                var spans = await _producer.GetErrorsFromUpdateSource(workspace, document, updateArgs);

                Assert.Equal(1, spans.Count());
                var first = spans.First();

                Assert.Equal(1, first.Span.Span.Length);
            }
        }
            private void GetInitialDiagnostics(CancellationToken cancellationToken)
            {
                cancellationToken.ThrowIfCancellationRequested();

                // Also, collect the initial set of diagnostics to show.
                var document = _subjectBuffer.AsTextContainer().GetOpenDocumentInCurrentContext();

                if (document != null)
                {
                    var project   = document.Project;
                    var workspace = project.Solution.Workspace;
                    foreach (var updateArgs in _owner._diagnosticService.GetDiagnosticsUpdatedEventArgs(workspace, project.Id, document.Id, cancellationToken))
                    {
                        var diagnostics = AdjustInitialDiagnostics(project.Solution, updateArgs, cancellationToken);
                        if (diagnostics.Length == 0)
                        {
                            continue;
                        }

                        OnDiagnosticsUpdated(
                            DiagnosticsUpdatedArgs.DiagnosticsCreated(
                                updateArgs.Id, updateArgs.Workspace, project.Solution, updateArgs.ProjectId, updateArgs.DocumentId, diagnostics));
                    }
                }
            }
Пример #3
0
        private static DiagnosticData RaiseDiagnosticEvent(
            ManualResetEvent set,
            TestDiagnosticUpdateSource source,
            TestWorkspace workspace,
            ProjectId projectId,
            DocumentId documentId,
            object id
            )
        {
            set.Reset();

            var diagnostic = CreateDiagnosticData(projectId, documentId);

            source.RaiseDiagnosticsUpdatedEvent(
                DiagnosticsUpdatedArgs.DiagnosticsCreated(
                    id,
                    workspace,
                    workspace.CurrentSolution,
                    projectId,
                    documentId,
                    ImmutableArray.Create(diagnostic)
                    )
                );

            set.WaitOne();

            return(diagnostic);
        }
Пример #4
0
        public ImmutableArray <DocumentId> ReportDiagnostics(object errorId, Solution solution, ProjectId projectId, IEnumerable <Diagnostic> diagnostics)
        {
            Debug.Assert(errorId != null);
            Debug.Assert(solution != null);
            Debug.Assert(projectId != null);

            var updateEvent = DiagnosticsUpdated;
            var documentIds = ArrayBuilder <DocumentId> .GetInstance();

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

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

            var project = solution.GetProject(projectId);

            foreach (var diagnostic in diagnostics)
            {
                var documentOpt = solution.GetDocument(diagnostic.Location.SourceTree, projectId);

                if (documentOpt != null)
                {
                    if (updateEvent != null)
                    {
                        documentDiagnosticData.Add(DiagnosticData.Create(documentOpt, diagnostic));
                    }

                    documentIds.Add(documentOpt.Id);
                }
                else if (updateEvent != null)
                {
                    projectDiagnosticData.Add(DiagnosticData.Create(project, diagnostic));
                }
            }

            foreach (var documentDiagnostics in documentDiagnosticData.ToDictionary(data => data.DocumentId))
            {
                updateEvent(this, DiagnosticsUpdatedArgs.DiagnosticsCreated(
                                errorId,
                                solution.Workspace,
                                solution,
                                projectId,
                                documentId: documentDiagnostics.Key,
                                diagnostics: documentDiagnostics.Value));
            }

            if (projectDiagnosticData.Count > 0)
            {
                updateEvent(this, DiagnosticsUpdatedArgs.DiagnosticsCreated(
                                errorId,
                                solution.Workspace,
                                solution,
                                projectId,
                                documentId: null,
                                diagnostics: projectDiagnosticData.ToImmutable()));
            }

            documentDiagnosticData.Free();
            projectDiagnosticData.Free();
            return(documentIds.ToImmutableAndFree());
        }
Пример #5
0
        internal void CreateDiagnosticAndFireEvents(Workspace workspace, Location location)
        {
            var document = workspace.CurrentSolution.Projects.Single().Documents.Single();

            _diagnostic = DiagnosticData.Create(
                Diagnostic.Create(
                    DiagnosticId,
                    "MockCategory",
                    "MockMessage",
                    DiagnosticSeverity.Error,
                    DiagnosticSeverity.Error,
                    isEnabledByDefault: true,
                    warningLevel: 0,
                    location: location
                    ),
                document
                );

            DiagnosticsUpdated?.Invoke(
                this,
                DiagnosticsUpdatedArgs.DiagnosticsCreated(
                    this,
                    workspace,
                    workspace.CurrentSolution,
                    GetProjectId(workspace),
                    GetDocumentId(workspace),
                    ImmutableArray.Create(_diagnostic)
                    )
                );
        }
Пример #6
0
        public async Task LiveErrorZeroLengthSpan()
        {
            var workspaceXml =
                @"<Workspace>
    <Project Language=""C#"" CommonReferences=""true"">
        <Document FilePath = ""Test.cs"" >
            class Test
{
}
        </Document>
    </Project>
</Workspace>";

            using var workspace = TestWorkspace.Create(workspaceXml);
            var document = workspace.Documents.First();

            var updateArgs = DiagnosticsUpdatedArgs.DiagnosticsCreated(
                new LiveId(), workspace, workspace.CurrentSolution, document.Project.Id, document.Id,
                ImmutableArray.Create(
                    TestDiagnosticTagProducer <DiagnosticsSquiggleTaggerProvider> .CreateDiagnosticData(document, new TextSpan(0, 0)),
                    TestDiagnosticTagProducer <DiagnosticsSquiggleTaggerProvider> .CreateDiagnosticData(document, new TextSpan(0, 1))));

            var spans = await TestDiagnosticTagProducer <DiagnosticsSquiggleTaggerProvider> .GetErrorsFromUpdateSource(workspace, updateArgs);

            Assert.Equal(2, spans.Count());
            var first  = spans.First();
            var second = spans.Last();

            Assert.Equal(1, first.Span.Span.Length);
            Assert.Equal(1, second.Span.Span.Length);
        }
            private async Task AnalyzeForKindAsync(TextDocument document, AnalysisKind kind, CancellationToken cancellationToken)
            {
                var diagnosticData = await GetDiagnosticsAsync(document, kind, cancellationToken).ConfigureAwait(false);

                _service.RaiseDiagnosticsUpdated(
                    DiagnosticsUpdatedArgs.DiagnosticsCreated(new DefaultUpdateArgsId(_workspace.Kind, kind, document.Id),
                                                              _workspace, document.Project.Solution, document.Project.Id, document.Id, diagnosticData));
            }
Пример #8
0
        private void RaiseDiagnosticsCreatedForProject(ProjectId projectId, object key, IEnumerable <DiagnosticData> items)
        {
            var args = DiagnosticsUpdatedArgs.DiagnosticsCreated(
                CreateId(projectId, key),
                _workspace,
                solution: null,
                projectId: projectId,
                documentId: null,
                diagnostics: items.AsImmutableOrEmpty());

            RaiseDiagnosticsUpdated(args);
        }
Пример #9
0
        private void RaiseDiagnosticsCreated(
            Project project, StateSet stateSet, ImmutableArray <DiagnosticData> items, Action <DiagnosticsUpdatedArgs> raiseEvents)
        {
            Contract.ThrowIfFalse(project.Solution.Workspace == Workspace);

            raiseEvents(DiagnosticsUpdatedArgs.DiagnosticsCreated(
                            CreateId(stateSet, project.Id, AnalysisKind.NonLocal),
                            project.Solution.Workspace,
                            project.Solution,
                            project.Id,
                            documentId: null,
                            diagnostics: items));
        }
Пример #10
0
        private void RaiseDiagnosticsCreated(
            TextDocument document, StateSet stateSet, AnalysisKind kind, ImmutableArray <DiagnosticData> items, Action <DiagnosticsUpdatedArgs> raiseEvents)
        {
            Contract.ThrowIfFalse(document.Project.Solution.Workspace == Workspace);

            raiseEvents(DiagnosticsUpdatedArgs.DiagnosticsCreated(
                            CreateId(stateSet, document.Id, kind),
                            document.Project.Solution.Workspace,
                            document.Project.Solution,
                            document.Project.Id,
                            document.Id,
                            items));
        }
        private void RaiseDiagnosticsCreated(
            StateType type, object key, StateSet stateSet, SolutionArgument solution, ImmutableArray <DiagnosticData> diagnostics)
        {
            if (Owner == null)
            {
                return;
            }

            // get right arg id for the given analyzer
            var id = CreateArgumentKey(type, key, stateSet);

            Owner.RaiseDiagnosticsUpdated(this,
                                          DiagnosticsUpdatedArgs.DiagnosticsCreated(id, Workspace, solution.Solution, solution.ProjectId, solution.DocumentId, diagnostics));
        }
            private void GetInitialDiagnostics(CancellationToken cancellationToken)
            {
                cancellationToken.ThrowIfCancellationRequested();

                // Also, collect the initial set of diagnostics to show.
                var document = _subjectBuffer.AsTextContainer().GetOpenDocumentInCurrentContext();

                if (document != null)
                {
                    var project   = document.Project;
                    var workspace = project.Solution.Workspace;
                    foreach (var updateArgs in _owner._diagnosticService.GetDiagnosticsUpdatedEventArgs(workspace, project.Id, document.Id, cancellationToken))
                    {
                        var diagnostics = _owner._diagnosticService.GetDiagnostics(updateArgs.Workspace, updateArgs.ProjectId, updateArgs.DocumentId, updateArgs.Id, includeSuppressedDiagnostics: false, cancellationToken: cancellationToken);
                        OnDiagnosticsUpdated(DiagnosticsUpdatedArgs.DiagnosticsCreated(updateArgs.Id, updateArgs.Workspace, project.Solution, updateArgs.ProjectId, updateArgs.DocumentId, diagnostics.AsImmutableOrEmpty()));
                    }
                }
            }
Пример #13
0
        void UpdateInitialDiagnostics()
        {
            if (!AnalysisOptions.EnableFancyFeatures)
            {
                return;
            }

            var doc = DocumentContext.AnalysisDocument;

            if (doc == null || DocumentContext.IsAdHocProject)
            {
                return;
            }

            var ad = new AnalysisDocument(Editor, DocumentContext);

            Task.Run(() => {
                var ws = DocumentContext.RoslynWorkspace;
                var analysisDocument = DocumentContext.AnalysisDocument;
                if (analysisDocument == null)
                {
                    return;
                }
                var project  = analysisDocument.Project.Id;
                var document = analysisDocument.Id;

                // Force an initial diagnostic update from the engine.
                foreach (var updateArgs in diagService.GetDiagnosticsUpdatedEventArgs(ws, project, document, src.Token))
                {
                    var diagnostics = AdjustInitialDiagnostics(analysisDocument.Project.Solution, updateArgs, src.Token);
                    if (diagnostics.Length == 0)
                    {
                        continue;
                    }

                    var e = DiagnosticsUpdatedArgs.DiagnosticsCreated(
                        updateArgs.Id, updateArgs.Workspace, analysisDocument.Project.Solution, updateArgs.ProjectId, updateArgs.DocumentId, diagnostics);

                    OnDiagnosticsUpdated(this, e);
                }
            });
        }
Пример #14
0
            public async Task AnalyzeSyntaxAsync(Document document, CancellationToken cancellationToken)
            {
                // if closed file diagnostic is off and document is not opened, then don't do anything
                if (!_workspace.Options.GetOption(ServiceFeatureOnOffOptions.ClosedFileDiagnostic, document.Project.Language) && !document.IsOpen())
                {
                    return;
                }

                var tree = await document.GetSyntaxTreeAsync(cancellationToken).ConfigureAwait(false);

                var diagnostics = tree.GetDiagnostics(cancellationToken);

                Contract.Requires(document.Project.Solution.Workspace == _workspace);

                var diagnosticData = diagnostics == null ? ImmutableArray <DiagnosticData> .Empty : diagnostics.Select(d => DiagnosticData.Create(document, d)).ToImmutableArrayOrEmpty();

                _service.RaiseDiagnosticsUpdated(
                    DiagnosticsUpdatedArgs.DiagnosticsCreated(new MiscUpdateArgsId(document.Id),
                                                              _workspace, document.Project.Solution, document.Project.Id, document.Id, diagnosticData));
            }
Пример #15
0
        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(DiagnosticsUpdatedArgs.DiagnosticsCreated(
                                                      ValueTuple.Create(this, project.Id), workspace, solution, project.Id, null, kv.ToImmutableArrayOrEmpty()));
                    continue;
                }

                Owner.RaiseDiagnosticsUpdated(DiagnosticsUpdatedArgs.DiagnosticsCreated(
                                                  ValueTuple.Create(this, kv.Key), workspace, solution, project.Id, kv.Key, kv.ToImmutableArrayOrEmpty()));
            }
        }
            public async Task AnalyzeSyntaxAsync(Document document, CancellationToken cancellationToken)
            {
                // right now, there is no way to observe diagnostics for closed file.
                if (!_workspace.IsDocumentOpen(document.Id) ||
                    !_workspace.Options.GetOption(InternalRuntimeDiagnosticOptions.Syntax))
                {
                    return;
                }

                var tree = await document.GetSyntaxTreeAsync(cancellationToken).ConfigureAwait(false);

                var diagnostics = tree.GetDiagnostics(cancellationToken);

                Contract.Requires(document.Project.Solution.Workspace == _workspace);

                var diagnosticData = diagnostics == null ? ImmutableArray <DiagnosticData> .Empty : diagnostics.Select(d => DiagnosticData.Create(document, d)).ToImmutableArrayOrEmpty();

                _service.RaiseDiagnosticsUpdated(
                    DiagnosticsUpdatedArgs.DiagnosticsCreated(new DefaultUpdateArgsId(_workspace.Kind, Syntax, document.Id),
                                                              _workspace, document.Project.Solution, document.Project.Id, document.Id, diagnosticData));
            }
Пример #17
0
            public void OnAnalyzerLoadFailed(object sender, AnalyzerLoadFailureEventArgs e)
            {
                if (!(sender is AnalyzerFileReference reference))
                {
                    return;
                }

                var diagnostic = AnalyzerHelper.CreateAnalyzerLoadFailureDiagnostic(e, reference.FullPath, projectId: null, language: null);

                // diagnostic from host analyzer can never go away
                var args = DiagnosticsUpdatedArgs.DiagnosticsCreated(
                    id: Tuple.Create(this, reference.FullPath, e.ErrorCode, e.TypeName),
                    workspace: _primaryWorkspace.Workspace,
                    solution: null,
                    projectId: null,
                    documentId: null,
                    diagnostics: ImmutableArray.Create <DiagnosticData>(diagnostic));

                // this can be null in test. but in product code, this should never be null.
                _hostUpdateSource?.RaiseDiagnosticsUpdated(args);
            }
            public async Task AnalyzeDocumentAsync(Document document, SyntaxNode bodyOpt, CancellationToken cancellationToken)
            {
                // right now, there is no way to observe diagnostics for closed file.
                if (!_workspace.IsDocumentOpen(document.Id) ||
                    !_workspace.Options.GetOption(InternalRuntimeDiagnosticOptions.Semantic))
                {
                    return;
                }

                var model = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);

                var diagnostics = model.GetMethodBodyDiagnostics(span: null, cancellationToken: cancellationToken).Concat(
                    model.GetDeclarationDiagnostics(span: null, cancellationToken: cancellationToken));

                Contract.Requires(document.Project.Solution.Workspace == _workspace);

                var diagnosticData = diagnostics == null ? ImmutableArray <DiagnosticData> .Empty : diagnostics.Select(d => DiagnosticData.Create(document, d)).ToImmutableArrayOrEmpty();

                _service.RaiseDiagnosticsUpdated(
                    DiagnosticsUpdatedArgs.DiagnosticsCreated(new DefaultUpdateArgsId(_workspace.Kind, Semantic, document.Id),
                                                              _workspace, document.Project.Solution, document.Project.Id, document.Id, diagnosticData));
            }
            private void GetInitialDiagnosticsInBackground(
                Document document, CancellationToken cancellationToken)
            {
                this.AssertIsBackground();
                cancellationToken.ThrowIfCancellationRequested();

                if (document != null)
                {
                    var project   = document.Project;
                    var workspace = project.Solution.Workspace;
                    foreach (var updateArgs in _owner._diagnosticService.GetDiagnosticsUpdatedEventArgs(workspace, project.Id, document.Id, cancellationToken))
                    {
                        var diagnostics = AdjustInitialDiagnostics(project.Solution, updateArgs, cancellationToken);
                        if (diagnostics.Length == 0)
                        {
                            continue;
                        }

                        OnDiagnosticsUpdatedOnBackground(
                            DiagnosticsUpdatedArgs.DiagnosticsCreated(
                                updateArgs.Id, updateArgs.Workspace, project.Solution, updateArgs.ProjectId, updateArgs.DocumentId, diagnostics));
                    }
                }
            }
Пример #20
0
        /// <summary>
        /// Reports diagnostics.
        /// </summary>
        /// <returns>Returns ids of documents that belong to <paramref name="projectId"/> and containing one or more diagnostics.</returns>
        public ImmutableArray <DocumentId> ReportDiagnostics(object errorId, Solution solution, ProjectId projectId, IEnumerable <Diagnostic> diagnostics)
        {
            Debug.Assert(errorId != null);
            Debug.Assert(solution != null);
            Debug.Assert(projectId != null);

            var updateEvent = DiagnosticsUpdated;

            using var documentIdsDisposer = PooledHashSet <DocumentId> .GetInstance(out var documentIds);

            using var documentDiagnosticDataDisposer = ArrayBuilder <DiagnosticData> .GetInstance(out var documentDiagnosticData);

            using var projectDiagnosticDataDisposer = ArrayBuilder <DiagnosticData> .GetInstance(out var projectDiagnosticData);

            foreach (var diagnostic in diagnostics)
            {
                var documentOpt = solution.GetDocument(diagnostic.Location.SourceTree);

                if (documentOpt != null)
                {
                    if (updateEvent != null)
                    {
                        documentDiagnosticData.Add(DiagnosticData.Create(documentOpt, diagnostic));
                    }

                    // only add documents from the current project:
                    if (documentOpt.Project.Id == projectId)
                    {
                        documentIds.Add(documentOpt.Id);
                    }
                }
                else if (updateEvent != null)
                {
                    projectDiagnosticData.Add(DiagnosticData.Create(solution.Workspace, diagnostic, projectId));
                }
            }

            foreach (var documentDiagnostics in documentDiagnosticData.ToDictionary(data => data.DocumentId))
            {
                updateEvent(this, DiagnosticsUpdatedArgs.DiagnosticsCreated(
                                errorId,
                                solution.Workspace,
                                solution,
                                projectId,
                                documentId: documentDiagnostics.Key,
                                diagnostics: documentDiagnostics.Value));
            }

            if (projectDiagnosticData.Count > 0)
            {
                updateEvent(this, DiagnosticsUpdatedArgs.DiagnosticsCreated(
                                errorId,
                                solution.Workspace,
                                solution,
                                projectId,
                                documentId: null,
                                diagnostics: projectDiagnosticData.ToImmutable()));
            }

            return(documentIds.AsImmutableOrEmpty());
        }
Пример #21
0
        public void ReportDiagnostics(Solution solution, ProjectId?projectId, IEnumerable <Diagnostic> diagnostics)
        {
            RoslynDebug.Assert(solution != null);

            var updateEvent = DiagnosticsUpdated;

            if (updateEvent == null)
            {
                return;
            }

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

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

            var workspace = solution.Workspace;
            var options   = solution.Options;
            var project   = (projectId != null) ? solution.GetProject(projectId) : null;

            foreach (var diagnostic in diagnostics)
            {
                var document = solution.GetDocument(diagnostic.Location.SourceTree);

                if (document != null)
                {
                    documentDiagnosticData.Add(DiagnosticData.Create(diagnostic, document));
                }
                else if (project != null)
                {
                    nonDocumentDiagnosticData.Add(DiagnosticData.Create(diagnostic, project));
                }
                else
                {
                    nonDocumentDiagnosticData.Add(DiagnosticData.Create(diagnostic, options));
                }
            }

            if (documentDiagnosticData.Count > 0)
            {
                foreach (var(documentId, diagnosticData) in documentDiagnosticData.ToDictionary(data => data.DocumentId))
                {
                    var diagnosticGroupId = (this, documentId, projectId);

                    updateEvent(this, DiagnosticsUpdatedArgs.DiagnosticsCreated(
                                    diagnosticGroupId,
                                    workspace,
                                    solution,
                                    projectId,
                                    documentId: documentId,
                                    diagnostics: diagnosticData));
                }
            }

            if (nonDocumentDiagnosticData.Count > 0)
            {
                var diagnosticGroupId = (this, projectId);

                updateEvent(this, DiagnosticsUpdatedArgs.DiagnosticsCreated(
                                diagnosticGroupId,
                                workspace,
                                solution,
                                projectId,
                                documentId: null,
                                diagnostics: nonDocumentDiagnosticData.ToImmutable()));
            }

            documentDiagnosticData.Free();
            nonDocumentDiagnosticData.Free();
        }
Пример #22
0
 private void RaiseDiagnosticsCreated(object id, Solution solution, ProjectId projectId, DocumentId documentId, ImmutableArray <DiagnosticData> items)
 {
     DiagnosticsUpdated?.Invoke(this, DiagnosticsUpdatedArgs.DiagnosticsCreated(
                                    CreateArgumentKey(id), _workspace, solution, projectId, documentId, items));
 }
Пример #23
0
 private DiagnosticsUpdatedArgs MakeCreatedArgs(
     DebuggingSession session, object errorId, Workspace workspace, Solution solution, ProjectId projectId, DocumentId documentId, ImmutableArray <DiagnosticData> items)
 {
     return(DiagnosticsUpdatedArgs.DiagnosticsCreated(
                CreateId(session, errorId), workspace, solution, projectId, documentId, items));
 }