Skip to content

Commit e470584

Browse files
authored
Merge pull request #3642 from bjornhellander/feature/settings-performance
Update AnalyzersExtensions and SettingsHelper to use cached JsonValue objects where possible
2 parents ccaac20 + 308a2db commit e470584

File tree

34 files changed

+341
-199
lines changed

34 files changed

+341
-199
lines changed

StyleCop.Analyzers/StyleCop.Analyzers/AnalyzerExtensions.cs

Lines changed: 9 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ namespace StyleCop.Analyzers
88
using System;
99
using System.Collections.Concurrent;
1010
using System.Collections.Immutable;
11+
using System.Diagnostics.CodeAnalysis;
1112
using Microsoft.CodeAnalysis;
1213
using Microsoft.CodeAnalysis.Diagnostics;
1314
using StyleCop.Analyzers.Settings.ObjectModel;
@@ -23,72 +24,19 @@ internal static class AnalyzerExtensions
2324
/// </summary>
2425
/// <param name="context">The analysis context.</param>
2526
/// <param name="action">Action to be executed at completion of parsing of a document.</param>
26-
public static void RegisterSyntaxTreeAction(this AnalysisContext context, Action<SyntaxTreeAnalysisContext, StyleCopSettings> action)
27-
{
28-
context.RegisterSyntaxTreeAction(
29-
context =>
30-
{
31-
StyleCopSettings settings = context.GetStyleCopSettings(context.CancellationToken);
32-
action(context, settings);
33-
});
34-
}
35-
36-
/// <summary>
37-
/// Register an action to be executed at completion of parsing of a code document. A syntax tree action reports
38-
/// diagnostics about the <see cref="SyntaxTree"/> of a document.
39-
/// </summary>
40-
/// <param name="context">The analysis context.</param>
41-
/// <param name="action">Action to be executed at completion of parsing of a document.</param>
27+
[SuppressMessage("MicrosoftCodeAnalysisPerformance", "RS1012:Start action has no registered actions", Justification = "This is not a start action")]
4228
public static void RegisterSyntaxTreeAction(this CompilationStartAnalysisContext context, Action<SyntaxTreeAnalysisContext, StyleCopSettings> action)
4329
{
30+
var settingsFile = context.GetStyleCopSettingsFile(context.CancellationToken);
31+
4432
context.RegisterSyntaxTreeAction(
4533
context =>
4634
{
47-
StyleCopSettings settings = context.GetStyleCopSettings(context.CancellationToken);
35+
StyleCopSettings settings = context.GetStyleCopSettings(settingsFile);
4836
action(context, settings);
4937
});
5038
}
5139

