Skip to content

Commit 193c659

Browse files
authored
Merge pull request #68014 from mavasani/SpanApiAnalyzers
Move the SyntaxTree and SemanticModel action based analyzers to respect context.FilterSpan
2 parents 53ac77e + ebfa9b6 commit 193c659

File tree

30 files changed

+168
-65
lines changed

30 files changed

+168
-65
lines changed

src/Analyzers/CSharp/Analyzers/AddAccessibilityModifiers/CSharpAddAccessibilityModifiersDiagnosticAnalyzer.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ private void ProcessMemberDeclaration(
3737
SyntaxTreeAnalysisContext context,
3838
CodeStyleOption2<AccessibilityModifiersRequired> option, MemberDeclarationSyntax member)
3939
{
40+
if (!context.ShouldAnalyzeSpan(member.Span))
41+
return;
42+
4043
if (member is BaseNamespaceDeclarationSyntax namespaceDeclaration)
4144
ProcessMembers(context, option, namespaceDeclaration.Members);
4245

src/Analyzers/CSharp/Analyzers/NewLines/ArrowExpressionClausePlacement/ArrowExpressionClausePlacementDiagnosticAnalyzer.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ private void AnalyzeTree(SyntaxTreeAnalysisContext context)
3636
if (option.Value)
3737
return;
3838

39-
Recurse(context, option.Notification.Severity, context.Tree.GetRoot(context.CancellationToken));
39+
Recurse(context, option.Notification.Severity, context.GetAnalysisRoot(findInTrivia: false));
4040
}
4141

4242
private void Recurse(SyntaxTreeAnalysisContext context, ReportDiagnostic severity, SyntaxNode node)
@@ -48,6 +48,9 @@ private void Recurse(SyntaxTreeAnalysisContext context, ReportDiagnostic severit
4848

4949
foreach (var child in node.ChildNodesAndTokens())
5050
{
51+
if (!context.ShouldAnalyzeSpan(child.Span))
52+
continue;
53+
5154
if (child.IsNode)
5255
Recurse(context, severity, child.AsNode()!);
5356
}

src/Analyzers/CSharp/Analyzers/NewLines/ConditionalExpressionPlacement/ConditionalExpressionPlacementDiagnosticAnalyzer.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ private void AnalyzeTree(SyntaxTreeAnalysisContext context)
3737
if (option.Value)
3838
return;
3939

40-
Recurse(context, option.Notification.Severity, context.Tree.GetRoot(context.CancellationToken));
40+
Recurse(context, option.Notification.Severity, context.GetAnalysisRoot(findInTrivia: false));
4141
}
4242

