Example #1
0
        public static async Task <IEnumerable <Result> > Check(AnalysisDocument analysisDocument, CancellationToken cancellationToken, ImmutableArray <DiagnosticData> results)
        {
            var input = analysisDocument.DocumentContext;

            if (!AnalysisOptions.EnableFancyFeatures || input.Project == null || !input.IsCompileableInProject || input.AnalysisDocument == null)
            {
                return(Enumerable.Empty <Result> ());
            }
            if (SkipContext(input))
            {
                return(Enumerable.Empty <Result> ());
            }
            try {
#if DEBUG
                Debug.Listeners.Add(consoleTraceListener);
#endif

                await GetDescriptorTable(analysisDocument, cancellationToken);

                var resultList = new List <Result> (results.Length);
                foreach (var data in results)
                {
                    if (data.Id.StartsWith("CS", StringComparison.Ordinal))
                    {
                        continue;
                    }

                    if (DataHasTag(data, WellKnownDiagnosticTags.EditAndContinue))
                    {
                        continue;
                    }

                    if (!diagnosticTable [data.Id].IsEnabled)
                    {
                        continue;
                    }

                    var diagnostic = await data.ToDiagnosticAsync(analysisDocument, cancellationToken);

                    resultList.Add(new DiagnosticResult(diagnostic));
                }
                return(resultList);
            } catch (OperationCanceledException) {
                return(Enumerable.Empty <Result> ());
            } catch (AggregateException ae) {
                ae.Flatten().Handle(ix => ix is OperationCanceledException);
                return(Enumerable.Empty <Result> ());
            } catch (Exception e) {
                LoggingService.LogError("Error while running diagnostics.", e);
                return(Enumerable.Empty <Result> ());
            }
        }
Example #2
0
        static async Task <Diagnostic> ToDiagnosticAsync(this DiagnosticData data, AnalysisDocument analysisDocument, CancellationToken cancellationToken, CodeDiagnosticDescriptor desc)
        {
            var project  = analysisDocument.DocumentContext.AnalysisDocument.Project;
            var location = await data.DataLocation.ConvertLocationAsync(project, cancellationToken).ConfigureAwait(false);

            var additionalLocations = await data.AdditionalLocations.ConvertLocationsAsync(project, cancellationToken).ConfigureAwait(false);

            DiagnosticSeverity severity = data.Severity;

            return(Diagnostic.Create(
                       data.Id, data.Category, data.Message, severity, data.DefaultSeverity,
                       data.IsEnabledByDefault, GetWarningLevel(severity), data.IsSuppressed, data.Title, data.Description, data.HelpLink,
                       location, additionalLocations, customTags: data.CustomTags, properties: data.Properties));
        }
        static async Task GetDescriptorTable(AnalysisDocument analysisDocument, CancellationToken cancellationToken)
        {
            if (diagnosticTable != null)
            {
                return;
            }

            bool locked = await diagnosticLock.WaitAsync(Timeout.Infinite, cancellationToken).ConfigureAwait(false);

            if (diagnosticTable != null)
            {
                return;
            }

            try {
                var language     = CodeRefactoringService.MimeTypeToLanguage(analysisDocument.Editor.MimeType);
                var alreadyAdded = new HashSet <Type> ();

                var table = new Dictionary <string, CodeDiagnosticDescriptor> ();

                diagnostics = await CodeRefactoringService.GetCodeDiagnosticsAsync(analysisDocument.DocumentContext, language, cancellationToken);

                foreach (var diagnostic in diagnostics)
                {
                    if (!alreadyAdded.Add(diagnostic.DiagnosticAnalyzerType))
                    {
                        continue;
                    }
                    var provider = diagnostic.GetProvider();
                    if (provider == null)
                    {
                        continue;
                    }
                    foreach (var diag in provider.SupportedDiagnostics)
                    {
                        table [diag.Id] = diagnostic;
                    }

                    providers.Add(provider);
                }

                diagnosticTable = table;
            } finally {
                if (locked)
                {
                    diagnosticLock.Release();
                }
            }
        }