52-
/// <summary>
53-
/// Register an action to be executed at completion of semantic analysis of a <see cref="SyntaxNode"/> with an
54-
/// appropriate kind. A syntax node action can report diagnostics about a <see cref="SyntaxNode"/>, and can also
55-
/// collect state information to be used by other syntax node actions or code block end actions.
56-
/// </summary>
57-
/// <param name="context">The analysis context.</param>
58-
/// <param name="action">Action to be executed at completion of semantic analysis of a
59-
/// <see cref="SyntaxNode"/>.</param>
60-
/// <param name="syntaxKind">The kind of syntax that should be analyzed.</param>
61-
/// <typeparam name="TLanguageKindEnum">Enum type giving the syntax node kinds of the source language for which
62-
/// the action applies.</typeparam>
63-
public static void RegisterSyntaxNodeAction<TLanguageKindEnum>(this AnalysisContext context, Action<SyntaxNodeAnalysisContext, StyleCopSettings> action, TLanguageKindEnum syntaxKind)
64-
where TLanguageKindEnum : struct
65-
{
66-
context.RegisterSyntaxNodeAction(action, LanguageKindArrays<TLanguageKindEnum>.GetOrCreateArray(syntaxKind));
67-
}
68-
69-
/// <summary>
70-
/// Register an action to be executed at completion of semantic analysis of a <see cref="SyntaxNode"/> with an
71-
/// appropriate kind. A syntax node action can report diagnostics about a <see cref="SyntaxNode"/>, and can also
72-
/// collect state information to be used by other syntax node actions or code block end actions.
73-
/// </summary>
74-
/// <param name="context">The analysis context.</param>
75-
/// <param name="action">Action to be executed at completion of semantic analysis of a
76-
/// <see cref="SyntaxNode"/>.</param>
77-
/// <param name="syntaxKinds">The kinds of syntax that should be analyzed.</param>
78-
/// <typeparam name="TLanguageKindEnum">Enum type giving the syntax node kinds of the source language for which
79-
/// the action applies.</typeparam>
80-
public static void RegisterSyntaxNodeAction<TLanguageKindEnum>(this AnalysisContext context, Action<SyntaxNodeAnalysisContext, StyleCopSettings> action, ImmutableArray<TLanguageKindEnum> syntaxKinds)
81-
where TLanguageKindEnum : struct
82-
{
83-
context.RegisterSyntaxNodeAction(
84-
context =>
85-
{
86-
StyleCopSettings settings = context.GetStyleCopSettings(context.CancellationToken);
87-
action(context, settings);
88-
},
89-
syntaxKinds);
90-
}
91-
9240
/// <summary>
9341
/// Register an action to be executed at completion of semantic analysis of a <see cref="SyntaxNode"/> with an
9442
/// appropriate kind. A syntax node action can report diagnostics about a <see cref="SyntaxNode"/>, and can also
@@ -117,13 +65,16 @@ public static void RegisterSyntaxNodeAction<TLanguageKindEnum>(this CompilationS
11765
/// <param name="syntaxKinds">The kinds of syntax that should be analyzed.</param>
11866
/// <typeparam name="TLanguageKindEnum">Enum type giving the syntax node kinds of the source language for which
11967
/// the action applies.</typeparam>
68+
[SuppressMessage("MicrosoftCodeAnalysisPerformance", "RS1012:Start action has no registered actions", Justification = "This is not a start action")]
12069
public static void RegisterSyntaxNodeAction<TLanguageKindEnum>(this CompilationStartAnalysisContext context, Action<SyntaxNodeAnalysisContext, StyleCopSettings> action, ImmutableArray<TLanguageKindEnum> syntaxKinds)
12170
where TLanguageKindEnum : struct
12271
{
72+
var settingsFile = context.GetStyleCopSettingsFile(context.CancellationToken);
73+
12374
context.RegisterSyntaxNodeAction(
12475
context =>
12576
{
126-
StyleCopSettings settings = context.GetStyleCopSettings(context.CancellationToken);
77+
StyleCopSettings settings = context.GetStyleCopSettings(settingsFile);
12778
action(context, settings);
12879
},
12980
syntaxKinds);

StyleCop.Analyzers/StyleCop.Analyzers/DocumentationRules/ElementDocumentationBase.cs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,16 +58,19 @@ public override void Initialize(AnalysisContext context)
5858
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
5959
context.EnableConcurrentExecution();
6060

61-
context.RegisterSyntaxNodeAction(this.methodDeclarationAction, SyntaxKind.MethodDeclaration);
62-
context.RegisterSyntaxNodeAction(this.constructorDeclarationAction, SyntaxKind.ConstructorDeclaration);
63-
context.RegisterSyntaxNodeAction(this.delegateDeclarationAction, SyntaxKind.DelegateDeclaration);
64-
context.RegisterSyntaxNodeAction(this.indexerDeclarationAction, SyntaxKind.IndexerDeclaration);
65-
context.RegisterSyntaxNodeAction(this.operatorDeclarationAction, SyntaxKind.OperatorDeclaration);
66-
context.RegisterSyntaxNodeAction(this.conversionOperatorDeclarationAction, SyntaxKind.ConversionOperatorDeclaration);
67-
context.RegisterSyntaxNodeAction(this.baseTypeDeclarationAction, SyntaxKinds.BaseTypeDeclaration);
68-
context.RegisterSyntaxNodeAction(this.fieldDeclarationAction, SyntaxKind.FieldDeclaration);
69-
context.RegisterSyntaxNodeAction(this.propertyDeclarationAction, SyntaxKind.PropertyDeclaration);
70-
context.RegisterSyntaxNodeAction(this.enumMemberDeclarationAction, SyntaxKind.EnumMemberDeclaration);
61+
context.RegisterCompilationStartAction(context =>
62+
{
63+
context.RegisterSyntaxNodeAction(this.methodDeclarationAction, SyntaxKind.MethodDeclaration);
64+
context.RegisterSyntaxNodeAction(this.constructorDeclarationAction, SyntaxKind.ConstructorDeclaration);
65+
context.RegisterSyntaxNodeAction(this.delegateDeclarationAction, SyntaxKind.DelegateDeclaration);
66+
context.RegisterSyntaxNodeAction(this.indexerDeclarationAction, SyntaxKind.IndexerDeclaration);
67+
context.RegisterSyntaxNodeAction(this.operatorDeclarationAction, SyntaxKind.OperatorDeclaration);
68+
context.RegisterSyntaxNodeAction(this.conversionOperatorDeclarationAction, SyntaxKind.ConversionOperatorDeclaration);
69+
context.RegisterSyntaxNodeAction(this.baseTypeDeclarationAction, SyntaxKinds.BaseTypeDeclaration);
70+
context.RegisterSyntaxNodeAction(this.fieldDeclarationAction, SyntaxKind.FieldDeclaration);
71+
context.RegisterSyntaxNodeAction(this.propertyDeclarationAction, SyntaxKind.PropertyDeclaration);
72+
context.RegisterSyntaxNodeAction(this.enumMemberDeclarationAction, SyntaxKind.EnumMemberDeclaration);
73+
});
7174
}
7275

7376
/// <summary>

StyleCop.Analyzers/StyleCop.Analyzers/DocumentationRules/ElementDocumentationSummaryBase.cs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,18 @@ public override void Initialize(AnalysisContext context)
4949
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
5050
context.EnableConcurrentExecution();
5151

52-
context.RegisterSyntaxNodeAction(this.typeDeclarationAction, SyntaxKinds.BaseTypeDeclaration);
53-
context.RegisterSyntaxNodeAction(this.methodDeclarationAction, SyntaxKind.MethodDeclaration);
54-
context.RegisterSyntaxNodeAction(this.constructorDeclarationAction, SyntaxKind.ConstructorDeclaration);
55-
context.RegisterSyntaxNodeAction(this.destructorDeclarationAction, SyntaxKind.DestructorDeclaration);
56-
context.RegisterSyntaxNodeAction(this.propertyDeclarationAction, SyntaxKind.PropertyDeclaration);
57-
context.RegisterSyntaxNodeAction(this.indexerDeclarationAction, SyntaxKind.IndexerDeclaration);
58-
context.RegisterSyntaxNodeAction(this.fieldDeclarationAction, SyntaxKinds.BaseFieldDeclaration);
59-
context.RegisterSyntaxNodeAction(this.delegateDeclarationAction, SyntaxKind.DelegateDeclaration);
60-
context.RegisterSyntaxNodeAction(this.eventDeclarationAction, SyntaxKind.EventDeclaration);
52+
context.RegisterCompilationStartAction(context =>
53+
{
54+
context.RegisterSyntaxNodeAction(this.typeDeclarationAction, SyntaxKinds.BaseTypeDeclaration);
55+
context.RegisterSyntaxNodeAction(this.methodDeclarationAction, SyntaxKind.MethodDeclaration);
56+
context.RegisterSyntaxNodeAction(this.constructorDeclarationAction, SyntaxKind.ConstructorDeclaration);
57+
context.RegisterSyntaxNodeAction(this.destructorDeclarationAction, SyntaxKind.DestructorDeclaration);
58+
context.RegisterSyntaxNodeAction(this.propertyDeclarationAction, SyntaxKind.PropertyDeclaration);
59+
context.RegisterSyntaxNodeAction(this.indexerDeclarationAction, SyntaxKind.IndexerDeclaration);
60+
context.RegisterSyntaxNodeAction(this.fieldDeclarationAction, SyntaxKinds.BaseFieldDeclaration);
61+
context.RegisterSyntaxNodeAction(this.delegateDeclarationAction, SyntaxKind.DelegateDeclaration);
62+
context.RegisterSyntaxNodeAction(this.eventDeclarationAction, SyntaxKind.EventDeclaration);
63+
});
6164
}
6265

