Ejemplo n.º 1
0
        private AnalyzerActions GetPerCompilationAnalyzerActions(
            DiagnosticAnalyzer analyzer,
            AnalyzerActions sessionActions,
            Compilation compilation,
            Action <Diagnostic> addDiagnostic)
        {
            Contract.ThrowIfFalse(sessionActions.CompilationStartActionsCount > 0);

            var             compilationActionsMap = compilationActionsTable.GetOrCreateValue(compilation);
            AnalyzerActions result;

            if (compilationActionsMap.TryGetValue(analyzer, out result))
            {
                return(result);
            }

            result = AnalyzerDriverHelper.ExecuteCompilationStartActions(sessionActions, analyzer, compilation,
                                                                         this.analyzerOptions, addDiagnostic, CatchAnalyzerException, this.cancellationToken);

            if (!compilationActionsMap.TryAdd(analyzer, result))
            {
                return(compilationActionsMap[analyzer]);
            }

            return(result);
        }
Ejemplo n.º 2
0
        private static ImmutableArray <Diagnostic> AnalyzerExceptionToDiagnostics(DiagnosticAnalyzer analyzer, Exception e, CancellationToken cancellationToken)
        {
            if (!IsCanceled(e, cancellationToken))
            {
                // Create a info diagnostic saying that the analyzer failed
                return(ImmutableArray.Create(AnalyzerDriverHelper.GetAnalyzerDiagnostic(analyzer, e)));
            }

            return(ImmutableArray <Diagnostic> .Empty);
        }
Ejemplo n.º 3
0
 private Tuple <AnalyzerActions, ImmutableArray <Diagnostic> > GetSessionAnalyzerActionsAndDiagnosticsCore(DiagnosticAnalyzer analyzer)
 {
     using (var pooledObject = SharedPools.Default <List <Diagnostic> >().GetPooledObject())
     {
         var diagnostics = pooledObject.Object;
         var actions     = AnalyzerDriverHelper.GetSessionAnalyzerActions(analyzer, diagnostics.Add, CatchAnalyzerException, cancellationToken);
         var diagArray   = diagnostics.ToImmutableArrayOrEmpty();
         return(Tuple.Create(actions, diagArray));
     }
 }
Ejemplo n.º 4
0
 public void ExecuteCodeBlockActions(
     AnalyzerActions actions,
     IEnumerable <DeclarationInfo> declarationsInNode,
     SemanticModel semanticModel,
     AnalyzerOptions analyzerOptions,
     Action <Diagnostic> reportDiagnostic,
     Func <Exception, DiagnosticAnalyzer, bool> continueOnAnalyzerException,
     CancellationToken cancellationToken)
 {
     AnalyzerDriverHelper.ExecuteCodeBlockActions(actions, declarationsInNode,
                                                  semanticModel, analyzerOptions, reportDiagnostic, continueOnAnalyzerException, this.GetKind, cancellationToken);
 }
Ejemplo n.º 5
0
 public void ExecuteSyntaxNodeActions(
     AnalyzerActions actions,
     IEnumerable <SyntaxNode> descendantNodes,
     SemanticModel semanticModel,
     AnalyzerOptions analyzerOptions,
     Action <Diagnostic> reportDiagnostic,
     Func <Exception, DiagnosticAnalyzer, bool> continueOnAnalyzerException,
     CancellationToken cancellationToken)
 {
     AnalyzerDriverHelper.ExecuteSyntaxNodeActions(actions, descendantNodes, semanticModel,
                                                   analyzerOptions, reportDiagnostic, continueOnAnalyzerException, this.GetKind, cancellationToken);
 }
