提交 170f1295 编写于 作者: T Tomáš Matoušek

Merge pull request #2882 from tmat/AnalyzerErrorReporting

Fix analyzer type load error reporting, API
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:4.0.30319.42000
// Runtime Version:4.0.30319.0
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
......@@ -728,6 +728,15 @@ internal class CodeAnalysisResources {
}
}
/// <summary>
/// Looks up a localized string similar to No analyzers found.
/// </summary>
internal static string NoAnalyzersFound {
get {
return ResourceManager.GetString("NoAnalyzersFound", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Cannot deserialize type &apos;{0}&apos;, no binder supplied..
/// </summary>
......
......@@ -469,4 +469,7 @@
<data name="AnalyzerNameColumnHeader" xml:space="preserve">
<value>Analyzer</value>
</data>
<data name="NoAnalyzersFound" xml:space="preserve">
<value>No analyzers found</value>
</data>
</root>
\ No newline at end of file
......@@ -349,10 +349,10 @@ internal ImmutableArray<DiagnosticAnalyzer> ResolveAnalyzersFromArguments(string
switch (e.ErrorCode)
{
case AnalyzerLoadFailureEventArgs.FailureErrorCode.UnableToLoadAnalyzer:
diagnostic = new DiagnosticInfo(messageProvider, messageProvider.WRN_UnableToLoadAnalyzer, analyzerReference.FullPath, e.Exception.Message);
diagnostic = new DiagnosticInfo(messageProvider, messageProvider.WRN_UnableToLoadAnalyzer, analyzerReference.FullPath, e.Message);
break;
case AnalyzerLoadFailureEventArgs.FailureErrorCode.UnableToCreateAnalyzer:
diagnostic = new DiagnosticInfo(messageProvider, messageProvider.WRN_AnalyzerCannotBeCreated, e.TypeName, analyzerReference.FullPath, e.Exception.Message);
diagnostic = new DiagnosticInfo(messageProvider, messageProvider.WRN_AnalyzerCannotBeCreated, e.TypeName, analyzerReference.FullPath, e.Message);
break;
case AnalyzerLoadFailureEventArgs.FailureErrorCode.NoAnalyzers:
diagnostic = new DiagnosticInfo(messageProvider, messageProvider.WRN_NoAnalyzerInAssembly, analyzerReference.FullPath);
......
......@@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
......@@ -160,7 +161,7 @@ internal void AddAnalyzers(ImmutableDictionary<string, ImmutableArray<Diagnostic
}
catch (Exception e)
{
this.AnalyzerLoadFailed?.Invoke(this, new AnalyzerLoadFailureEventArgs(AnalyzerLoadFailureEventArgs.FailureErrorCode.UnableToLoadAnalyzer, e, null));
this.AnalyzerLoadFailed?.Invoke(this, new AnalyzerLoadFailureEventArgs(AnalyzerLoadFailureEventArgs.FailureErrorCode.UnableToLoadAnalyzer, e.Message, e));
return;
}
......@@ -183,7 +184,7 @@ internal void AddAnalyzers(ImmutableDictionary<string, ImmutableArray<Diagnostic
// If we've reported errors already while trying to instantiate types, don't complain that there are no analyzers.
if (builder.Count == initialCount && !reportedError)
{
this.AnalyzerLoadFailed?.Invoke(this, new AnalyzerLoadFailureEventArgs(AnalyzerLoadFailureEventArgs.FailureErrorCode.NoAnalyzers, null, null));
this.AnalyzerLoadFailed?.Invoke(this, new AnalyzerLoadFailureEventArgs(AnalyzerLoadFailureEventArgs.FailureErrorCode.NoAnalyzers, CodeAnalysisResources.NoAnalyzersFound));
}
}
......@@ -209,7 +210,7 @@ internal void AddAnalyzers(ImmutableArray<DiagnosticAnalyzer>.Builder builder, s
}
catch (Exception e)
{
this.AnalyzerLoadFailed?.Invoke(this, new AnalyzerLoadFailureEventArgs(AnalyzerLoadFailureEventArgs.FailureErrorCode.UnableToLoadAnalyzer, e, null));
this.AnalyzerLoadFailed?.Invoke(this, new AnalyzerLoadFailureEventArgs(AnalyzerLoadFailureEventArgs.FailureErrorCode.UnableToLoadAnalyzer, e.Message));
return;
}
......@@ -224,7 +225,7 @@ internal void AddAnalyzers(ImmutableArray<DiagnosticAnalyzer>.Builder builder, s
// If we've reported errors already while trying to instantiate types, don't complain that there are no analyzers.
if (builder.Count == initialCount && !reportedError)
{
this.AnalyzerLoadFailed?.Invoke(this, new AnalyzerLoadFailureEventArgs(AnalyzerLoadFailureEventArgs.FailureErrorCode.NoAnalyzers, null, null));
this.AnalyzerLoadFailed?.Invoke(this, new AnalyzerLoadFailureEventArgs(AnalyzerLoadFailureEventArgs.FailureErrorCode.NoAnalyzers, CodeAnalysisResources.NoAnalyzersFound));
}
}
......@@ -252,20 +253,30 @@ private IEnumerable<DiagnosticAnalyzer> GetAnalyzersForTypeNames(Assembly analyz
// Given the type names, get the actual System.Type and try to create an instance of the type through reflection.
foreach (var typeName in analyzerTypeNames)
{
DiagnosticAnalyzer analyzer = null;
Type type;
try
{
var type = analyzerAssembly.GetType(typeName);
if (DerivesFromDiagnosticAnalyzer(type))
{
analyzer = (DiagnosticAnalyzer)Activator.CreateInstance(type);
}
type = Type.GetType(typeName + ", " + analyzerAssembly.FullName, throwOnError: true);
}
catch (Exception e)
{
this.AnalyzerLoadFailed?.Invoke(this, new AnalyzerLoadFailureEventArgs(AnalyzerLoadFailureEventArgs.FailureErrorCode.UnableToCreateAnalyzer, e.Message, e, typeName));
reportedError = true;
continue;
}
Debug.Assert(type != null);
DiagnosticAnalyzer analyzer;
try
{
analyzer = Activator.CreateInstance(type) as DiagnosticAnalyzer;
}
catch (Exception e)
{
this.AnalyzerLoadFailed?.Invoke(this, new AnalyzerLoadFailureEventArgs(AnalyzerLoadFailureEventArgs.FailureErrorCode.UnableToCreateAnalyzer, e, typeName));
analyzer = null;
this.AnalyzerLoadFailed?.Invoke(this, new AnalyzerLoadFailureEventArgs(AnalyzerLoadFailureEventArgs.FailureErrorCode.UnableToCreateAnalyzer, e.Message, e, typeName));
reportedError = true;
continue;
}
if (analyzer != null)
......@@ -381,11 +392,6 @@ private static string GetFullyQualifiedTypeName(TypeDefinition typeDef, PEModule
}
}
private static bool DerivesFromDiagnosticAnalyzer(Type type)
{
return type.GetTypeInfo().IsSubclassOf(typeof(DiagnosticAnalyzer));
}
public override bool Equals(object obj)
{
return Equals(obj as AnalyzerFileReference);
......
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Diagnostics;
namespace Microsoft.CodeAnalysis.Diagnostics
{
public class AnalyzerLoadFailureEventArgs : EventArgs
public sealed class AnalyzerLoadFailureEventArgs : EventArgs
{
public enum FailureErrorCode
{
......@@ -14,15 +15,42 @@ public enum FailureErrorCode
NoAnalyzers = 3
}
public readonly string TypeName;
public readonly Exception Exception;
public readonly FailureErrorCode ErrorCode;
/// <summary>
/// If a specific analyzer failed to load the namespace-qualified name of its type, null otherwise.
/// </summary>
public string TypeName { get; }
public AnalyzerLoadFailureEventArgs(FailureErrorCode errorCode, Exception ex, string typeName)
/// <summary>
/// Error message.
/// </summary>
public string Message { get; }
/// <summary>
/// Error code.
/// </summary>
public FailureErrorCode ErrorCode { get; }
/// <summary>
/// Exception that was thrown while loading the analyzer. May be null.
/// </summary>
public Exception Exception { get; }
public AnalyzerLoadFailureEventArgs(FailureErrorCode errorCode, string message, Exception exceptionOpt = null, string typeNameOpt = null)
{
this.TypeName = typeName;
this.ErrorCode = errorCode;
this.Exception = ex;
if (errorCode <= FailureErrorCode.None || errorCode > FailureErrorCode.NoAnalyzers)
{
throw new ArgumentOutOfRangeException(nameof(errorCode));
}
if (message == null)
{
throw new ArgumentNullException(nameof(message));
}
ErrorCode = errorCode;
Message = message;
TypeName = typeNameOpt;
Exception = exceptionOpt;
}
}
}
......@@ -294,12 +294,16 @@ Microsoft.CodeAnalysis.Diagnostics.AnalyzerFileReference.GetAssembly() -> System
Microsoft.CodeAnalysis.Diagnostics.AnalyzerImageReference
Microsoft.CodeAnalysis.Diagnostics.AnalyzerImageReference.AnalyzerImageReference(System.Collections.Immutable.ImmutableArray<Microsoft.CodeAnalysis.Diagnostics.DiagnosticAnalyzer> analyzers, string fullPath = null, string display = null) -> void
Microsoft.CodeAnalysis.Diagnostics.AnalyzerLoadFailureEventArgs
Microsoft.CodeAnalysis.Diagnostics.AnalyzerLoadFailureEventArgs.AnalyzerLoadFailureEventArgs(Microsoft.CodeAnalysis.Diagnostics.AnalyzerLoadFailureEventArgs.FailureErrorCode errorCode, System.Exception ex, string typeName) -> void
Microsoft.CodeAnalysis.Diagnostics.AnalyzerLoadFailureEventArgs.AnalyzerLoadFailureEventArgs(Microsoft.CodeAnalysis.Diagnostics.AnalyzerLoadFailureEventArgs.FailureErrorCode errorCode, string message, System.Exception exceptionOpt = null, string typeNameOpt = null) -> void
Microsoft.CodeAnalysis.Diagnostics.AnalyzerLoadFailureEventArgs.ErrorCode.get -> Microsoft.CodeAnalysis.Diagnostics.AnalyzerLoadFailureEventArgs.FailureErrorCode
Microsoft.CodeAnalysis.Diagnostics.AnalyzerLoadFailureEventArgs.Exception.get -> System.Exception
Microsoft.CodeAnalysis.Diagnostics.AnalyzerLoadFailureEventArgs.FailureErrorCode
Microsoft.CodeAnalysis.Diagnostics.AnalyzerLoadFailureEventArgs.FailureErrorCode.NoAnalyzers = 3 -> Microsoft.CodeAnalysis.Diagnostics.AnalyzerLoadFailureEventArgs.FailureErrorCode
Microsoft.CodeAnalysis.Diagnostics.AnalyzerLoadFailureEventArgs.FailureErrorCode.None = 0 -> Microsoft.CodeAnalysis.Diagnostics.AnalyzerLoadFailureEventArgs.FailureErrorCode
Microsoft.CodeAnalysis.Diagnostics.AnalyzerLoadFailureEventArgs.FailureErrorCode.UnableToCreateAnalyzer = 2 -> Microsoft.CodeAnalysis.Diagnostics.AnalyzerLoadFailureEventArgs.FailureErrorCode
Microsoft.CodeAnalysis.Diagnostics.AnalyzerLoadFailureEventArgs.FailureErrorCode.UnableToLoadAnalyzer = 1 -> Microsoft.CodeAnalysis.Diagnostics.AnalyzerLoadFailureEventArgs.FailureErrorCode
Microsoft.CodeAnalysis.Diagnostics.AnalyzerLoadFailureEventArgs.Message.get -> string
Microsoft.CodeAnalysis.Diagnostics.AnalyzerLoadFailureEventArgs.TypeName.get -> string
Microsoft.CodeAnalysis.Diagnostics.AnalyzerOptions
Microsoft.CodeAnalysis.Diagnostics.AnalyzerOptions.AdditionalFiles.get -> System.Collections.Immutable.ImmutableArray<Microsoft.CodeAnalysis.AdditionalText>
Microsoft.CodeAnalysis.Diagnostics.AnalyzerOptions.AnalyzerOptions(System.Collections.Immutable.ImmutableArray<Microsoft.CodeAnalysis.AdditionalText> additionalFiles) -> void
......@@ -1988,9 +1992,6 @@ override sealed Microsoft.CodeAnalysis.Diagnostics.DiagnosticAnalyzer.ToString()
override sealed Microsoft.CodeAnalysis.LocalizableString.Equals(object other) -> bool
override sealed Microsoft.CodeAnalysis.LocalizableString.GetHashCode() -> int
override sealed Microsoft.CodeAnalysis.LocalizableString.ToString() -> string
readonly Microsoft.CodeAnalysis.Diagnostics.AnalyzerLoadFailureEventArgs.ErrorCode -> Microsoft.CodeAnalysis.Diagnostics.AnalyzerLoadFailureEventArgs.FailureErrorCode
readonly Microsoft.CodeAnalysis.Diagnostics.AnalyzerLoadFailureEventArgs.Exception -> System.Exception
readonly Microsoft.CodeAnalysis.Diagnostics.AnalyzerLoadFailureEventArgs.TypeName -> string
static Microsoft.CodeAnalysis.AnnotationExtensions.WithAdditionalAnnotations<TNode>(this TNode node, System.Collections.Generic.IEnumerable<Microsoft.CodeAnalysis.SyntaxAnnotation> annotations) -> TNode
static Microsoft.CodeAnalysis.AnnotationExtensions.WithAdditionalAnnotations<TNode>(this TNode node, params Microsoft.CodeAnalysis.SyntaxAnnotation[] annotations) -> TNode
static Microsoft.CodeAnalysis.AnnotationExtensions.WithoutAnnotations<TNode>(this TNode node, System.Collections.Generic.IEnumerable<Microsoft.CodeAnalysis.SyntaxAnnotation> annotations) -> TNode
......
......@@ -85,12 +85,12 @@ private void OnAnalyzerLoadError(object sender, AnalyzerLoadFailureEventArgs e)
case AnalyzerLoadFailureEventArgs.FailureErrorCode.UnableToLoadAnalyzer:
id = _language == LanguageNames.CSharp ? WRN_UnableToLoadAnalyzerIdCS : WRN_UnableToLoadAnalyzerIdVB;
messageFormat = ServicesVSResources.WRN_UnableToLoadAnalyzer;
message = string.Format(ServicesVSResources.WRN_UnableToLoadAnalyzer, _fullPath, e.Exception.Message);
message = string.Format(ServicesVSResources.WRN_UnableToLoadAnalyzer, _fullPath, e.Message);
break;
case AnalyzerLoadFailureEventArgs.FailureErrorCode.UnableToCreateAnalyzer:
id = _language == LanguageNames.CSharp ? WRN_AnalyzerCannotBeCreatedIdCS : WRN_AnalyzerCannotBeCreatedIdVB;
messageFormat = ServicesVSResources.WRN_AnalyzerCannotBeCreated;
message = string.Format(ServicesVSResources.WRN_AnalyzerCannotBeCreated, e.TypeName, _fullPath, e.Exception.Message);
message = string.Format(ServicesVSResources.WRN_AnalyzerCannotBeCreated, e.TypeName, _fullPath, e.Message);
break;
case AnalyzerLoadFailureEventArgs.FailureErrorCode.NoAnalyzers:
id = _language == LanguageNames.CSharp ? WRN_NoAnalyzerInAssemblyIdCS : WRN_NoAnalyzerInAssemblyIdVB;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册