6366
/// <summary>

StyleCop.Analyzers/StyleCop.Analyzers/DocumentationRules/PartialElementDocumentationSummaryBase.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,11 @@ public override void Initialize(AnalysisContext context)
3737
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
3838
context.EnableConcurrentExecution();
3939

40-
context.RegisterSyntaxNodeAction(this.typeDeclarationAction, SyntaxKinds.TypeDeclaration);
41-
context.RegisterSyntaxNodeAction(this.methodDeclarationAction, SyntaxKind.MethodDeclaration);
40+
context.RegisterCompilationStartAction(context =>
41+
{
42+
context.RegisterSyntaxNodeAction(this.typeDeclarationAction, SyntaxKinds.TypeDeclaration);
43+
context.RegisterSyntaxNodeAction(this.methodDeclarationAction, SyntaxKind.MethodDeclaration);
44+
});
4245
}
4346

4447
/// <summary>

StyleCop.Analyzers/StyleCop.Analyzers/DocumentationRules/PropertyDocumentationBase.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,10 @@ public override void Initialize(AnalysisContext context)
4444
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
4545
context.EnableConcurrentExecution();
4646

47-
context.RegisterSyntaxNodeAction(this.propertyDeclarationAction, SyntaxKind.PropertyDeclaration);
47+
context.RegisterCompilationStartAction(context =>
48+
{
49+
context.RegisterSyntaxNodeAction(this.propertyDeclarationAction, SyntaxKind.PropertyDeclaration);
50+
});
4851
}
4952

