internal static Diagnostic CreateAnalyzerExceptionDiagnostic(DiagnosticAnalyzer analyzer, Exception e) { var analyzerName = analyzer.ToString(); var title = CodeAnalysisResources.CompilerAnalyzerFailure; var messageFormat = CodeAnalysisResources.CompilerAnalyzerThrows; var messageArguments = new[] { analyzerName, e.GetType().ToString(), e.Message }; var description = string.Format(CodeAnalysisResources.CompilerAnalyzerThrowsDescription, analyzerName, e.ToString()); return(CreateExceptionDiagnostic(AnalyzerExceptionDiagnosticId, title, description, messageFormat, messageArguments)); }
internal static Diagnostic GetAnalyzerExceptionDiagnostic(DiagnosticAnalyzer analyzer, Exception e) { var descriptor = new DiagnosticDescriptor(AnalyzerExceptionDiagnosticId, AnalyzerDriverResources.AnalyzerFailure, AnalyzerDriverResources.AnalyzerThrows, category: DiagnosticCategory, defaultSeverity: DiagnosticSeverity.Info, isEnabledByDefault: true, customTags: WellKnownDiagnosticTags.AnalyzerException); return(Diagnostic.Create(descriptor, Location.None, analyzer.ToString(), e.Message)); }
private Rules GetAnalyzerRules(DiagnosticAnalyzer analyzer) { // For info on SonarQube rules see http://docs.sonarqube.org/display/SONAR/Rules Rules rules = new Rules(); foreach (DiagnosticDescriptor diagnostic in analyzer.SupportedDiagnostics) { if (String.IsNullOrWhiteSpace(diagnostic.Id)) { logger.LogWarning(UIResources.RuleGen_EmptyKey, analyzer.ToString()); continue; } Rule newRule = new Rule(); newRule.Key = diagnostic.Id; newRule.InternalKey = diagnostic.Id; newRule.Description = GetDescriptionAsRawHtml(diagnostic); newRule.Name = diagnostic.Title.ToString(CultureInfo.InvariantCulture); newRule.Severity = GetSonarQubeSeverity(diagnostic.DefaultSeverity); // Rule XML properties that don't have an obvious Diagnostic equivalent: newRule.Cardinality = Cardinality; newRule.Status = Status; // Diagnostic properties that don't have an obvious Rule xml equivalent: // diagnostic.Category // diagnostic.IsEnabledByDefault // diagnostic.MessageFormat /* Remark: Custom tags are used so that Visual Studio handles diagnostics and are not equivalent to SonarQube's tags * * http://stackoverflow.com/questions/24257222/relevance-of-new-parameters-for-diagnosticdescriptor-constructor * customTags is a general way to mark that a diagnostic should be treated or displayed somewhat * different than normal diagnostics. The "unnecessary" tag means that in the IDE we fade out the span * that the diagnostic applies to: this is how we fade out unnecessary usings or casts or such in the IDE. * In some fancy scenarios you might want to define your own, but for the most part you'll either leave that empty * or pass Unnecessary if you want the different UI handling. * The EditAndContinue tag is for errors that are created if an edit-and-continue edit can't be applied * (which are also displayed somewhat differently)...that's just for us (n.b. Roslyn) to use. */ rules.Add(newRule); } return rules; }
internal static Diagnostic GetAnalyzerExceptionDiagnostic(DiagnosticAnalyzer analyzer, Exception e) { var analyzerName = analyzer.ToString(); // TODO: It is not ideal to create a new descriptor per analyzer exception diagnostic instance. // However, until we add a LongMessage field to the Diagnostic, we are forced to park the instance specific description onto the Descriptor's Description field. // This requires us to create a new DiagnosticDescriptor instance per diagnostic instance. var descriptor = new DiagnosticDescriptor(AnalyzerExceptionDiagnosticId, title: AnalyzerDriverResources.AnalyzerFailure, messageFormat: AnalyzerDriverResources.AnalyzerThrows, description: string.Format(AnalyzerDriverResources.AnalyzerThrowsDescription, analyzerName, e.ToString()), category: DiagnosticCategory, defaultSeverity: DiagnosticSeverity.Info, isEnabledByDefault: true, customTags: WellKnownDiagnosticTags.AnalyzerException); return(Diagnostic.Create(descriptor, Location.None, analyzerName, e.GetType(), e.Message)); }
/// <summary> /// Create a diagnostic for exception thrown by the given analyzer. /// </summary> /// <remarks> /// Keep this method in sync with "AnalyzerExecutor.CreateAnalyzerExceptionDiagnostic". /// </remarks> internal static Diagnostic CreateAnalyzerExceptionDiagnostic(DiagnosticAnalyzer analyzer, Exception e) { var analyzerName = analyzer.ToString(); // TODO: It is not ideal to create a new descriptor per analyzer exception diagnostic instance. // However, until we add a LongMessage field to the Diagnostic, we are forced to park the instance specific description onto the Descriptor's Description field. // This requires us to create a new DiagnosticDescriptor instance per diagnostic instance. var descriptor = new DiagnosticDescriptor(AnalyzerExceptionDiagnosticId, title: FeaturesResources.User_Diagnostic_Analyzer_Failure, messageFormat: FeaturesResources.Analyzer_0_threw_an_exception_of_type_1_with_message_2, description: string.Format(FeaturesResources.Analyzer_0_threw_the_following_exception_colon_1, analyzerName, e.CreateDiagnosticDescription()), category: AnalyzerExceptionDiagnosticCategory, defaultSeverity: DiagnosticSeverity.Warning, isEnabledByDefault: true, customTags: WellKnownDiagnosticTags.AnalyzerException); return(Diagnostic.Create(descriptor, Location.None, analyzerName, e.GetType(), e.Message)); }
/// <summary> /// Create a diagnostic for exception thrown by the given analyzer. /// </summary> /// <remarks> /// Keep this method in sync with "AnalyzerExecutor.CreateAnalyzerExceptionDiagnostic". /// </remarks> internal static Diagnostic CreateAnalyzerExceptionDiagnostic(DiagnosticAnalyzer analyzer, Exception e) { var analyzerName = analyzer.ToString(); // TODO: It is not ideal to create a new descriptor per analyzer exception diagnostic instance. // However, until we add a LongMessage field to the Diagnostic, we are forced to park the instance specific description onto the Descriptor's Description field. // This requires us to create a new DiagnosticDescriptor instance per diagnostic instance. var descriptor = new DiagnosticDescriptor(AnalyzerExceptionDiagnosticId, title: FeaturesResources.User_Diagnostic_Analyzer_Failure, messageFormat: FeaturesResources.Analyzer_0_threw_an_exception_of_type_1_with_message_2, description: string.Format(FeaturesResources.Analyzer_0_threw_the_following_exception_colon_1, analyzerName, e.CreateDiagnosticDescription()), category: AnalyzerExceptionDiagnosticCategory, defaultSeverity: DiagnosticSeverity.Warning, isEnabledByDefault: true, customTags: WellKnownDiagnosticTags.AnalyzerException); return Diagnostic.Create(descriptor, Location.None, analyzerName, e.GetType(), e.Message); }
private static ImmutableArray <SuppressionDescriptor> ComputeSuppressionDescriptors( DiagnosticAnalyzer analyzer, AnalyzerExecutor analyzerExecutor) { var descriptors = ImmutableArray <SuppressionDescriptor> .Empty; if (analyzer is DiagnosticSuppressor suppressor) { // Catch Exception from suppressor.SupportedSuppressions analyzerExecutor.ExecuteAndCatchIfThrows( analyzer, _ => { var descriptorsLocal = suppressor.SupportedSuppressions; if (!descriptorsLocal.IsDefaultOrEmpty) { foreach (var descriptor in descriptorsLocal) { if (descriptor == null) { // Disallow null descriptors. throw new ArgumentException(string.Format(CodeAnalysisResources.SupportedSuppressionsHasNullDescriptor, analyzer.ToString()), nameof(DiagnosticSuppressor.SupportedSuppressions)); } } descriptors = descriptorsLocal; } }, argument: default(object)); } return(descriptors); }
/// <summary> /// Compute <see cref="DiagnosticAnalyzer.SupportedDiagnostics"/> and exception handler for the given <paramref name="analyzer"/>. /// </summary> private static ImmutableArray <DiagnosticDescriptor> ComputeDiagnosticDescriptors( DiagnosticAnalyzer analyzer, AnalyzerExecutor analyzerExecutor) { var supportedDiagnostics = ImmutableArray <DiagnosticDescriptor> .Empty; // Catch Exception from analyzer.SupportedDiagnostics analyzerExecutor.ExecuteAndCatchIfThrows( analyzer, _ => { var supportedDiagnosticsLocal = analyzer.SupportedDiagnostics; if (!supportedDiagnosticsLocal.IsDefaultOrEmpty) { foreach (var descriptor in supportedDiagnosticsLocal) { if (descriptor == null) { // Disallow null descriptors. throw new ArgumentException(string.Format(CodeAnalysisResources.SupportedDiagnosticsHasNullDescriptor, analyzer.ToString()), nameof(DiagnosticAnalyzer.SupportedDiagnostics)); } } supportedDiagnostics = supportedDiagnosticsLocal; } }, argument: default(object)); // Force evaluate and report exception diagnostics from LocalizableString.ToString(). Action <Exception, DiagnosticAnalyzer, Diagnostic> onAnalyzerException = analyzerExecutor.OnAnalyzerException; if (onAnalyzerException != null) { var handler = new EventHandler <Exception>((sender, ex) => { var diagnostic = AnalyzerExecutor.CreateAnalyzerExceptionDiagnostic(analyzer, ex); onAnalyzerException(ex, analyzer, diagnostic); }); foreach (var descriptor in supportedDiagnostics) { ForceLocalizableStringExceptions(descriptor.Title, handler); ForceLocalizableStringExceptions(descriptor.MessageFormat, handler); ForceLocalizableStringExceptions(descriptor.Description, handler); } } return(supportedDiagnostics); }
/// <summary> /// Create a diagnostic for exception thrown by the given analyzer. /// </summary> /// <remarks> /// Keep this method in sync with "AnalyzerExecutor.CreateAnalyzerExceptionDiagnostic". /// </remarks> internal static Diagnostic CreateAnalyzerExceptionDiagnostic(DiagnosticAnalyzer analyzer, Exception e) { var analyzerName = analyzer.ToString(); // TODO: It is not ideal to create a new descriptor per analyzer exception diagnostic instance. // However, until we add a LongMessage field to the Diagnostic, we are forced to park the instance specific description onto the Descriptor's Description field. // This requires us to create a new DiagnosticDescriptor instance per diagnostic instance. var descriptor = new DiagnosticDescriptor(AnalyzerExceptionDiagnosticId, title: AnalyzerDriverResources.AnalyzerFailure, messageFormat: AnalyzerDriverResources.AnalyzerThrows, description: string.Format(AnalyzerDriverResources.AnalyzerThrowsDescription, analyzerName, e.ToString()), category: AnalyzerExceptionDiagnosticCategory, defaultSeverity: DiagnosticSeverity.Info, isEnabledByDefault: true, customTags: WellKnownDiagnosticTags.AnalyzerException); return Diagnostic.Create(descriptor, Location.None, analyzerName, e.GetType(), e.Message); }