Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache RegisterNodeAction checks result per SyntaxTree #8414

Merged
merged 6 commits into from
Nov 30, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

using System.Collections.Concurrent;
using Roslyn.Utilities;

namespace SonarAnalyzer.AnalysisContext;

public sealed class SonarCompilationStartAnalysisContext : SonarAnalysisContextBase<CompilationStartAnalysisContext>
{
public override Compilation Compilation => Context.Compilation;
public override AnalyzerOptions Options => Context.Options;
public override CancellationToken Cancel => Context.CancellationToken;

internal SonarCompilationStartAnalysisContext(SonarAnalysisContext analysisContext, CompilationStartAnalysisContext context) : base(analysisContext, context) { }

public void RegisterSymbolAction(Action<SonarSymbolReportingContext> action, params SymbolKind[] symbolKinds) =>
martin-strecker-sonarsource marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -37,27 +39,41 @@ public void RegisterCompilationEndAction(Action<SonarCompilationReportingContext
public void RegisterSemanticModelAction(Action<SonarSemanticModelReportingContext> action) =>
Context.RegisterSemanticModelAction(x => action(new(AnalysisContext, x)));

#pragma warning disable HAA0303, HAA0302, HAA0301, HAA0502

[PerformanceSensitive("https://github.com/SonarSource/sonar-dotnet/issues/8406", AllowCaptures = false, AllowGenericEnumeration = false, AllowImplicitBoxing = false)]
public void RegisterNodeAction<TSyntaxKind>(GeneratedCodeRecognizer generatedCodeRecognizer, Action<SonarSyntaxNodeReportingContext> action, params TSyntaxKind[] syntaxKinds)
where TSyntaxKind : struct
{
if (HasMatchingScope(AnalysisContext.SupportedDiagnostics))
{
ConcurrentDictionary<SyntaxTree, bool> shouldAnalyzeCache = new();
Context.RegisterSyntaxNodeAction(x =>
Execute<SonarSyntaxNodeReportingContext, SyntaxNodeAnalysisContext>(
new(AnalysisContext, x), action, x.Node.SyntaxTree, generatedCodeRecognizer),
// The hot path starts in the lambda below.
#pragma warning restore HAA0303, HAA0302, HAA0301, HAA0502
{
if (!shouldAnalyzeCache.TryGetValue(x.Node.SyntaxTree, out var canProceedWithAnalysis))
{
canProceedWithAnalysis = GetOrAddCanProceedWithAnalysis(generatedCodeRecognizer, shouldAnalyzeCache, x.Node.SyntaxTree);
}

if (canProceedWithAnalysis)
{
#pragma warning disable HAA0502
// https://github.com/SonarSource/sonar-dotnet/issues/8425
action(new(AnalysisContext, x));
martin-strecker-sonarsource marked this conversation as resolved.
Show resolved Hide resolved
#pragma warning restore HAA0502
}
},
syntaxKinds);
}
}

/// <inheritdoc cref="SonarAnalysisContext.Execute" />
private void Execute<TSonarContext, TRoslynContext>(TSonarContext context, Action<TSonarContext> action, SyntaxTree sourceTree, GeneratedCodeRecognizer generatedCodeRecognizer = null)
where TSonarContext : SonarAnalysisContextBase<TRoslynContext>
{
if (ShouldAnalyzeTree(sourceTree, generatedCodeRecognizer)
&& SonarAnalysisContext.LegacyIsRegisteredActionEnabled(AnalysisContext.SupportedDiagnostics, sourceTree)
&& AnalysisContext.ShouldAnalyzeRazorFile(sourceTree))
{
action(context);
}
}
// Performance: Don't inline to avoid capture class and delegate allocations.
private bool GetOrAddCanProceedWithAnalysis(GeneratedCodeRecognizer codeRecognizer, ConcurrentDictionary<SyntaxTree, bool> cache, SyntaxTree tree) =>
cache.GetOrAdd(tree,
x =>
ShouldAnalyzeTree(x, codeRecognizer)
&& SonarAnalysisContext.LegacyIsRegisteredActionEnabled(AnalysisContext.SupportedDiagnostics, x)
&& AnalysisContext.ShouldAnalyzeRazorFile(x));
}