예제 #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
        public static void WriteUsedFixers(
            ImmutableArray <CodeFixProvider> fixers,
            CodeAnalysisOptions options,
            ConsoleColor color,
            Verbosity verbosity)
        {
            if (!ShouldWrite(verbosity))
            {
                return;
            }

            foreach (IGrouping <string, string> grouping in fixers
                     .SelectMany(f => f.FixableDiagnosticIds)
                     .Distinct()
                     .Where(f => options.IsSupportedDiagnosticId(f))
                     .GroupBy(f => f, DiagnosticIdComparer.Prefix)
                     .OrderBy(f => f.Key, DiagnosticIdComparer.Prefix))
            {
                int    count  = grouping.Count();
                string prefix = DiagnosticIdPrefix.GetPrefix(grouping.Key);

                Write($"  {count} fixable {((count == 1) ? "diagnostic" : "diagnostics")} with ", color, verbosity);
                Write((string.IsNullOrEmpty(prefix)) ? "no prefix" : $"prefix '{prefix}'", color, verbosity);

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

                        while (true)
                        {
                            Write(en.Current, color, verbosity);

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

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

                WriteLine("", color, verbosity);
            }
        }
예제 #3
0
 public static (ImmutableArray <DiagnosticAnalyzer> analyzers, ImmutableArray <CodeFixProvider> fixers) GetAnalyzersAndFixers(
     Project project,
     AnalyzerAssemblyList analyzerAssemblies,
     AnalyzerAssemblyList analyzerReferences,
     CodeAnalysisOptions options)
 {
     return(GetAnalyzersAndFixers(
                project: project,
                analyzerAssemblies: analyzerAssemblies,
                analyzerReferences: analyzerReferences,
                options: options,
                loadFixers: true));
 }
예제 #4
0
        public static ImmutableArray <DiagnosticAnalyzer> GetAnalyzers(
            Project project,
            AnalyzerAssemblyList analyzerAssemblies,
            AnalyzerAssemblyList analyzerReferences,
            CodeAnalysisOptions options)
        {
            (ImmutableArray <DiagnosticAnalyzer> analyzers, ImmutableArray <CodeFixProvider> _) = GetAnalyzersAndFixers(
                project: project,
                analyzerAssemblies: analyzerAssemblies,
                analyzerReferences: analyzerReferences,
                options: options,
                loadFixers: false);

            return(analyzers);
        }
예제 #5
0
        public AnalyzerLoader(IEnumerable <AnalyzerAssembly> defaultAssemblies, CodeAnalysisOptions options)
        {
            DefaultAssemblies = ImmutableArray <AnalyzerAssembly> .Empty;

            if (defaultAssemblies != null)
            {
                foreach (AnalyzerAssembly analyzerAssembly in defaultAssemblies)
                {
                    if (!_defaultAssemblies.ContainsKey(analyzerAssembly.FullName))
                    {
                        _defaultAssemblies.Add(analyzerAssembly.FullName, analyzerAssembly);
                        OnAnalyzerAssemblyAdded(new AnalyzerAssemblyEventArgs(analyzerAssembly));
                    }
                }

                DefaultAssemblies = _defaultAssemblies.Select(f => f.Value).ToImmutableArray();
            }

            Options = options;
        }
예제 #6
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)
                {
                    ReportDiagnostic reportDiagnostic = supportedDiagnostic.GetEffectiveSeverity(project.CompilationOptions);

                    if (reportDiagnostic != ReportDiagnostic.Suppress &&
                        reportDiagnostic.ToDiagnosticSeverity() >= options.SeverityLevel)
                    {
                        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);
        }
예제 #7
0
        public static void WriteUsedAnalyzers(
            ImmutableArray <DiagnosticAnalyzer> analyzers,
            Func <DiagnosticDescriptor, bool> predicate,
            CodeAnalysisOptions options,
            ConsoleColor color,
            Verbosity verbosity)
        {
            if (!analyzers.Any())
            {
                return;
            }

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

            IEnumerable <DiagnosticDescriptor> descriptors = analyzers
                                                             .SelectMany(f => f.SupportedDiagnostics)
                                                             .Distinct(DiagnosticDescriptorComparer.Id)
                                                             .Where(f => options.IsSupportedDiagnosticId(f.Id));

            if (predicate != null)
            {
                descriptors = descriptors.Where(predicate);
            }

            foreach (IGrouping <DiagnosticDescriptor, DiagnosticDescriptor> grouping in descriptors
                     .OrderBy(f => f.Id)
                     .GroupBy(f => f, 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> en = grouping.GetEnumerator())
                {
                    if (en.MoveNext())
                    {
                        Write(" (", color, verbosity);

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

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

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

                WriteLine("", color, verbosity);
            }
        }
예제 #8
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)
                {
                    var 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)
                {
                    var success = false;

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

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

                return(true);
            })
                                                            .ToImmutableArray();

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

            if (loadFixers)
            {
                fixers = analyzerAssemblies
                         .GetFixers(language)
                         .Concat(analyzerReferences.GetOrAddFixers(assemblies, language))
                         .Where(fixProvider =>
                {
                    ImmutableArray <string> fixableDiagnosticIds = fixProvider.FixableDiagnosticIds;

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

                        foreach (string fixableDiagnosticId in fixableDiagnosticIds)
                        {
                            if (options.SupportedDiagnosticIds.Contains(fixableDiagnosticId))
                            {
                                success = true;
                                break;
                            }
                        }

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

                        foreach (string fixableDiagnosticId in fixableDiagnosticIds)
                        {
                            if (!options.IgnoredDiagnosticIds.Contains(fixableDiagnosticId))
                            {
                                success = true;
                                break;
                            }
                        }

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

                    return(true);
                })
                         .ToImmutableArray();
            }

            return(analyzers, fixers);
        }