Exemplo n.º 1
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);
 }
Exemplo n.º 2
0
        protected AnalyzerActions GetOrCreateAnalyzerActions(DiagnosticAnalyzer analyzer)
        {
            AnalyzerActions actions;

            if (!_analyzerActions.TryGetValue(analyzer, out actions))
            {
                actions = new AnalyzerActions();
                _analyzerActions[analyzer] = actions;
            }

            return(actions);
        }
Exemplo n.º 3
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);
 }
Exemplo n.º 4
0
        internal protected static void VerifyArguments(
            AnalyzerActions actions,
            AnalyzerOptions analyzerOptions,
            Action <Diagnostic> addDiagnostic,
            Func <Exception, DiagnosticAnalyzer, bool> continueOnAnalyzerException)
        {
            if (actions == null)
            {
                throw new ArgumentNullException(nameof(actions));
            }

            VerifyArguments(analyzerOptions, addDiagnostic, continueOnAnalyzerException);
        }
            public AnalyzerInfo(DiagnosticAnalyzer analyzer, AnalyzerActions analyzerActions, bool telemetry)
            {
                CLRType = analyzer.GetType();
                Telemetry = telemetry;

                Counts[0] = analyzerActions.CodeBlockEndActionsCount;
                Counts[1] = analyzerActions.CodeBlockStartActionsCount;
                Counts[2] = analyzerActions.CompilationEndActionsCount;
                Counts[3] = analyzerActions.CompilationStartActionsCount;
                Counts[4] = analyzerActions.SemanticModelActionsCount;
                Counts[5] = analyzerActions.SymbolActionsCount;
                Counts[6] = analyzerActions.SyntaxNodeActionsCount;
                Counts[7] = analyzerActions.SyntaxTreeActionsCount;
            }
Exemplo n.º 6
0
        private static void VerifyArguments(
            SyntaxTree syntaxTree,
            AnalyzerActions actions,
            AnalyzerOptions analyzerOptions,
            Action <Diagnostic> addDiagnostic,
            Func <Exception, DiagnosticAnalyzer, bool> continueOnAnalyzerException)
        {
            if (syntaxTree == null)
            {
                throw new ArgumentNullException(nameof(syntaxTree));
            }

            VerifyArguments(actions, analyzerOptions, addDiagnostic, continueOnAnalyzerException);
        }
Exemplo n.º 7
0
            private static ImmutableDictionary <DiagnosticAnalyzer, ImmutableArray <ActionType> > GetBlockActionsByAnalyzer <ActionType>(
                ref ImmutableDictionary <DiagnosticAnalyzer, ImmutableArray <ActionType> > lazyCodeBlockActionsByAnalyzer,
                Func <AnalyzerActions, ImmutableArray <ActionType> > codeBlockActionsFactory,
                AnalyzerActions analyzerActions)
                where ActionType : AnalyzerAction
            {
                if (lazyCodeBlockActionsByAnalyzer == null)
                {
                    var codeBlockActionsByAnalyzer = CreateBlockActionsByAnalyzer(codeBlockActionsFactory, analyzerActions);
                    Interlocked.CompareExchange(ref lazyCodeBlockActionsByAnalyzer, codeBlockActionsByAnalyzer, null);
                }

                return(lazyCodeBlockActionsByAnalyzer);
            }
        public void UpdateAnalyzerTypeCount(DiagnosticAnalyzer analyzer, AnalyzerActions analyzerActions)
        {
            var telemetry = DiagnosticAnalyzerLogger.AllowsTelemetry(_owner, analyzer);

            ImmutableInterlocked.AddOrUpdate(
                ref _analyzerInfoMap,
                analyzer.GetType(),
                addValue: new AnalyzerInfo(analyzer, analyzerActions, telemetry),
                updateValueFactory: (k, ai) =>
                {
                    ai.SetAnalyzerTypeCount(analyzerActions);
                    return ai;
                });
        }
Exemplo n.º 9
0
        private static void VerifyArguments <TLanguageKindEnum>(
            Func <SyntaxNode, TLanguageKindEnum> getKind,
            SemanticModel semanticModel,
            AnalyzerActions actions,
            AnalyzerOptions analyzerOptions,
            Action <Diagnostic> addDiagnostic,
            Func <Exception, DiagnosticAnalyzer, bool> continueOnAnalyzerException)
        {
            if (getKind == null)
            {
                throw new ArgumentNullException(nameof(getKind));
            }

            VerifyArguments(semanticModel, actions, analyzerOptions, addDiagnostic, continueOnAnalyzerException);
        }
