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

Remove the ProjectDiagnosticAnalyzer type #74353

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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 @@ -244,9 +244,7 @@ public async Task TestHostAnalyzerOrderingAsync()

var analyzerReference = new AnalyzerImageReference(ImmutableArray.Create<DiagnosticAnalyzer>(
new Priority20Analyzer(),
new Priority15Analyzer(),
new Priority10Analyzer(),
new Priority1Analyzer(),
new Priority0Analyzer(),
new CSharpCompilerDiagnosticAnalyzer(),
new Analyzer()
Expand Down Expand Up @@ -275,9 +273,7 @@ public async Task TestHostAnalyzerOrderingAsync()
typeof(CSharpCompilerDiagnosticAnalyzer),
typeof(Analyzer),
typeof(Priority0Analyzer),
typeof(Priority1Analyzer),
typeof(Priority10Analyzer),
typeof(Priority15Analyzer),
typeof(Priority20Analyzer)
], analyzersArray.Select(a => a.GetType()));
}
Expand All @@ -290,7 +286,7 @@ public async Task TestHostAnalyzerErrorNotLeaking()
var solution = workspace.CurrentSolution;

var analyzerReference = new AnalyzerImageReference(ImmutableArray.Create<DiagnosticAnalyzer>(
new LeakDocumentAnalyzer(), new LeakProjectAnalyzer()));
new LeakDocumentAnalyzer()));

var globalOptions = GetGlobalOptions(workspace);
globalOptions.SetGlobalOption(SolutionCrawlerOptionsStorage.BackgroundAnalysisScopeOption, LanguageNames.CSharp, BackgroundAnalysisScope.FullSolution);
Expand Down Expand Up @@ -960,21 +956,11 @@ private class Priority20Analyzer : PriorityTestDocumentDiagnosticAnalyzer
public Priority20Analyzer() : base(priority: 20) { }
}

private class Priority15Analyzer : PriorityTestProjectDiagnosticAnalyzer
{
public Priority15Analyzer() : base(priority: 15) { }
}

private class Priority10Analyzer : PriorityTestDocumentDiagnosticAnalyzer
{
public Priority10Analyzer() : base(priority: 10) { }
}

private class Priority1Analyzer : PriorityTestProjectDiagnosticAnalyzer
{
public Priority1Analyzer() : base(priority: 1) { }
}

