Skip to content
Merged
Show file tree
Hide file tree
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 @@ -37,6 +37,9 @@ private void ProcessMemberDeclaration(
SyntaxTreeAnalysisContext context,
CodeStyleOption2<AccessibilityModifiersRequired> option, MemberDeclarationSyntax member)
{
if (!context.ShouldAnalyzeSpan(member.Span))
return;

if (member is BaseNamespaceDeclarationSyntax namespaceDeclaration)
ProcessMembers(context, option, namespaceDeclaration.Members);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ private void AnalyzeTree(SyntaxTreeAnalysisContext context)
if (option.Value)
return;

Recurse(context, option.Notification.Severity, context.Tree.GetRoot(context.CancellationToken));
Recurse(context, option.Notification.Severity, context.GetAnalysisRoot(findInTrivia: false));
}

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

foreach (var child in node.ChildNodesAndTokens())
{
if (!context.ShouldAnalyzeSpan(child.Span))
continue;

if (child.IsNode)
Recurse(context, severity, child.AsNode()!);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ private void AnalyzeTree(SyntaxTreeAnalysisContext context)
if (option.Value)
return;

Recurse(context, option.Notification.Severity, context.Tree.GetRoot(context.CancellationToken));
Recurse(context, option.Notification.Severity, context.GetAnalysisRoot(findInTrivia: false));
}

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

