Skip to content

Commit

Permalink
Merge pull request #74340 from CyrusNajmabadi/builtIn
Browse files Browse the repository at this point in the history
  • Loading branch information
CyrusNajmabadi authored Jul 11, 2024
2 parents bb5ea47 + 1773dc5 commit 718c6b5
Show file tree
Hide file tree
Showing 39 changed files with 36 additions and 215 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,6 @@ protected CSharpTypeStyleDiagnosticAnalyzerBase(

public override DiagnosticAnalyzerCategory GetAnalyzerCategory() => DiagnosticAnalyzerCategory.SemanticSpanAnalysis;

public override bool OpenFileOnly(SimplifierOptions? options)
{
// analyzer is only active in C# projects
Contract.ThrowIfNull(options);

var csOptions = (CSharpSimplifierOptions)options;
return !(csOptions.VarForBuiltInTypes.Notification.Severity is ReportDiagnostic.Warn or ReportDiagnostic.Error ||
csOptions.VarWhenTypeIsApparent.Notification.Severity is ReportDiagnostic.Warn or ReportDiagnostic.Error ||
csOptions.VarElsewhere.Notification.Severity is ReportDiagnostic.Warn or ReportDiagnostic.Error);
}

protected override void InitializeWorker(AnalysisContext context)
=> context.RegisterSyntaxNodeAction(
HandleVariableDeclaration, SyntaxKind.VariableDeclaration, SyntaxKind.ForEachStatement, SyntaxKind.DeclarationExpression);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,4 @@ private static void AddDiagnosticIdToOptionMapping(string diagnosticId, Immutabl
=> IDEDiagnosticIdToOptionMappingHelper.AddOptionMapping(diagnosticId, options);

public abstract DiagnosticAnalyzerCategory GetAnalyzerCategory();

public virtual bool OpenFileOnly(SimplifierOptions? options)
=> false;
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,6 @@ public sealed override void Initialize(AnalysisContext context)

public abstract DiagnosticAnalyzerCategory GetAnalyzerCategory();

public bool OpenFileOnly(SimplifierOptions? options)
=> false;

protected static DiagnosticDescriptor CreateDescriptor(
string id,
EnforceOnBuild enforceOnBuild,
Expand Down
13 changes: 0 additions & 13 deletions src/Analyzers/Core/Analyzers/AnalyzerOptionsProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,6 @@ internal IOptionsReader GetAnalyzerConfigOptions()

internal static partial class AnalyzerOptionsProviders
{
public static IdeAnalyzerOptions GetIdeOptions(this AnalyzerOptions options)
#if CODE_STYLE
=> IdeAnalyzerOptions.CommonDefault;
#else
=> (options is WorkspaceAnalyzerOptions workspaceOptions) ? workspaceOptions.IdeOptions : IdeAnalyzerOptions.CommonDefault;
#endif

public static AnalyzerOptionsProvider GetAnalyzerOptions(this AnalyzerOptions analyzerOptions, SyntaxTree syntaxTree)
=> new(analyzerOptions.AnalyzerConfigOptionsProvider.GetOptions(syntaxTree).GetOptionsReader(), syntaxTree.Options.Language);

Expand All @@ -107,10 +100,4 @@ public static AnalyzerOptionsProvider GetAnalyzerOptions(this OperationAnalysisC

public static AnalyzerOptionsProvider GetAnalyzerOptions(this CodeBlockAnalysisContext context)
=> GetAnalyzerOptions(context.Options, context.CodeBlock.SyntaxTree);

public static IdeAnalyzerOptions GetIdeAnalyzerOptions(this SemanticModelAnalysisContext context)
=> context.Options.GetIdeOptions();

public static IdeAnalyzerOptions GetIdeAnalyzerOptions(this SyntaxNodeAnalysisContext context)
=> context.Options.GetIdeOptions();
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,6 @@ protected AbstractQualifyMemberAccessDiagnosticAnalyzer()
{
}

public override bool OpenFileOnly(SimplifierOptions? options)
{
// analyzer is only active in C# and VB projects
Contract.ThrowIfNull(options);

return
!(options.QualifyFieldAccess.Notification.Severity is ReportDiagnostic.Warn or ReportDiagnostic.Error ||
options.QualifyPropertyAccess.Notification.Severity is ReportDiagnostic.Warn or ReportDiagnostic.Error ||
options.QualifyMethodAccess.Notification.Severity is ReportDiagnostic.Warn or ReportDiagnostic.Error ||
options.QualifyEventAccess.Notification.Severity is ReportDiagnostic.Warn or ReportDiagnostic.Error);
}

/// <summary>
/// Reports on whether the specified member is suitable for qualification. Some member
/// access expressions cannot be qualified; for instance if they begin with <c>base.</c>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,16 +81,6 @@ internal abstract bool CanSimplifyTypeNameExpression(
out TextSpan issueSpan, out string diagnosticId, out bool inDeclaration,
CancellationToken cancellationToken);

public override bool OpenFileOnly(SimplifierOptions? options)
{
// analyzer is only active in C# and VB projects
Contract.ThrowIfNull(options);

return
!(options.PreferPredefinedTypeKeywordInDeclaration.Notification.Severity is ReportDiagnostic.Warn or ReportDiagnostic.Error ||
options.PreferPredefinedTypeKeywordInMemberAccess.Notification.Severity is ReportDiagnostic.Warn or ReportDiagnostic.Error);
}

protected static ImmutableArray<NotificationOption2> GetAllNotifications(SimplifierOptions options)
=> [
options.PreferPredefinedTypeKeywordInDeclaration.Notification,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,6 @@ private class ThrowingDoNotCatchDiagnosticAnalyzer<TLanguageKindEnum> : Throwing
{
public bool IsHighPriority => false;

public bool OpenFileOnly(SimplifierOptions? options) => false;

public DiagnosticAnalyzerCategory GetAnalyzerCategory()
=> DiagnosticAnalyzerCategory.SyntaxTreeWithoutSemanticsAnalysis | DiagnosticAnalyzerCategory.SemanticDocumentAnalysis | DiagnosticAnalyzerCategory.ProjectAnalysis;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -702,7 +702,7 @@ internal async Task TestOnlyRequiredAnalyzerExecutedDuringDiagnosticComputation(
workspace.TryApplyChanges(workspace.CurrentSolution.WithAnalyzerReferences(new[] { analyzerReference }));
var project = workspace.CurrentSolution.Projects.Single();
var document = documentAnalysis ? project.Documents.Single() : null;
var ideAnalyzerOptions = IdeAnalyzerOptions.GetDefault(project.Services);
var ideAnalyzerOptions = IdeAnalyzerOptions.CommonDefault;
var diagnosticsMapResults = await DiagnosticComputer.GetDiagnosticsAsync(
document, project, Checksum.Null, ideAnalyzerOptions, span: null, analyzerIdsToRequestDiagnostics,
AnalysisKind.Semantic, new DiagnosticAnalyzerInfoCache(), workspace.Services,
Expand Down Expand Up @@ -750,7 +750,7 @@ void M()
workspace.TryApplyChanges(project.Solution);

project = workspace.CurrentSolution.Projects.Single();
var ideAnalyzerOptions = IdeAnalyzerOptions.GetDefault(project.Services);
var ideAnalyzerOptions = IdeAnalyzerOptions.CommonDefault;
var document = project.Documents.Single();
var additionalDocument = project.AdditionalDocuments.Single();

Expand Down Expand Up @@ -821,7 +821,7 @@ void M()
var document = project.Documents.Single();
var diagnosticAnalyzerInfoCache = new DiagnosticAnalyzerInfoCache();

var ideAnalyzerOptions = IdeAnalyzerOptions.GetDefault(project.Services);
var ideAnalyzerOptions = IdeAnalyzerOptions.CommonDefault;
var kind = actionKind == AnalyzerRegisterActionKind.SyntaxTree ? AnalysisKind.Syntax : AnalysisKind.Semantic;
var analyzerIds = new[] { analyzer.GetAnalyzerId() };

Expand Down Expand Up @@ -945,24 +945,6 @@ public override void Initialize(AnalysisContext context)
}
}

private class OpenFileOnlyAnalyzer : DiagnosticAnalyzer, IBuiltInAnalyzer
{
internal static readonly DiagnosticDescriptor s_syntaxRule = new DiagnosticDescriptor("syntax", "test", "test", "test", DiagnosticSeverity.Error, isEnabledByDefault: true);

public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(s_syntaxRule);

public override void Initialize(AnalysisContext context)
=> context.RegisterSyntaxTreeAction(c => c.ReportDiagnostic(Diagnostic.Create(s_syntaxRule, c.Tree.GetRoot().GetLocation())));

public DiagnosticAnalyzerCategory GetAnalyzerCategory()
=> DiagnosticAnalyzerCategory.SyntaxTreeWithoutSemanticsAnalysis;

public bool IsHighPriority => false;

public bool OpenFileOnly(SimplifierOptions options)
=> true;
}

private class NoNameAnalyzer : DocumentDiagnosticAnalyzer
{
internal static readonly DiagnosticDescriptor s_syntaxRule = new DiagnosticDescriptor("syntax", "test", "test", "test", DiagnosticSeverity.Error, isEnabledByDefault: true);
Expand Down
2 changes: 1 addition & 1 deletion src/EditorFeatures/Test/Options/GlobalOptionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ public void ReadingOptionsFromGlobalOptions(string language)
using var workspace = CreateWorkspace(out var globalOptions);
var languageServices = workspace.Services.SolutionServices.GetLanguageServices(language);

VerifyDataMembersHaveNonDefaultValues(globalOptions.GetIdeAnalyzerOptions(languageServices), IdeAnalyzerOptions.GetDefault(languageServices), language);
VerifyDataMembersHaveNonDefaultValues(globalOptions.GetIdeAnalyzerOptions(languageServices), IdeAnalyzerOptions.CommonDefault, language);
VerifyDataMembersHaveNonDefaultValues(globalOptions.GetCodeActionOptions(languageServices), CodeActionOptions.GetDefault(languageServices), language);
VerifyDataMembersHaveNonDefaultValues(globalOptions.GetBraceMatchingOptions(language), BraceMatchingOptions.Default, language);
VerifyDataMembersHaveNonDefaultValues(globalOptions.GetFindUsagesOptions(language), FindUsagesOptions.Default, language);
Expand Down
2 changes: 1 addition & 1 deletion src/EditorFeatures/Test/Preview/PreviewWorkspaceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ private static void ExecuteAnalyzers(PreviewWorkspace previewWorkspace, Immutabl
{
var analyzerOptions = new AnalyzerOptions(additionalFiles: ImmutableArray<AdditionalText>.Empty);
var project = previewWorkspace.CurrentSolution.Projects.Single();
var ideAnalyzerOptions = IdeAnalyzerOptions.GetDefault(project.Services);
var ideAnalyzerOptions = IdeAnalyzerOptions.CommonDefault;
var workspaceAnalyzerOptions = new WorkspaceAnalyzerOptions(analyzerOptions, ideAnalyzerOptions);
var compilationWithAnalyzersOptions = new CompilationWithAnalyzersOptions(workspaceAnalyzerOptions, onAnalyzerException: null, concurrentAnalysis: false, logAnalyzerExecutionTime: false);
var compilation = project.GetRequiredCompilationAsync(CancellationToken.None).Result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2391,10 +2391,6 @@ class MyClass
Return _category
End Function

Public Function OpenFileOnly(options As SimplifierOptions) As Boolean Implements IBuiltInAnalyzer.OpenFileOnly
Return False
End Function

Public Overrides ReadOnly Property SupportedDiagnostics As ImmutableArray(Of DiagnosticDescriptor)
Get
Return ImmutableArray.Create(Descriptor)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,6 @@ public bool IsHighPriority
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics
=> [DiagnosticDescriptor];

public bool OpenFileOnly(SimplifierOptions? options)
=> false;

public override void Initialize(AnalysisContext context)
{
context.EnableConcurrentExecution();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@ public static bool IsWorkspaceDiagnosticAnalyzer(this DiagnosticAnalyzer analyze
public static bool IsBuiltInAnalyzer(this DiagnosticAnalyzer analyzer)
=> analyzer is IBuiltInAnalyzer || analyzer.IsWorkspaceDiagnosticAnalyzer() || analyzer.IsCompilerAnalyzer();

public static bool IsOpenFileOnly(this DiagnosticAnalyzer analyzer, SimplifierOptions? options)
=> analyzer is IBuiltInAnalyzer builtInAnalyzer && builtInAnalyzer.OpenFileOnly(options);

public static ReportDiagnostic GetEffectiveSeverity(this DiagnosticDescriptor descriptor, CompilationOptions options)
{
return options == null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,6 @@ protected AbstractJsonDetectionAnalyzer(EmbeddedLanguageInfo info)
public override DiagnosticAnalyzerCategory GetAnalyzerCategory()
=> DiagnosticAnalyzerCategory.SemanticSpanAnalysis;

public override bool OpenFileOnly(SimplifierOptions? options)
=> false;

protected override void InitializeWorker(AnalysisContext context)
=> context.RegisterSemanticModelAction(Analyze);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@ protected AbstractJsonDiagnosticAnalyzer(EmbeddedLanguageInfo info)
public override DiagnosticAnalyzerCategory GetAnalyzerCategory()
=> DiagnosticAnalyzerCategory.SemanticSpanAnalysis;

public override bool OpenFileOnly(SimplifierOptions? options)
=> false;

protected override void InitializeWorker(AnalysisContext context)
=> context.RegisterSemanticModelAction(Analyze);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,6 @@ protected PreferFrameworkTypeDiagnosticAnalyzerBase()
{
}

public override bool OpenFileOnly(SimplifierOptions? options)
{
// analyzer is only active in C# and VB projects
Contract.ThrowIfNull(options);

return
!(options.PreferPredefinedTypeKeywordInDeclaration.Notification.Severity is ReportDiagnostic.Warn or ReportDiagnostic.Error ||
options.PreferPredefinedTypeKeywordInMemberAccess.Notification.Severity is ReportDiagnostic.Warn or ReportDiagnostic.Error);
}

public override DiagnosticAnalyzerCategory GetAnalyzerCategory()
=> DiagnosticAnalyzerCategory.SemanticSpanAnalysis;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ protected override CompilationOptions CreateCompilationOptions()

#if !CODE_STYLE
protected override AnalyzerOptions GetAnalyzerOptions(Project project)
=> new WorkspaceAnalyzerOptions(base.GetAnalyzerOptions(project), _sharedState.GetIdeAnalyzerOptions(project));
=> new WorkspaceAnalyzerOptions(base.GetAnalyzerOptions(project), _sharedState.GetIdeAnalyzerOptions());

protected override CodeFixContext CreateCodeFixContext(Document document, TextSpan span, ImmutableArray<Diagnostic> diagnostics, Action<CodeAction, ImmutableArray<Diagnostic>> registerCodeFix, CancellationToken cancellationToken)
=> new(document, span, diagnostics, registerCodeFix, _sharedState.CodeActionOptions, cancellationToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ protected override CompilationOptions CreateCompilationOptions()
#if !CODE_STYLE

protected override AnalyzerOptions GetAnalyzerOptions(Project project)
=> new WorkspaceAnalyzerOptions(base.GetAnalyzerOptions(project), _sharedState.GetIdeAnalyzerOptions(project));
=> new WorkspaceAnalyzerOptions(base.GetAnalyzerOptions(project), _sharedState.GetIdeAnalyzerOptions());

protected override CodeRefactoringContext CreateCodeRefactoringContext(Document document, TextSpan span, Action<CodeAction> registerRefactoring, CancellationToken cancellationToken)
=> new CodeRefactoringContext(document, span, (action, textSpan) => registerRefactoring(action), _sharedState.CodeActionOptions, cancellationToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ public SharedVerifierState(AnalyzerTest<DefaultVerifier> test, string defaultFil
internal CodeActionOptionsProvider CodeActionOptions { get; set; } = CodeAnalysis.CodeActions.CodeActionOptions.DefaultProvider;
internal IdeAnalyzerOptions? IdeAnalyzerOptions { get; set; }

internal IdeAnalyzerOptions GetIdeAnalyzerOptions(Project project)
=> IdeAnalyzerOptions ?? IdeAnalyzerOptions.GetDefault(project.Services);
internal IdeAnalyzerOptions GetIdeAnalyzerOptions()
=> IdeAnalyzerOptions ?? IdeAnalyzerOptions.CommonDefault;
#endif
internal void Apply()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ protected override ParseOptions CreateParseOptions()
#if !CODE_STYLE

protected override AnalyzerOptions GetAnalyzerOptions(Project project)
=> new WorkspaceAnalyzerOptions(base.GetAnalyzerOptions(project), _sharedState.GetIdeAnalyzerOptions(project));
=> new WorkspaceAnalyzerOptions(base.GetAnalyzerOptions(project), _sharedState.GetIdeAnalyzerOptions());

protected override CodeFixContext CreateCodeFixContext(Document document, TextSpan span, ImmutableArray<Diagnostic> diagnostics, Action<CodeAction, ImmutableArray<Diagnostic>> registerCodeFix, CancellationToken cancellationToken)
=> new(document, span, diagnostics, registerCodeFix, _sharedState.CodeActionOptions, cancellationToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ protected override ParseOptions CreateParseOptions()
#if !CODE_STYLE

protected override AnalyzerOptions GetAnalyzerOptions(Project project)
=> new WorkspaceAnalyzerOptions(base.GetAnalyzerOptions(project), _sharedState.GetIdeAnalyzerOptions(project));
=> new WorkspaceAnalyzerOptions(base.GetAnalyzerOptions(project), _sharedState.GetIdeAnalyzerOptions());

protected override CodeRefactoringContext CreateCodeRefactoringContext(Document document, TextSpan span, Action<CodeAction> registerRefactoring, CancellationToken cancellationToken)
=> new CodeRefactoringContext(document, span, (action, textSpan) => registerRefactoring(action), _sharedState.CodeActionOptions, cancellationToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,6 @@ internal class Analyzer : DiagnosticAnalyzer, IBuiltInAnalyzer

public bool IsHighPriority => false;

public bool OpenFileOnly(SimplifierOptions options) => false;

public ImmutableArray<SyntaxNode> AllNodes { get; set; }

public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ namespace Microsoft.CodeAnalysis.ExternalAccess.OmniSharp.Analyzers
{
internal static class OmniSharpWorkspaceAnalyzerOptionsFactory
{
#pragma warning disable IDE0060 // Remove unused parameter
public static AnalyzerOptions Create(Solution solution, AnalyzerOptions options)
=> new WorkspaceAnalyzerOptions(options, IdeAnalyzerOptions.GetDefault(solution.Services.GetLanguageServices(LanguageNames.CSharp)));
=> new WorkspaceAnalyzerOptions(options, IdeAnalyzerOptions.CommonDefault);
#pragma warning restore IDE0060 // Remove unused parameter
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ static string GetLanguageSpecificId(string? language, string noLanguageId, strin
public static async Task<CompilationWithAnalyzers?> CreateCompilationWithAnalyzersAsync(
Project project,
IdeAnalyzerOptions ideOptions,
IEnumerable<DiagnosticAnalyzer> analyzers,
ImmutableArray<DiagnosticAnalyzer> analyzers,
bool includeSuppressedDiagnostics,
CancellationToken cancellationToken)
{
Expand All @@ -142,7 +142,7 @@ static string GetLanguageSpecificId(string? language, string noLanguageId, strin
}

// Create driver that holds onto compilation and associated analyzers
var filteredAnalyzers = analyzers.Where(a => !a.IsWorkspaceDiagnosticAnalyzer()).ToImmutableArrayOrEmpty();
var filteredAnalyzers = analyzers.WhereAsArray(a => !a.IsWorkspaceDiagnosticAnalyzer());

// PERF: there is no analyzers for this compilation.
// compilationWithAnalyzer will throw if it is created with no analyzers which is perf optimization.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
// See the LICENSE file in the project root for more information.

using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Roslyn.Utilities;

namespace Microsoft.CodeAnalysis.Diagnostics.EngineV2
{
internal partial class DiagnosticIncrementalAnalyzer
{
private static Task<CompilationWithAnalyzers?> CreateCompilationWithAnalyzersAsync(Project project, IdeAnalyzerOptions ideOptions, IEnumerable<StateSet> stateSets, bool includeSuppressedDiagnostics, CancellationToken cancellationToken)
=> DocumentAnalysisExecutor.CreateCompilationWithAnalyzersAsync(project, ideOptions, stateSets.Select(s => s.Analyzer), includeSuppressedDiagnostics, cancellationToken);
=> DocumentAnalysisExecutor.CreateCompilationWithAnalyzersAsync(project, ideOptions, stateSets.SelectAsArray(s => s.Analyzer), includeSuppressedDiagnostics, cancellationToken);
}
}
Loading

0 comments on commit 718c6b5

Please sign in to comment.