4343
private void Recurse(SyntaxTreeAnalysisContext context, ReportDiagnostic severity, SyntaxNode node)
@@ -49,6 +49,9 @@ private void Recurse(SyntaxTreeAnalysisContext context, ReportDiagnostic severit
4949

5050
foreach (var child in node.ChildNodesAndTokens())
5151
{
52+
if (!context.ShouldAnalyzeSpan(child.Span))
53+
continue;
54+
5255
if (child.IsNode)
5356
Recurse(context, severity, child.AsNode()!);
5457
}

src/Analyzers/CSharp/Analyzers/NewLines/ConsecutiveBracePlacement/ConsecutiveBracePlacementDiagnosticAnalyzer.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ private void Recurse(SyntaxTreeAnalysisContext context, ReportDiagnostic severit
4646
var tree = context.Tree;
4747
var cancellationToken = context.CancellationToken;
4848

49-
var root = tree.GetRoot(cancellationToken);
49+
var root = context.GetAnalysisRoot(findInTrivia: false);
5050
var text = tree.GetText(cancellationToken);
5151

5252
stack.Add(root);
@@ -63,6 +63,9 @@ private void Recurse(SyntaxTreeAnalysisContext context, ReportDiagnostic severit
6363

6464
foreach (var child in current.ChildNodesAndTokens())
6565
{
66+
if (!context.ShouldAnalyzeSpan(child.FullSpan))
67+
continue;
68+
6669
if (child.IsNode)
6770
stack.Add(child.AsNode()!);
6871
else if (child.IsToken)

src/Analyzers/CSharp/Analyzers/NewLines/ConstructorInitializerPlacement/ConstructorInitializerPlacementDiagnosticAnalyzer.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ private void AnalyzeTree(SyntaxTreeAnalysisContext context)
3838
if (option.Value)
3939
return;
4040

41-
Recurse(context, option.Notification.Severity, context.Tree.GetRoot(context.CancellationToken));
41+
Recurse(context, option.Notification.Severity, context.GetAnalysisRoot(findInTrivia: false));
4242
}
4343

4444
private void Recurse(SyntaxTreeAnalysisContext context, ReportDiagnostic severity, SyntaxNode node)
@@ -54,6 +54,9 @@ private void Recurse(SyntaxTreeAnalysisContext context, ReportDiagnostic severit
5454

5555
foreach (var child in node.ChildNodesAndTokens())
5656
{
57+
if (!context.ShouldAnalyzeSpan(child.Span))
58+
continue;
59+
5760
if (child.IsNode)
5861
Recurse(context, severity, child.AsNode()!);
5962
}

src/Analyzers/CSharp/Analyzers/NewLines/EmbeddedStatementPlacement/EmbeddedStatementPlacementDiagnosticAnalyzer.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ private void AnalyzeTree(SyntaxTreeAnalysisContext context)
3737
if (option.Value)
3838
return;
3939

40-
Recurse(context, option.Notification.Severity, context.Tree.GetRoot(context.CancellationToken));
40+
Recurse(context, option.Notification.Severity, context.GetAnalysisRoot(findInTrivia: false));
4141
}
4242

4343
private void Recurse(SyntaxTreeAnalysisContext context, ReportDiagnostic severity, SyntaxNode node)
@@ -59,6 +59,9 @@ private void Recurse(SyntaxTreeAnalysisContext context, ReportDiagnostic severit
5959

6060
foreach (var child in node.ChildNodesAndTokens())
6161
{
62+
if (!context.ShouldAnalyzeSpan(child.Span))
63+
continue;
64+
6265
if (child.IsNode)
6366
Recurse(context, severity, child.AsNode()!);
6467
}

src/Analyzers/CSharp/Analyzers/OrderModifiers/CSharpOrderModifiersDiagnosticAnalyzer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ protected override void Recurse(
3434
{
3535
foreach (var child in root.ChildNodesAndTokens())
3636
{
37-
if (child.IsNode)
37+
if (child.IsNode && context.ShouldAnalyzeSpan(child.Span))
3838
{
3939
var node = child.AsNode();
4040
if (node is MemberDeclarationSyntax memberDeclaration)

src/Analyzers/CSharp/Analyzers/RemoveUnnecessaryNullableDirective/CSharpRemoveRedundantNullableDirectiveDiagnosticAnalyzer.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
using System;
66
using System.Collections.Generic;
7+
using System.Linq;
78
using System.Text;
89
using System.Threading;
910
using Microsoft.CodeAnalysis.CodeStyle;
@@ -46,7 +47,12 @@ protected override void InitializeWorker(AnalysisContext context)
4647
var defaultNullableContext = ((CSharpCompilation)context.Compilation).Options.NullableContextOptions;
4748
context.RegisterSyntaxTreeAction(context =>
4849
{
49-
var root = context.Tree.GetCompilationUnitRoot(context.CancellationToken);
50+
var root = context.GetAnalysisRoot(findInTrivia: true);
51+
52+
// Bail out if the root contains no nullable directives.
53+
if (!root.ContainsDirective(SyntaxKind.NullableDirectiveTrivia))
54+
return;
55+
5056
var initialState = context.Tree.IsGeneratedCode(context.Options, CSharpSyntaxFacts.Instance, context.CancellationToken)
5157
? NullableContextOptions.Disable
5258
: defaultNullableContext;

src/Analyzers/CSharp/Analyzers/RemoveUnnecessaryNullableDirective/CSharpRemoveUnnecessaryNullableDirectiveDiagnosticAnalyzer.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Collections.Concurrent;
77
using System.Collections.Immutable;
88
using System.Diagnostics.CodeAnalysis;
9+
using System.Linq;
910
using System.Threading;
1011
using Microsoft.CodeAnalysis.CodeStyle;
1112
using Microsoft.CodeAnalysis.CSharp;
@@ -105,10 +106,10 @@ private ImmutableArray<Diagnostic> AnalyzeSemanticModel(SemanticModelAnalysisCon
105106

106107
var compilationOptions = ((CSharpCompilationOptions)context.SemanticModel.Compilation.Options).NullableContextOptions;
107108

108-
DirectiveTriviaSyntax? previousRetainedDirective = null;
109+
NullableDirectiveTriviaSyntax? previousRetainedDirective = null;
109110
NullableContextOptions? retainedOptions = compilationOptions;
110111

111-
DirectiveTriviaSyntax? currentOptionsDirective = null;
112+
NullableDirectiveTriviaSyntax? currentOptionsDirective = null;
112113
var currentOptions = retainedOptions;
113114

114115
for (var directive = root.GetFirstDirective(); directive is not null; directive = directive.GetNextDirective())
@@ -288,6 +289,12 @@ public void AnalyzeCodeBlock(CodeBlockAnalysisContext context)
288289

289290
public void AnalyzeSemanticModel(SemanticModelAnalysisContext context)
290291
{
292+
var root = context.GetAnalysisRoot(findInTrivia: true);
293+
294+
// Bail out if the root contains no nullable directives.
295+
if (!root.ContainsDirective(SyntaxKind.NullableDirectiveTrivia))
296+
return;
297+
291298
// Get the state information for the syntax tree. If the state information is not available, it is
292299
// initialized directly to a completed state, ensuring that concurrent (or future) calls to
293300
// AnalyzeCodeBlock will always read completed==true, and intervalTree does not need to be initialized

src/Analyzers/CSharp/Analyzers/RemoveUnreachableCode/CSharpRemoveUnreachableCodeDiagnosticAnalyzer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ private void AnalyzeSemanticModel(SemanticModelAnalysisContext context)
5454
// binding diagnostics directly on the SourceMethodSymbol containing this block, and
5555
// so it can retrieve the diagnostics at practically no cost.
5656
var root = semanticModel.SyntaxTree.GetRoot(cancellationToken);
57-
var diagnostics = semanticModel.GetDiagnostics(cancellationToken: cancellationToken);
57+
var diagnostics = semanticModel.GetDiagnostics(context.FilterSpan, cancellationToken);
5858
foreach (var diagnostic in diagnostics)
5959
{
6060
cancellationToken.ThrowIfCancellationRequested();

0 commit comments

Comments
 (0)