提交 072a536f 编写于 作者: M Manish Vasani

Merge pull request #1286 from mavasani/Issue252

Handle unsupported diagnostics reported by analyzers.

Fixes #252 : If an analyzer reports a diagnostic with an unsupported diagnostic ID, i.e. no descriptor returned by SupportedDiagnostics has that ID, then throw an ArgumentException in ReportDiagnostic method. This exception would be turned into an analyzer diagnostic by the driver and reported back to the analyzer host.

Also fix a few tests that were reporting diagnostics with unsupported ID!
......@@ -133,7 +133,7 @@ internal override Diagnostic WithSeverity(DiagnosticSeverity severity)
private class ComplainAboutX : DiagnosticAnalyzer
{
private static readonly DiagnosticDescriptor s_CA9999_UseOfVariableThatStartsWithX =
new DiagnosticDescriptor(id: "CA9999", title: "CA9999_UseOfVariableThatStartsWithX", messageFormat: "Use of variable whose name starts with 'x': '{0}'", category: "Test", defaultSeverity: DiagnosticSeverity.Warning, isEnabledByDefault: true);
new DiagnosticDescriptor(id: "CA9999_UseOfVariableThatStartsWithX", title: "CA9999_UseOfVariableThatStartsWithX", messageFormat: "Use of variable whose name starts with 'x': '{0}'", category: "Test", defaultSeverity: DiagnosticSeverity.Warning, isEnabledByDefault: true);
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics
{
......@@ -153,7 +153,7 @@ private static void AnalyzeNode(SyntaxNodeAnalysisContext context)
var id = (IdentifierNameSyntax)context.Node;
if (id.Identifier.ValueText.StartsWith("x", StringComparison.Ordinal))
{
context.ReportDiagnostic(new TestDiagnostic("CA9999_UseOfVariableThatStartsWithX", "CsTest", DiagnosticSeverity.Warning, id.Location, "Use of variable whose name starts with 'x': '{0}'", id.Identifier.ValueText));
context.ReportDiagnostic(CodeAnalysis.Diagnostic.Create(s_CA9999_UseOfVariableThatStartsWithX, id.Location, id.Identifier.ValueText));
}
}
}
......@@ -935,5 +935,45 @@ public override void Initialize(AnalysisContext context)
}, SymbolKind.Method);
}
}
[Fact, WorkItem(252, "https://github.com/dotnet/roslyn/issues/252")]
public void TestReportingUnsupportedDiagnostic()
{
string source = @"";
var analyzers = new DiagnosticAnalyzer[] { new AnalyzerReportingUnsupportedDiagnostic() };
CreateCompilationWithMscorlib45(source)
.VerifyDiagnostics()
.VerifyAnalyzerDiagnostics(analyzers, null, null, logAnalyzerExceptionAsDiagnostics: true,
expected: Diagnostic("AD0001")
.WithArguments("Microsoft.CodeAnalysis.CSharp.UnitTests.DiagnosticAnalyzerTests+AnalyzerReportingUnsupportedDiagnostic",
@"Reported diagnostic with ID 'ID_2' is not supported by the analyzer.
Parameter name: diagnostic")
.WithLocation(1, 1));
}
[DiagnosticAnalyzer(LanguageNames.CSharp, LanguageNames.VisualBasic)]
public class AnalyzerReportingUnsupportedDiagnostic : DiagnosticAnalyzer
{
public static readonly DiagnosticDescriptor SupportedDescriptor =
new DiagnosticDescriptor("ID_1", "DummyTitle", "DummyMessage", "DummyCategory", DiagnosticSeverity.Warning, isEnabledByDefault: true);
public static readonly DiagnosticDescriptor UnsupportedDescriptor =
new DiagnosticDescriptor("ID_2", "DummyTitle", "DummyMessage", "DummyCategory", DiagnosticSeverity.Warning, isEnabledByDefault: true);
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics
{
get
{
return ImmutableArray.Create(SupportedDescriptor);
}
}
public override void Initialize(AnalysisContext context)
{
context.RegisterCompilationAction(compilationContext =>
compilationContext.ReportDiagnostic(CodeAnalysis.Diagnostic.Create(UnsupportedDescriptor, Location.None)));
}
}
}
}
......@@ -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.
/// 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>
/// <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 @@ internal class 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 @@ public void ExecuteSyntaxTreeActions(ImmutableArray<SyntaxTreeAnalyzerAction> sy
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);
}
}
}
......@@ -223,6 +223,28 @@ internal void ClearAnalyzerExceptionHandlers(DiagnosticAnalyzer analyzer)
}
}
internal bool IsSupportedDiagnostic(DiagnosticAnalyzer analyzer, Diagnostic diagnostic, Func<DiagnosticAnalyzer, bool> isCompilerAnalyzer, AnalyzerExecutor analyzerExecutor)
{
// Avoid realizing all the descriptors for all compiler diagnostics by assuming that compiler analyzer doesn't report unsupported diagnostics.
if (isCompilerAnalyzer(analyzer))
{
return true;
}
// Get all the supported diagnostics and scan them linearly to see if the reported diagnostic is supported by the analyzer.
// The linear scan is okay, given that this runs only if a diagnostic is being reported and a given analyzer is quite unlikely to have hundreds of thousands of supported diagnostics.
var supportedDescriptors = GetSupportedDiagnosticDescriptors(analyzer, analyzerExecutor);
foreach (var descriptor in supportedDescriptors)
{
if (descriptor.Id.Equals(diagnostic.Id, StringComparison.OrdinalIgnoreCase))
{
return true;
}
}
return false;
}
/// <summary>
/// Returns true if all the diagnostics that can be produced by this analyzer are suppressed through options.
/// </summary>
......
......@@ -25,12 +25,17 @@ internal static void VerifyArguments<TContext>(Action<TContext> action, Immutabl
VerifySyntaxKinds(syntaxKinds);
}
internal static void VerifyArguments(Diagnostic diagnostic)
internal static void VerifyArguments(Diagnostic diagnostic, Func<Diagnostic, bool> isSupportedDiagnostic)
{
if (diagnostic == null)
{
throw new ArgumentNullException(nameof(diagnostic));
}
if (!isSupportedDiagnostic(diagnostic))
{
throw new ArgumentException(string.Format(AnalyzerDriverResources.UnsupportedDiagnosticReported, diagnostic.Id), nameof(diagnostic));
}
}
private static void VerifyAction<TContext>(Action<TContext> action)
......
......@@ -276,7 +276,7 @@ private static void TestDescriptorIsExceptionSafeCore(DiagnosticDescriptor descr
var analyzer = new MyAnalyzer(descriptor);
var exceptionDiagnostics = new List<Diagnostic>();
Action<Exception, DiagnosticAnalyzer, Diagnostic> onAnalyzerException = (ex, a, diag) => exceptionDiagnostics.Add(diag);
var analyzerExecutor = AnalyzerExecutor.CreateForSupportedDiagnostics(onAnalyzerException, CancellationToken.None);
var analyzerExecutor = AnalyzerExecutor.CreateForSupportedDiagnostics(onAnalyzerException, AnalyzerManager.Instance, CancellationToken.None);
var descriptors = AnalyzerManager.Instance.GetSupportedDiagnosticDescriptors(analyzer, analyzerExecutor);
Assert.Equal(1, descriptors.Length);
......
......@@ -970,6 +970,15 @@ internal class CodeAnalysisResources {
}
}
/// <summary>
/// Looks up a localized string similar to Reported diagnostic with ID &apos;{0}&apos; is not supported by the analyzer..
/// </summary>
internal static string UnsupportedDiagnosticReported {
get {
return ResourceManager.GetString("UnsupportedDiagnosticReported", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Unsupported hash algorithm..
/// </summary>
......
......@@ -402,6 +402,9 @@
<data name="ArgumentElementCannotBeNull" xml:space="preserve">
<value>Argument cannot have a null element.</value>
</data>
<data name="UnsupportedDiagnosticReported" xml:space="preserve">
<value>Reported diagnostic with ID '{0}' is not supported by the analyzer.</value>
</data>
<data name="NoBinderException" xml:space="preserve">
<value>Cannot deserialize type '{0}', no binder supplied.</value>
</data>
......
......@@ -215,7 +215,7 @@ private Task ExecuteSyntaxTreeActions(CancellationToken cancellationToken)
newOnAnalyzerException = (ex, analyzer, diagnostic) => addDiagnostic(diagnostic);
}
var analyzerExecutor = AnalyzerExecutor.Create(newCompilation, options, addDiagnostic, newOnAnalyzerException, cancellationToken);
var analyzerExecutor = AnalyzerExecutor.Create(newCompilation, options, addDiagnostic, newOnAnalyzerException, IsCompilerAnalyzer, analyzerManager, cancellationToken);
analyzerDriver.Initialize(newCompilation, analyzerExecutor, cancellationToken);
......@@ -969,5 +969,6 @@ internal static class AnalyzerDriverResources
internal static string DiagnosticDescriptorThrows => CodeAnalysisResources.DiagnosticDescriptorThrows;
internal static string ArgumentElementCannotBeNull => CodeAnalysisResources.ArgumentElementCannotBeNull;
internal static string ArgumentCannotBeEmpty => CodeAnalysisResources.ArgumentCannotBeEmpty;
internal static string UnsupportedDiagnosticReported => CodeAnalysisResources.UnsupportedDiagnosticReported;
}
}
......@@ -120,7 +120,7 @@ public static bool IsDiagnosticAnalyzerSuppressed(DiagnosticAnalyzer analyzer, C
Action<Exception, DiagnosticAnalyzer, Diagnostic> voidHandler = (ex, a, diag) => { };
onAnalyzerException = onAnalyzerException ?? voidHandler;
var analyzerExecutor = AnalyzerExecutor.CreateForSupportedDiagnostics(onAnalyzerException, CancellationToken.None);
var analyzerExecutor = AnalyzerExecutor.CreateForSupportedDiagnostics(onAnalyzerException, AnalyzerManager.Instance, CancellationToken.None);
return AnalyzerDriver.IsDiagnosticAnalyzerSuppressed(analyzer, options, AnalyzerManager.Instance, analyzerExecutor);
}
......
......@@ -94,7 +94,7 @@ public void RegisterSymbolAction(Action<SymbolAnalysisContext> action, params Sy
/// A code block action reports <see cref="Diagnostic"/>s about code blocks.
/// </summary>
/// <param name="action">Action to be executed for a code block.</param>
public abstract void RegisterCodeBlockAction(Action<CodeBlockAnalysisContext> action);
public abstract void RegisterCodeBlockAction(Action<CodeBlockAnalysisContext> action);
/// <summary>
/// Register an action to be executed at completion of parsing of a code document.
......@@ -224,7 +224,7 @@ public void RegisterSymbolAction(Action<SymbolAnalysisContext> action, params Sy
/// <typeparam name="TLanguageKindEnum">Enum type giving the syntax node kinds of the source language for which the action applies.</typeparam>
/// <param name="action">Action to be executed at the start of semantic analysis of a code block.</param>
public abstract void RegisterCodeBlockStartAction<TLanguageKindEnum>(Action<CodeBlockStartAnalysisContext<TLanguageKindEnum>> action) where TLanguageKindEnum : struct;
/// <summary>
/// Register an action to be executed at the end of semantic analysis of a method body or an expression appearing outside a method body.
/// A code block action reports <see cref="Diagnostic"/>s about code blocks.
......@@ -272,6 +272,7 @@ public struct CompilationAnalysisContext
private readonly Compilation _compilation;
private readonly AnalyzerOptions _options;
private readonly Action<Diagnostic> _reportDiagnostic;
private readonly Func<Diagnostic, bool> _isSupportedDiagnostic;
private readonly CancellationToken _cancellationToken;
/// <summary>
......@@ -289,11 +290,12 @@ public struct CompilationAnalysisContext
/// </summary>
public CancellationToken CancellationToken { get { return _cancellationToken; } }
public CompilationAnalysisContext(Compilation compilation, AnalyzerOptions options, Action<Diagnostic> reportDiagnostic, CancellationToken cancellationToken)
public CompilationAnalysisContext(Compilation compilation, AnalyzerOptions options, Action<Diagnostic> reportDiagnostic, Func<Diagnostic, bool> isSupportedDiagnostic, CancellationToken cancellationToken)
{
_compilation = compilation;
_options = options;
_reportDiagnostic = reportDiagnostic;
_isSupportedDiagnostic = isSupportedDiagnostic;
_cancellationToken = cancellationToken;
}
......@@ -303,7 +305,7 @@ public CompilationAnalysisContext(Compilation compilation, AnalyzerOptions optio
/// <param name="diagnostic"><see cref="Diagnostic"/> to be reported.</param>
public void ReportDiagnostic(Diagnostic diagnostic)
{
DiagnosticAnalysisContextHelpers.VerifyArguments(diagnostic);
DiagnosticAnalysisContextHelpers.VerifyArguments(diagnostic, _isSupportedDiagnostic);
lock (_reportDiagnostic)
{
_reportDiagnostic(diagnostic);
......@@ -320,6 +322,7 @@ public struct SemanticModelAnalysisContext
private readonly SemanticModel _semanticModel;
private readonly AnalyzerOptions _options;
private readonly Action<Diagnostic> _reportDiagnostic;
private readonly Func<Diagnostic, bool> _isSupportedDiagnostic;
private readonly CancellationToken _cancellationToken;
/// <summary>
......@@ -337,11 +340,12 @@ public struct SemanticModelAnalysisContext
/// </summary>
public CancellationToken CancellationToken { get { return _cancellationToken; } }
public SemanticModelAnalysisContext(SemanticModel semanticModel, AnalyzerOptions options, Action<Diagnostic> reportDiagnostic, CancellationToken cancellationToken)
public SemanticModelAnalysisContext(SemanticModel semanticModel, AnalyzerOptions options, Action<Diagnostic> reportDiagnostic, Func<Diagnostic, bool> isSupportedDiagnostic, CancellationToken cancellationToken)
{
_semanticModel = semanticModel;
_options = options;
_reportDiagnostic = reportDiagnostic;
_isSupportedDiagnostic = isSupportedDiagnostic;
_cancellationToken = cancellationToken;
}
......@@ -351,7 +355,7 @@ public SemanticModelAnalysisContext(SemanticModel semanticModel, AnalyzerOptions
/// <param name="diagnostic"><see cref="Diagnostic"/> to be reported.</param>
public void ReportDiagnostic(Diagnostic diagnostic)
{
DiagnosticAnalysisContextHelpers.VerifyArguments(diagnostic);
DiagnosticAnalysisContextHelpers.VerifyArguments(diagnostic, _isSupportedDiagnostic);
lock (_reportDiagnostic)
{
_reportDiagnostic(diagnostic);
......@@ -369,6 +373,7 @@ public struct SymbolAnalysisContext
private readonly Compilation _compilation;
private readonly AnalyzerOptions _options;
private readonly Action<Diagnostic> _reportDiagnostic;
private readonly Func<Diagnostic, bool> _isSupportedDiagnostic;
private readonly CancellationToken _cancellationToken;
/// <summary>
......@@ -391,12 +396,13 @@ public struct SymbolAnalysisContext
/// </summary>
public CancellationToken CancellationToken { get { return _cancellationToken; } }
public SymbolAnalysisContext(ISymbol symbol, Compilation compilation, AnalyzerOptions options, Action<Diagnostic> reportDiagnostic, CancellationToken cancellationToken)
public SymbolAnalysisContext(ISymbol symbol, Compilation compilation, AnalyzerOptions options, Action<Diagnostic> reportDiagnostic, Func<Diagnostic, bool> isSupportedDiagnostic, CancellationToken cancellationToken)
{
_symbol = symbol;
_compilation = compilation;
_options = options;
_reportDiagnostic = reportDiagnostic;
_isSupportedDiagnostic = isSupportedDiagnostic;
_cancellationToken = cancellationToken;
}
......@@ -406,7 +412,7 @@ public SymbolAnalysisContext(ISymbol symbol, Compilation compilation, AnalyzerOp
/// <param name="diagnostic"><see cref="Diagnostic"/> to be reported.</param>
public void ReportDiagnostic(Diagnostic diagnostic)
{
DiagnosticAnalysisContextHelpers.VerifyArguments(diagnostic);
DiagnosticAnalysisContextHelpers.VerifyArguments(diagnostic, _isSupportedDiagnostic);
lock (_reportDiagnostic)
{
_reportDiagnostic(diagnostic);
......@@ -509,6 +515,7 @@ public struct CodeBlockAnalysisContext
private readonly SemanticModel _semanticModel;
private readonly AnalyzerOptions _options;
private readonly Action<Diagnostic> _reportDiagnostic;
private readonly Func<Diagnostic, bool> _isSupportedDiagnostic;
private readonly CancellationToken _cancellationToken;
/// <summary>
......@@ -536,13 +543,14 @@ public struct CodeBlockAnalysisContext
/// </summary>
public CancellationToken CancellationToken { get { return _cancellationToken; } }
public CodeBlockAnalysisContext(SyntaxNode codeBlock, ISymbol owningSymbol, SemanticModel semanticModel, AnalyzerOptions options, Action<Diagnostic> reportDiagnostic, CancellationToken cancellationToken)
public CodeBlockAnalysisContext(SyntaxNode codeBlock, ISymbol owningSymbol, SemanticModel semanticModel, AnalyzerOptions options, Action<Diagnostic> reportDiagnostic, Func<Diagnostic, bool> isSupportedDiagnostic, CancellationToken cancellationToken)
{
_codeBlock = codeBlock;
_owningSymbol = owningSymbol;
_semanticModel = semanticModel;
_options = options;
_reportDiagnostic = reportDiagnostic;
_isSupportedDiagnostic = isSupportedDiagnostic;
_cancellationToken = cancellationToken;
}
......@@ -552,7 +560,7 @@ public CodeBlockAnalysisContext(SyntaxNode codeBlock, ISymbol owningSymbol, Sema
/// <param name="diagnostic"><see cref="Diagnostic"/> to be reported.</param>
public void ReportDiagnostic(Diagnostic diagnostic)
{
DiagnosticAnalysisContextHelpers.VerifyArguments(diagnostic);
DiagnosticAnalysisContextHelpers.VerifyArguments(diagnostic, _isSupportedDiagnostic);
lock (_reportDiagnostic)
{
_reportDiagnostic(diagnostic);
......@@ -569,6 +577,7 @@ public struct SyntaxTreeAnalysisContext
private readonly SyntaxTree _tree;
private readonly AnalyzerOptions _options;
private readonly Action<Diagnostic> _reportDiagnostic;
private readonly Func<Diagnostic, bool> _isSupportedDiagnostic;
private readonly CancellationToken _cancellationToken;
/// <summary>
......@@ -586,11 +595,12 @@ public struct SyntaxTreeAnalysisContext
/// </summary>
public CancellationToken CancellationToken { get { return _cancellationToken; } }
public SyntaxTreeAnalysisContext(SyntaxTree tree, AnalyzerOptions options, Action<Diagnostic> reportDiagnostic, CancellationToken cancellationToken)
public SyntaxTreeAnalysisContext(SyntaxTree tree, AnalyzerOptions options, Action<Diagnostic> reportDiagnostic, Func<Diagnostic, bool> isSupportedDiagnostic, CancellationToken cancellationToken)
{
_tree = tree;
_options = options;
_reportDiagnostic = reportDiagnostic;
_isSupportedDiagnostic = isSupportedDiagnostic;
_cancellationToken = cancellationToken;
}
......@@ -600,7 +610,7 @@ public SyntaxTreeAnalysisContext(SyntaxTree tree, AnalyzerOptions options, Actio
/// <param name="diagnostic"><see cref="Diagnostic"/> to be reported.</param>
public void ReportDiagnostic(Diagnostic diagnostic)
{
DiagnosticAnalysisContextHelpers.VerifyArguments(diagnostic);
DiagnosticAnalysisContextHelpers.VerifyArguments(diagnostic, _isSupportedDiagnostic);
lock (_reportDiagnostic)
{
_reportDiagnostic(diagnostic);
......@@ -618,6 +628,7 @@ public struct SyntaxNodeAnalysisContext
private readonly SemanticModel _semanticModel;
private readonly AnalyzerOptions _options;
private readonly Action<Diagnostic> _reportDiagnostic;
private readonly Func<Diagnostic, bool> _isSupportedDiagnostic;
private readonly CancellationToken _cancellationToken;
/// <summary>
......@@ -640,12 +651,13 @@ public struct SyntaxNodeAnalysisContext
/// </summary>
public CancellationToken CancellationToken { get { return _cancellationToken; } }
public SyntaxNodeAnalysisContext(SyntaxNode node, SemanticModel semanticModel, AnalyzerOptions options, Action<Diagnostic> reportDiagnostic, CancellationToken cancellationToken)
public SyntaxNodeAnalysisContext(SyntaxNode node, SemanticModel semanticModel, AnalyzerOptions options, Action<Diagnostic> reportDiagnostic, Func<Diagnostic, bool> isSupportedDiagnostic, CancellationToken cancellationToken)
{
_node = node;
_semanticModel = semanticModel;
_options = options;
_reportDiagnostic = reportDiagnostic;
_isSupportedDiagnostic = isSupportedDiagnostic;
_cancellationToken = cancellationToken;
}
......@@ -655,7 +667,7 @@ public SyntaxNodeAnalysisContext(SyntaxNode node, SemanticModel semanticModel, A
/// <param name="diagnostic"><see cref="Diagnostic"/> to be reported.</param>
public void ReportDiagnostic(Diagnostic diagnostic)
{
DiagnosticAnalysisContextHelpers.VerifyArguments(diagnostic);
DiagnosticAnalysisContextHelpers.VerifyArguments(diagnostic, _isSupportedDiagnostic);
lock (_reportDiagnostic)
{
_reportDiagnostic(diagnostic);
......
......@@ -243,7 +243,7 @@ Microsoft.CodeAnalysis.Diagnostics.AnalyzerReference.AnalyzerReference() -> void
Microsoft.CodeAnalysis.Diagnostics.CodeBlockAnalysisContext
Microsoft.CodeAnalysis.Diagnostics.CodeBlockAnalysisContext.CancellationToken.get -> System.Threading.CancellationToken
Microsoft.CodeAnalysis.Diagnostics.CodeBlockAnalysisContext.CodeBlock.get -> Microsoft.CodeAnalysis.SyntaxNode
Microsoft.CodeAnalysis.Diagnostics.CodeBlockAnalysisContext.CodeBlockAnalysisContext(Microsoft.CodeAnalysis.SyntaxNode codeBlock, Microsoft.CodeAnalysis.ISymbol owningSymbol, Microsoft.CodeAnalysis.SemanticModel semanticModel, Microsoft.CodeAnalysis.Diagnostics.AnalyzerOptions options, System.Action<Microsoft.CodeAnalysis.Diagnostic> reportDiagnostic, System.Threading.CancellationToken cancellationToken) -> void
Microsoft.CodeAnalysis.Diagnostics.CodeBlockAnalysisContext.CodeBlockAnalysisContext(Microsoft.CodeAnalysis.SyntaxNode codeBlock, Microsoft.CodeAnalysis.ISymbol owningSymbol, Microsoft.CodeAnalysis.SemanticModel semanticModel, Microsoft.CodeAnalysis.Diagnostics.AnalyzerOptions options, System.Action<Microsoft.CodeAnalysis.Diagnostic> reportDiagnostic, System.Func<Microsoft.CodeAnalysis.Diagnostic, bool> isSupportedDiagnostic, System.Threading.CancellationToken cancellationToken) -> void
Microsoft.CodeAnalysis.Diagnostics.CodeBlockAnalysisContext.Options.get -> Microsoft.CodeAnalysis.Diagnostics.AnalyzerOptions
Microsoft.CodeAnalysis.Diagnostics.CodeBlockAnalysisContext.OwningSymbol.get -> Microsoft.CodeAnalysis.ISymbol
Microsoft.CodeAnalysis.Diagnostics.CodeBlockAnalysisContext.ReportDiagnostic(Microsoft.CodeAnalysis.Diagnostic diagnostic) -> void
......@@ -259,7 +259,7 @@ Microsoft.CodeAnalysis.Diagnostics.CodeBlockStartAnalysisContext<TLanguageKindEn
Microsoft.CodeAnalysis.Diagnostics.CompilationAnalysisContext
Microsoft.CodeAnalysis.Diagnostics.CompilationAnalysisContext.CancellationToken.get -> System.Threading.CancellationToken
Microsoft.CodeAnalysis.Diagnostics.CompilationAnalysisContext.Compilation.get -> Microsoft.CodeAnalysis.Compilation
Microsoft.CodeAnalysis.Diagnostics.CompilationAnalysisContext.CompilationAnalysisContext(Microsoft.CodeAnalysis.Compilation compilation, Microsoft.CodeAnalysis.Diagnostics.AnalyzerOptions options, System.Action<Microsoft.CodeAnalysis.Diagnostic> reportDiagnostic, System.Threading.CancellationToken cancellationToken) -> void
Microsoft.CodeAnalysis.Diagnostics.CompilationAnalysisContext.CompilationAnalysisContext(Microsoft.CodeAnalysis.Compilation compilation, Microsoft.CodeAnalysis.Diagnostics.AnalyzerOptions options, System.Action<Microsoft.CodeAnalysis.Diagnostic> reportDiagnostic, System.Func<Microsoft.CodeAnalysis.Diagnostic, bool> isSupportedDiagnostic, System.Threading.CancellationToken cancellationToken) -> void
Microsoft.CodeAnalysis.Diagnostics.CompilationAnalysisContext.Options.get -> Microsoft.CodeAnalysis.Diagnostics.AnalyzerOptions
Microsoft.CodeAnalysis.Diagnostics.CompilationAnalysisContext.ReportDiagnostic(Microsoft.CodeAnalysis.Diagnostic diagnostic) -> void
Microsoft.CodeAnalysis.Diagnostics.CompilationStartAnalysisContext
......@@ -284,26 +284,26 @@ Microsoft.CodeAnalysis.Diagnostics.SemanticModelAnalysisContext.CancellationToke
Microsoft.CodeAnalysis.Diagnostics.SemanticModelAnalysisContext.Options.get -> Microsoft.CodeAnalysis.Diagnostics.AnalyzerOptions
Microsoft.CodeAnalysis.Diagnostics.SemanticModelAnalysisContext.ReportDiagnostic(Microsoft.CodeAnalysis.Diagnostic diagnostic) -> void
Microsoft.CodeAnalysis.Diagnostics.SemanticModelAnalysisContext.SemanticModel.get -> Microsoft.CodeAnalysis.SemanticModel
Microsoft.CodeAnalysis.Diagnostics.SemanticModelAnalysisContext.SemanticModelAnalysisContext(Microsoft.CodeAnalysis.SemanticModel semanticModel, Microsoft.CodeAnalysis.Diagnostics.AnalyzerOptions options, System.Action<Microsoft.CodeAnalysis.Diagnostic> reportDiagnostic, System.Threading.CancellationToken cancellationToken) -> void
Microsoft.CodeAnalysis.Diagnostics.SemanticModelAnalysisContext.SemanticModelAnalysisContext(Microsoft.CodeAnalysis.SemanticModel semanticModel, Microsoft.CodeAnalysis.Diagnostics.AnalyzerOptions options, System.Action<Microsoft.CodeAnalysis.Diagnostic> reportDiagnostic, System.Func<Microsoft.CodeAnalysis.Diagnostic, bool> isSupportedDiagnostic, System.Threading.CancellationToken cancellationToken) -> void
Microsoft.CodeAnalysis.Diagnostics.SymbolAnalysisContext
Microsoft.CodeAnalysis.Diagnostics.SymbolAnalysisContext.CancellationToken.get -> System.Threading.CancellationToken
Microsoft.CodeAnalysis.Diagnostics.SymbolAnalysisContext.Compilation.get -> Microsoft.CodeAnalysis.Compilation
Microsoft.CodeAnalysis.Diagnostics.SymbolAnalysisContext.Options.get -> Microsoft.CodeAnalysis.Diagnostics.AnalyzerOptions
Microsoft.CodeAnalysis.Diagnostics.SymbolAnalysisContext.ReportDiagnostic(Microsoft.CodeAnalysis.Diagnostic diagnostic) -> void
Microsoft.CodeAnalysis.Diagnostics.SymbolAnalysisContext.Symbol.get -> Microsoft.CodeAnalysis.ISymbol
Microsoft.CodeAnalysis.Diagnostics.SymbolAnalysisContext.SymbolAnalysisContext(Microsoft.CodeAnalysis.ISymbol symbol, Microsoft.CodeAnalysis.Compilation compilation, Microsoft.CodeAnalysis.Diagnostics.AnalyzerOptions options, System.Action<Microsoft.CodeAnalysis.Diagnostic> reportDiagnostic, System.Threading.CancellationToken cancellationToken) -> void
Microsoft.CodeAnalysis.Diagnostics.SymbolAnalysisContext.SymbolAnalysisContext(Microsoft.CodeAnalysis.ISymbol symbol, Microsoft.CodeAnalysis.Compilation compilation, Microsoft.CodeAnalysis.Diagnostics.AnalyzerOptions options, System.Action<Microsoft.CodeAnalysis.Diagnostic> reportDiagnostic, System.Func<Microsoft.CodeAnalysis.Diagnostic, bool> isSupportedDiagnostic, System.Threading.CancellationToken cancellationToken) -> void
Microsoft.CodeAnalysis.Diagnostics.SyntaxNodeAnalysisContext
Microsoft.CodeAnalysis.Diagnostics.SyntaxNodeAnalysisContext.CancellationToken.get -> System.Threading.CancellationToken
Microsoft.CodeAnalysis.Diagnostics.SyntaxNodeAnalysisContext.Node.get -> Microsoft.CodeAnalysis.SyntaxNode
Microsoft.CodeAnalysis.Diagnostics.SyntaxNodeAnalysisContext.Options.get -> Microsoft.CodeAnalysis.Diagnostics.AnalyzerOptions
Microsoft.CodeAnalysis.Diagnostics.SyntaxNodeAnalysisContext.ReportDiagnostic(Microsoft.CodeAnalysis.Diagnostic diagnostic) -> void
Microsoft.CodeAnalysis.Diagnostics.SyntaxNodeAnalysisContext.SemanticModel.get -> Microsoft.CodeAnalysis.SemanticModel
Microsoft.CodeAnalysis.Diagnostics.SyntaxNodeAnalysisContext.SyntaxNodeAnalysisContext(Microsoft.CodeAnalysis.SyntaxNode node, Microsoft.CodeAnalysis.SemanticModel semanticModel, Microsoft.CodeAnalysis.Diagnostics.AnalyzerOptions options, System.Action<Microsoft.CodeAnalysis.Diagnostic> reportDiagnostic, System.Threading.CancellationToken cancellationToken) -> void
Microsoft.CodeAnalysis.Diagnostics.SyntaxNodeAnalysisContext.SyntaxNodeAnalysisContext(Microsoft.CodeAnalysis.SyntaxNode node, Microsoft.CodeAnalysis.SemanticModel semanticModel, Microsoft.CodeAnalysis.Diagnostics.AnalyzerOptions options, System.Action<Microsoft.CodeAnalysis.Diagnostic> reportDiagnostic, System.Func<Microsoft.CodeAnalysis.Diagnostic, bool> isSupportedDiagnostic, System.Threading.CancellationToken cancellationToken) -> void
Microsoft.CodeAnalysis.Diagnostics.SyntaxTreeAnalysisContext
Microsoft.CodeAnalysis.Diagnostics.SyntaxTreeAnalysisContext.CancellationToken.get -> System.Threading.CancellationToken
Microsoft.CodeAnalysis.Diagnostics.SyntaxTreeAnalysisContext.Options.get -> Microsoft.CodeAnalysis.Diagnostics.AnalyzerOptions
Microsoft.CodeAnalysis.Diagnostics.SyntaxTreeAnalysisContext.ReportDiagnostic(Microsoft.CodeAnalysis.Diagnostic diagnostic) -> void
Microsoft.CodeAnalysis.Diagnostics.SyntaxTreeAnalysisContext.SyntaxTreeAnalysisContext(Microsoft.CodeAnalysis.SyntaxTree tree, Microsoft.CodeAnalysis.Diagnostics.AnalyzerOptions options, System.Action<Microsoft.CodeAnalysis.Diagnostic> reportDiagnostic, System.Threading.CancellationToken cancellationToken) -> void
Microsoft.CodeAnalysis.Diagnostics.SyntaxTreeAnalysisContext.SyntaxTreeAnalysisContext(Microsoft.CodeAnalysis.SyntaxTree tree, Microsoft.CodeAnalysis.Diagnostics.AnalyzerOptions options, System.Action<Microsoft.CodeAnalysis.Diagnostic> reportDiagnostic, System.Func<Microsoft.CodeAnalysis.Diagnostic, bool> isSupportedDiagnostic, System.Threading.CancellationToken cancellationToken) -> void
Microsoft.CodeAnalysis.Diagnostics.SyntaxTreeAnalysisContext.Tree.get -> Microsoft.CodeAnalysis.SyntaxTree
Microsoft.CodeAnalysis.Diagnostics.UnresolvedAnalyzerReference
Microsoft.CodeAnalysis.Diagnostics.UnresolvedAnalyzerReference.UnresolvedAnalyzerReference(string unresolvedPath) -> void
......@@ -2132,4 +2132,4 @@ virtual Microsoft.CodeAnalysis.Text.SourceText.ToString(Microsoft.CodeAnalysis.T
virtual Microsoft.CodeAnalysis.Text.SourceText.WithChanges(System.Collections.Generic.IEnumerable<Microsoft.CodeAnalysis.Text.TextChange> changes) -> Microsoft.CodeAnalysis.Text.SourceText
virtual Microsoft.CodeAnalysis.Text.SourceText.Write(System.IO.TextWriter writer, Microsoft.CodeAnalysis.Text.TextSpan span, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> void
virtual Microsoft.CodeAnalysis.Text.TextLineCollection.GetLineFromPosition(int position) -> Microsoft.CodeAnalysis.Text.TextLine
virtual Microsoft.CodeAnalysis.Text.TextLineCollection.GetLinePosition(int position) -> Microsoft.CodeAnalysis.Text.LinePosition
virtual Microsoft.CodeAnalysis.Text.TextLineCollection.GetLinePosition(int position) -> Microsoft.CodeAnalysis.Text.LinePosition
\ No newline at end of file
......@@ -147,7 +147,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.UnitTests.Semantics
Class ComplainAboutX
Inherits DiagnosticAnalyzer
Private Shared ReadOnly CA9999_UseOfVariableThatStartsWithX As DiagnosticDescriptor = New DiagnosticDescriptor(id:="CA9999", title:="CA9999_UseOfVariableThatStartsWithX", messageFormat:="Use of variable whose name starts with 'x': '{0}'", category:="Test", defaultSeverity:=DiagnosticSeverity.Warning, isEnabledByDefault:=True)
Private Shared ReadOnly CA9999_UseOfVariableThatStartsWithX As DiagnosticDescriptor = New DiagnosticDescriptor(id:="CA9999_UseOfVariableThatStartsWithX", title:="CA9999_UseOfVariableThatStartsWithX", messageFormat:="Use of variable whose name starts with 'x': '{0}'", category:="Test", defaultSeverity:=DiagnosticSeverity.Warning, isEnabledByDefault:=True)
Public Overrides ReadOnly Property SupportedDiagnostics() As ImmutableArray(Of DiagnosticDescriptor)
Get
......@@ -162,7 +162,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.UnitTests.Semantics
Public Sub AnalyzeNode(context As SyntaxNodeAnalysisContext)
Dim id = CType(context.Node, IdentifierNameSyntax)
If id.Identifier.ValueText.StartsWith("x", StringComparison.Ordinal) Then
context.ReportDiagnostic(New TestDiagnostic("CA9999_UseOfVariableThatStartsWithX", "CsTest", DiagnosticSeverity.Warning, id.GetLocation(), "Use of variable whose name starts with 'x': '{0}'", False, id.Identifier.ValueText))
context.ReportDiagnostic(CodeAnalysis.Diagnostic.Create(CA9999_UseOfVariableThatStartsWithX, id.GetLocation, id.Identifier.ValueText))
End If
End Sub
End Class
......
......@@ -9,5 +9,6 @@ internal static class AnalyzerDriverResources
internal static string DiagnosticDescriptorThrows => FeaturesResources.DiagnosticDescriptorThrows;
internal static string ArgumentElementCannotBeNull => FeaturesResources.ArgumentElementCannotBeNull;
internal static string ArgumentCannotBeEmpty => FeaturesResources.ArgumentCannotBeEmpty;
internal static string UnsupportedDiagnosticReported => FeaturesResources.UnsupportedDiagnosticReported;
}
}
......@@ -54,7 +54,7 @@ public static bool IsCompilerAnalyzer(this DiagnosticAnalyzer analyzer)
Action<Exception, DiagnosticAnalyzer, Diagnostic> defaultOnAnalyzerException = (ex, a, diagnostic) =>
OnAnalyzerException_NoTelemetryLogging(ex, a, diagnostic, hostDiagnosticUpdateSource);
return AnalyzerExecutor.CreateForSupportedDiagnostics(onAnalyzerException ?? defaultOnAnalyzerException, cancellationToken);
return AnalyzerExecutor.CreateForSupportedDiagnostics(onAnalyzerException ?? defaultOnAnalyzerException, AnalyzerManager.Instance, cancellationToken);
}
internal static void OnAnalyzerException_NoTelemetryLogging(
......
......@@ -321,7 +321,7 @@ internal void OnAnalyzerException(Exception ex, DiagnosticAnalyzer analyzer, Com
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, AnalyzerHelper.IsCompilerAnalyzer, AnalyzerManager.Instance, _cancellationToken);
}
public async Task<AnalyzerActions> GetAnalyzerActionsAsync(DiagnosticAnalyzer analyzer)
......
......@@ -1763,6 +1763,15 @@ internal class FeaturesResources {
}
}
/// <summary>
/// Looks up a localized string similar to Reported diagnostic with ID &apos;{0}&apos; is not supported by the analyzer..
/// </summary>
internal static string UnsupportedDiagnosticReported {
get {
return ResourceManager.GetString("UnsupportedDiagnosticReported", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Updating an active statement will prevent the debug session from continuing..
/// </summary>
......
......@@ -737,6 +737,9 @@ Do you want to continue?</value>
<data name="ArgumentCannotBeEmpty" xml:space="preserve">
<value>Argument cannot be empty.</value>
</data>
<data name="UnsupportedDiagnosticReported" xml:space="preserve">
<value>Reported diagnostic with ID '{0}' is not supported by the analyzer.</value>
</data>
<data name="FixAllTitle_Document" xml:space="preserve">
<value>Document</value>
</data>
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册