public bool SupportAnalysisKind(DiagnosticAnalyzer analyzer, string language, AnalysisKind kind) { // compiler diagnostic analyzer always supports all kinds: if (IsCompilerDiagnosticAnalyzer(language, analyzer)) { return(true); } return(kind switch { AnalysisKind.Syntax => analyzer.SupportsSyntaxDiagnosticAnalysis(), AnalysisKind.Semantic => analyzer.SupportsSemanticDiagnosticAnalysis(), _ => throw ExceptionUtilities.UnexpectedValue(kind) });
private static bool ShouldRunProviderForStateType(StateType stateTypeId, DiagnosticAnalyzer provider, DiagnosticAnalyzerDriver driver, out bool supportsSemanticInSpan, ImmutableHashSet<string> diagnosticIds = null, Func<DiagnosticAnalyzer, ImmutableArray<DiagnosticDescriptor>> getDescriptor = null) { Debug.Assert(!IsAnalyzerSuppressed(provider, driver.Project.CompilationOptions, driver)); supportsSemanticInSpan = false; if (diagnosticIds != null && getDescriptor(provider).All(d => !diagnosticIds.Contains(d.Id))) { return false; } switch (stateTypeId) { case StateType.Syntax: return provider.SupportsSyntaxDiagnosticAnalysis(driver); case StateType.Document: return provider.SupportsSemanticDiagnosticAnalysis(driver, out supportsSemanticInSpan); case StateType.Project: return provider.SupportsProjectDiagnosticAnalysis(driver); default: throw ExceptionUtilities.Unreachable; } }
public async Task <IEnumerable <Diagnostic> > GetSemanticDiagnosticsAsync(DiagnosticAnalyzer analyzer) { var model = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false); Contract.ThrowIfNull(this.document); Contract.ThrowIfFalse(analyzer.SupportsSemanticDiagnosticAnalysis(this)); using (var pooledObject = SharedPools.Default <List <Diagnostic> >().GetPooledObject()) { var diagnostics = pooledObject.Object; // Stateless semantic analyzers: // 1) ISemanticModelAnalyzer/IDocumentBasedDiagnosticAnalyzer // 2) ISymbolAnalyzer // 3) ISyntaxNodeAnalyzer cancellationToken.ThrowIfCancellationRequested(); var documentAnalyzer = analyzer as DocumentDiagnosticAnalyzer; if (documentAnalyzer != null) { try { await documentAnalyzer.AnalyzeSemanticsAsync(this.document, diagnostics.Add, this.cancellationToken).ConfigureAwait(false); } catch (Exception e) when(CatchAnalyzerException(e, analyzer)) { var exceptionDiagnostics = AnalyzerExceptionToDiagnostics(analyzer, e, cancellationToken); return(model == null ? exceptionDiagnostics : GetFilteredDocumentDiagnostics(exceptionDiagnostics, model.Compilation)); } } else { var analyzerActions = await GetAnalyzerActionsAsync(analyzer, diagnostics.Add).ConfigureAwait(false); if (analyzerActions != null) { // SemanticModel actions. if (analyzerActions.SemanticModelActionsCount > 0) { AnalyzerDriverHelper.ExecuteSemanticModelActions(analyzerActions, model, this.analyzerOptions, diagnostics.Add, CatchAnalyzerException, cancellationToken); } var compilation = model.Compilation; // Symbol actions. if (analyzerActions.SymbolActionsCount > 0) { var symbols = this.GetSymbolsToAnalyze(model); AnalyzerDriverHelper.ExecuteSymbolActions(analyzerActions, symbols, compilation, this.analyzerOptions, diagnostics.Add, CatchAnalyzerException, this.cancellationToken); } if (this.SyntaxNodeAnalyzerService != null) { // SyntaxNode actions. if (analyzerActions.SyntaxNodeActionsCount > 0) { this.SyntaxNodeAnalyzerService.ExecuteSyntaxNodeActions(analyzerActions, GetSyntaxNodesToAnalyze(), model, this.analyzerOptions, diagnostics.Add, CatchAnalyzerException, cancellationToken); } // CodeBlockStart, CodeBlockEnd, and generated SyntaxNode actions. if (analyzerActions.CodeBlockStartActionsCount > 0 || analyzerActions.CodeBlockEndActionsCount > 0) { this.SyntaxNodeAnalyzerService.ExecuteCodeBlockActions(analyzerActions, this.GetDeclarationInfos(model), model, this.analyzerOptions, diagnostics.Add, CatchAnalyzerException, cancellationToken); } } } } var result = model == null ? diagnostics : GetFilteredDocumentDiagnostics(diagnostics, model.Compilation); return(result.ToImmutableArray()); } }
public async Task<IEnumerable<Diagnostic>> GetSemanticDiagnosticsAsync(DiagnosticAnalyzer analyzer) { var model = await _document.GetSemanticModelAsync(_cancellationToken).ConfigureAwait(false); Contract.ThrowIfNull(_document); Contract.ThrowIfFalse(analyzer.SupportsSemanticDiagnosticAnalysis(this)); using (var pooledObject = SharedPools.Default<List<Diagnostic>>().GetPooledObject()) { var diagnostics = pooledObject.Object; // Stateless semantic analyzers: // 1) ISemanticModelAnalyzer/IDocumentBasedDiagnosticAnalyzer // 2) ISymbolAnalyzer // 3) ISyntaxNodeAnalyzer _cancellationToken.ThrowIfCancellationRequested(); var documentAnalyzer = analyzer as DocumentDiagnosticAnalyzer; if (documentAnalyzer != null) { try { await documentAnalyzer.AnalyzeSemanticsAsync(_document, diagnostics.Add, _cancellationToken).ConfigureAwait(false); } catch (Exception e) when(CatchAnalyzerException(e, analyzer)) { var exceptionDiagnostics = AnalyzerExceptionToDiagnostics(analyzer, e, _cancellationToken); return model == null ? exceptionDiagnostics : GetFilteredDocumentDiagnostics(exceptionDiagnostics, model.Compilation); } } else { var analyzerActions = await GetAnalyzerActionsAsync(analyzer, diagnostics.Add).ConfigureAwait(false); if (analyzerActions != null) { // SemanticModel actions. if (analyzerActions.SemanticModelActionsCount > 0) { AnalyzerDriverHelper.ExecuteSemanticModelActions(analyzerActions, model, _analyzerOptions, diagnostics.Add, CatchAnalyzerException, _cancellationToken); } var compilation = model.Compilation; // Symbol actions. if (analyzerActions.SymbolActionsCount > 0) { var symbols = this.GetSymbolsToAnalyze(model); AnalyzerDriverHelper.ExecuteSymbolActions(analyzerActions, symbols, compilation, _analyzerOptions, diagnostics.Add, CatchAnalyzerException, _cancellationToken); } if (this.SyntaxNodeAnalyzerService != null) { // SyntaxNode actions. if (analyzerActions.SyntaxNodeActionsCount > 0) { this.SyntaxNodeAnalyzerService.ExecuteSyntaxNodeActions(analyzerActions, GetSyntaxNodesToAnalyze(), model, _analyzerOptions, diagnostics.Add, CatchAnalyzerException, _cancellationToken); } // CodeBlockStart, CodeBlockEnd, and generated SyntaxNode actions. if (analyzerActions.CodeBlockStartActionsCount > 0 || analyzerActions.CodeBlockEndActionsCount > 0) { this.SyntaxNodeAnalyzerService.ExecuteCodeBlockActions(analyzerActions, this.GetDeclarationInfos(model), model, _analyzerOptions, diagnostics.Add, CatchAnalyzerException, _cancellationToken); } } } } var result = model == null ? diagnostics : GetFilteredDocumentDiagnostics(diagnostics, model.Compilation); return result.ToImmutableArray(); } }