private class Priority0Analyzer : PriorityTestDocumentDiagnosticAnalyzer
{
public Priority0Analyzer() : base(priority: -1) { }
Expand All @@ -993,17 +979,6 @@ public override Task<ImmutableArray<Diagnostic>> AnalyzeSyntaxAsync(Document doc
=> Task.FromResult(ImmutableArray<Diagnostic>.Empty);
}

private class PriorityTestProjectDiagnosticAnalyzer : ProjectDiagnosticAnalyzer
{
protected PriorityTestProjectDiagnosticAnalyzer(int priority)
=> Priority = priority;

public override int Priority { get; }
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray<DiagnosticDescriptor>.Empty;
public override Task<ImmutableArray<Diagnostic>> AnalyzeProjectAsync(Project project, CancellationToken cancellationToken)
=> Task.FromResult(ImmutableArray<Diagnostic>.Empty);
}

private class LeakDocumentAnalyzer : DocumentDiagnosticAnalyzer
{
internal static readonly DiagnosticDescriptor s_syntaxRule = new DiagnosticDescriptor("leak", "test", "test", "test", DiagnosticSeverity.Error, isEnabledByDefault: true);
Expand All @@ -1020,13 +995,6 @@ public override Task<ImmutableArray<Diagnostic>> AnalyzeSemanticsAsync(Document
=> SpecializedTasks.Default<ImmutableArray<Diagnostic>>();
}

private class LeakProjectAnalyzer : ProjectDiagnosticAnalyzer
{
private static readonly DiagnosticDescriptor s_rule = new DiagnosticDescriptor("project", "test", "test", "test", DiagnosticSeverity.Error, isEnabledByDefault: true);
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(s_rule);
public override Task<ImmutableArray<Diagnostic>> AnalyzeProjectAsync(Project project, CancellationToken cancellationToken) => SpecializedTasks.Default<ImmutableArray<Diagnostic>>();
}

[DiagnosticAnalyzer(LanguageNames.CSharp)]
private class NamedTypeAnalyzer : DiagnosticAnalyzer
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ internal static class DiagnosticAnalyzerExtensions
{
public static bool IsWorkspaceDiagnosticAnalyzer(this DiagnosticAnalyzer analyzer)
=> analyzer is DocumentDiagnosticAnalyzer
|| analyzer is ProjectDiagnosticAnalyzer
|| analyzer == FileContentLoadAnalyzer.Instance
|| analyzer == GeneratorDiagnosticsPlaceholderAnalyzer.Instance;

Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -265,39 +265,6 @@ public static async Task<ImmutableArray<Diagnostic>> ComputeDocumentDiagnosticAn
return diagnostics;
}

public static async Task<ImmutableArray<Diagnostic>> ComputeProjectDiagnosticAnalyzerDiagnosticsAsync(
ProjectDiagnosticAnalyzer analyzer,
Project project,
Compilation? compilation,
CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();

ImmutableArray<Diagnostic> diagnostics;
try
{
diagnostics = (await analyzer.AnalyzeProjectAsync(project, cancellationToken).ConfigureAwait(false)).NullToEmpty();
#if DEBUG
// since all ProjectDiagnosticAnalyzers are from internal users, we only do debug check. also this can be expensive at runtime
// since it requires await. if we find any offender through NFW, we should be able to fix those since all those should
// from intern teams.
await VerifyDiagnosticLocationsAsync(diagnostics, project, cancellationToken).ConfigureAwait(false);
#endif
}
catch (Exception e) when (!IsCanceled(e, cancellationToken))
{
diagnostics = [CreateAnalyzerExceptionDiagnostic(analyzer, e)];
}

// Apply filtering from compilation options (source suppressions, ruleset, etc.)
if (compilation != null)
{
diagnostics = CompilationWithAnalyzers.GetEffectiveDiagnostics(diagnostics, compilation).ToImmutableArrayOrEmpty();
}

return diagnostics;
}

private static bool IsCanceled(Exception ex, CancellationToken cancellationToken)
=> (ex as OperationCanceledException)?.CancellationToken == cancellationToken;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ private async Task<ImmutableDictionary<DiagnosticAnalyzer, DiagnosticAnalysisRes
// than what we used to create the cache.
var version = await GetDiagnosticVersionAsync(project, cancellationToken).ConfigureAwait(false);

var ideAnalyzers = stateSets.Select(s => s.Analyzer).Where(a => a is ProjectDiagnosticAnalyzer or DocumentDiagnosticAnalyzer).ToImmutableArrayOrEmpty();
var ideAnalyzers = stateSets.Select(s => s.Analyzer).Where(a => a is DocumentDiagnosticAnalyzer).ToImmutableArrayOrEmpty();

if (compilationWithAnalyzers != null && TryReduceAnalyzersToRun(compilationWithAnalyzers, version, existing, out var analyzersToRun))
{
Expand Down Expand Up @@ -252,10 +252,6 @@ private async Task<ImmutableDictionary<DiagnosticAnalyzer, DiagnosticAnalysisRes
}

break;

case ProjectDiagnosticAnalyzer projectAnalyzer:
builder.AddCompilationDiagnostics(await DocumentAnalysisExecutor.ComputeProjectDiagnosticAnalyzerDiagnosticsAsync(projectAnalyzer, project, compilation, cancellationToken).ConfigureAwait(false));
break;
}

// merge the result to existing one.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,9 @@ private static int GetPriority(StateSet state)

return state.Analyzer switch
{
FileContentLoadAnalyzer _ => FileContentLoadAnalyzerPriority,
GeneratorDiagnosticsPlaceholderAnalyzer _ => GeneratorDiagnosticsPlaceholderAnalyzerPriority,
FileContentLoadAnalyzer => FileContentLoadAnalyzerPriority,
GeneratorDiagnosticsPlaceholderAnalyzer => GeneratorDiagnosticsPlaceholderAnalyzerPriority,
DocumentDiagnosticAnalyzer analyzer => Math.Max(0, analyzer.Priority),
ProjectDiagnosticAnalyzer analyzer => Math.Max(0, analyzer.Priority),
_ => RegularDiagnosticAnalyzerPriority,
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ public static DiagnosticAnalyzerCategory GetDiagnosticAnalyzerCategory(this Diag
{
FileContentLoadAnalyzer => DiagnosticAnalyzerCategory.SyntaxTreeWithoutSemanticsAnalysis,
DocumentDiagnosticAnalyzer => DiagnosticAnalyzerCategory.SyntaxTreeWithoutSemanticsAnalysis | DiagnosticAnalyzerCategory.SemanticDocumentAnalysis,
ProjectDiagnosticAnalyzer => DiagnosticAnalyzerCategory.None,
IBuiltInAnalyzer builtInAnalyzer => builtInAnalyzer.GetAnalyzerCategory(),

// Compiler analyzer supports syntax diagnostics, span-based semantic diagnostics and project level diagnostics.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ internal abstract class DocumentDiagnosticAnalyzer : DiagnosticAnalyzer
public abstract Task<ImmutableArray<Diagnostic>> AnalyzeSemanticsAsync(Document document, CancellationToken cancellationToken);

/// <summary>
/// it is not allowed one to implement both DocumentDiagnosticAnalzyer and DiagnosticAnalyzer
/// it is not allowed one to implement both DocumentDiagnosticAnalyzer and DiagnosticAnalyzer
/// </summary>
#pragma warning disable RS1026 // Enable concurrent execution
#pragma warning disable RS1025 // Configure generated code analysis
Expand All @@ -33,10 +33,10 @@ public sealed override void Initialize(AnalysisContext context)
}

/// <summary>
/// This lets vsix installed <see cref="DocumentDiagnosticAnalyzer"/> or <see cref="ProjectDiagnosticAnalyzer"/> to
/// specify priority of the analyzer. Regular <see cref="DiagnosticAnalyzer"/> always comes before those 2 different types.
/// Priority is ascending order and this only works on HostDiagnosticAnalyzer meaning Vsix installed analyzers in VS.
/// This is to support partner teams (such as typescript and F#) who want to order their analyzer's execution order.
/// This lets vsix installed <see cref="DocumentDiagnosticAnalyzer"/> to specify priority of the analyzer. Regular
/// <see cref="DiagnosticAnalyzer"/> always comes before those 2 different types. Priority is ascending order and
/// this only works on HostDiagnosticAnalyzer meaning Vsix installed analyzers in VS. This is to support partner
/// teams (such as typescript and F#) who want to order their analyzer's execution order.
/// </summary>
public virtual int Priority => DefaultPriority;
}

This file was deleted.

Loading