-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
Handle unsupported diagnostics reported by analyzers #1286
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,10 +24,12 @@ internal class AnalyzerExecutor | |
private readonly AnalyzerOptions _analyzerOptions; | ||
private readonly Action<Diagnostic> _addDiagnostic; | ||
private readonly Action<Exception, DiagnosticAnalyzer, Diagnostic> _onAnalyzerException; | ||
private readonly AnalyzerManager _analyzerManager; | ||
private readonly Func<DiagnosticAnalyzer, bool> _isCompilerAnalyzer; | ||
private readonly CancellationToken _cancellationToken; | ||
|
||
/// <summary> | ||
/// Creates AnalyzerActionsExecutor to execute analyzer actions with given arguments | ||
/// Creates <see cref="AnalyzerExecutor"/> to execute analyzer actions with given arguments | ||
/// </summary> | ||
/// <param name="compilation">Compilation to be used in the analysis.</param> | ||
/// <param name="analyzerOptions">Analyzer options.</param> | ||
|
@@ -36,34 +38,43 @@ internal class AnalyzerExecutor | |
/// Optional delegate which is invoked when an analyzer throws an exception. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The same question about |
||
/// Delegate can do custom tasks such as report the given analyzer exception diagnostic, report a non-fatal watson for the exception, etc. | ||
/// </param> | ||
/// <param name="isCompilerAnalyzer">Delegate to determine if the given analyzer is compiler analyzer. | ||
/// We need to special case the compiler analyzer at few places for performance reasons.</param> | ||
/// <param name="analyzerManager">Analyzer manager to fetch supported diagnostics.</param> | ||
/// <param name="cancellationToken">Cancellation token.</param> | ||
public static AnalyzerExecutor Create( | ||
Compilation compilation, | ||
AnalyzerOptions analyzerOptions, | ||
Action<Diagnostic> addDiagnostic, | ||
Action<Exception, DiagnosticAnalyzer, Diagnostic> onAnalyzerException, | ||
Func<DiagnosticAnalyzer, bool> isCompilerAnalyzer, | ||
AnalyzerManager analyzerManager, | ||
CancellationToken cancellationToken) | ||
{ | ||
return new AnalyzerExecutor(compilation, analyzerOptions, addDiagnostic, onAnalyzerException, cancellationToken); | ||
return new AnalyzerExecutor(compilation, analyzerOptions, addDiagnostic, onAnalyzerException, isCompilerAnalyzer, analyzerManager, cancellationToken); | ||
} | ||
|
||
/// <summary> | ||
/// Creates AnalyzerActionsExecutor to fetch <see cref="DiagnosticAnalyzer.SupportedDiagnostics"/>. | ||
/// Creates <see cref="AnalyzerExecutor"/> to fetch <see cref="DiagnosticAnalyzer.SupportedDiagnostics"/>. | ||
/// </summary> | ||
/// <param name="onAnalyzerException"> | ||
/// Optional delegate which is invoked when an analyzer throws an exception. | ||
/// Delegate can do custom tasks such as report the given analyzer exception diagnostic, report a non-fatal watson for the exception, etc. | ||
/// </param> | ||
/// <param name="analyzerManager">Analyzer manager to fetch supported diagnostics.</param> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is the manager providing supported diagnostics or diagnostic identifiers? |
||
/// <param name="cancellationToken">Cancellation token.</param> | ||
public static AnalyzerExecutor CreateForSupportedDiagnostics( | ||
Action<Exception, DiagnosticAnalyzer, Diagnostic> onAnalyzerException, | ||
AnalyzerManager analyzerManager, | ||
CancellationToken cancellationToken) | ||
{ | ||
return new AnalyzerExecutor( | ||
compilation: null, | ||
analyzerOptions: null, | ||
addDiagnostic: null, | ||
isCompilerAnalyzer: null, | ||
onAnalyzerException: onAnalyzerException, | ||
analyzerManager: analyzerManager, | ||
cancellationToken: cancellationToken); | ||
} | ||
|
||
|
@@ -72,12 +83,16 @@ private AnalyzerExecutor( | |
AnalyzerOptions analyzerOptions, | ||
Action<Diagnostic> addDiagnostic, | ||
Action<Exception, DiagnosticAnalyzer, Diagnostic> onAnalyzerException, | ||
Func<DiagnosticAnalyzer, bool> isCompilerAnalyzer, | ||
AnalyzerManager analyzerManager, | ||
CancellationToken cancellationToken) | ||
{ | ||
_compilation = compilation; | ||
_analyzerOptions = analyzerOptions; | ||
_addDiagnostic = addDiagnostic; | ||
_onAnalyzerException = onAnalyzerException; | ||
_isCompilerAnalyzer = isCompilerAnalyzer; | ||
_analyzerManager = analyzerManager; | ||
_cancellationToken = cancellationToken; | ||
} | ||
|
||
|
@@ -127,7 +142,8 @@ public void ExecuteCompilationActions(ImmutableArray<CompilationAnalyzerAction> | |
{ | ||
_cancellationToken.ThrowIfCancellationRequested(); | ||
ExecuteAndCatchIfThrows(endAction.Analyzer, | ||
() => endAction.Action(new CompilationAnalysisContext(_compilation, _analyzerOptions, _addDiagnostic, _cancellationToken))); | ||
() => endAction.Action(new CompilationAnalysisContext(_compilation, _analyzerOptions, _addDiagnostic, | ||
d => IsSupportedDiagnostic(endAction.Analyzer, d), _cancellationToken))); | ||
} | ||
} | ||
|
||
|
@@ -172,7 +188,8 @@ public void ExecuteSymbolActions(ImmutableArray<SymbolAnalyzerAction> symbolActi | |
{ | ||
_cancellationToken.ThrowIfCancellationRequested(); | ||
ExecuteAndCatchIfThrows(symbolAction.Analyzer, | ||
() => action(new SymbolAnalysisContext(symbol, _compilation, _analyzerOptions, addDiagnostic, _cancellationToken))); | ||
() => action(new SymbolAnalysisContext(symbol, _compilation, _analyzerOptions, addDiagnostic, | ||
d => IsSupportedDiagnostic(symbolAction.Analyzer, d), _cancellationToken))); | ||
} | ||
} | ||
} | ||
|
@@ -200,7 +217,8 @@ public void ExecuteSemanticModelActions(ImmutableArray<SemanticModelAnalyzerActi | |
|
||
// Catch Exception from action. | ||
ExecuteAndCatchIfThrows(semanticModelAction.Analyzer, | ||
() => semanticModelAction.Action(new SemanticModelAnalysisContext(semanticModel, _analyzerOptions, _addDiagnostic, _cancellationToken))); | ||
() => semanticModelAction.Action(new SemanticModelAnalysisContext(semanticModel, _analyzerOptions, _addDiagnostic, | ||
d => IsSupportedDiagnostic(semanticModelAction.Analyzer, d), _cancellationToken))); | ||
} | ||
} | ||
|
||
|
@@ -227,7 +245,8 @@ public void ExecuteSyntaxTreeActions(ImmutableArray<SyntaxTreeAnalyzerAction> sy | |
|
||
// Catch Exception from action. | ||
ExecuteAndCatchIfThrows(syntaxTreeAction.Analyzer, | ||
() => syntaxTreeAction.Action(new SyntaxTreeAnalysisContext(tree, _analyzerOptions, _addDiagnostic, _cancellationToken))); | ||
() => syntaxTreeAction.Action(new SyntaxTreeAnalysisContext(tree, _analyzerOptions, _addDiagnostic, | ||
d => IsSupportedDiagnostic(syntaxTreeAction.Analyzer, d), _cancellationToken))); | ||
} | ||
} | ||
|
||
|
@@ -265,7 +284,8 @@ private void ExecuteSyntaxNodeAction<TLanguageKindEnum>( | |
SemanticModel semanticModel) | ||
where TLanguageKindEnum : struct | ||
{ | ||
var syntaxNodeContext = new SyntaxNodeAnalysisContext(node, semanticModel, _analyzerOptions, _addDiagnostic, _cancellationToken); | ||
var syntaxNodeContext = new SyntaxNodeAnalysisContext(node, semanticModel, _analyzerOptions, _addDiagnostic, | ||
d => IsSupportedDiagnostic(syntaxNodeAction.Analyzer, d), _cancellationToken); | ||
ExecuteAndCatchIfThrows(syntaxNodeAction.Analyzer, () => syntaxNodeAction.Action(syntaxNodeContext)); | ||
} | ||
|
||
|
@@ -369,7 +389,8 @@ private void ExecuteCodeBlockActions(PooledHashSet<CodeBlockAnalyzerAction> bloc | |
foreach (var blockAction in blockActions) | ||
{ | ||
ExecuteAndCatchIfThrows(blockAction.Analyzer, | ||
() => blockAction.Action(new CodeBlockAnalysisContext(declaredNode, declaredSymbol, semanticModel, _analyzerOptions, _addDiagnostic, _cancellationToken))); | ||
() => blockAction.Action(new CodeBlockAnalysisContext(declaredNode, declaredSymbol, semanticModel, _analyzerOptions, _addDiagnostic, | ||
d => IsSupportedDiagnostic(blockAction.Analyzer, d), _cancellationToken))); | ||
} | ||
|
||
blockActions.Free(); | ||
|
@@ -513,5 +534,12 @@ internal static bool IsAnalyzerExceptionDiagnostic(Diagnostic diagnostic) | |
|
||
return false; | ||
} | ||
|
||
private bool IsSupportedDiagnostic(DiagnosticAnalyzer analyzer, Diagnostic diagnostic) | ||
{ | ||
Debug.Assert(_isCompilerAnalyzer != null); | ||
|
||
return _analyzerManager.IsSupportedDiagnostic(analyzer, diagnostic, _isCompilerAnalyzer, this); | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the name
AnalyzerActionsExecutor
in the<summary>
element above up-to-date? Consider usingcref
for it.