示例#1
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);
        }
示例#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)
                {
                    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);
        }