Exemplo n.º 10
0
        public override AnalyzerActions GetAnalyzerActions(DiagnosticAnalyzer analyzer)
        {
            AnalyzerActions compilationActions = base.GetAnalyzerActions(analyzer);
            AnalyzerActions sessionActions     = _sessionScope.GetAnalyzerActions(analyzer);

            if (sessionActions == null)
            {
                return(compilationActions);
            }

            if (compilationActions == null)
            {
                return(sessionActions);
            }

            return(compilationActions.Append(sessionActions));
        }
Exemplo n.º 11
0
        /// <summary>
        /// Initializes the compilation for the analyzer driver.
        /// It also computes and initializes <see cref="analyzerActions"/> and <see cref="_symbolActionsByKind"/>.
        /// Finally, it initializes and starts the <see cref="_primaryTask"/> for the driver.
        /// </summary>
        /// <remarks>
        /// NOTE: This method must only be invoked from <see cref="AnalyzerDriver.Create(Compilation, ImmutableArray{DiagnosticAnalyzer}, AnalyzerOptions, AnalyzerManager, Action{Diagnostic}, out Compilation, CancellationToken)"/>.
        /// </remarks>
        private void Initialize(Compilation comp, AnalyzerExecutor analyzerExecutor, CancellationToken cancellationToken)
        {
            try
            {
                Debug.Assert(_compilation == null);
                Debug.Assert(comp.EventQueue == this.CompilationEventQueue);

                _compilation          = comp;
                this.analyzerExecutor = analyzerExecutor;

                // Compute the set of effective actions based on suppression, and running the initial analyzers
                var analyzerActionsTask = GetAnalyzerActionsAsync(_analyzers, analyzerManager, analyzerExecutor);
                var initializeTask      = analyzerActionsTask.ContinueWith(t =>
                {
                    this.analyzerActions      = t.Result;
                    _symbolActionsByKind      = MakeSymbolActionsByKind();
                    _semanticModelActionsMap  = MakeSemanticModelActionsByAnalyzer();
                    _compilationActionsMap    = MakeCompilationActionsByAnalyzer(this.analyzerActions.CompilationActions);
                    _compilationEndActionsMap = MakeCompilationActionsByAnalyzer(this.analyzerActions.CompilationEndActions);
                }, cancellationToken);

                // create the primary driver task.
                cancellationToken.ThrowIfCancellationRequested();
                _primaryTask = Task.Run(async() =>
                {
                    await initializeTask.ConfigureAwait(false);
                    await ProcessCompilationEventsAsync(cancellationToken).ConfigureAwait(false);
                    await ExecuteSyntaxTreeActions(cancellationToken).ConfigureAwait(false);
                }, cancellationToken)
                               .ContinueWith(c => DiagnosticQueue.TryComplete(), cancellationToken, TaskContinuationOptions.ExecuteSynchronously, TaskScheduler.Default);
            }
            finally
            {
                if (_primaryTask == null)
                {
                    // Set primaryTask to be a cancelled task.
                    var tcs = new TaskCompletionSource <int>();
                    tcs.SetCanceled();
                    _primaryTask = tcs.Task;

                    // Try to set the DiagnosticQueue to be complete.
                    this.DiagnosticQueue.TryComplete();
                }
            }
        }
Exemplo n.º 12
0
        private static void VerifyArguments(
            Compilation compilation,
            AnalyzerActions actions,
            AnalyzerOptions analyzerOptions,
            DiagnosticAnalyzer analyzer,
            Action <Diagnostic> addDiagnostic,
            Func <Exception, DiagnosticAnalyzer, bool> continueOnAnalyzerException)
        {
            if (compilation == null)
            {
                throw new ArgumentNullException(nameof(compilation));
            }

            if (analyzer == null)
            {
                throw new ArgumentNullException(nameof(analyzer));
            }

            VerifyArguments(actions, analyzerOptions, addDiagnostic, continueOnAnalyzerException);
        }
Exemplo n.º 13
0
        private static void VerifyArguments(
            IEnumerable <ISymbol> symbols,
            Compilation compilation,
            AnalyzerActions actions,
            AnalyzerOptions analyzerOptions,
            Action <Diagnostic> addDiagnostic,
            Func <Exception, DiagnosticAnalyzer, bool> continueOnAnalyzerException)
        {
            if (symbols == null)
            {
                throw new ArgumentNullException(nameof(symbols));
            }

            if (symbols.Any(s => s == null))
            {
                throw new ArgumentException(AnalyzerDriverResources.ArgumentElementCannotBeNull, nameof(symbols));
            }

            VerifyArguments(compilation, actions, analyzerOptions, addDiagnostic, continueOnAnalyzerException);
        }
