Example #1
0
        /// <summary>
        /// Executes the compilation start actions and returns the per-compilation analyzer actions added by these actions.
        /// </summary>
        /// <param name="actions"><see cref="AnalyzerActions"/> whose compilation start actions are to be executed.</param>
        /// <param name="analyzer">Analyzer on which compilation start actions have 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 AnalyzerActions ExecuteCompilationStartActions(
            AnalyzerActions actions,
            DiagnosticAnalyzer analyzer,
            Compilation compilation,
            AnalyzerOptions analyzerOptions,
            Action <Diagnostic> addDiagnostic,
            Func <Exception, DiagnosticAnalyzer, bool> continueOnAnalyzerException,
            CancellationToken cancellationToken)
        {
            VerifyArguments(compilation, actions, analyzerOptions, analyzer, addDiagnostic, continueOnAnalyzerException);

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

            foreach (var startAction in actions.CompilationStartActions)
            {
                if (startAction.Analyzer == analyzer)
                {
                    cancellationToken.ThrowIfCancellationRequested();
                    ExecuteAndCatchIfThrows(analyzer, addDiagnostic, continueOnAnalyzerException, () =>
                    {
                        startAction.Action(new AnalyzerCompilationStartAnalysisContext(analyzer, compilationScope, compilation, analyzerOptions, cancellationToken));
                    }, cancellationToken);
                }
            }

            return(compilationScope.GetAnalyzerActions(analyzer));
        }
Example #2
0
 public AnalyzerCompilationStartAnalysisContext(
     DiagnosticAnalyzer analyzer,
     HostCompilationStartAnalysisScope scope,
     Compilation compilation,
     AnalyzerOptions options,
     CompilationAnalysisValueProviderFactory compilationAnalysisValueProviderFactory,
     CancellationToken cancellationToken)
     : base(compilation, options, cancellationToken)
 {
     _analyzer = analyzer;
     _scope    = scope;
     _compilationAnalysisValueProviderFactory = compilationAnalysisValueProviderFactory;
 }
Example #3
0
        /// <summary>
        /// Get all the analyzer actions to execute for the given analyzer against a given compilation.
        /// The returned actions include the actions registered during <see cref="DiagnosticAnalyzer.Initialize(AnalysisContext)"/> method as well as
        /// the actions registered during <see cref="CompilationStartAnalyzerAction"/> for the given compilation.
        /// </summary>
        public async Task <AnalyzerActions> GetAnalyzerActionsAsync(DiagnosticAnalyzer analyzer, AnalyzerExecutor analyzerExecutor)
        {
            HostSessionStartAnalysisScope sessionScope = await GetSessionAnalysisScopeAsync(analyzer, analyzerExecutor).ConfigureAwait(false);

            if (sessionScope.CompilationStartActions.Length > 0 && analyzerExecutor.Compilation != null)
            {
                HostCompilationStartAnalysisScope compilationScope = await GetCompilationAnalysisScopeAsync(analyzer, sessionScope, analyzerExecutor).ConfigureAwait(false);

                return(compilationScope.GetAnalyzerActions(analyzer));
            }

            return(sessionScope.GetAnalyzerActions(analyzer));
        }
Example #4
0
        private Task <HostCompilationStartAnalysisScope> GetCompilationAnalysisScopeCoreAsync(
            DiagnosticAnalyzer analyzer,
            HostSessionStartAnalysisScope sessionScope,
            AnalyzerExecutor analyzerExecutor)
        {
            Func <DiagnosticAnalyzer, Task <HostCompilationStartAnalysisScope> > getTask = a =>
            {
                return(Task.Run(() =>
                {
                    var compilationAnalysisScope = new HostCompilationStartAnalysisScope(sessionScope);
                    analyzerExecutor.ExecuteCompilationStartActions(sessionScope.CompilationStartActions, compilationAnalysisScope);
                    return compilationAnalysisScope;
                }, analyzerExecutor.CancellationToken));
            };

            var compilationActionsMap = _compilationScopeMap.GetOrCreateValue(analyzerExecutor.Compilation);

            return(compilationActionsMap.GetOrAdd(analyzer, getTask));
        }
            public Task <HostCompilationStartAnalysisScope> GetCompilationAnalysisScopeAsync(
                HostSessionStartAnalysisScope sessionScope,
                AnalyzerExecutor analyzerExecutor)
            {
                lock (_gate)
                {
                    if (_lazyCompilationScopeTask == null)
                    {
                        _lazyCompilationScopeTask = Task.Run(() =>
                        {
                            var compilationAnalysisScope = new HostCompilationStartAnalysisScope(sessionScope);
                            analyzerExecutor.ExecuteCompilationStartActions(sessionScope.GetAnalyzerActions(_analyzer).CompilationStartActions, compilationAnalysisScope);
                            return(compilationAnalysisScope);
                        }, analyzerExecutor.CancellationToken);
                    }

                    return(_lazyCompilationScopeTask);
                }
            }
