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

Only include compiler diagnostics if a fixer supports them #750

Merged
merged 1 commit into from Aug 11, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 11 additions & 4 deletions src/Analyzers/AnalyzerFormatter.cs
Expand Up @@ -49,6 +49,11 @@ internal class AnalyzerFormatter : ICodeFormatter
return solution;
}

// Only include compiler diagnostics if we have a fixer that can fix them.
var includeCompilerDiagnostics = fixers.Any(
codefix => codefix.FixableDiagnosticIds.Any(
id => id.StartsWith("CS") || id.StartsWith("BC")));

var analysisStopwatch = Stopwatch.StartNew();
logger.LogTrace(Resources.Running_0_analysis, _name);

Expand All @@ -63,15 +68,15 @@ internal class AnalyzerFormatter : ICodeFormatter
var projectAnalyzers = await FilterBySeverityAsync(solution.Projects, analyzers, formattablePaths, severity, cancellationToken).ConfigureAwait(false);

// Determine which diagnostics are being reported for each project.
var projectDiagnostics = await GetProjectDiagnosticsAsync(solution, projectAnalyzers, formattablePaths, formatOptions, severity, logger, formattedFiles, cancellationToken).ConfigureAwait(false);
var projectDiagnostics = await GetProjectDiagnosticsAsync(solution, projectAnalyzers, formattablePaths, formatOptions, severity, includeCompilerDiagnostics, logger, formattedFiles, cancellationToken).ConfigureAwait(false);

var projectDiagnosticsMS = analysisStopwatch.ElapsedMilliseconds;
logger.LogTrace(Resources.Complete_in_0_ms, projectDiagnosticsMS);

logger.LogTrace(Resources.Fixing_diagnostics);

// Run each analyzer individually and apply fixes if possible.
solution = await FixDiagnosticsAsync(solution, analyzers, fixers, projectDiagnostics, formattablePaths, severity, logger, cancellationToken).ConfigureAwait(false);
solution = await FixDiagnosticsAsync(solution, analyzers, fixers, projectDiagnostics, formattablePaths, severity, includeCompilerDiagnostics, logger, cancellationToken).ConfigureAwait(false);

var fixDiagnosticsMS = analysisStopwatch.ElapsedMilliseconds - projectDiagnosticsMS;
logger.LogTrace(Resources.Complete_in_0_ms, fixDiagnosticsMS);
Expand All @@ -87,6 +92,7 @@ internal class AnalyzerFormatter : ICodeFormatter
ImmutableHashSet<string> formattablePaths,
FormatOptions options,
DiagnosticSeverity severity,
bool includeCompilerDiagnostics,
ILogger logger,
List<FormattedFile> formattedFiles,
CancellationToken cancellationToken)
Expand All @@ -101,7 +107,7 @@ internal class AnalyzerFormatter : ICodeFormatter
}

// Run all the filtered analyzers to determine which are reporting diagnostic.
await _runner.RunCodeAnalysisAsync(result, analyzers, project, formattablePaths, severity, logger, cancellationToken).ConfigureAwait(false);
await _runner.RunCodeAnalysisAsync(result, analyzers, project, formattablePaths, severity, includeCompilerDiagnostics, logger, cancellationToken).ConfigureAwait(false);
}

LogDiagnosticLocations(solution, result.Diagnostics.SelectMany(kvp => kvp.Value), options.WorkspaceFilePath, options.ChangesAreErrors, logger, formattedFiles);
Expand Down Expand Up @@ -143,6 +149,7 @@ static void LogDiagnosticLocations(Solution solution, IEnumerable<Diagnostic> di
ImmutableDictionary<ProjectId, ImmutableHashSet<string>> projectDiagnostics,
ImmutableHashSet<string> formattablePaths,
DiagnosticSeverity severity,
bool includeCompilerDiagnostics,
ILogger logger,
CancellationToken cancellationToken)
{
Expand Down Expand Up @@ -180,7 +187,7 @@ static void LogDiagnosticLocations(Solution solution, IEnumerable<Diagnostic> di
}

var analyzers = analyzersByLanguage[project.Language];
await _runner.RunCodeAnalysisAsync(result, analyzers, project, formattablePaths, severity, logger, cancellationToken).ConfigureAwait(false);
await _runner.RunCodeAnalysisAsync(result, analyzers, project, formattablePaths, severity, includeCompilerDiagnostics, logger, cancellationToken).ConfigureAwait(false);
}

var hasDiagnostics = result.Diagnostics.Any(kvp => kvp.Value.Count > 0);
Expand Down
16 changes: 5 additions & 11 deletions src/Analyzers/AnalyzerRunner.cs
@@ -1,7 +1,6 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the MIT license. See License.txt in the project root for license information.