Exemplo n.º 14
0
        private static Task <AnalyzerActions> GetAnalyzerActionsAsync(
            ImmutableArray <DiagnosticAnalyzer> analyzers,
            AnalyzerManager analyzerManager,
            AnalyzerExecutor analyzerExecutor)
        {
            return(Task.Run(async() =>
            {
                AnalyzerActions allAnalyzerActions = new AnalyzerActions();
                foreach (var analyzer in analyzers)
                {
                    if (!IsDiagnosticAnalyzerSuppressed(analyzer, analyzerExecutor.Compilation.Options, analyzerManager, analyzerExecutor))
                    {
                        var analyzerActions = await analyzerManager.GetAnalyzerActionsAsync(analyzer, analyzerExecutor).ConfigureAwait(false);
                        allAnalyzerActions = allAnalyzerActions.Append(analyzerActions);
                    }
                }

                return allAnalyzerActions;
            }, analyzerExecutor.CancellationToken));
        }
Exemplo n.º 15
0
        /// <summary>
        /// Executes the syntax node actions on the given syntax nodes.
        /// </summary>
        /// <param name="actions"><see cref="AnalyzerActions"/> whose code block start actions and end actions are to be executed.</param>
        /// <param name="nodes">Syntax nodes to be analyzed.</param>
        /// <param name="semanticModel">SemanticModel to be used in the analysis.</param>
        /// <param name="getKind">Delegate to compute language specific syntax kind for a syntax node.</param>
        public void ExecuteSyntaxNodeActions <TLanguageKindEnum>(
            AnalyzerActions actions,
            IEnumerable <SyntaxNode> nodes,
            SemanticModel semanticModel,
            Func <SyntaxNode, TLanguageKindEnum> getKind)
            where TLanguageKindEnum : struct
        {
            var syntaxNodeActions = actions.GetSyntaxNodeActions <TLanguageKindEnum>();

            foreach (var syntaxNodeAction in syntaxNodeActions)
            {
                foreach (var node in nodes)
                {
                    if (syntaxNodeAction.Kinds.Contains(getKind(node)))
                    {
                        ExecuteSyntaxNodeAction(syntaxNodeAction, node, semanticModel);
                    }
                }
            }
        }
Exemplo n.º 16
0
        /// <summary>
        /// Append analyzer actions from <paramref name="otherActions"/> to actions from this instance.
        /// </summary>
        /// <param name="otherActions">Analyzer actions to append</param>.
        public AnalyzerActions Append(AnalyzerActions otherActions)
        {
            if (otherActions == null)
            {
                throw new ArgumentNullException(nameof(otherActions));
            }

            AnalyzerActions actions = new AnalyzerActions();

            actions._compilationStartActions = _compilationStartActions.AddRange(otherActions._compilationStartActions);
            actions._compilationEndActions   = _compilationEndActions.AddRange(otherActions._compilationEndActions);
            actions._syntaxTreeActions       = _syntaxTreeActions.AddRange(otherActions._syntaxTreeActions);
            actions._semanticModelActions    = _semanticModelActions.AddRange(otherActions._semanticModelActions);
            actions._symbolActions           = _symbolActions.AddRange(otherActions._symbolActions);
            actions._codeBlockStartActions   = _codeBlockStartActions.AddRange(otherActions._codeBlockStartActions);
            actions._codeBlockEndActions     = _codeBlockEndActions.AddRange(otherActions._codeBlockEndActions);
            actions._syntaxNodeActions       = _syntaxNodeActions.AddRange(otherActions._syntaxNodeActions);

            return(actions);
        }
Exemplo n.º 17
0
        private static void VerifyArguments <TLanguageKindEnum>(
            IEnumerable <SyntaxNode> nodes,
            Func <SyntaxNode, TLanguageKindEnum> getKind,
            SemanticModel semanticModel,
            AnalyzerActions actions,
            AnalyzerOptions analyzerOptions,
            Action <Diagnostic> addDiagnostic,
            Func <Exception, DiagnosticAnalyzer, bool> continueOnAnalyzerException)
        {
            if (nodes == null)
            {
                throw new ArgumentNullException(nameof(nodes));
            }

            if (nodes.Any(n => n == null))
            {
                throw new ArgumentException(AnalyzerDriverResources.ArgumentElementCannotBeNull, nameof(nodes));
            }

            VerifyArguments(getKind, semanticModel, actions, analyzerOptions, addDiagnostic, continueOnAnalyzerException);
        }