Example #6
0
        private Task <HostCompilationStartAnalysisScope> GetCompilationAnalysisScopeCoreAsync(
            AnalyzerAndOptions analyzerAndOptions,
            HostSessionStartAnalysisScope sessionScope,
            AnalyzerExecutor analyzerExecutor)
        {
            Func <AnalyzerAndOptions, Task <HostCompilationStartAnalysisScope> > getTask = a =>
            {
                return(Task.Run(() =>
                {
                    var compilationAnalysisScope = new HostCompilationStartAnalysisScope(sessionScope);
                    analyzerExecutor.ExecuteCompilationStartActions(sessionScope.CompilationStartActions, compilationAnalysisScope);
                    return compilationAnalysisScope;
                }, analyzerExecutor.CancellationToken));
            };

            var compilationActionsMap = _compilationScopeMap.GetValue(analyzerExecutor.Compilation, _compilationScopeMapCallback);

            return(compilationActionsMap.GetOrAdd(analyzerAndOptions, getTask));
        }
            public Task <HostCompilationStartAnalysisScope> GetCompilationAnalysisScopeAsync(
                HostSessionStartAnalysisScope sessionScope,
                AnalyzerExecutor analyzerExecutor)
            {
                Func <Compilation, Task <HostCompilationStartAnalysisScope> > getTask = comp =>
                {
                    return(Task.Run(() =>
                    {
                        var compilationAnalysisScope = new HostCompilationStartAnalysisScope(sessionScope);
                        analyzerExecutor.ExecuteCompilationStartActions(sessionScope.CompilationStartActions, compilationAnalysisScope);
                        return compilationAnalysisScope;
                    }, analyzerExecutor.CancellationToken));
                };

                var callback = new ConditionalWeakTable <Compilation, Task <HostCompilationStartAnalysisScope> > .CreateValueCallback(getTask);

                var compilationActionsCache = GetOrCreateCompilationActionsCache(analyzerExecutor.AnalyzerOptions);

                return(compilationActionsCache.GetValue(analyzerExecutor.Compilation, callback));
            }
        private Task <HostCompilationStartAnalysisScope> GetCompilationAnalysisScopeCoreAsync(
            AnalyzerAndOptions analyzerAndOptions,
            HostSessionStartAnalysisScope sessionScope,
            AnalyzerExecutor analyzerExecutor)
        {
            Func <Compilation, Task <HostCompilationStartAnalysisScope> > getTask = comp =>
            {
                return(Task.Run(() =>
                {
                    var compilationAnalysisScope = new HostCompilationStartAnalysisScope(sessionScope);
                    analyzerExecutor.ExecuteCompilationStartActions(sessionScope.CompilationStartActions, compilationAnalysisScope);
                    return compilationAnalysisScope;
                }, analyzerExecutor.CancellationToken));
            };

            var callback = new ConditionalWeakTable <Compilation, Task <HostCompilationStartAnalysisScope> > .CreateValueCallback(getTask);

            var compilationActionsMap = _compilationScopeMap.GetOrAdd(analyzerAndOptions, new ConditionalWeakTable <Compilation, Task <HostCompilationStartAnalysisScope> >());

            return(compilationActionsMap.GetValue(analyzerExecutor.Compilation, callback));
        }
Example #9
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);
            }
        }
Example #10
0
            public Task <HostCompilationStartAnalysisScope> GetCompilationAnalysisScopeAsync(
                HostSessionStartAnalysisScope sessionScope,
                AnalyzerExecutor analyzerExecutor)
            {
                lock (_gate)
                {
                    _lazyCompilationScopeCache = _lazyCompilationScopeCache ?? new ConditionalWeakTable <Compilation, Dictionary <AnalyzerOptions, Task <HostCompilationStartAnalysisScope> > >();
                    var compilationActionsMap = _lazyCompilationScopeCache.GetOrCreateValue(analyzerExecutor.Compilation);
                    Task <HostCompilationStartAnalysisScope> task;
                    if (!compilationActionsMap.TryGetValue(analyzerExecutor.AnalyzerOptions, out task))
                    {
                        task = Task.Run(() =>
                        {
                            var compilationAnalysisScope = new HostCompilationStartAnalysisScope(sessionScope);
                            analyzerExecutor.ExecuteCompilationStartActions(sessionScope.CompilationStartActions, compilationAnalysisScope);
                            return(compilationAnalysisScope);
                        }, analyzerExecutor.CancellationToken);

                        compilationActionsMap.Add(analyzerExecutor.AnalyzerOptions, task);
                    }

                    return(task);
                }
            }
 public AnalyzerCompilationStartAnalysisContext(DiagnosticAnalyzer analyzer, HostCompilationStartAnalysisScope scope, Compilation compilation, AnalyzerOptions options, CancellationToken cancellationToken)
     : base(compilation, options, cancellationToken)
 {
     _analyzer = analyzer;
     _scope    = scope;
 }
Example #12
0
 /// <summary>
 /// Executes the compilation start actions.
 /// </summary>
 /// <param name="actions"><see cref="AnalyzerActions"/> whose compilation start actions are to be executed.</param>
 /// <param name="compilationScope">Compilation scope to store the analyzer actions.</param>
 public void ExecuteCompilationStartActions(ImmutableArray <CompilationStartAnalyzerAction> actions, HostCompilationStartAnalysisScope compilationScope)
 {
     foreach (var startAction in actions)
     {
         _cancellationToken.ThrowIfCancellationRequested();
         ExecuteAndCatchIfThrows(startAction.Analyzer,
                                 () => startAction.Action(new AnalyzerCompilationStartAnalysisContext(startAction.Analyzer, compilationScope, _compilation, _analyzerOptions, _cancellationToken)));
     }
 }