private CompilationWithAnalyzers GetCompilationWithAnalyzers(Compilation compilation)
        {
            Contract.ThrowIfFalse(_project.SupportsCompilation);

            if (_lazyCompilationWithAnalyzers == null)
            {
                var analyzers = _owner
                                .GetAnalyzers(_project)
                                .Where(a => !CompilationWithAnalyzers.IsDiagnosticAnalyzerSuppressed(a, compilation.Options, _analysisOptions.OnAnalyzerException))
                                .ToImmutableArray()
                                .Distinct();
                Interlocked.CompareExchange(ref _lazyCompilationWithAnalyzers, new CompilationWithAnalyzers(compilation, analyzers, _analysisOptions), null);
            }

            return(_lazyCompilationWithAnalyzers);
        }
Beispiel #2
0
        /// <summary>
        /// Return true if the given <paramref name="analyzer"/> is suppressed for the given project.
        /// </summary>
        public bool IsAnalyzerSuppressed(DiagnosticAnalyzer analyzer, Project project)
        {
            var options = project.CompilationOptions;

            if (options == null || IsCompilerDiagnosticAnalyzer(project.Language, analyzer))
            {
                return(false);
            }

            // don't capture project
            var projectId = project.Id;

            // Skip telemetry logging for supported diagnostics, as that can cause an infinite loop.
            void onAnalyzerException(Exception ex, DiagnosticAnalyzer a, Diagnostic diagnostic) =>
            AnalyzerHelper.OnAnalyzerException_NoTelemetryLogging(ex, a, diagnostic, _hostDiagnosticUpdateSource, projectId);

            return(CompilationWithAnalyzers.IsDiagnosticAnalyzerSuppressed(analyzer, options, onAnalyzerException));
        }
        internal CompilationWithAnalyzers GetCompilationWithAnalyzers(Project project, Compilation compilation, bool concurrentAnalysis, bool reportSuppressedDiagnostics)
        {
            Contract.ThrowIfFalse(project.SupportsCompilation);
            Contract.ThrowIfNull(compilation);

            Func <Exception, bool> analyzerExceptionFilter = ex =>
            {
                if (project.Solution.Workspace.Options.GetOption(InternalDiagnosticsOptions.CrashOnAnalyzerException))
                {
                    // if option is on, crash the host to get crash dump.
                    FatalError.ReportUnlessCanceled(ex);
                }

                return(true);
            };

            var analysisOptions = new CompilationWithAnalyzersOptions(
                new WorkspaceAnalyzerOptions(project.AnalyzerOptions, project.Solution.Workspace),
                GetOnAnalyzerException(project.Id),
                analyzerExceptionFilter,
                concurrentAnalysis,
                logAnalyzerExecutionTime: true,
                reportSuppressedDiagnostics: reportSuppressedDiagnostics);

            var analyzers         = _stateManager.GetAnalyzers(project);
            var filteredAnalyzers = analyzers
                                    .Where(a => !CompilationWithAnalyzers.IsDiagnosticAnalyzerSuppressed(a, compilation.Options, analysisOptions.OnAnalyzerException))
                                    .Distinct()
                                    .ToImmutableArray();

            if (filteredAnalyzers.IsEmpty)
            {
                return(null);
            }

            return(new CompilationWithAnalyzers(compilation, filteredAnalyzers, analysisOptions));
        }
Beispiel #4
0
 /// <summary>
 /// Returns true if all the diagnostics that can be produced by this analyzer are suppressed through options.
 /// </summary>
 public static bool IsDiagnosticAnalyzerSuppressed(this DiagnosticAnalyzer analyzer, CompilationOptions options)
 {
     return(CompilationWithAnalyzers.IsDiagnosticAnalyzerSuppressed(analyzer, options));
 }
Beispiel #5
0
 /// <summary>
 /// Returns true if all the diagnostics that can be produced by this analyzer are suppressed through options.
 /// <paramref name="continueOnError"/> says whether the caller would like the exception thrown by the analyzers to be handled or not. If true - Handles ; False - Not handled.
 /// </summary>
 public static bool IsDiagnosticAnalyzerSuppressed(this DiagnosticAnalyzer analyzer, CompilationOptions options)
 {
     return(CompilationWithAnalyzers.IsDiagnosticAnalyzerSuppressed(analyzer, options, (exception, throwingAnalyzer) => true));
 }