Skip to content

Commit

Permalink
Reduce string allocations from `GetCategoryBasedDotnetAnalyzerDiagnos…
Browse files Browse the repository at this point in the history
…ticSeverityKey`
  • Loading branch information
Youssef1313 committed Feb 17, 2024
1 parent 291255b commit 5e50199
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -973,12 +973,13 @@ static ImmutableHashSet<ReportDiagnostic> GetEffectiveSeverities(
}

var builder = ImmutableHashSet.CreateBuilder<ReportDiagnostic>();
var categoryBasedKey = AnalyzerOptionsExtensions.GetCategoryBasedDotnetAnalyzerDiagnosticSeverityKey(descriptor.Category);
foreach (var tree in compilation.SyntaxTrees)
{
var severityForTree = defaultSeverity;

if (syntaxTreeProvider.TryGetDiagnosticValue(tree, descriptor.Id, cancellationToken, out severity) ||
analyzerOptions.TryGetSeverityFromBulkConfiguration(tree, compilation, descriptor, cancellationToken, out severity))
analyzerOptions.TryGetSeverityFromBulkConfiguration(tree, compilation, descriptor, categoryBasedKey, cancellationToken, out severity))
{
Debug.Assert(severity != ReportDiagnostic.Default);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -317,11 +317,12 @@ bool isEnabledWithAnalyzerConfigOptions(DiagnosticDescriptor descriptor)
{
if (analyzerExecutor.Compilation.Options.SyntaxTreeOptionsProvider is { } treeOptions)
{
var categoryBasedKey = AnalyzerOptionsExtensions.GetCategoryBasedDotnetAnalyzerDiagnosticSeverityKey(descriptor.Category);
foreach (var tree in analysisScope.SyntaxTrees)
{
// Check if diagnostic is enabled by SyntaxTree.DiagnosticOptions or Bulk configuration from AnalyzerConfigOptions.
if (treeOptions.TryGetDiagnosticValue(tree, descriptor.Id, cancellationToken, out var configuredValue) ||
analyzerExecutor.AnalyzerOptions.TryGetSeverityFromBulkConfiguration(tree, analyzerExecutor.Compilation, descriptor, cancellationToken, out configuredValue))
analyzerExecutor.AnalyzerOptions.TryGetSeverityFromBulkConfiguration(tree, analyzerExecutor.Compilation, descriptor, categoryBasedKey, cancellationToken, out configuredValue))
{
if (configuredValue != ReportDiagnostic.Suppress && !severityFilter.Contains(configuredValue))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ internal static class AnalyzerOptionsExtensions

private const string DotnetAnalyzerDiagnosticSeverityKey = DotnetAnalyzerDiagnosticPrefix + "." + SeveritySuffix;

private static string GetCategoryBasedDotnetAnalyzerDiagnosticSeverityKey(string category)
=> $"{DotnetAnalyzerDiagnosticPrefix}.{CategoryPrefix}-{category}.{SeveritySuffix}";
internal static string GetCategoryBasedDotnetAnalyzerDiagnosticSeverityKey(string category)
{
return $"{DotnetAnalyzerDiagnosticPrefix}.{CategoryPrefix}-{category}.{SeveritySuffix}";
}

/// <summary>
/// Tries to get configured severity for the given <paramref name="descriptor"/>
Expand All @@ -32,6 +34,7 @@ public static bool TryGetSeverityFromBulkConfiguration(
SyntaxTree tree,
Compilation compilation,
DiagnosticDescriptor descriptor,
string? categoryBasedKey,
CancellationToken cancellationToken,
out ReportDiagnostic severity)
{
Expand Down Expand Up @@ -63,7 +66,8 @@ public static bool TryGetSeverityFromBulkConfiguration(

// 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);
Debug.Assert(categoryBasedKey is null || categoryBasedKey == GetCategoryBasedDotnetAnalyzerDiagnosticSeverityKey(descriptor.Category));
categoryBasedKey ??= GetCategoryBasedDotnetAnalyzerDiagnosticSeverityKey(descriptor.Category);
if (analyzerConfigOptions.TryGetValue(categoryBasedKey, out var value) &&
AnalyzerConfigSet.TryParseSeverity(value, out severity))
{
Expand All @@ -89,5 +93,14 @@ public static bool TryGetSeverityFromBulkConfiguration(
severity = default;
return false;
}

public static bool TryGetSeverityFromBulkConfiguration(
this AnalyzerOptions? analyzerOptions,
SyntaxTree tree,
Compilation compilation,
DiagnosticDescriptor descriptor,
CancellationToken cancellationToken,
out ReportDiagnostic severity)
=> TryGetSeverityFromBulkConfiguration(analyzerOptions, tree, compilation, descriptor, categoryBasedKey: null, cancellationToken, out severity);
}
}

0 comments on commit 5e50199

Please sign in to comment.