提交 8eb11040 编写于 作者: M Manish Vasani

Address CR feedback: Move all the fail fast on analyzer exceptions to test layer.

上级 0fab5816
......@@ -87,7 +87,7 @@ public void AnalyzerDriverIsSafeAgainstAnalyzerExceptions()
{
var compilation = CreateCompilationWithMscorlib45(TestResource.AllInOneCSharpCode, parseOptions: TestOptions.Regular);
ThrowingDiagnosticAnalyzer<SyntaxKind>.VerifyAnalyzerEngineIsSafeAgainstExceptions(analyzer =>
compilation.GetAnalyzerDiagnostics(new[] { analyzer }, null, CodeAnalysis.DiagnosticExtensions.AlwaysCatchAnalyzerException));
compilation.GetAnalyzerDiagnostics(new[] { analyzer }, null, CodeAnalysis.DiagnosticExtensions.CatchAndIgnoreAnalyzerException));
}
[Fact]
......
......@@ -1123,7 +1123,7 @@ public class C2
}
",
new[] { new ThrowExceptionForEachNamedTypeAnalyzer() },
DiagnosticExtensions.AlwaysCatchAnalyzerException,
DiagnosticExtensions.CatchAndIgnoreAnalyzerException,
expected);
}
......
......@@ -43,7 +43,7 @@ End Enum
Public Sub AnalyzerDriverIsSafeAgainstAnalyzerExceptions()
Dim compilation = CreateCompilationWithMscorlib({TestResource.AllInOneVisualBasicCode})
ThrowingDiagnosticAnalyzer(Of SyntaxKind).VerifyAnalyzerEngineIsSafeAgainstExceptions(
Function(analyzer) compilation.GetAnalyzerDiagnostics({analyzer}, Nothing, DiagnosticExtensions.AlwaysCatchAnalyzerException))
Function(analyzer) compilation.GetAnalyzerDiagnostics({analyzer}, Nothing, DiagnosticExtensions.CatchAndIgnoreAnalyzerException))
End Sub
<Fact>
......
......@@ -44,7 +44,7 @@ public class Class6<TTypeParameter>
";
var diagnosticsBag = DiagnosticBag.GetInstance();
var documentsAndSpan = GetDocumentsAndSpans(new[] { source }, LanguageNames.CSharp);
AnalyzeDocumentCore(GetCSharpDiagnosticAnalyzer(), documentsAndSpan.Item1[0], diagnosticsBag.Add, null, onAnalyzerException: DiagnosticExtensions.AlwaysCatchAnalyzerException);
AnalyzeDocumentCore(GetCSharpDiagnosticAnalyzer(), documentsAndSpan.Item1[0], diagnosticsBag.Add, null, onAnalyzerException: DiagnosticExtensions.CatchAndIgnoreAnalyzerException);
var diagnostics = diagnosticsBag.ToReadOnlyAndFree();
Assert.True(diagnostics.Length > 0);
Assert.Equal(string.Format("info AD0001: " + AnalyzerDriverResources.AnalyzerThrows, GetCSharpDiagnosticAnalyzer().GetType(), "The method or operation is not implemented."),
......
......@@ -94,7 +94,7 @@ public void DiagnosticAnalyzerDriverIsSafeAgainstAnalyzerExceptions()
{
var document = workspace.CurrentSolution.Projects.Single().Documents.Single();
ThrowingDiagnosticAnalyzer<SyntaxKind>.VerifyAnalyzerEngineIsSafeAgainstExceptions(analyzer =>
DiagnosticProviderTestUtilities.GetAllDiagnostics(analyzer, document, new Text.TextSpan(0, document.GetTextAsync().Result.Length), donotCatchAnalyzerExceptions: false));
DiagnosticProviderTestUtilities.GetAllDiagnostics(analyzer, document, new Text.TextSpan(0, document.GetTextAsync().Result.Length), logAnalyzerExceptionAsDiagnostics: true));
}
}
......
......@@ -2,7 +2,6 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Threading;
using Microsoft.CodeAnalysis.CSharp.Diagnostics;
......@@ -19,7 +18,7 @@ namespace Microsoft.CodeAnalysis.UnitTests.Diagnostics
{
public static class DiagnosticProviderTestUtilities
{
private static IEnumerable<Diagnostic> GetDiagnostics(DiagnosticAnalyzer analyzerOpt, Document document, TextSpan span, Project project, bool getDocumentDiagnostics, bool getProjectDiagnostics, bool donotCatchAnalyzerExceptions)
private static IEnumerable<Diagnostic> GetDiagnostics(DiagnosticAnalyzer analyzerOpt, Document document, TextSpan span, Project project, bool getDocumentDiagnostics, bool getProjectDiagnostics, Action<Exception, DiagnosticAnalyzer, Diagnostic> onAnalyzerException, bool logAnalyzerExceptionAsDiagnostics)
{
var documentDiagnostics = SpecializedCollections.EmptyEnumerable<Diagnostic>();
var projectDiagnostics = SpecializedCollections.EmptyEnumerable<Diagnostic>();
......@@ -27,6 +26,12 @@ private static IEnumerable<Diagnostic> GetDiagnostics(DiagnosticAnalyzer analyze
// If no user diagnostic analyzer, then test compiler diagnostics.
var analyzer = analyzerOpt ?? DiagnosticExtensions.GetCompilerDiagnosticAnalyzer(project.Language);
// If the test is not configured with a custom onAnalyzerException handler AND has not requested exceptions to be handled and logged as diagnostics, then FailFast on exceptions.
if (onAnalyzerException != null && !logAnalyzerExceptionAsDiagnostics)
{
onAnalyzerException = DiagnosticExtensions.FailFastOnAnalyzerException;
}
var exceptionDiagnosticsSource = new TestHostDiagnosticUpdateSource(project.Solution.Workspace);
if (getDocumentDiagnostics)
......@@ -48,8 +53,7 @@ private static IEnumerable<Diagnostic> GetDiagnostics(DiagnosticAnalyzer analyze
var driver = new DiagnosticAnalyzerDriver(document, spanToTest, root,
syntaxNodeAnalyzerService: nodeInBodyAnalyzerService,
hostDiagnosticUpdateSource: exceptionDiagnosticsSource,
cancellationToken: CancellationToken.None,
testOnly_RethrowAnalyzerException: donotCatchAnalyzerExceptions);
overriddenOnAnalyzerException: onAnalyzerException);
var diagnosticAnalyzerCategory = analyzer.GetDiagnosticAnalyzerCategory(driver);
bool supportsSemanticInSpan = (diagnosticAnalyzerCategory & DiagnosticAnalyzerCategory.SemanticSpanAnalysis) != 0;
if (supportsSemanticInSpan)
......@@ -84,7 +88,7 @@ private static IEnumerable<Diagnostic> GetDiagnostics(DiagnosticAnalyzer analyze
var nodeInBodyAnalyzerService = project.Language == LanguageNames.CSharp ?
(ISyntaxNodeAnalyzerService)new CSharpSyntaxNodeAnalyzerService() :
new VisualBasicSyntaxNodeAnalyzerService();
var driver = new DiagnosticAnalyzerDriver(project, nodeInBodyAnalyzerService, exceptionDiagnosticsSource, CancellationToken.None);
var driver = new DiagnosticAnalyzerDriver(project, nodeInBodyAnalyzerService, exceptionDiagnosticsSource, overriddenOnAnalyzerException: onAnalyzerException);
if (analyzer.SupportsProjectDiagnosticAnalysis(driver))
{
......@@ -97,46 +101,46 @@ private static IEnumerable<Diagnostic> GetDiagnostics(DiagnosticAnalyzer analyze
return documentDiagnostics.Concat(projectDiagnostics).Concat(exceptionDiagnostics);
}
public static IEnumerable<Diagnostic> GetAllDiagnostics(DiagnosticAnalyzer providerOpt, Document document, TextSpan span, bool donotCatchAnalyzerExceptions = true)
public static IEnumerable<Diagnostic> GetAllDiagnostics(DiagnosticAnalyzer providerOpt, Document document, TextSpan span, Action<Exception, DiagnosticAnalyzer, Diagnostic> onAnalyzerException = null, bool logAnalyzerExceptionAsDiagnostics = false)
{
return GetDiagnostics(providerOpt, document, span, document.Project, getDocumentDiagnostics: true, getProjectDiagnostics: true, donotCatchAnalyzerExceptions: donotCatchAnalyzerExceptions);
return GetDiagnostics(providerOpt, document, span, document.Project, getDocumentDiagnostics: true, getProjectDiagnostics: true, onAnalyzerException: onAnalyzerException, logAnalyzerExceptionAsDiagnostics: logAnalyzerExceptionAsDiagnostics);
}
public static IEnumerable<Diagnostic> GetAllDiagnostics(DiagnosticAnalyzer providerOpt, Project project, bool donotCatchAnalyzerExceptions = true)
public static IEnumerable<Diagnostic> GetAllDiagnostics(DiagnosticAnalyzer providerOpt, Project project, Action<Exception, DiagnosticAnalyzer, Diagnostic> onAnalyzerException = null, bool logAnalyzerExceptionAsDiagnostics = false)
{
var diagnostics = new List<Diagnostic>();
foreach (var document in project.Documents)
{
var span = document.GetSyntaxRootAsync().Result.FullSpan;
var documentDiagnostics = GetDocumentDiagnostics(providerOpt, document, span, donotCatchAnalyzerExceptions);
var documentDiagnostics = GetDocumentDiagnostics(providerOpt, document, span, onAnalyzerException, logAnalyzerExceptionAsDiagnostics);
diagnostics.AddRange(documentDiagnostics);
}
var projectDiagnostics = GetProjectDiagnostics(providerOpt, project, donotCatchAnalyzerExceptions);
var projectDiagnostics = GetProjectDiagnostics(providerOpt, project, onAnalyzerException, logAnalyzerExceptionAsDiagnostics);
diagnostics.AddRange(projectDiagnostics);
return diagnostics;
}
public static IEnumerable<Diagnostic> GetAllDiagnostics(DiagnosticAnalyzer providerOpt, Solution solution, bool donotCatchAnalyzerExceptions = true)
public static IEnumerable<Diagnostic> GetAllDiagnostics(DiagnosticAnalyzer providerOpt, Solution solution, Action<Exception, DiagnosticAnalyzer, Diagnostic> onAnalyzerException = null, bool logAnalyzerExceptionAsDiagnostics = false)
{
var diagnostics = new List<Diagnostic>();
foreach (var project in solution.Projects)
{
var projectDiagnostics = GetAllDiagnostics(providerOpt, project, donotCatchAnalyzerExceptions);
var projectDiagnostics = GetAllDiagnostics(providerOpt, project, onAnalyzerException, logAnalyzerExceptionAsDiagnostics);
diagnostics.AddRange(projectDiagnostics);
}
return diagnostics;
}
public static IEnumerable<Diagnostic> GetDocumentDiagnostics(DiagnosticAnalyzer providerOpt, Document document, TextSpan span, bool donotCatchAnalyzerExceptions = true)
public static IEnumerable<Diagnostic> GetDocumentDiagnostics(DiagnosticAnalyzer providerOpt, Document document, TextSpan span, Action<Exception, DiagnosticAnalyzer, Diagnostic> onAnalyzerException = null, bool logAnalyzerExceptionAsDiagnostics = false)
{
return GetDiagnostics(providerOpt, document, span, document.Project, getDocumentDiagnostics: true, getProjectDiagnostics: false, donotCatchAnalyzerExceptions: donotCatchAnalyzerExceptions);
return GetDiagnostics(providerOpt, document, span, document.Project, getDocumentDiagnostics: true, getProjectDiagnostics: false, onAnalyzerException: onAnalyzerException, logAnalyzerExceptionAsDiagnostics: logAnalyzerExceptionAsDiagnostics);
}
public static IEnumerable<Diagnostic> GetProjectDiagnostics(DiagnosticAnalyzer providerOpt, Project project, bool donotCatchAnalyzerExceptions = true)
public static IEnumerable<Diagnostic> GetProjectDiagnostics(DiagnosticAnalyzer providerOpt, Project project, Action<Exception, DiagnosticAnalyzer, Diagnostic> onAnalyzerException = null, bool logAnalyzerExceptionAsDiagnostics = false)
{
return GetDiagnostics(providerOpt, null, default(TextSpan), project, getDocumentDiagnostics: false, getProjectDiagnostics: true, donotCatchAnalyzerExceptions: donotCatchAnalyzerExceptions);
return GetDiagnostics(providerOpt, null, default(TextSpan), project, getDocumentDiagnostics: false, getProjectDiagnostics: true, onAnalyzerException: onAnalyzerException, logAnalyzerExceptionAsDiagnostics: logAnalyzerExceptionAsDiagnostics);
}
}
}
......@@ -24,7 +24,7 @@ protected override void Verify(string source, string language, DiagnosticAnalyze
var actualDiagnostics = new List<Diagnostic>();
foreach (var analyzer in analyzers)
{
actualDiagnostics.AddRange(DiagnosticProviderTestUtilities.GetAllDiagnostics(analyzer, document, span, donotCatchAnalyzerExceptions: onAnalyzerException == null));
actualDiagnostics.AddRange(DiagnosticProviderTestUtilities.GetAllDiagnostics(analyzer, document, span, onAnalyzerException));
}
actualDiagnostics.Verify(expectedDiagnostics);
......
......@@ -659,7 +659,7 @@ class AnonymousFunctions
' Verify that for an analyzer which has a registered compilation start action such that the start action registered an end action,
' we go and force complete all document diagnostics for entire project and then invoke and report end action diagnostics.
Dim driver = New DiagnosticAnalyzerDriver(project, project.LanguageServices.GetService(Of ISyntaxNodeAnalyzerService)(), Nothing, CancellationToken.None)
Dim driver = New DiagnosticAnalyzerDriver(project, project.LanguageServices.GetService(Of ISyntaxNodeAnalyzerService)(), Nothing)
Dim projectDiagnostics = driver.GetProjectDiagnosticsAsync(analyzer, AddressOf incrementalAnalyzer.ForceAnalyzeAllDocuments).WaitAndGetResult(CancellationToken.None)
Assert.Equal(1, projectDiagnostics.Count())
Dim diagnostic = projectDiagnostics.Single()
......
......@@ -65,7 +65,7 @@ End Class
Using Workspace = VisualBasicWorkspaceFactory.CreateWorkspaceFromFile(source)
Dim document = Workspace.CurrentSolution.Projects.Single().Documents.Single()
ThrowingDiagnosticAnalyzer(Of SyntaxKind).VerifyAnalyzerEngineIsSafeAgainstExceptions(
Function(analyzer) DiagnosticProviderTestUtilities.GetAllDiagnostics(analyzer, document, New TextSpan(0, document.GetTextAsync().Result.Length), donotCatchAnalyzerExceptions:=False))
Function(analyzer) DiagnosticProviderTestUtilities.GetAllDiagnostics(analyzer, document, New TextSpan(0, document.GetTextAsync().Result.Length), logAnalyzerExceptionAsDiagnostics:=True))
End Using
End Sub
......
......@@ -37,14 +37,15 @@ public static bool IsCompilerAnalyzer(DiagnosticAnalyzer analyzer)
internal static AnalyzerExecutor GetAnalyzerExecutorForSupportedDiagnostics(
DiagnosticAnalyzer analyzer,
AbstractHostDiagnosticUpdateSource hostDiagnosticUpdateSource,
Action<Exception, DiagnosticAnalyzer, Diagnostic> onAnalyzerException = null,
CancellationToken cancellationToken = default(CancellationToken))
{
// Skip telemetry logging if the exception is thrown as we are computing supported diagnostics and
// we can't determine if any descriptors support getting telemetry without having the descriptors.
Action<Exception, DiagnosticAnalyzer, Diagnostic> onAnalyzerException = (ex, a, diagnostic) =>
Action<Exception, DiagnosticAnalyzer, Diagnostic> defaultOnAnalyzerException = (ex, a, diagnostic) =>
OnAnalyzerException_NoTelemetryLogging(ex, a, diagnostic, hostDiagnosticUpdateSource);
return AnalyzerExecutor.CreateForSupportedDiagnostics(onAnalyzerException, cancellationToken);
return AnalyzerExecutor.CreateForSupportedDiagnostics(onAnalyzerException ?? defaultOnAnalyzerException, cancellationToken);
}
internal static void OnAnalyzerException_NoTelemetryLogging(
......@@ -52,19 +53,13 @@ public static bool IsCompilerAnalyzer(DiagnosticAnalyzer analyzer)
DiagnosticAnalyzer analyzer,
Diagnostic diagnostic,
AbstractHostDiagnosticUpdateSource hostDiagnosticUpdateSource,
Project projectOpt = null,
bool testOnly_RethrowAnalyzerException = false)
Project projectOpt = null)
{
if (diagnostic != null)
{
hostDiagnosticUpdateSource?.ReportAnalyzerDiagnostic(analyzer, diagnostic, hostDiagnosticUpdateSource?.Workspace, projectOpt);
}
if (testOnly_RethrowAnalyzerException)
{
throw e;
}
if (IsBuiltInAnalyzer(analyzer))
{
FatalError.ReportWithoutCrashUnlessCanceled(e);
......
......@@ -7,7 +7,6 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Diagnostics.Log;
using Microsoft.CodeAnalysis.ErrorReporting;
using Microsoft.CodeAnalysis.GeneratedCodeRecognition;
using Microsoft.CodeAnalysis.Internal.Log;
using Microsoft.CodeAnalysis.LanguageServices;
......@@ -34,7 +33,9 @@ internal class DiagnosticAnalyzerDriver
private readonly ISyntaxFactsService _syntaxFacts;
private readonly IGeneratedCodeRecognitionService _generatedCodeService;
private readonly IAnalyzerDriverService _analyzerDriverService;
private readonly bool _testOnly_RethrowAnalyzerException;
private readonly Action<Exception, DiagnosticAnalyzer, Diagnostic> _onAnalyzerException;
private readonly Action<Exception, DiagnosticAnalyzer, Diagnostic> _onAnalyzerException_NoTelemetryLogging;
private LogAggregator _logAggregator;
......@@ -45,13 +46,13 @@ internal class DiagnosticAnalyzerDriver
private AnalyzerOptions _analyzerOptions = null;
public DiagnosticAnalyzerDriver(Document document, TextSpan? span, SyntaxNode root, LogAggregator logAggregator, AbstractHostDiagnosticUpdateSource hostDiagnosticUpdateSource, CancellationToken cancellationToken)
: this(document, span, root, document.Project.LanguageServices.GetService<ISyntaxNodeAnalyzerService>(), hostDiagnosticUpdateSource, cancellationToken)
: this(document, span, root, document.Project.LanguageServices.GetService<ISyntaxNodeAnalyzerService>(), hostDiagnosticUpdateSource, overriddenOnAnalyzerException: null, cancellationToken: cancellationToken)
{
_logAggregator = logAggregator;
}
public DiagnosticAnalyzerDriver(Project project, LogAggregator logAggregator, AbstractHostDiagnosticUpdateSource hostDiagnosticUpdateSource, CancellationToken cancellationToken)
: this(project, project.LanguageServices.GetService<ISyntaxNodeAnalyzerService>(), hostDiagnosticUpdateSource, cancellationToken)
: this(project, project.LanguageServices.GetService<ISyntaxNodeAnalyzerService>(), hostDiagnosticUpdateSource, overriddenOnAnalyzerException: null, cancellationToken: cancellationToken)
{
_logAggregator = logAggregator;
}
......@@ -63,8 +64,8 @@ public DiagnosticAnalyzerDriver(Project project, LogAggregator logAggregator, Ab
SyntaxNode root,
ISyntaxNodeAnalyzerService syntaxNodeAnalyzerService,
AbstractHostDiagnosticUpdateSource hostDiagnosticUpdateSource,
CancellationToken cancellationToken,
bool testOnly_RethrowAnalyzerException = false)
Action<Exception, DiagnosticAnalyzer, Diagnostic> overriddenOnAnalyzerException = null,
CancellationToken cancellationToken = default(CancellationToken))
{
_document = document;
_span = span;
......@@ -78,15 +79,17 @@ public DiagnosticAnalyzerDriver(Project project, LogAggregator logAggregator, Ab
_generatedCodeService = document.Project.Solution.Workspace.Services.GetService<IGeneratedCodeRecognitionService>();
_analyzerDriverService = document.Project.LanguageServices.GetService<IAnalyzerDriverService>();
_analyzerOptions = new WorkspaceAnalyzerOptions(_project.AnalyzerOptions, _project.Solution.Workspace);
_testOnly_RethrowAnalyzerException = testOnly_RethrowAnalyzerException;
_onAnalyzerException = overriddenOnAnalyzerException ?? Default_OnAnalyzerException;
_onAnalyzerException_NoTelemetryLogging = overriddenOnAnalyzerException ?? Default_OnAnalyzerException_NoTelemetryLogging;
}
// internal for testing purposes
internal DiagnosticAnalyzerDriver(
Project project,
ISyntaxNodeAnalyzerService syntaxNodeAnalyzerService,
AbstractHostDiagnosticUpdateSource hostDiagnosticUpdateSource,
CancellationToken cancellationToken)
Project project,
ISyntaxNodeAnalyzerService syntaxNodeAnalyzerService,
AbstractHostDiagnosticUpdateSource hostDiagnosticUpdateSource,
Action<Exception, DiagnosticAnalyzer, Diagnostic> overriddenOnAnalyzerException = null,
CancellationToken cancellationToken = default(CancellationToken))
{
_project = project;
_cancellationToken = cancellationToken;
......@@ -96,6 +99,8 @@ public DiagnosticAnalyzerDriver(Project project, LogAggregator logAggregator, Ab
_hostDiagnosticUpdateSource = hostDiagnosticUpdateSource;
_descendantExecutableNodesMap = null;
_analyzerOptions = _project.AnalyzerOptions;
_onAnalyzerException = overriddenOnAnalyzerException ?? Default_OnAnalyzerException;
_onAnalyzerException_NoTelemetryLogging = overriddenOnAnalyzerException ?? Default_OnAnalyzerException_NoTelemetryLogging;
}
public Document Document
......@@ -314,14 +319,14 @@ internal void OnAnalyzerException(Exception ex, DiagnosticAnalyzer analyzer, Com
if (compilation != null)
{
exceptionDiagnostic = CompilationWithAnalyzers.GetEffectiveDiagnostics(ImmutableArray.Create(exceptionDiagnostic), compilation).SingleOrDefault();
}
}
OnAnalyzerException(ex, analyzer, exceptionDiagnostic);
_onAnalyzerException(ex, analyzer, exceptionDiagnostic);
}
private AnalyzerExecutor GetAnalyzerExecutor(DiagnosticAnalyzer analyzer, Compilation compilation, Action<Diagnostic> addDiagnostic)
{
return AnalyzerExecutor.Create(compilation, _analyzerOptions, addDiagnostic, OnAnalyzerException, _cancellationToken);
return AnalyzerExecutor.Create(compilation, _analyzerOptions, addDiagnostic, _onAnalyzerException, _cancellationToken);
}
public async Task<AnalyzerActions> GetAnalyzerActionsAsync(DiagnosticAnalyzer analyzer)
......@@ -339,7 +344,7 @@ public bool IsAnalyzerSuppressed(DiagnosticAnalyzer analyzer)
return false;
}
var analyzerExecutor = AnalyzerHelper.GetAnalyzerExecutorForSupportedDiagnostics(analyzer, _hostDiagnosticUpdateSource);
var analyzerExecutor = AnalyzerHelper.GetAnalyzerExecutorForSupportedDiagnostics(analyzer, _hostDiagnosticUpdateSource, _onAnalyzerException_NoTelemetryLogging, _cancellationToken);
return AnalyzerManager.Instance.IsDiagnosticAnalyzerSuppressed(analyzer, options, AnalyzerHelper.IsCompilerAnalyzer, analyzerExecutor);
}
......@@ -376,7 +381,7 @@ public async Task<ImmutableArray<Diagnostic>> GetSemanticDiagnosticsAsync(Diagno
await documentAnalyzer.AnalyzeSemanticsAsync(_document, diagnostics.Add, _cancellationToken).ConfigureAwait(false);
}
catch (Exception e)
{
{
OnAnalyzerException(e, analyzer, compilation);
return ImmutableArray<Diagnostic>.Empty;
}
......@@ -425,7 +430,7 @@ public async Task<ImmutableArray<Diagnostic>> GetProjectDiagnosticsAsync(Diagnos
{
Contract.ThrowIfNull(_project);
Contract.ThrowIfFalse(_document == null);
using (var diagnostics = SharedPools.Default<List<Diagnostic>>().GetPooledObject())
{
if (_project.SupportsCompilation)
......@@ -452,8 +457,8 @@ private async Task GetProjectDiagnosticsWorkerAsync(DiagnosticAnalyzer analyzer,
await projectAnalyzer.AnalyzeProjectAsync(_project, diagnostics.Add, _cancellationToken).ConfigureAwait(false);
}
catch (Exception e)
{
var compilation = await _project.GetCompilationAsync(_cancellationToken).ConfigureAwait(false);
{
var compilation = await _project.GetCompilationAsync(_cancellationToken).ConfigureAwait(false);
OnAnalyzerException(e, analyzer, compilation);
}
}
......@@ -504,21 +509,17 @@ private static bool IsCanceled(Exception e, CancellationToken cancellationToken)
return canceled != null && canceled.CancellationToken == cancellationToken;
}
private void OnAnalyzerException(Exception e, DiagnosticAnalyzer analyzer, Diagnostic diagnostic)
{
OnAnalyzerException(e, analyzer, diagnostic, _testOnly_RethrowAnalyzerException);
}
private void OnAnalyzerException_NoTelemetryLogging(Exception e, DiagnosticAnalyzer analyzer, Diagnostic diagnostic)
private void Default_OnAnalyzerException_NoTelemetryLogging(Exception e, DiagnosticAnalyzer analyzer, Diagnostic diagnostic)
{
AnalyzerHelper.OnAnalyzerException_NoTelemetryLogging(e, analyzer, diagnostic, _hostDiagnosticUpdateSource, _project, _testOnly_RethrowAnalyzerException);
AnalyzerHelper.OnAnalyzerException_NoTelemetryLogging(e, analyzer, diagnostic, _hostDiagnosticUpdateSource, _project);
}
private void OnAnalyzerException(Exception e, DiagnosticAnalyzer analyzer, Diagnostic diagnostic, bool testOnly_RethrowAnalyzerException)
private void Default_OnAnalyzerException(Exception e, DiagnosticAnalyzer analyzer, Diagnostic diagnostic)
{
// Log telemetry, if analyzer supports telemetry.
DiagnosticAnalyzerLogger.LogAnalyzerCrashCount(analyzer, e, _logAggregator);
AnalyzerHelper.OnAnalyzerException_NoTelemetryLogging(e, analyzer, diagnostic, _hostDiagnosticUpdateSource, _project, testOnly_RethrowAnalyzerException);
Default_OnAnalyzerException_NoTelemetryLogging(e, analyzer, diagnostic);
}
}
}
......@@ -20,8 +20,9 @@ namespace Microsoft.CodeAnalysis
public static class DiagnosticExtensions
{
private const int EN_US = 1033;
public static Action<Exception, DiagnosticAnalyzer, Diagnostic> AlwaysCatchAnalyzerException = (e, a, d) => { };
public static Action<Exception, DiagnosticAnalyzer, Diagnostic> RethrowAnalyzerException = (e, a, d) => { if (!(e is OperationCanceledException)) throw e; };
public static Action<Exception, DiagnosticAnalyzer, Diagnostic> CatchAndIgnoreAnalyzerException = (e, a, d) => { };
public static Action<Exception, DiagnosticAnalyzer, Diagnostic> FailFastOnAnalyzerException = (e, a, d) => FailFast.OnFatalException(e);
/// <summary>
/// This is obsolete. Use Verify instead.
......@@ -160,7 +161,7 @@ public static TCompilation VerifyDiagnostics<TCompilation>(this TCompilation c,
else
{
// We want unit tests to throw if any analyzer OR the driver throws, unless the test explicitly provides a delegate.
newOnAnalyzerException = RethrowAnalyzerException;
newOnAnalyzerException = FailFastOnAnalyzerException;
}
Compilation newCompilation;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册