5053
/// <summary>

StyleCop.Analyzers/StyleCop.Analyzers/DocumentationRules/SA1600ElementsMustBeDocumented.cs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -112,16 +112,19 @@ public override void Initialize(AnalysisContext context)
112112
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
113113
context.EnableConcurrentExecution();
114114

115-
context.RegisterSyntaxNodeAction(BaseTypeDeclarationAction, BaseTypeDeclarationKinds);
116-
context.RegisterSyntaxNodeAction(MethodDeclarationAction, SyntaxKind.MethodDeclaration);
117-
context.RegisterSyntaxNodeAction(ConstructorDeclarationAction, SyntaxKind.ConstructorDeclaration);
118-
context.RegisterSyntaxNodeAction(DestructorDeclarationAction, SyntaxKind.DestructorDeclaration);
119-
context.RegisterSyntaxNodeAction(PropertyDeclarationAction, SyntaxKind.PropertyDeclaration);
120-
context.RegisterSyntaxNodeAction(IndexerDeclarationAction, SyntaxKind.IndexerDeclaration);
121-
context.RegisterSyntaxNodeAction(FieldDeclarationAction, SyntaxKind.FieldDeclaration);
122-
context.RegisterSyntaxNodeAction(DelegateDeclarationAction, SyntaxKind.DelegateDeclaration);
123-
context.RegisterSyntaxNodeAction(EventDeclarationAction, SyntaxKind.EventDeclaration);
124-
context.RegisterSyntaxNodeAction(EventFieldDeclarationAction, SyntaxKind.EventFieldDeclaration);
115+
context.RegisterCompilationStartAction(context =>
116+
{
117+
context.RegisterSyntaxNodeAction(BaseTypeDeclarationAction, BaseTypeDeclarationKinds);
118+
context.RegisterSyntaxNodeAction(MethodDeclarationAction, SyntaxKind.MethodDeclaration);
119+
context.RegisterSyntaxNodeAction(ConstructorDeclarationAction, SyntaxKind.ConstructorDeclaration);
120+
context.RegisterSyntaxNodeAction(DestructorDeclarationAction, SyntaxKind.DestructorDeclaration);
121+
context.RegisterSyntaxNodeAction(PropertyDeclarationAction, SyntaxKind.PropertyDeclaration);
122+
context.RegisterSyntaxNodeAction(IndexerDeclarationAction, SyntaxKind.IndexerDeclaration);
123+
context.RegisterSyntaxNodeAction(FieldDeclarationAction, SyntaxKind.FieldDeclaration);
124+
context.RegisterSyntaxNodeAction(DelegateDeclarationAction, SyntaxKind.DelegateDeclaration);
125+
context.RegisterSyntaxNodeAction(EventDeclarationAction, SyntaxKind.EventDeclaration);
126+
context.RegisterSyntaxNodeAction(EventFieldDeclarationAction, SyntaxKind.EventFieldDeclaration);
127+
});
125128
}
126129