Example #4
0
        public static async Task <IEnumerable <Result> > Check(AnalysisDocument analysisDocument, CancellationToken cancellationToken, ImmutableArray <DiagnosticData> results)
        {
            var input = analysisDocument.DocumentContext;

            if (!AnalysisOptions.EnableFancyFeatures || input.Project == null || !input.IsCompileableInProject || input.AnalysisDocument == null)
            {
                return(Enumerable.Empty <Result> ());
            }
            try {
                var resultList = new List <Result> (results.Length);
                foreach (var data in results)
                {
                    if (input.IsAdHocProject && SkipError(data.Id))
                    {
                        continue;
                    }

                    if (data.IsSuppressed)
                    {
                        continue;
                    }

                    if (DataHasTag(data, WellKnownDiagnosticTags.EditAndContinue))
                    {
                        continue;
                    }
                    var options = await((MonoDevelopWorkspaceDiagnosticAnalyzerProviderService)Ide.Composition.CompositionManager.GetExportedValue <IWorkspaceDiagnosticAnalyzerProviderService> ()).GetOptionsAsync();
                    if (options.TryGetDiagnosticDescriptor(data.Id, out var desc) && !data.IsEnabledByDefault)
                    {
                        continue;
                    }

                    var diagnostic = await data.ToDiagnosticAsync(analysisDocument, cancellationToken, desc);

                    resultList.Add(new DiagnosticResult(diagnostic));
                }
                return(resultList);
            } catch (OperationCanceledException) {
                return(Enumerable.Empty <Result> ());
            } catch (AggregateException ae) {
                ae.Flatten().Handle(ix => ix is OperationCanceledException);
                return(Enumerable.Empty <Result> ());
            } catch (Exception e) {
                LoggingService.LogError("Error while running diagnostics.", e);
                return(Enumerable.Empty <Result> ());
            }
        }
Example #5
0
        public static IEnumerable <Result> Check(AnalysisDocument analysisDocument, CancellationToken cancellationToken)
        {
            var input = analysisDocument.DocumentContext;

            if (!AnalysisOptions.EnableFancyFeatures || input.Project == null || !input.IsCompileableInProject || input.AnalysisDocument == null)
            {
                return(Enumerable.Empty <Result> ());
            }
            try {
                var model = input.ParsedDocument.GetAst <SemanticModel> ();
                if (model == null)
                {
                    return(Enumerable.Empty <Result> ());
                }
                var compilation = model.Compilation;
                var language    = CodeRefactoringService.MimeTypeToLanguage(analysisDocument.Editor.MimeType);

                var providers    = new List <DiagnosticAnalyzer> ();
                var alreadyAdded = new HashSet <Type>();
                if (diagnostics == null)
                {
                    diagnostics = CodeRefactoringService.GetCodeDiagnosticsAsync(analysisDocument.DocumentContext, language, cancellationToken).Result.ToList();
                }
                foreach (var diagnostic in diagnostics)
                {
                    if (alreadyAdded.Contains(diagnostic.DiagnosticAnalyzerType))
                    {
                        continue;
                    }
                    alreadyAdded.Add(diagnostic.DiagnosticAnalyzerType);
                    var provider = diagnostic.GetProvider();
                    if (provider == null)
                    {
                        continue;
                    }
                    providers.Add(provider);
                }

                if (providers.Count == 0 || cancellationToken.IsCancellationRequested)
                {
                    return(Enumerable.Empty <Result> ());
                }

                CompilationWithAnalyzers compilationWithAnalyzer;
                var analyzers = System.Collections.Immutable.ImmutableArray <DiagnosticAnalyzer> .Empty.AddRange(providers);

                var diagnosticList = new List <Diagnostic> ();
                try {
                    compilationWithAnalyzer = compilation.WithAnalyzers(analyzers, null, cancellationToken);
                    if (input.ParsedDocument == null || cancellationToken.IsCancellationRequested)
                    {
                        return(Enumerable.Empty <Result> ());
                    }
                    diagnosticList.AddRange(compilationWithAnalyzer.GetAnalyzerSemanticDiagnosticsAsync(model, null, cancellationToken).Result);
                } catch (Exception) {
                    return(Enumerable.Empty <Result> ());
                } finally {
                    CompilationWithAnalyzers.ClearAnalyzerState(analyzers);
                }

                return(diagnosticList
                       .Where(d => !d.Id.StartsWith("CS", StringComparison.Ordinal))
                       .Select(diagnostic => {
                    var res = new DiagnosticResult(diagnostic);
                    // var line = analysisDocument.Editor.GetLineByOffset (res.Region.Start);
                    // Console.WriteLine (diagnostic.Id + "/" + res.Region +"/" + analysisDocument.Editor.GetTextAt (line));
                    return res;
                }));
            } catch (OperationCanceledException) {
                return(Enumerable.Empty <Result> ());
            }  catch (AggregateException ae) {
                ae.Flatten().Handle(ix => ix is OperationCanceledException);
                return(Enumerable.Empty <Result> ());
            } catch (Exception e) {
                LoggingService.LogError("Error while running diagnostics.", e);
                return(Enumerable.Empty <Result> ());
            }
        }
