private void AnalyzeCodeBlock(CodeBlockAnalysisContext blockContext, AnalyzerConfig analyzerConfig) { if (blockContext.OwningSymbol is IMethodSymbol methodSymbol && analyzerConfig.ShouldAnalyze(methodSymbol.ContainingType)) { AnalyzeMethodBlock(blockContext, methodSymbol, analyzerConfig); } }
private void CompilationStart(CompilationStartAnalysisContext context) { var checkExceptionAttributes = from attr in context.Compilation.Assembly.GetAttributes() let c = attr.AttributeClass where KnownTypes.CheckExceptionsAttributesFullName == c.GetFullName() select attr; var analyzerConfig = new AnalyzerConfig(checkExceptionAttributes); context.RegisterCodeBlockAction(blockContext => AnalyzeCodeBlock(blockContext, analyzerConfig)); }
private void AnalyzeMethodBlock(CodeBlockAnalysisContext blockContext, IMethodSymbol methodSymbol, AnalyzerConfig analyzerConfig) { var declaredThrowTypes = GetDeclaredTypes(methodSymbol); var throwStatements = blockContext.CodeBlock.DescendantNodes().OfType <ThrowStatementSyntax>(); var offendingThrows = from thrw in throwStatements let exType = blockContext.SemanticModel.GetTypeInfo(thrw.Expression).Type where !exType.InheritsFromOrEqualsAny(declaredThrowTypes) select(expression: thrw, type: exType); var uncaughtThrows = from thrw in offendingThrows where !blockContext.SemanticModel.IsCaught(thrw.type, thrw.expression) select thrw; foreach (var thrw in uncaughtThrows) { var exceptionType = thrw.type; var diagnostic = Diagnostic.Create( Rule, thrw.expression.GetLocation(), methodSymbol.Name, exceptionType.Name ); blockContext.ReportDiagnostic(diagnostic); } }