127130
private static class Analyzer

StyleCop.Analyzers/StyleCop.Analyzers/DocumentationRules/SA1601PartialElementsMustBeDocumented.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,11 @@ public override void Initialize(AnalysisContext context)
9797
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
9898
context.EnableConcurrentExecution();
9999

100-
context.RegisterSyntaxNodeAction(BaseTypeDeclarationAction, BaseTypeDeclarationKinds);
101-
context.RegisterSyntaxNodeAction(MethodDeclarationAction, SyntaxKind.MethodDeclaration);
100+
context.RegisterCompilationStartAction(context =>
101+
{
102+
context.RegisterSyntaxNodeAction(BaseTypeDeclarationAction, BaseTypeDeclarationKinds);
103+
context.RegisterSyntaxNodeAction(MethodDeclarationAction, SyntaxKind.MethodDeclaration);
104+
});
102105
}
103106

104107
private static class Analyzer

StyleCop.Analyzers/StyleCop.Analyzers/DocumentationRules/SA1602EnumerationItemsMustBeDocumented.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,10 @@ public override void Initialize(AnalysisContext context)
6363
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
6464
context.EnableConcurrentExecution();
6565

66-
context.RegisterSyntaxNodeAction(EnumMemberDeclarationAction, SyntaxKind.EnumMemberDeclaration);
66+
context.RegisterCompilationStartAction(context =>
67+
{
68+
context.RegisterSyntaxNodeAction(EnumMemberDeclarationAction, SyntaxKind.EnumMemberDeclaration);
69+
});
6770
}
6871

6972
private static class Analyzer

StyleCop.Analyzers/StyleCop.Analyzers/DocumentationRules/SA1615ElementReturnValueMustBeDocumented.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,11 @@ public override void Initialize(AnalysisContext context)
5454
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
5555
context.EnableConcurrentExecution();
5656

57-
context.RegisterSyntaxNodeAction(MethodDeclarationAction, SyntaxKind.MethodDeclaration);
58-
context.RegisterSyntaxNodeAction(DelegateDeclarationAction, SyntaxKind.DelegateDeclaration);
57+
context.RegisterCompilationStartAction(context =>
58+
{
59+
context.RegisterSyntaxNodeAction(MethodDeclarationAction, SyntaxKind.MethodDeclaration);
60+
context.RegisterSyntaxNodeAction(DelegateDeclarationAction, SyntaxKind.DelegateDeclaration);
61+
});
5962
}
6063

6164
private static void HandleMethodDeclaration(SyntaxNodeAnalysisContext context, StyleCopSettings settings)

StyleCop.Analyzers/StyleCop.Analyzers/DocumentationRules/SA1618GenericTypeParametersMustBeDocumented.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,12 @@ public override void Initialize(AnalysisContext context)
5555
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
5656
context.EnableConcurrentExecution();
5757

58-
context.RegisterSyntaxNodeAction(TypeDeclarationAction, SyntaxKinds.TypeDeclaration);
59-
context.RegisterSyntaxNodeAction(MethodDeclarationAction, SyntaxKind.MethodDeclaration);
60-
context.RegisterSyntaxNodeAction(DelegateDeclarationAction, SyntaxKind.DelegateDeclaration);
58+
context.RegisterCompilationStartAction(context =>
59+
{
60+
context.RegisterSyntaxNodeAction(TypeDeclarationAction, SyntaxKinds.TypeDeclaration);
61+
context.RegisterSyntaxNodeAction(MethodDeclarationAction, SyntaxKind.MethodDeclaration);
62+
context.RegisterSyntaxNodeAction(DelegateDeclarationAction, SyntaxKind.DelegateDeclaration);
63+
});
6164
}
6265

6366
private static void HandleTypeDeclaration(SyntaxNodeAnalysisContext context, StyleCopSettings settings)

0 commit comments

Comments
 (0)