Example #6
0
        public static async Task <IEnumerable <Result> > Check(AnalysisDocument analysisDocument, CancellationToken cancellationToken)
        {
            var input = analysisDocument.DocumentContext;

            if (!AnalysisOptions.EnableFancyFeatures || input.Project == null || !input.IsCompileableInProject || input.AnalysisDocument == null)
            {
                return(Enumerable.Empty <Result> ());
            }
            if (SkipContext(input))
            {
                return(Enumerable.Empty <Result> ());
            }
            try {
                var model = await analysisDocument.DocumentContext.AnalysisDocument.GetSemanticModelAsync(cancellationToken);

                if (model == null)
                {
                    return(Enumerable.Empty <Result> ());
                }
                var compilation = model.Compilation;
                var language    = CodeRefactoringService.MimeTypeToLanguage(analysisDocument.Editor.MimeType);

                var providers    = new List <DiagnosticAnalyzer> ();
                var alreadyAdded = new HashSet <Type>();
                if (diagnostics == null)
                {
                    diagnostics = await CodeRefactoringService.GetCodeDiagnosticsAsync(analysisDocument.DocumentContext, language, cancellationToken);
                }
                var diagnosticTable = new Dictionary <string, CodeDiagnosticDescriptor> ();
                foreach (var diagnostic in diagnostics)
                {
                    if (alreadyAdded.Contains(diagnostic.DiagnosticAnalyzerType))
                    {
                        continue;
                    }
                    if (!diagnostic.IsEnabled)
                    {
                        continue;
                    }
                    alreadyAdded.Add(diagnostic.DiagnosticAnalyzerType);
                    var provider = diagnostic.GetProvider();
                    if (provider == null)
                    {
                        continue;
                    }
                    foreach (var diag in provider.SupportedDiagnostics)
                    {
                        diagnosticTable [diag.Id] = diagnostic;
                    }
                    providers.Add(provider);
                }

                if (providers.Count == 0 || cancellationToken.IsCancellationRequested)
                {
                    return(Enumerable.Empty <Result> ());
                }
                                #if DEBUG
                Debug.Listeners.Add(consoleTraceListener);
                                #endif

                CompilationWithAnalyzers compilationWithAnalyzer;
                var analyzers = ImmutableArray <DiagnosticAnalyzer> .Empty.AddRange(providers);

                var diagnosticList = new List <Diagnostic> ();
                try {
                    var sol     = analysisDocument.DocumentContext.AnalysisDocument.Project.Solution;
                    var options = new CompilationWithAnalyzersOptions(
                        new WorkspaceAnalyzerOptions(
                            new AnalyzerOptions(ImmutableArray <AdditionalText> .Empty),
                            sol.Options,
                            sol),
                        delegate(Exception exception, DiagnosticAnalyzer analyzer, Diagnostic diag) {
                        LoggingService.LogError("Exception in diagnostic analyzer " + diag.Id + ":" + diag.GetMessage(), exception);
                    },
                        false,
                        false
                        );

                    compilationWithAnalyzer = compilation.WithAnalyzers(analyzers, options);
                    if (input.ParsedDocument == null || cancellationToken.IsCancellationRequested)
                    {
                        return(Enumerable.Empty <Result> ());
                    }

                    diagnosticList.AddRange(await compilationWithAnalyzer.GetAnalyzerSemanticDiagnosticsAsync(model, null, cancellationToken).ConfigureAwait(false));
                    diagnosticList.AddRange(await compilationWithAnalyzer.GetAnalyzerSyntaxDiagnosticsAsync(model.SyntaxTree, cancellationToken).ConfigureAwait(false));
                } catch (OperationCanceledException) {
                } catch (AggregateException ae) {
                    ae.Flatten().Handle(ix => ix is OperationCanceledException);
                } catch (Exception ex) {
                    LoggingService.LogError("Error creating analyzer compilation", ex);
                    return(Enumerable.Empty <Result> ());
                } finally {
                                        #if DEBUG
                    Debug.Listeners.Remove(consoleTraceListener);
                                        #endif
                    CompilationWithAnalyzers.ClearAnalyzerState(analyzers);
                }

                return(diagnosticList
                       .Where(d => !d.Id.StartsWith("CS", StringComparison.Ordinal))
                       .Where(d => diagnosticTable[d.Id].GetIsEnabled(d.Descriptor))
                       .Select(diagnostic => {
                    var res = new DiagnosticResult(diagnostic);
                    // var line = analysisDocument.Editor.GetLineByOffset (res.Region.Start);
                    // Console.WriteLine (diagnostic.Id + "/" + res.Region +"/" + analysisDocument.Editor.GetTextAt (line));
                    return res;
                }));
            } catch (OperationCanceledException) {
                return(Enumerable.Empty <Result> ());
            }  catch (AggregateException ae) {
                ae.Flatten().Handle(ix => ix is OperationCanceledException);
                return(Enumerable.Empty <Result> ());
            } catch (Exception e) {
                LoggingService.LogError("Error while running diagnostics.", e);
                return(Enumerable.Empty <Result> ());
            }
        }