Ejemplo n.º 6
0
        public async Task <IEnumerable <Diagnostic> > GetSyntaxDiagnosticsAsync(DiagnosticAnalyzer analyzer)
        {
            var compilation = this.document.Project.SupportsCompilation ? await this.document.Project.GetCompilationAsync(cancellationToken).ConfigureAwait(false) : null;

            Contract.ThrowIfNull(this.document);
            Contract.ThrowIfFalse(analyzer.SupportsSyntaxDiagnosticAnalysis(this));

            using (var pooledObject = SharedPools.Default <List <Diagnostic> >().GetPooledObject())
            {
                var diagnostics = pooledObject.Object;

                this.cancellationToken.ThrowIfCancellationRequested();
                var documentAnalyzer = analyzer as DocumentDiagnosticAnalyzer;
                if (documentAnalyzer != null)
                {
                    try
                    {
                        await documentAnalyzer.AnalyzeSyntaxAsync(this.document, diagnostics.Add, this.cancellationToken).ConfigureAwait(false);

                        return(diagnostics.ToImmutableArrayOrEmpty());
                    }
                    catch (Exception e) when(CatchAnalyzerException(e, analyzer))
                    {
                        var exceptionDiagnostics = AnalyzerExceptionToDiagnostics(analyzer, e, cancellationToken);

                        return(GetFilteredDocumentDiagnostics(exceptionDiagnostics, compilation).ToImmutableArray());
                    }
                }

                var analyzerActions = await this.GetAnalyzerActionsAsync(analyzer, diagnostics.Add).ConfigureAwait(false);

                DiagnosticAnalyzerLogger.UpdateAnalyzerTypeCount(analyzer, analyzerActions, (DiagnosticLogAggregator)logAggregator);

                if (analyzerActions != null)
                {
                    if (this.document.SupportsSyntaxTree)
                    {
                        AnalyzerDriverHelper.ExecuteSyntaxTreeActions(analyzerActions, this.root.SyntaxTree,
                                                                      this.analyzerOptions, diagnostics.Add, CatchAnalyzerException, this.cancellationToken);
                    }
                }

                if (diagnostics.Count == 0)
                {
                    return(SpecializedCollections.EmptyEnumerable <Diagnostic>());
                }

                return(GetFilteredDocumentDiagnostics(diagnostics, compilation).ToImmutableArray());
            }
        }
Ejemplo n.º 7
0
        private async Task GetCompilationDiagnosticsAsync(DiagnosticAnalyzer analyzer, List <Diagnostic> diagnostics, Action <Project, DiagnosticAnalyzer, CancellationToken> forceAnalyzeAllDocuments)
        {
            Contract.ThrowIfFalse(this.project.SupportsCompilation);

            var analyzerActionsAndDiagnostics = GetSessionAnalyzerActionsAndDiagnostics(analyzer);
            var analyzerActions = analyzerActionsAndDiagnostics.Item1;

            var compilation = await project.GetCompilationAsync(cancellationToken).ConfigureAwait(false);

            diagnostics.AddRange(CompilationWithAnalyzers.GetEffectiveDiagnostics(analyzerActionsAndDiagnostics.Item2, compilation));

            DiagnosticAnalyzerLogger.UpdateAnalyzerTypeCount(analyzer, analyzerActions, (DiagnosticLogAggregator)logAggregator);

            if (analyzerActions == null || (analyzerActions.CompilationStartActionsCount == 0 && analyzerActions.CompilationEndActionsCount == 0))
            {
                return;
            }

            using (var pooledObject = SharedPools.Default <List <Diagnostic> >().GetPooledObject())
            {
                var localDiagnostics = pooledObject.Object;

                // Get all the analyzer actions, including the per-compilation actions.
                var allAnalyzerActions = await GetAnalyzerActionsAsync(analyzer, localDiagnostics.Add).ConfigureAwait(false);

                if (analyzerActions.CompilationEndActionsCount < allAnalyzerActions.CompilationEndActionsCount && forceAnalyzeAllDocuments != null)
                {
                    if (allAnalyzerActions.CodeBlockEndActionsCount > analyzerActions.CodeBlockEndActionsCount ||
                        allAnalyzerActions.CodeBlockStartActionsCount > analyzerActions.CodeBlockStartActionsCount ||
                        allAnalyzerActions.SemanticModelActionsCount > analyzerActions.SemanticModelActionsCount ||
                        allAnalyzerActions.SymbolActionsCount > analyzerActions.SymbolActionsCount ||
                        allAnalyzerActions.SyntaxNodeActionsCount > analyzerActions.SyntaxNodeActionsCount ||
                        allAnalyzerActions.SyntaxTreeActionsCount > analyzerActions.SyntaxTreeActionsCount)
                    {
                        // Analyzer registered a compilation end action and at least one other analyzer action during it's compilation start action.
                        // We need to ensure that we have force analyzed all documents in this project for this analyzer before executing the end actions.
                        forceAnalyzeAllDocuments(project, analyzer, cancellationToken);
                    }
                }

                // CompilationEnd actions.
                AnalyzerDriverHelper.ExecuteCompilationEndActions(allAnalyzerActions, compilation, analyzerOptions, localDiagnostics.Add, CatchAnalyzerException, cancellationToken);
                diagnostics.AddRange(CompilationWithAnalyzers.GetEffectiveDiagnostics(localDiagnostics, compilation));
            }
        }
Ejemplo n.º 8
0
        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());
            }
        }