Skip to content

Commit

Permalink
Cache RegisterNodeAction checks result
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastien-marichal committed Nov 27, 2023
1 parent 3c62b3b commit e4e3810
Showing 1 changed file with 14 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

using System.Collections.Concurrent;

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) =>
Expand All @@ -42,22 +43,19 @@ public void RegisterNodeAction<TSyntaxKind>(GeneratedCodeRecognizer generatedCod
{
if (HasMatchingScope(AnalysisContext.SupportedDiagnostics))
{
ConcurrentDictionary<SyntaxTree, bool> canProceedAnalysisCache = new();
Context.RegisterSyntaxNodeAction(x =>
Execute<SonarSyntaxNodeReportingContext, SyntaxNodeAnalysisContext>(
new(AnalysisContext, x), action, x.Node.SyntaxTree, generatedCodeRecognizer),
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);
{
if (canProceedAnalysisCache.GetOrAdd(x.Node.SyntaxTree, CanProceedWithAnalysis))
{
action(new(AnalysisContext, x));
}

bool CanProceedWithAnalysis(SyntaxTree tree) =>
ShouldAnalyzeTree(tree, generatedCodeRecognizer)
&& SonarAnalysisContext.LegacyIsRegisteredActionEnabled(AnalysisContext.SupportedDiagnostics, tree)
&& AnalysisContext.ShouldAnalyzeRazorFile(tree);
}, syntaxKinds);
}
}
}

0 comments on commit e4e3810

Please sign in to comment.