diff --git a/src/Compilers/CSharp/Test/Emit2/Diagnostics/DiagnosticAnalyzerTests.cs b/src/Compilers/CSharp/Test/Emit2/Diagnostics/DiagnosticAnalyzerTests.cs index 1e24fc44adfa7..146bf4c5dcb74 100644 --- a/src/Compilers/CSharp/Test/Emit2/Diagnostics/DiagnosticAnalyzerTests.cs +++ b/src/Compilers/CSharp/Test/Emit2/Diagnostics/DiagnosticAnalyzerTests.cs @@ -3511,8 +3511,7 @@ public void M() { } var compWithAnalyzers = new CompilationWithAnalyzers( compilation, analyzers.ToImmutableArray(), - new AnalyzerOptions(ImmutableArray.Empty), - CancellationToken.None); + new AnalyzerOptions(ImmutableArray.Empty)); var diagnostics = await compWithAnalyzers.GetAnalyzerSemanticDiagnosticsAsync(model, filterSpan: null, CancellationToken.None); diagnostics.Verify(Diagnostic("ID0001", "M").WithLocation(4, 17)); } diff --git a/src/Compilers/Core/CodeAnalysisTest/Diagnostics/CompilationWithAnalyzersTests.cs b/src/Compilers/Core/CodeAnalysisTest/Diagnostics/CompilationWithAnalyzersTests.cs index 995ddab41fdf6..afbec11d10025 100644 --- a/src/Compilers/Core/CodeAnalysisTest/Diagnostics/CompilationWithAnalyzersTests.cs +++ b/src/Compilers/Core/CodeAnalysisTest/Diagnostics/CompilationWithAnalyzersTests.cs @@ -59,7 +59,7 @@ public void GetAnalyzerTelemetry() DiagnosticAnalyzer analyzer = new AnalyzerWithDisabledRules(); var analyzers = ImmutableArray.Create(analyzer); var analyzerOptions = new AnalyzerOptions(ImmutableArray.Empty); - var compWithAnalyzers = new CompilationWithAnalyzers(compilation, analyzers, analyzerOptions, CancellationToken.None); + var compWithAnalyzers = new CompilationWithAnalyzers(compilation, analyzers, analyzerOptions); var analysisResult = compWithAnalyzers.GetAnalysisResultAsync(CancellationToken.None).Result; Assert.Empty(analysisResult.CompilationDiagnostics); diff --git a/src/Compilers/Core/Portable/DiagnosticAnalyzer/CompilationWithAnalyzers.cs b/src/Compilers/Core/Portable/DiagnosticAnalyzer/CompilationWithAnalyzers.cs index c7fafa539fbd9..421dbe30abcbe 100644 --- a/src/Compilers/Core/Portable/DiagnosticAnalyzer/CompilationWithAnalyzers.cs +++ b/src/Compilers/Core/Portable/DiagnosticAnalyzer/CompilationWithAnalyzers.cs @@ -5,6 +5,7 @@ using System; using System.Collections.Generic; using System.Collections.Immutable; +using System.ComponentModel; using System.Diagnostics; using System.Diagnostics.CodeAnalysis; using System.Linq; @@ -26,7 +27,6 @@ public class CompilationWithAnalyzers private readonly ImmutableArray _analyzers; private readonly ImmutableArray _suppressors; private readonly CompilationWithAnalyzersOptions _analysisOptions; - private readonly CancellationToken _cancellationToken; /// /// Builder for storing current, possibly partial, analysis results: @@ -61,7 +61,17 @@ public class CompilationWithAnalyzers /// An optional cancellation token which can be used to cancel analysis. /// Note: This token is only used if the API invoked to get diagnostics doesn't provide a cancellation token. /// - public CancellationToken CancellationToken => _cancellationToken; + [Obsolete("This CancellationToken is always 'None'", error: false)] + [EditorBrowsable(EditorBrowsableState.Never)] + public CancellationToken CancellationToken => CancellationToken.None; + + /// + [Obsolete("Use constructor without a cancellation token")] + [EditorBrowsable(EditorBrowsableState.Never)] + public CompilationWithAnalyzers(Compilation compilation, ImmutableArray analyzers, AnalyzerOptions? options, CancellationToken cancellationToken) + : this(compilation, analyzers, options) + { + } /// /// Creates a new compilation by attaching diagnostic analyzers to an existing compilation. @@ -69,9 +79,8 @@ public class CompilationWithAnalyzers /// The original compilation. /// The set of analyzers to include in future analyses. /// Options that are passed to analyzers. - /// A cancellation token that can be used to abort analysis. - public CompilationWithAnalyzers(Compilation compilation, ImmutableArray analyzers, AnalyzerOptions? options, CancellationToken cancellationToken) - : this(compilation, analyzers, new CompilationWithAnalyzersOptions(options, onAnalyzerException: null, analyzerExceptionFilter: null, concurrentAnalysis: true, logAnalyzerExecutionTime: true, reportSuppressedDiagnostics: false), cancellationToken) + public CompilationWithAnalyzers(Compilation compilation, ImmutableArray analyzers, AnalyzerOptions? options) + : this(compilation, analyzers, new CompilationWithAnalyzersOptions(options, onAnalyzerException: null, analyzerExceptionFilter: null, concurrentAnalysis: true, logAnalyzerExecutionTime: true, reportSuppressedDiagnostics: false)) { } @@ -82,11 +91,6 @@ public CompilationWithAnalyzers(Compilation compilation, ImmutableArrayThe set of analyzers to include in future analyses. /// Options to configure analyzer execution. public CompilationWithAnalyzers(Compilation compilation, ImmutableArray analyzers, CompilationWithAnalyzersOptions analysisOptions) - : this(compilation, analyzers, analysisOptions, cancellationToken: CancellationToken.None) - { - } - - private CompilationWithAnalyzers(Compilation compilation, ImmutableArray analyzers, CompilationWithAnalyzersOptions analysisOptions, CancellationToken cancellationToken) { VerifyArguments(compilation, analyzers, analysisOptions); @@ -98,7 +102,6 @@ private CompilationWithAnalyzers(Compilation compilation, ImmutableArray().ToImmutableArrayOrEmpty(); _analysisOptions = analysisOptions; - _cancellationToken = cancellationToken; _analysisResultBuilder = new AnalysisResultBuilder(analysisOptions.LogAnalyzerExecutionTime, analyzers, _analysisOptions.Options?.AdditionalFiles ?? ImmutableArray.Empty); _compilationAnalysisScope = AnalysisScope.Create(_compilation, _analyzers, this); @@ -225,15 +228,18 @@ private void VerifyAdditionalFile(AdditionalText file) /// /// Returns diagnostics produced by all . /// + [EditorBrowsable(EditorBrowsableState.Never)] public Task> GetAnalyzerDiagnosticsAsync() { - return GetAnalyzerDiagnosticsAsync(_cancellationToken); + return GetAnalyzerDiagnosticsAsync(CancellationToken.None); } /// /// Returns diagnostics produced by all . /// - public async Task> GetAnalyzerDiagnosticsAsync(CancellationToken cancellationToken) +#pragma warning disable RS0027 // API with optional parameter(s) should have the most parameters amongst its public overloads + public async Task> GetAnalyzerDiagnosticsAsync(CancellationToken cancellationToken = default) +#pragma warning restore RS0027 // API with optional parameter(s) should have the most parameters amongst its public overloads { return await GetAnalyzerDiagnosticsCoreAsync(Analyzers, cancellationToken).ConfigureAwait(false); } @@ -273,15 +279,16 @@ public async Task GetAnalysisResultAsync(ImmutableArray /// Returns all diagnostics produced by compilation and by all . /// + [EditorBrowsable(EditorBrowsableState.Never)] public Task> GetAllDiagnosticsAsync() { - return GetAllDiagnosticsAsync(_cancellationToken); + return GetAllDiagnosticsAsync(CancellationToken.None); } /// /// Returns all diagnostics produced by compilation and by all . /// - public async Task> GetAllDiagnosticsAsync(CancellationToken cancellationToken) + public async Task> GetAllDiagnosticsAsync(CancellationToken cancellationToken = default) { var diagnostics = await getAllDiagnosticsWithoutStateTrackingAsync(Analyzers, cancellationToken: cancellationToken).ConfigureAwait(false); return diagnostics.AddRange(_exceptionDiagnostics); diff --git a/src/Compilers/Core/Portable/DiagnosticAnalyzer/DiagnosticAnalyzerExtensions.cs b/src/Compilers/Core/Portable/DiagnosticAnalyzer/DiagnosticAnalyzerExtensions.cs index 4f9b117e3b75b..698e0330297d8 100644 --- a/src/Compilers/Core/Portable/DiagnosticAnalyzer/DiagnosticAnalyzerExtensions.cs +++ b/src/Compilers/Core/Portable/DiagnosticAnalyzer/DiagnosticAnalyzerExtensions.cs @@ -2,23 +2,34 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +using System; using System.Collections.Immutable; +using System.ComponentModel; using System.Threading; namespace Microsoft.CodeAnalysis.Diagnostics { public static class DiagnosticAnalyzerExtensions { + /// + [Obsolete("Use WithAnalyzers overload without a cancellation token", error: false)] + [EditorBrowsable(EditorBrowsableState.Never)] + public static CompilationWithAnalyzers WithAnalyzers(this Compilation compilation, ImmutableArray analyzers, AnalyzerOptions? options, CancellationToken cancellationToken) + { + return new CompilationWithAnalyzers(compilation, analyzers, options, cancellationToken); + } + /// /// Returns a new compilation with attached diagnostic analyzers. /// /// Compilation to which analyzers are to be added. /// The set of analyzers to include in future analyses. /// Options that are passed to analyzers. - /// A cancellation token that can be used to abort analysis. - public static CompilationWithAnalyzers WithAnalyzers(this Compilation compilation, ImmutableArray analyzers, AnalyzerOptions? options = null, CancellationToken cancellationToken = default) +#pragma warning disable RS0027 // API with optional parameter(s) should have the most parameters amongst its public overloads + public static CompilationWithAnalyzers WithAnalyzers(this Compilation compilation, ImmutableArray analyzers, AnalyzerOptions? options = null) +#pragma warning restore RS0027 // API with optional parameter(s) should have the most parameters amongst its public overloads { - return new CompilationWithAnalyzers(compilation, analyzers, options, cancellationToken); + return new CompilationWithAnalyzers(compilation, analyzers, options); } /// diff --git a/src/Compilers/Core/Portable/PublicAPI.Unshipped.txt b/src/Compilers/Core/Portable/PublicAPI.Unshipped.txt index 870cf2c12c007..75c51e1b5bcbd 100644 --- a/src/Compilers/Core/Portable/PublicAPI.Unshipped.txt +++ b/src/Compilers/Core/Portable/PublicAPI.Unshipped.txt @@ -1,5 +1,8 @@ *REMOVED*static Microsoft.CodeAnalysis.SeparatedSyntaxList.implicit operator Microsoft.CodeAnalysis.SeparatedSyntaxList(Microsoft.CodeAnalysis.SeparatedSyntaxList nodes) -> Microsoft.CodeAnalysis.SeparatedSyntaxList *REMOVED*static Microsoft.CodeAnalysis.SyntaxList.implicit operator Microsoft.CodeAnalysis.SyntaxList(Microsoft.CodeAnalysis.SyntaxList nodes) -> Microsoft.CodeAnalysis.SyntaxList +*REMOVED*Microsoft.CodeAnalysis.Diagnostics.CompilationWithAnalyzers.GetAllDiagnosticsAsync(System.Threading.CancellationToken cancellationToken) -> System.Threading.Tasks.Task>! +*REMOVED*Microsoft.CodeAnalysis.Diagnostics.CompilationWithAnalyzers.GetAnalyzerDiagnosticsAsync(System.Threading.CancellationToken cancellationToken) -> System.Threading.Tasks.Task>! +*REMOVED*static Microsoft.CodeAnalysis.Diagnostics.DiagnosticAnalyzerExtensions.WithAnalyzers(this Microsoft.CodeAnalysis.Compilation! compilation, System.Collections.Immutable.ImmutableArray analyzers, Microsoft.CodeAnalysis.Diagnostics.AnalyzerOptions? options = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> Microsoft.CodeAnalysis.Diagnostics.CompilationWithAnalyzers! Microsoft.CodeAnalysis.CommandLineArguments.ReportInternalsVisibleToAttributes.get -> bool Microsoft.CodeAnalysis.Diagnostics.AdditionalTextValueProvider Microsoft.CodeAnalysis.Diagnostics.AdditionalTextValueProvider.AdditionalTextValueProvider(System.Func! computeValue, System.Collections.Generic.IEqualityComparer? additionalTextComparer = null) -> void @@ -11,10 +14,13 @@ Microsoft.CodeAnalysis.Diagnostics.CompilationStartAnalysisContext.TryGetValue Microsoft.CodeAnalysis.Text.TextSpan? Microsoft.CodeAnalysis.Diagnostics.CodeBlockAnalysisContext.FilterSpan.get -> Microsoft.CodeAnalysis.Text.TextSpan? Microsoft.CodeAnalysis.Diagnostics.CodeBlockStartAnalysisContext.FilterSpan.get -> Microsoft.CodeAnalysis.Text.TextSpan? +Microsoft.CodeAnalysis.Diagnostics.CompilationWithAnalyzers.CompilationWithAnalyzers(Microsoft.CodeAnalysis.Compilation! compilation, System.Collections.Immutable.ImmutableArray analyzers, Microsoft.CodeAnalysis.Diagnostics.AnalyzerOptions? options) -> void +Microsoft.CodeAnalysis.Diagnostics.CompilationWithAnalyzers.GetAllDiagnosticsAsync(System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.Task>! Microsoft.CodeAnalysis.Diagnostics.CompilationWithAnalyzers.GetAnalysisResultAsync(Microsoft.CodeAnalysis.AdditionalText! file, Microsoft.CodeAnalysis.Text.TextSpan? filterSpan, System.Collections.Immutable.ImmutableArray analyzers, System.Threading.CancellationToken cancellationToken) -> System.Threading.Tasks.Task! Microsoft.CodeAnalysis.Diagnostics.CompilationWithAnalyzers.GetAnalysisResultAsync(Microsoft.CodeAnalysis.AdditionalText! file, Microsoft.CodeAnalysis.Text.TextSpan? filterSpan, System.Threading.CancellationToken cancellationToken) -> System.Threading.Tasks.Task! Microsoft.CodeAnalysis.Diagnostics.CompilationWithAnalyzers.GetAnalysisResultAsync(Microsoft.CodeAnalysis.SyntaxTree! tree, Microsoft.CodeAnalysis.Text.TextSpan? filterSpan, System.Collections.Immutable.ImmutableArray analyzers, System.Threading.CancellationToken cancellationToken) -> System.Threading.Tasks.Task! Microsoft.CodeAnalysis.Diagnostics.CompilationWithAnalyzers.GetAnalysisResultAsync(Microsoft.CodeAnalysis.SyntaxTree! tree, Microsoft.CodeAnalysis.Text.TextSpan? filterSpan, System.Threading.CancellationToken cancellationToken) -> System.Threading.Tasks.Task! +Microsoft.CodeAnalysis.Diagnostics.CompilationWithAnalyzers.GetAnalyzerDiagnosticsAsync(System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.Task>! Microsoft.CodeAnalysis.Diagnostics.CompilationWithAnalyzers.GetAnalyzerSyntaxDiagnosticsAsync(Microsoft.CodeAnalysis.SyntaxTree! tree, Microsoft.CodeAnalysis.Text.TextSpan? filterSpan, System.Collections.Immutable.ImmutableArray analyzers, System.Threading.CancellationToken cancellationToken) -> System.Threading.Tasks.Task>! Microsoft.CodeAnalysis.Diagnostics.CompilationWithAnalyzers.GetAnalyzerSyntaxDiagnosticsAsync(Microsoft.CodeAnalysis.SyntaxTree! tree, Microsoft.CodeAnalysis.Text.TextSpan? filterSpan, System.Threading.CancellationToken cancellationToken) -> System.Threading.Tasks.Task>! Microsoft.CodeAnalysis.Diagnostics.OperationAnalysisContext.FilterSpan.get -> Microsoft.CodeAnalysis.Text.TextSpan? @@ -32,6 +38,8 @@ Microsoft.CodeAnalysis.Diagnostics.SymbolStartAnalysisContext.FilterTree.get -> Microsoft.CodeAnalysis.Diagnostics.SyntaxNodeAnalysisContext.FilterSpan.get -> Microsoft.CodeAnalysis.Text.TextSpan? Microsoft.CodeAnalysis.Diagnostics.SyntaxNodeAnalysisContext.FilterTree.get -> Microsoft.CodeAnalysis.SyntaxTree! Microsoft.CodeAnalysis.Diagnostics.SyntaxTreeAnalysisContext.FilterSpan.get -> Microsoft.CodeAnalysis.Text.TextSpan? +static Microsoft.CodeAnalysis.Diagnostics.DiagnosticAnalyzerExtensions.WithAnalyzers(this Microsoft.CodeAnalysis.Compilation! compilation, System.Collections.Immutable.ImmutableArray analyzers, Microsoft.CodeAnalysis.Diagnostics.AnalyzerOptions? options = null) -> Microsoft.CodeAnalysis.Diagnostics.CompilationWithAnalyzers! +static Microsoft.CodeAnalysis.Diagnostics.DiagnosticAnalyzerExtensions.WithAnalyzers(this Microsoft.CodeAnalysis.Compilation! compilation, System.Collections.Immutable.ImmutableArray analyzers, Microsoft.CodeAnalysis.Diagnostics.AnalyzerOptions? options, System.Threading.CancellationToken cancellationToken) -> Microsoft.CodeAnalysis.Diagnostics.CompilationWithAnalyzers! Microsoft.CodeAnalysis.RefKind.RefReadOnlyParameter = 4 -> Microsoft.CodeAnalysis.RefKind static Microsoft.CodeAnalysis.SeparatedSyntaxList.explicit operator Microsoft.CodeAnalysis.SeparatedSyntaxList(Microsoft.CodeAnalysis.SeparatedSyntaxList nodes) -> Microsoft.CodeAnalysis.SeparatedSyntaxList static Microsoft.CodeAnalysis.SeparatedSyntaxList.op_Implicit(Microsoft.CodeAnalysis.SeparatedSyntaxList nodes) -> Microsoft.CodeAnalysis.SeparatedSyntaxList