提交 f67a6e6a 编写于 作者: M Manish Vasani

Merge pull request #4363 from mavasani/Issue4068

Don't skip executing analyzer compilation actions in the IDE when ClosedFileDiagnostics flag is turned off OR the analyzer reports only hidden diagnostics. Compilation actions and compilation end actions can report diagnostics on open files, so they must always be executed.

Fixes #4068 
......@@ -1779,5 +1779,46 @@ class MyClass
Assert.Equal(0, diagnostics.Count())
End Using
End Sub
<Fact, WorkItem(4068, "https://github.com/dotnet/roslyn/issues/4068")>
Public Sub TestAnalyzerWithCompilationActionReportingHiddenDiagnostics()
Dim test = <Workspace>
<Project Language="C#" CommonReferences="true">
<Document><![CDATA[
class MyClass
{
}]]>
</Document>
</Project>
</Workspace>
Using workspace = TestWorkspaceFactory.CreateWorkspace(test)
Dim project = workspace.CurrentSolution.Projects.Single()
' Add analyzer
Dim analyzer = New HiddenDiagnosticsCompilationAnalyzer()
Dim analyzerReference = New AnalyzerImageReference(ImmutableArray.Create(Of DiagnosticAnalyzer)(analyzer))
project = project.AddAnalyzerReference(analyzerReference)
Dim diagnosticService = New TestDiagnosticAnalyzerService()
Dim incrementalAnalyzer = diagnosticService.CreateIncrementalAnalyzer(workspace)
' Verify available diagnostic descriptors
Dim descriptorsMap = diagnosticService.GetDiagnosticDescriptors(project)
Assert.Equal(1, descriptorsMap.Count)
Dim descriptors = descriptorsMap.First().Value
Assert.Equal(1, descriptors.Length)
Assert.Equal(HiddenDiagnosticsCompilationAnalyzer.Descriptor.Id, descriptors.Single().Id)
' Force project analysis
incrementalAnalyzer.AnalyzeProjectAsync(project, semanticsChanged:=True, cancellationToken:=CancellationToken.None).Wait()
' Get cached project diagnostics.
Dim diagnostics = diagnosticService.GetCachedDiagnosticsAsync(workspace, project.Id).WaitAndGetResult(CancellationToken.None)
Assert.Equal(1, diagnostics.Count())
Assert.Equal(HiddenDiagnosticsCompilationAnalyzer.Descriptor.Id, diagnostics.Single().Id)
End Using
End Sub
End Class
End Namespace
......@@ -316,7 +316,8 @@ private async Task AnalyzeProjectAsync(Project project, CancellationToken cancel
{
try
{
if (!CheckOption(project.Solution.Workspace, project.Language, documentOpened: false))
// Compilation actions can report diagnostics on open files, so "documentOpened = true"
if (!CheckOption(project.Solution.Workspace, project.Language, documentOpened: true))
{
return;
}
......@@ -329,7 +330,8 @@ private async Task AnalyzeProjectAsync(Project project, CancellationToken cancel
var versions = new VersionArgument(projectTextVersion, semanticVersion, projectVersion);
foreach (var stateSet in _stateManager.GetOrUpdateStateSets(project))
{
if (SkipRunningAnalyzer(project.CompilationOptions, analyzerDriver, openedDocument: false, skipClosedFileChecks: false, stateSet: stateSet))
// Compilation actions can report diagnostics on open files, so we skipClosedFileChecks.
if (SkipRunningAnalyzer(project.CompilationOptions, analyzerDriver, openedDocument: true, skipClosedFileChecks: true, stateSet: stateSet))
{
await ClearExistingDiagnostics(project, stateSet, cancellationToken).ConfigureAwait(false);
continue;
......
......@@ -336,5 +336,34 @@ protected override string GetText(IFormatProvider formatProvider)
}
}
}
[DiagnosticAnalyzer(LanguageNames.CSharp, LanguageNames.VisualBasic)]
public class HiddenDiagnosticsCompilationAnalyzer : DiagnosticAnalyzer
{
public static readonly DiagnosticDescriptor Descriptor = new DiagnosticDescriptor(
"ID 1000",
"Description1",
string.Empty,
"Analysis",
DiagnosticSeverity.Hidden,
true,
customTags: WellKnownDiagnosticTags.NotConfigurable);
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(Descriptor);
public override void Initialize(AnalysisContext context)
{
context.RegisterCompilationAction(this.OnCompilation);
}
private void OnCompilation(CompilationAnalysisContext context)
{
// Report the hidden diagnostic on all trees in compilation.
foreach (var tree in context.Compilation.SyntaxTrees)
{
context.ReportDiagnostic(Diagnostic.Create(Descriptor, tree.GetRoot().GetLocation()));
}
}
}
}
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册