foreach (var child in node.ChildNodesAndTokens())
{
if (!context.ShouldAnalyzeSpan(child.Span))
continue;

if (child.IsNode)
Recurse(context, severity, child.AsNode()!);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ private void Recurse(SyntaxTreeAnalysisContext context, ReportDiagnostic severit
var tree = context.Tree;
var cancellationToken = context.CancellationToken;

var root = tree.GetRoot(cancellationToken);
var root = context.GetAnalysisRoot(findInTrivia: false);
var text = tree.GetText(cancellationToken);

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

foreach (var child in current.ChildNodesAndTokens())
{
if (!context.ShouldAnalyzeSpan(child.FullSpan))
continue;

if (child.IsNode)
stack.Add(child.AsNode()!);
else if (child.IsToken)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ private void AnalyzeTree(SyntaxTreeAnalysisContext context)
if (option.Value)
return;

Recurse(context, option.Notification.Severity, context.Tree.GetRoot(context.CancellationToken));
Recurse(context, option.Notification.Severity, context.GetAnalysisRoot(findInTrivia: false));
}

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

foreach (var child in node.ChildNodesAndTokens())
{
if (!context.ShouldAnalyzeSpan(child.Span))
continue;

if (child.IsNode)
Recurse(context, severity, child.AsNode()!);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ private void AnalyzeTree(SyntaxTreeAnalysisContext context)
if (option.Value)
return;

Recurse(context, option.Notification.Severity, context.Tree.GetRoot(context.CancellationToken));
Recurse(context, option.Notification.Severity, context.GetAnalysisRoot(findInTrivia: false));
}

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

foreach (var child in node.ChildNodesAndTokens())
{
if (!context.ShouldAnalyzeSpan(child.Span))
continue;

if (child.IsNode)
Recurse(context, severity, child.AsNode()!);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ protected override void Recurse(
{
foreach (var child in root.ChildNodesAndTokens())
{
if (child.IsNode)
if (child.IsNode && context.ShouldAnalyzeSpan(child.Span))
{
var node = child.AsNode();
if (node is MemberDeclarationSyntax memberDeclaration)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using Microsoft.CodeAnalysis.CodeStyle;
Expand Down Expand Up @@ -46,7 +47,12 @@ protected override void InitializeWorker(AnalysisContext context)
var defaultNullableContext = ((CSharpCompilation)context.Compilation).Options.NullableContextOptions;
context.RegisterSyntaxTreeAction(context =>
{
var root = context.Tree.GetCompilationUnitRoot(context.CancellationToken);
var root = context.GetAnalysisRoot(findInTrivia: true);

// Bail out if the root contains no nullable directives.
if (!root.ContainsDirective(SyntaxKind.NullableDirectiveTrivia))
return;

var initialState = context.Tree.IsGeneratedCode(context.Options, CSharpSyntaxFacts.Instance, context.CancellationToken)
? NullableContextOptions.Disable
: defaultNullableContext;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Collections.Concurrent;
using System.Collections.Immutable;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Threading;
using Microsoft.CodeAnalysis.CodeStyle;
using Microsoft.CodeAnalysis.CSharp;
Expand Down Expand Up @@ -105,10 +106,10 @@ private ImmutableArray<Diagnostic> AnalyzeSemanticModel(SemanticModelAnalysisCon

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

DirectiveTriviaSyntax? previousRetainedDirective = null;
NullableDirectiveTriviaSyntax? previousRetainedDirective = null;
NullableContextOptions? retainedOptions = compilationOptions;

DirectiveTriviaSyntax? currentOptionsDirective = null;
NullableDirectiveTriviaSyntax? currentOptionsDirective = null;
var currentOptions = retainedOptions;

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

public void AnalyzeSemanticModel(SemanticModelAnalysisContext context)
{
var root = context.GetAnalysisRoot(findInTrivia: true);

// Bail out if the root contains no nullable directives.
if (!root.ContainsDirective(SyntaxKind.NullableDirectiveTrivia))
return;

// Get the state information for the syntax tree. If the state information is not available, it is
// initialized directly to a completed state, ensuring that concurrent (or future) calls to
// AnalyzeCodeBlock will always read completed==true, and intervalTree does not need to be initialized
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ private void AnalyzeSemanticModel(SemanticModelAnalysisContext context)
// binding diagnostics directly on the SourceMethodSymbol containing this block, and
// so it can retrieve the diagnostics at practically no cost.
var root = semanticModel.SyntaxTree.GetRoot(cancellationToken);
var diagnostics = semanticModel.GetDiagnostics(cancellationToken: cancellationToken);
var diagnostics = semanticModel.GetDiagnostics(context.FilterSpan, cancellationToken);
foreach (var diagnostic in diagnostics)
{
cancellationToken.ThrowIfCancellationRequested();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ private void HandleSyntaxTree(SyntaxTreeAnalysisContext context)
}

var fileHeader = FileHeaderHelper.ParseFileHeader(root);

if (!context.ShouldAnalyzeSpan(fileHeader.GetLocation(tree).SourceSpan))
{
return;
}

if (fileHeader.IsMissing)
{
context.ReportDiagnostic(Diagnostic.Create(s_missingHeaderDescriptor, fileHeader.GetLocation(tree)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,7 @@ private void AnalyzeSyntaxTree(SyntaxTreeAnalysisContext context)
if (option.Value)
return;

var cancellationToken = context.CancellationToken;
var root = context.Tree.GetRoot(cancellationToken);

Recurse(context, option.Notification.Severity, root, cancellationToken);
Recurse(context, option.Notification.Severity, context.GetAnalysisRoot(findInTrivia: false), context.CancellationToken);
}

private void Recurse(SyntaxTreeAnalysisContext context, ReportDiagnostic severity, SyntaxNode node, CancellationToken cancellationToken)
Expand All @@ -58,6 +55,9 @@ private void Recurse(SyntaxTreeAnalysisContext context, ReportDiagnostic severit

foreach (var child in node.ChildNodesAndTokens())
{
if (!context.ShouldAnalyzeSpan(child.FullSpan))
continue;

if (child.IsNode)
Recurse(context, severity, child.AsNode()!, cancellationToken);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,7 @@ private void AnalyzeTree(SyntaxTreeAnalysisContext context)
if (option.Value)
return;

var cancellationToken = context.CancellationToken;
var root = context.Tree.GetRoot(cancellationToken);

Recurse(context, option.Notification.Severity, root, cancellationToken);
Recurse(context, option.Notification.Severity, context.GetAnalysisRoot(findInTrivia: false), context.CancellationToken);
}

private void Recurse(
Expand All @@ -58,6 +55,9 @@ private void Recurse(

foreach (var child in node.ChildNodesAndTokens())
{
if (!context.ShouldAnalyzeSpan(child.FullSpan))
continue;

if (child.IsNode)
Recurse(context, severity, child.AsNode()!, cancellationToken);
else if (child.IsToken)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ private void AnalyzeSyntaxTree(SyntaxTreeAnalysisContext context)
return;
}

var root = context.Tree.GetRoot(context.CancellationToken);
Recurse(context, preferredOrder, option.Notification.Severity, root);
Recurse(context, preferredOrder, option.Notification.Severity, context.GetAnalysisRoot(findInTrivia: false));
}

protected abstract void Recurse(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ private void AnalyzeSemanticModel(SemanticModelAnalysisContext context)
var tree = context.SemanticModel.SyntaxTree;
var cancellationToken = context.CancellationToken;

var unnecessaryImports = UnnecessaryImportsProvider.GetUnnecessaryImports(context.SemanticModel, cancellationToken);
var unnecessaryImports = UnnecessaryImportsProvider.GetUnnecessaryImports(context.SemanticModel, context.FilterSpan, cancellationToken);
if (unnecessaryImports.Any())
{
// The IUnnecessaryImportsService will return individual import pieces that
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ private void AnalyzeCompilation(CompilationStartAnalysisContext context)
/// <see cref="AnalyzeSemanticModel"/>.</returns>
protected abstract bool IsIgnoredCodeBlock(SyntaxNode codeBlock);
protected abstract ImmutableArray<Diagnostic> AnalyzeCodeBlock(CodeBlockAnalysisContext context);
protected abstract ImmutableArray<Diagnostic> AnalyzeSemanticModel(SemanticModelAnalysisContext context, SimpleIntervalTree<TextSpan, TextSpanIntervalIntrospector>? codeBlockIntervalTree);
protected abstract ImmutableArray<Diagnostic> AnalyzeSemanticModel(SemanticModelAnalysisContext context, SyntaxNode root, SimpleIntervalTree<TextSpan, TextSpanIntervalIntrospector>? codeBlockIntervalTree);

public bool TrySimplify(SemanticModel model, SyntaxNode node, [NotNullWhen(true)] out Diagnostic? diagnostic, TSimplifierOptions options, CancellationToken cancellationToken)
{
Expand Down Expand Up @@ -281,7 +281,7 @@ public void AnalyzeSemanticModel(SemanticModelAnalysisContext context)
// true: the state was initialized on the previous line, and either intervalTree will be null, or
// a previous call to AnalyzeSemanticModel was cancelled and the new one will operate on the
// same interval tree presented during the previous call.
if (!completed.Value)
if (!completed.Value && !context.FilterSpan.HasValue)
{
// This lock ensures we do not use intervalTree while it is being updated by a concurrent call to
// AnalyzeCodeBlock.
Expand All @@ -292,7 +292,8 @@ public void AnalyzeSemanticModel(SemanticModelAnalysisContext context)
}
}

var diagnostics = _analyzer.AnalyzeSemanticModel(context, intervalTree);
var root = context.GetAnalysisRoot(findInTrivia: true);
var diagnostics = _analyzer.AnalyzeSemanticModel(context, root, intervalTree);

// After this point, cancellation is not allowed due to possible state alteration
foreach (var diagnostic in diagnostics)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.AddAccessibilityModifiers
[option] As CodeStyleOption2(Of AccessibilityModifiersRequired),
member As StatementSyntax)

If Not context.ShouldAnalyzeSpan(member.Span) Then
Return
End If

If member.Kind() = SyntaxKind.NamespaceBlock Then
Dim namespaceBlock = DirectCast(member, NamespaceBlockSyntax)
ProcessMembers(context, [option], namespaceBlock.Members)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.OrderModifiers
root As SyntaxNode)

For Each child In root.ChildNodesAndTokens()
If child.IsNode Then
If child.IsNode And context.ShouldAnalyzeSpan(child.Span) Then
Dim declarationStatement = TryCast(child.AsNode(), DeclarationStatementSyntax)
If declarationStatement IsNot Nothing Then
If ShouldCheck(declarationStatement) Then
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public CompilationAnalyzer(Compilation compilation)
public void AnalyzeSyntaxTree(SyntaxTreeAnalysisContext context)
{
var semanticModel = _compilation.GetSemanticModel(context.Tree);
var diagnostics = semanticModel.GetSyntaxDiagnostics(cancellationToken: context.CancellationToken);
var diagnostics = semanticModel.GetSyntaxDiagnostics(context.FilterSpan, context.CancellationToken);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the only compiler change in this PR

ReportDiagnostics(diagnostics, context.ReportDiagnostic, IsSourceLocation, s_syntactic);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,10 @@ protected override ImmutableArray<Diagnostic> AnalyzeCodeBlock(CodeBlockAnalysis
return simplifier.Diagnostics;
}

protected override ImmutableArray<Diagnostic> AnalyzeSemanticModel(SemanticModelAnalysisContext context, SimpleIntervalTree<TextSpan, TextSpanIntervalIntrospector>? codeBlockIntervalTree)
protected override ImmutableArray<Diagnostic> AnalyzeSemanticModel(SemanticModelAnalysisContext context, SyntaxNode root, SimpleIntervalTree<TextSpan, TextSpanIntervalIntrospector>? codeBlockIntervalTree)
{
var semanticModel = context.SemanticModel;
var cancellationToken = context.CancellationToken;

var options = context.GetCSharpAnalyzerOptions().GetSimplifierOptions();
var root = semanticModel.SyntaxTree.GetRoot(cancellationToken);

var simplifier = new TypeSyntaxSimplifierWalker(this, semanticModel, options, ignoredSpans: codeBlockIntervalTree, cancellationToken);
var simplifier = new TypeSyntaxSimplifierWalker(this, context.SemanticModel, options, ignoredSpans: codeBlockIntervalTree, context.CancellationToken);
simplifier.Visit(root);
return simplifier.Diagnostics;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,11 @@ protected override void InitializeWorker(AnalysisContext context)

public void Analyze(SemanticModelAnalysisContext context)
{
var semanticModel = context.SemanticModel;
var syntaxTree = semanticModel.SyntaxTree;
var cancellationToken = context.CancellationToken;

if (!context.GetIdeAnalyzerOptions().DetectAndOfferEditorFeaturesForProbableJsonStrings)
return;

var detector = JsonLanguageDetector.GetOrCreate(semanticModel.Compilation, _info);
var root = syntaxTree.GetRoot(cancellationToken);
Analyze(context, detector, root, cancellationToken);
var detector = JsonLanguageDetector.GetOrCreate(context.SemanticModel.Compilation, _info);
Analyze(context, detector, context.GetAnalysisRoot(findInTrivia: true), context.CancellationToken);
}

private void Analyze(
Expand All @@ -68,6 +63,9 @@ private void Analyze(

foreach (var child in node.ChildNodesAndTokens())
{
if (!context.ShouldAnalyzeSpan(child.FullSpan))
continue;

if (child.IsNode)
{
Analyze(context, detector, child.AsNode()!, cancellationToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,11 @@ protected override void InitializeWorker(AnalysisContext context)

public void Analyze(SemanticModelAnalysisContext context)
{
var semanticModel = context.SemanticModel;
var syntaxTree = semanticModel.SyntaxTree;
var cancellationToken = context.CancellationToken;

var option = context.GetIdeAnalyzerOptions().ReportInvalidJsonPatterns;
if (!option)
if (!context.GetIdeAnalyzerOptions().ReportInvalidJsonPatterns)
return;

var detector = JsonLanguageDetector.GetOrCreate(semanticModel.Compilation, _info);
var root = syntaxTree.GetRoot(cancellationToken);
Analyze(context, detector, root, cancellationToken);
var detector = JsonLanguageDetector.GetOrCreate(context.SemanticModel.Compilation, _info);
Analyze(context, detector, context.GetAnalysisRoot(findInTrivia: true), context.CancellationToken);
}

private void Analyze(
Expand All @@ -63,6 +57,9 @@ private void Analyze(

foreach (var child in node.ChildNodesAndTokens())
{
if (!context.ShouldAnalyzeSpan(child.FullSpan))
continue;

if (child.IsNode)
{
Analyze(context, detector, child.AsNode()!, cancellationToken);
Expand Down
Loading