예제 #1
0
        public static void WriteUsedAnalyzers(
            ImmutableArray <DiagnosticAnalyzer> analyzers,
            Project project,
            CodeAnalysisOptions options,
            ConsoleColor color,
            Verbosity verbosity)
        {
            if (!analyzers.Any())
            {
                return;
            }

            if (!ShouldWrite(verbosity))
            {
                return;
            }

            foreach (IGrouping <DiagnosticDescriptor, (DiagnosticDescriptor descriptor, string severity)> grouping in analyzers
                     .SelectMany(f => f.SupportedDiagnostics)
                     .Distinct(DiagnosticDescriptorComparer.Id)
                     .Select(f => (descriptor: f, reportDiagnostic: options.GetEffectiveSeverity(f, project.CompilationOptions)))
                     .Where(f => f.reportDiagnostic != ReportDiagnostic.Suppress)
                     .Select(f => (f.descriptor, severity: f.reportDiagnostic.ToDiagnosticSeverity().ToString()))
                     .OrderBy(f => f.descriptor.Id)
                     .GroupBy(f => f.descriptor, DiagnosticDescriptorComparer.IdPrefix))
            {
                int    count  = grouping.Count();
                string prefix = DiagnosticIdPrefix.GetPrefix(grouping.Key.Id);

                Write($"  {count} supported {((count == 1) ? "diagnostic" : "diagnostics")} with prefix '{prefix}'", color, verbosity);

                using (IEnumerator <(DiagnosticDescriptor descriptor, string severity)> en = grouping.GetEnumerator())
                {
                    if (en.MoveNext())
                    {
                        Write(" (", color, verbosity);

                        while (true)
                        {
                            Write(en.Current.descriptor.Id, color, verbosity);
                            Write(" ", color, verbosity);
                            Write(en.Current.severity, color, verbosity);

                            if (en.MoveNext())
                            {
                                Write(", ", color, verbosity);
                            }
                            else
                            {
                                break;
                            }
                        }

                        Write(")", color, verbosity);
                    }
                }

                WriteLine("", color, verbosity);
            }
        }
예제 #2
0
        private static (ImmutableArray <DiagnosticAnalyzer> analyzers, ImmutableArray <CodeFixProvider> fixers) GetAnalyzersAndFixers(
            Project project,
            AnalyzerAssemblyList analyzerAssemblies,
            AnalyzerAssemblyList analyzerReferences,
            CodeAnalysisOptions options,
            bool loadFixers = true)
        {
            string language = project.Language;

            ImmutableArray <Assembly> assemblies = ImmutableArray <Assembly> .Empty;

            if (!options.IgnoreAnalyzerReferences)
            {
                assemblies = project.AnalyzerReferences
                             .Distinct()
                             .OfType <AnalyzerFileReference>()
                             .Select(f => f.GetAssembly())
                             .Where(f => !analyzerAssemblies.ContainsAssembly(f.FullName))
                             .ToImmutableArray();
            }

            ImmutableArray <DiagnosticAnalyzer> analyzers = analyzerAssemblies
                                                            .GetAnalyzers(language)
                                                            .Concat(analyzerReferences.GetOrAddAnalyzers(assemblies, language))
                                                            .Where(analyzer =>
            {
                ImmutableArray <DiagnosticDescriptor> supportedDiagnostics = analyzer.SupportedDiagnostics;

                if (options.SupportedDiagnosticIds.Count > 0)
                {
                    bool success = false;

                    foreach (DiagnosticDescriptor supportedDiagnostic in supportedDiagnostics)
                    {
                        if (options.SupportedDiagnosticIds.Contains(supportedDiagnostic.Id))
                        {
                            success = true;
                            break;
                        }
                    }

                    if (!success)
                    {
                        return(false);
                    }
                }
                else if (options.IgnoredDiagnosticIds.Count > 0)
                {
                    bool success = false;

                    foreach (DiagnosticDescriptor supportedDiagnostic in supportedDiagnostics)
                    {
                        if (!options.IgnoredDiagnosticIds.Contains(supportedDiagnostic.Id))
                        {
                            success = true;
                            break;
                        }
                    }

                    if (!success)
                    {
                        return(false);
                    }
                }

                foreach (DiagnosticDescriptor supportedDiagnostic in supportedDiagnostics)
                {
                    if (options.GetEffectiveSeverity(supportedDiagnostic, project.CompilationOptions) != ReportDiagnostic.Suppress)
                    {
                        return(true);
                    }
                }

                return(false);
            })
                                                            .ToImmutableArray();

            ImmutableArray <CodeFixProvider> fixers = ImmutableArray <CodeFixProvider> .Empty;

            if (analyzers.Any() &&
                loadFixers)
            {
                fixers = analyzerAssemblies
                         .GetFixers(language)
                         .Concat(analyzerReferences.GetOrAddFixers(assemblies, language))
                         .ToImmutableArray();
            }

            return(analyzers, fixers);
        }