Exemplo n.º 18
0
        /// <summary>
        /// Executes the syntax tree actions on the given tree.
        /// </summary>
        /// <param name="actions"><see cref="AnalyzerActions"/> whose syntax tree actions are to be executed.</param>
        /// <param name="syntaxTree">Syntax tree to analyze.</param>
        /// <param name="analyzerOptions">Analyzer options.</param>
        /// <param name="addDiagnostic">Delegate to add diagnostics.</param>
        /// <param name="continueOnAnalyzerException">Predicate to decide if exceptions from the action should be handled or not.</param>
        /// <param name="cancellationToken">Cancellation token.</param>
        public static void ExecuteSyntaxTreeActions(
            AnalyzerActions actions,
            SyntaxTree syntaxTree,
            AnalyzerOptions analyzerOptions,
            Action <Diagnostic> addDiagnostic,
            Func <Exception, DiagnosticAnalyzer, bool> continueOnAnalyzerException,
            CancellationToken cancellationToken)
        {
            VerifyArguments(syntaxTree, actions, analyzerOptions, addDiagnostic, continueOnAnalyzerException);

            foreach (var syntaxTreeAction in actions.SyntaxTreeActions)
            {
                cancellationToken.ThrowIfCancellationRequested();

                // Catch Exception from action.
                ExecuteAndCatchIfThrows(syntaxTreeAction.Analyzer, addDiagnostic, continueOnAnalyzerException, () =>
                {
                    var context = new SyntaxTreeAnalysisContext(syntaxTree, analyzerOptions, addDiagnostic, cancellationToken);
                    syntaxTreeAction.Action(context);
                }, cancellationToken);
            }
        }
Exemplo n.º 19
0
        /// <summary>
        /// Executes the compilation end actions.
        /// </summary>
        /// <param name="actions"><see cref="AnalyzerActions"/> whose compilation end actions are to be executed.</param>
        /// <param name="compilation">Compilation to be used in the analysis.</param>
        /// <param name="analyzerOptions">Analyzer options.</param>
        /// <param name="addDiagnostic">Delegate to add diagnostics.</param>
        /// <param name="continueOnAnalyzerException">Predicate to decide if exceptions from the action should be handled or not.</param>
        /// <param name="cancellationToken">Cancellation token.</param>
        public static void ExecuteCompilationEndActions(
            AnalyzerActions actions,
            Compilation compilation,
            AnalyzerOptions analyzerOptions,
            Action <Diagnostic> addDiagnostic,
            Func <Exception, DiagnosticAnalyzer, bool> continueOnAnalyzerException,
            CancellationToken cancellationToken)
        {
            VerifyArguments(compilation, actions, analyzerOptions, addDiagnostic, continueOnAnalyzerException);

            HostCompilationStartAnalysisScope compilationScope = new HostCompilationStartAnalysisScope(new HostSessionStartAnalysisScope());

            foreach (var endAction in actions.CompilationEndActions)
            {
                cancellationToken.ThrowIfCancellationRequested();
                ExecuteAndCatchIfThrows(endAction.Analyzer, addDiagnostic, continueOnAnalyzerException, () =>
                {
                    var context = new CompilationEndAnalysisContext(compilation, analyzerOptions, addDiagnostic, cancellationToken);
                    endAction.Action(context);
                }, cancellationToken);
            }
        }
Exemplo n.º 20
0
 private GroupedAnalyzerActions(AnalyzerActions analyzerActions)
 {
     _analyzerActions = analyzerActions;
 }
Exemplo n.º 21
0
 /// <summary>
 /// Executes the syntax tree actions on the given syntax tree.
 /// </summary>
 /// <param name="actions"><see cref="AnalyzerActions"/> whose syntax tree actions are to be executed.</param>
 /// <param name="tree">Syntax tree to analyze.</param>
 public void ExecuteSyntaxTreeActions(AnalyzerActions actions, SyntaxTree tree)
 {
     ExecuteSyntaxTreeActions(actions.SyntaxTreeActions, tree);
 }
Exemplo n.º 22
0
 /// <summary>
 /// Executes the semantic model actions on the given semantic model.
 /// </summary>
 /// <param name="actions"><see cref="AnalyzerActions"/> whose semantic model actions are to be executed.</param>
 /// <param name="semanticModel">Semantic model to analyze.</param>
 public void ExecuteSemanticModelActions(AnalyzerActions actions, SemanticModel semanticModel)
 {
     ExecuteSemanticModelActions(actions.SemanticModelActions, semanticModel);
 }
Exemplo n.º 23
0
 /// <summary>
 /// Executes the symbol actions on the given symbols.
 /// </summary>
 /// <param name="actions"><see cref="AnalyzerActions"/> whose symbol actions are to be executed.</param>
 /// <param name="symbols">Symbols to be analyzed.</param>
 public void ExecuteSymbolActions(AnalyzerActions actions, IEnumerable <ISymbol> symbols)
 {
     ExecuteSymbolActions(actions.SymbolActions, symbols);
 }
Exemplo n.º 24
0
 /// <summary>
 /// Executes the compilation end actions.
 /// </summary>
 /// <param name="actions"><see cref="AnalyzerActions"/> whose compilation end actions are to be executed.</param>
 public void ExecuteCompilationEndActions(AnalyzerActions actions)
 {
     ExecuteCompilationEndActions(actions.CompilationEndActions);
 }