コード例 #1
0
        public ImmutableArray <AnalyzerConfigOptionsResult> BuildAnalyzerConfigs()
        {
            var cmdLineArgs     = Helpers.GetReproCommandLineArgs();
            var analyzerConfigs = cmdLineArgs.AnalyzerConfigPaths
                                  .SelectAsArray(p => AnalyzerConfig.Parse(File.ReadAllText(p), p));
            var set = AnalyzerConfigSet.Create(analyzerConfigs);

            return(cmdLineArgs.SourceFiles
                   .SelectAsArray(s => set.GetOptionsForSourcePath(s.Path)));
        }
コード例 #2
0
        /// <summary>
        /// Tries to get configured severity for the given <paramref name="descriptor"/>
        /// for the given <paramref name="tree"/> from bulk configuration analyzer config options, i.e.
        ///     'dotnet_analyzer_diagnostic.category-%RuleCategory%.severity = %severity%'
        ///         or
        ///     'dotnet_analyzer_diagnostic.severity = %severity%'
        /// </summary>
        public static bool TryGetSeverityFromBulkConfiguration(
            this AnalyzerOptions?analyzerOptions,
            SyntaxTree tree,
            Compilation compilation,
            DiagnosticDescriptor descriptor,
            CancellationToken cancellationToken,
            out ReportDiagnostic severity)
        {
            // Analyzer bulk configuration does not apply to:
            //  1. Disabled by default diagnostics
            //  2. Compiler diagnostics
            //  3. Non-configurable diagnostics
            if (analyzerOptions == null ||
                !descriptor.IsEnabledByDefault ||
                descriptor.CustomTags.Contains(tag => tag == WellKnownDiagnosticTags.Compiler || tag == WellKnownDiagnosticTags.NotConfigurable))
            {
                severity = default;
                return(false);
            }

            // If user has explicitly configured severity for this diagnostic ID, that should be respected and
            // bulk configuration should not be applied.
            // For example, 'dotnet_diagnostic.CA1000.severity = error'
            if (compilation.Options.SpecificDiagnosticOptions.ContainsKey(descriptor.Id) ||
                compilation.Options.SyntaxTreeOptionsProvider?.TryGetDiagnosticValue(tree, descriptor.Id, cancellationToken, out _) == true ||
                compilation.Options.SyntaxTreeOptionsProvider?.TryGetGlobalDiagnosticValue(descriptor.Id, cancellationToken, out _) == true)
            {
                severity = default;
                return(false);
            }

            var analyzerConfigOptions = analyzerOptions.AnalyzerConfigOptionsProvider.GetOptions(tree);

            // If user has explicitly configured default severity for the diagnostic category, that should be respected.
            // For example, 'dotnet_analyzer_diagnostic.category-security.severity = error'
            var categoryBasedKey = GetCategoryBasedDotnetAnalyzerDiagnosticSeverityKey(descriptor.Category);

            if (analyzerConfigOptions.TryGetValue(categoryBasedKey, out var value) &&
                AnalyzerConfigSet.TryParseSeverity(value, out severity))
            {
                return(true);
            }

            // Otherwise, if user has explicitly configured default severity for all analyzer diagnostics, that should be respected.
            // For example, 'dotnet_analyzer_diagnostic.severity = error'
            if (analyzerConfigOptions.TryGetValue(DotnetAnalyzerDiagnosticSeverityKey, out value) &&
                AnalyzerConfigSet.TryParseSeverity(value, out severity))
            {
                return(true);
            }

            severity = default;
            return(false);
        }
コード例 #3
0
        private async Task <SyntaxTreeAnalysisContext> CreateAnalysisContextFromEditorConfigAsync(string editorConfig)
        {
            var projectId  = ProjectId.CreateNewId();
            var documentId = DocumentId.CreateNewId(projectId);
            var analyzerConfigDocumentId = DocumentId.CreateNewId(projectId);

            var solution = GenericAnalyzerTest.CreateWorkspace()
                           .CurrentSolution
                           .AddProject(projectId, TestProjectName, TestProjectName, LanguageNames.CSharp)
                           .AddDocument(documentId, "/0/Test0.cs", SourceText.From(string.Empty))
                           .AddAnalyzerConfigDocument(analyzerConfigDocumentId, "/.editorconfig", SourceText.From(editorConfig), filePath: "/.editorconfig");

            var document   = solution.GetDocument(documentId);
            var syntaxTree = await document.GetSyntaxTreeAsync(CancellationToken.None).ConfigureAwait(false);

            var analyzerConfigSet = AnalyzerConfigSet.Create(new[] { AnalyzerConfig.Parse(SourceText.From(editorConfig), "/.editorconfig") });
            var additionalFiles   = ImmutableArray <AdditionalText> .Empty;
            var optionsProvider   = this.CreateAnalyzerConfigOptionsProvider(analyzerConfigSet);
            var analyzerOptions   = new AnalyzerOptions(additionalFiles, optionsProvider);

            return(new SyntaxTreeAnalysisContext(syntaxTree, analyzerOptions, reportDiagnostic: _ => { }, isSupportedDiagnostic: _ => true, CancellationToken.None));
        }
コード例 #4
0
 public TestAnalyzerConfigOptionsProvider(AnalyzerConfigSet analyzerConfigSet)
 {
     this.analyzerConfigSet = analyzerConfigSet;
 }
コード例 #5
0
 protected override AnalyzerConfigOptionsProvider CreateAnalyzerConfigOptionsProvider(AnalyzerConfigSet analyzerConfigSet)
 {
     return(new TestAnalyzerConfigOptionsProvider(analyzerConfigSet));
 }
コード例 #6
0
 protected virtual AnalyzerConfigOptionsProvider CreateAnalyzerConfigOptionsProvider(AnalyzerConfigSet analyzerConfigSet)
 => new TestAnalyzerConfigOptionsProvider(analyzerConfigSet);