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 2 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 @@ -42,22 +44,25 @@ public void RegisterNodeAction<TSyntaxKind>(GeneratedCodeRecognizer generatedCod
{
if (HasMatchingScope(AnalysisContext.SupportedDiagnostics))
{
ConcurrentDictionary<SyntaxTree, bool> shouldAnalyzeCache = new();
Context.RegisterSyntaxNodeAction(x =>
Execute<SonarSyntaxNodeReportingContext, SyntaxNodeAnalysisContext>(
new(AnalysisContext, x), action, x.Node.SyntaxTree, generatedCodeRecognizer),
syntaxKinds);
}
}
{
if (!shouldAnalyzeCache.TryGetValue(x.Node.SyntaxTree, out var canProceedWithAnalysis))
{
canProceedWithAnalysis = GetOrAddCanProceedWithAnalysis(generatedCodeRecognizer, shouldAnalyzeCache, x.Node.SyntaxTree);
}
if (canProceedWithAnalysis)
{
action(new(AnalysisContext, x));
}
}, 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.
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));
martin-strecker-sonarsource marked this conversation as resolved.
Show resolved Hide resolved
}
}
}