Example #7
0
        private void BuildGraph(AnalysisDocument response)
        {
            if (!response.Nodes.Any() && !response.Edges.Any())
            {
                MessageBox.Show("Neither nodes nor edges found");
                return;
            }

            var presentation = myPresentationCreationService.CreatePresentation(Path.GetTempPath());

            var builder = new RelaxedGraphBuilder();

            foreach (var edge in response.Edges)
            {
                builder.TryAddEdge(edge.Item1, edge.Item2);
            }

            foreach (var node in response.Nodes)
            {
                builder.TryAddNode(node);
            }

            foreach (var cluster in response.Clusters)
            {
                builder.TryAddCluster(cluster.Key, cluster.Value);
            }

            var captionModule = presentation.GetPropertySetFor <Caption>();

            // add potentially empty clusters
            {
                var spec          = SpecUtils.Deserialize(Document.Text);
                var emptyClusters = spec.Packages
                                    .Where(p => PackagesToAnalyze == null || PackagesToAnalyze.Contains(p.Name))
                                    .SelectMany(p => p.Clusters)
                                    .Where(cluster => !response.Clusters.Any(c => c.Key == cluster.Id));

                foreach (var cluster in emptyClusters)
                {
                    builder.TryAddCluster(cluster.Id, Enumerable.Empty <string>());
                    captionModule.Add(new Caption(cluster.Id, cluster.Name));
                }
            }

            presentation.Graph = builder.Graph;

            foreach (var caption in response.Captions)
            {
                captionModule.Add(new Caption(caption.Key, caption.Value));
            }

            var converter = new BrushConverter();

            var nodeStyles = presentation.GetPropertySetFor <NodeStyle>();

            foreach (var style in response.NodeStyles)
            {
                var brush = (Brush)converter.ConvertFromString(style.Value);
                brush.Freeze();
                nodeStyles.Add(new NodeStyle(style.Key)
                {
                    FillColor = brush
                });
            }

            var edgeStyles = presentation.GetPropertySetFor <EdgeStyle>();

            foreach (var style in response.EdgeStyles)
            {
                var brush = (Brush)converter.ConvertFromString(style.Value);
                brush.Freeze();
                edgeStyles.Add(new EdgeStyle(style.Key)
                {
                    Color = brush
                });
            }

            Model.Presentation = presentation;

            // only synchronize our own presentation
            myGraphToSpecSynchronizer.Presentation = presentation;
        }