using System.Collections.Immutable;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Diagnostics;
Expand All @@ -11,29 +10,24 @@ namespace Microsoft.CodeAnalysis.Tools.Analyzers
{
internal partial class AnalyzerRunner : IAnalyzerRunner
{
private readonly bool _includeComplilerDiagnostics;

public AnalyzerRunner(bool includeCompilerDiagnostics)
{
_includeComplilerDiagnostics = includeCompilerDiagnostics;
}

public Task RunCodeAnalysisAsync(
CodeAnalysisResult result,
DiagnosticAnalyzer analyzers,
Project project,
ImmutableHashSet<string> formattableDocumentPaths,
DiagnosticSeverity severity,
bool includeCompilerDiagnostics,
ILogger logger,
CancellationToken cancellationToken)
=> RunCodeAnalysisAsync(result, ImmutableArray.Create(analyzers), project, formattableDocumentPaths, severity, logger, cancellationToken);
=> RunCodeAnalysisAsync(result, ImmutableArray.Create(analyzers), project, formattableDocumentPaths, severity, includeCompilerDiagnostics, logger, cancellationToken);

public async Task RunCodeAnalysisAsync(
CodeAnalysisResult result,
ImmutableArray<DiagnosticAnalyzer> analyzers,
Project project,
ImmutableHashSet<string> formattableDocumentPaths,
DiagnosticSeverity severity,
bool includeCompilerDiagnostics,
ILogger logger,
CancellationToken cancellationToken)
{
Expand All @@ -45,7 +39,7 @@ public AnalyzerRunner(bool includeCompilerDiagnostics)

// If are not running any analyzers and are not reporting compiler diagnostics, then there is
// nothing to report.
if (analyzers.IsEmpty && !_includeComplilerDiagnostics)
if (analyzers.IsEmpty && !includeCompilerDiagnostics)
JoeRobich marked this conversation as resolved.
Show resolved Hide resolved
{
return;
}
Expand All @@ -64,7 +58,7 @@ public AnalyzerRunner(bool includeCompilerDiagnostics)
logAnalyzerExecutionTime: false,
reportSuppressedDiagnostics: false);
var analyzerCompilation = compilation.WithAnalyzers(analyzers, analyzerOptions);
diagnostics = _includeComplilerDiagnostics
diagnostics = includeCompilerDiagnostics
? await analyzerCompilation.GetAllDiagnosticsAsync(cancellationToken).ConfigureAwait(false)
: await analyzerCompilation.GetAnalyzerDiagnosticsAsync(cancellationToken).ConfigureAwait(false);
}
Expand Down
2 changes: 2 additions & 0 deletions src/Analyzers/Interfaces/IAnalyzerRunner.cs
Expand Up @@ -16,6 +16,7 @@ internal interface IAnalyzerRunner
Project project,
ImmutableHashSet<string> formattableDocumentPaths,
DiagnosticSeverity severity,
bool includeCompilerDiagnostics,
ILogger logger,
CancellationToken cancellationToken);

Expand All @@ -25,6 +26,7 @@ internal interface IAnalyzerRunner
Project project,
ImmutableHashSet<string> formattableDocumentPaths,
DiagnosticSeverity severity,
bool includeCompilerDiagnostics,
ILogger logger,
CancellationToken cancellationToken);
}
Expand Down
4 changes: 2 additions & 2 deletions src/CodeFormatter.cs
Expand Up @@ -29,8 +29,8 @@ internal static class CodeFormatter
new EndOfLineFormatter(),
new CharsetFormatter(),
new ImportsFormatter(),
new AnalyzerFormatter(Resources.Code_Style, new CodeStyleInformationProvider(), new AnalyzerRunner(includeCompilerDiagnostics: true), new SolutionCodeFixApplier()),
new AnalyzerFormatter(Resources.Analyzer_Reference, new AnalyzerReferenceInformationProvider(), new AnalyzerRunner(includeCompilerDiagnostics: true), new SolutionCodeFixApplier()),
new AnalyzerFormatter(Resources.Code_Style, new CodeStyleInformationProvider(), new AnalyzerRunner(), new SolutionCodeFixApplier()),
new AnalyzerFormatter(Resources.Analyzer_Reference, new AnalyzerReferenceInformationProvider(), new AnalyzerRunner(), new SolutionCodeFixApplier()),
}.ToImmutableArray();

public static async Task<WorkspaceFormatResult> FormatWorkspaceAsync(
Expand Down