// internal for testing purposes internal static AnalyzerDriver Create( Compilation compilation, ImmutableArray <DiagnosticAnalyzer> analyzers, AnalyzerOptions options, AnalyzerManager analyzerManager, Action <Diagnostic> addExceptionDiagnostic, out Compilation newCompilation, Func <Exception, DiagnosticAnalyzer, bool> continueOnAnalyzerException, CancellationToken cancellationToken) { options = options ?? AnalyzerOptions.Empty; AnalyzerDriver analyzerDriver = compilation.AnalyzerForLanguage(analyzers, analyzerManager, cancellationToken); newCompilation = compilation.WithEventQueue(analyzerDriver.CompilationEventQueue); continueOnAnalyzerException = continueOnAnalyzerException ?? ((exception, analyzer) => true); var addDiagnostic = GetDiagnosticSinkWithSuppression(analyzerDriver.DiagnosticQueue.Enqueue, newCompilation); addExceptionDiagnostic = addExceptionDiagnostic != null? GetDiagnosticSinkWithSuppression(addExceptionDiagnostic, newCompilation) : addDiagnostic; var analyzerExecutor = AnalyzerExecutor.Create(newCompilation, options, addDiagnostic, addExceptionDiagnostic, continueOnAnalyzerException, cancellationToken); analyzerDriver.Initialize(newCompilation, analyzerExecutor, cancellationToken); return(analyzerDriver); }
private CompilationWithAnalyzers(Compilation compilation, ImmutableArray <DiagnosticAnalyzer> analyzers, CompilationWithAnalyzersOptions analysisOptions, CancellationToken cancellationToken) { VerifyArguments(compilation, analyzers, analysisOptions); _compilation = compilation.WithEventQueue(new AsyncQueue <CompilationEvent>()); _analyzers = analyzers; _analysisOptions = analysisOptions; _cancellationToken = cancellationToken; _analysisState = new AnalysisState(analyzers); _analysisResult = new AnalysisResult(analysisOptions.LogAnalyzerExecutionTime, analyzers); _driverPool = new ObjectPool <AnalyzerDriver>(() => compilation.AnalyzerForLanguage(analyzers, AnalyzerManager.Instance)); _executingConcurrentTreeTasksOpt = analysisOptions.ConcurrentAnalysis ? new Dictionary <SyntaxTree, Tuple <Task, CancellationTokenSource> >() : null; _concurrentTreeTaskTokensOpt = analysisOptions.ConcurrentAnalysis ? new Dictionary <Task, int>() : null; _executingCompilationOrNonConcurrentTreeTask = null; }
// internal for testing purposes internal static AnalyzerDriver Create( Compilation compilation, ImmutableArray <DiagnosticAnalyzer> analyzers, AnalyzerOptions options, AnalyzerManager analyzerManager, Action <Exception, DiagnosticAnalyzer, Diagnostic> onAnalyzerException, out Compilation newCompilation, CancellationToken cancellationToken) { options = options ?? AnalyzerOptions.Empty; AnalyzerDriver analyzerDriver = compilation.AnalyzerForLanguage(analyzers, analyzerManager, cancellationToken); newCompilation = compilation.WithEventQueue(analyzerDriver.CompilationEventQueue); var addDiagnostic = GetDiagnosticSinkWithSuppression(analyzerDriver.DiagnosticQueue.Enqueue, newCompilation); Action <Exception, DiagnosticAnalyzer, Diagnostic> newOnAnalyzerException; if (onAnalyzerException != null) { // Wrap onAnalyzerException to pass in filtered diagnostic. var comp = newCompilation; newOnAnalyzerException = (ex, analyzer, diagnostic) => onAnalyzerException(ex, analyzer, GetFilteredDiagnostic(diagnostic, comp)); } else { // Add exception diagnostic to regular diagnostic bag. newOnAnalyzerException = (ex, analyzer, diagnostic) => addDiagnostic(diagnostic); } var analyzerExecutor = AnalyzerExecutor.Create(newCompilation, options, addDiagnostic, newOnAnalyzerException, cancellationToken); analyzerDriver.Initialize(newCompilation, analyzerExecutor, cancellationToken); return(analyzerDriver); }
// internal for testing purposes internal static AnalyzerDriver CreateAndAttachToCompilation( Compilation compilation, ImmutableArray<DiagnosticAnalyzer> analyzers, AnalyzerOptions options, AnalyzerManager analyzerManager, Action<Exception, DiagnosticAnalyzer, Diagnostic> onAnalyzerException, bool reportAnalyzer, out Compilation newCompilation, CancellationToken cancellationToken) { AnalyzerDriver analyzerDriver = compilation.AnalyzerForLanguage(analyzers, analyzerManager); newCompilation = compilation.WithEventQueue(new AsyncQueue<CompilationEvent>()); var categorizeDiagnostics = false; var analysisOptions = new CompilationWithAnalyzersOptions(options, onAnalyzerException, concurrentAnalysis: true, logAnalyzerExecutionTime: reportAnalyzer); analyzerDriver.Initialize(newCompilation, analysisOptions, categorizeDiagnostics, cancellationToken); var analysisScope = new AnalysisScope(newCompilation, analyzers, concurrentAnalysis: newCompilation.Options.ConcurrentBuild, categorizeDiagnostics: categorizeDiagnostics); analyzerDriver.AttachQueueAndStartProcessingEvents(newCompilation.EventQueue, analysisScope, cancellationToken: cancellationToken); return analyzerDriver; }
private CompilationWithAnalyzers(Compilation compilation, ImmutableArray<DiagnosticAnalyzer> analyzers, CompilationWithAnalyzersOptions analysisOptions, CancellationToken cancellationToken) { VerifyArguments(compilation, analyzers, analysisOptions); _compilation = compilation.WithEventQueue(new AsyncQueue<CompilationEvent>()); _analyzers = analyzers; _analysisOptions = analysisOptions; _cancellationToken = cancellationToken; _analysisState = new AnalysisState(analyzers); _analysisResult = new AnalysisResult(analysisOptions.LogAnalyzerExecutionTime, analyzers); _driverPool = new ObjectPool<AnalyzerDriver>(() => compilation.AnalyzerForLanguage(analyzers, AnalyzerManager.Instance)); _executingConcurrentTreeTasksOpt = analysisOptions.ConcurrentAnalysis ? new Dictionary<SyntaxTree, Tuple<Task, CancellationTokenSource>>() : null; _concurrentTreeTaskTokensOpt = analysisOptions.ConcurrentAnalysis ? new Dictionary<Task, int>() : null; _executingCompilationOrNonConcurrentTreeTask = null; }
internal static Compilation AttachAnalyzerDriverToCompilation(Compilation compilation, ImmutableArray<IDiagnosticAnalyzer> analyzers, out AnalyzerDriver analyzerDriver3, AnalyzerOptions options, CancellationToken cancellationToken) { analyzerDriver3 = compilation.AnalyzerForLanguage(analyzers, options, cancellationToken); return compilation.WithEventQueue(analyzerDriver3.CompilationEventQueue); }
internal static Compilation AttachAnalyzerDriverToCompilation(Compilation compilation, ImmutableArray <IDiagnosticAnalyzer> analyzers, out AnalyzerDriver3 analyzerDriver3, AnalyzerOptions options, CancellationToken cancellationToken) { analyzerDriver3 = compilation.AnalyzerForLanguage(analyzers, options, cancellationToken); return(compilation.WithEventQueue(analyzerDriver3.CompilationEventQueue)); }
// internal for testing purposes internal static AnalyzerDriver Create( Compilation compilation, ImmutableArray<DiagnosticAnalyzer> analyzers, AnalyzerOptions options, AnalyzerManager analyzerManager, Action<Exception, DiagnosticAnalyzer, Diagnostic> onAnalyzerException, out Compilation newCompilation, CancellationToken cancellationToken) { options = options ?? AnalyzerOptions.Empty; AnalyzerDriver analyzerDriver = compilation.AnalyzerForLanguage(analyzers, analyzerManager, cancellationToken); newCompilation = compilation.WithEventQueue(analyzerDriver.CompilationEventQueue); var addDiagnostic = GetDiagnosticSinkWithSuppression(analyzerDriver.DiagnosticQueue.Enqueue, newCompilation); Action<Exception, DiagnosticAnalyzer, Diagnostic> newOnAnalyzerException; if (onAnalyzerException != null) { // Wrap onAnalyzerException to pass in filtered diagnostic. var comp = newCompilation; newOnAnalyzerException = (ex, analyzer, diagnostic) => onAnalyzerException(ex, analyzer, GetFilteredDiagnostic(diagnostic, comp)); } else { // Add exception diagnostic to regular diagnostic bag. newOnAnalyzerException = (ex, analyzer, diagnostic) => addDiagnostic(diagnostic); } // Assume all analyzers are non-thread safe. var singleThreadedAnalyzerToGateMap = ImmutableDictionary.CreateRange(analyzers.Select(a => KeyValuePair.Create(a, new object()))); var analyzerExecutor = AnalyzerExecutor.Create(newCompilation, options, addDiagnostic, newOnAnalyzerException, IsCompilerAnalyzer, analyzerManager, singleThreadedAnalyzerToGateMap, cancellationToken); analyzerDriver.Initialize(newCompilation, analyzerExecutor, cancellationToken); return analyzerDriver; }
// internal for testing purposes internal static AnalyzerDriver Create(Compilation compilation, ImmutableArray<DiagnosticAnalyzer> analyzers, AnalyzerOptions options, out Compilation newCompilation, Func<Exception, DiagnosticAnalyzer, bool> continueOnAnalyzerException, CancellationToken cancellationToken) { options = options ?? AnalyzerOptions.Empty; AnalyzerDriver analyzerDriver = compilation.AnalyzerForLanguage(analyzers, options, continueOnAnalyzerException, cancellationToken); newCompilation = compilation.WithEventQueue(analyzerDriver.CompilationEventQueue); analyzerDriver.Initialize(newCompilation, cancellationToken); return analyzerDriver; }
// internal for testing purposes internal static AnalyzerDriver Create( Compilation compilation, ImmutableArray<DiagnosticAnalyzer> analyzers, AnalyzerOptions options, AnalyzerManager analyzerManager, Action<Exception, DiagnosticAnalyzer, Diagnostic> onAnalyzerException, bool reportAnalyzer, out Compilation newCompilation, CancellationToken cancellationToken) { options = options ?? AnalyzerOptions.Empty; AnalyzerDriver analyzerDriver = compilation.AnalyzerForLanguage(analyzers, analyzerManager, cancellationToken); newCompilation = compilation.WithEventQueue(analyzerDriver.CompilationEventQueue); var addDiagnostic = GetDiagnosticSinkWithSuppression(analyzerDriver.DiagnosticQueue.Enqueue, newCompilation); Action<Exception, DiagnosticAnalyzer, Diagnostic> newOnAnalyzerException; if (onAnalyzerException != null) { // Wrap onAnalyzerException to pass in filtered diagnostic. var comp = newCompilation; newOnAnalyzerException = (ex, analyzer, diagnostic) => onAnalyzerException(ex, analyzer, GetFilteredDiagnostic(diagnostic, comp)); } else { // Add exception diagnostic to regular diagnostic bag. newOnAnalyzerException = (ex, analyzer, diagnostic) => addDiagnostic(diagnostic); } // Assume all analyzers are non-thread safe. var singleThreadedAnalyzerToGateMap = ImmutableDictionary.CreateRange(analyzers.Select(a => KeyValuePair.Create(a, new object()))); if (reportAnalyzer) { // If we are reporting detailed analyzer performance numbers, then do a dummy invocation of Compilation.GetTypeByMetadataName API upfront. // This API seems to cause a severe hit for the first analyzer invoking it and hence introduces lot of noise in the computed analyzer execution times. var unused = newCompilation.GetTypeByMetadataName("System.Object"); } Func<DiagnosticAnalyzer, object> getAnalyzerGate = analyzer => singleThreadedAnalyzerToGateMap[analyzer]; var analyzerExecutor = AnalyzerExecutor.Create(newCompilation, options, addDiagnostic, newOnAnalyzerException, IsCompilerAnalyzer, analyzerManager, getAnalyzerGate, reportAnalyzer, cancellationToken); analyzerDriver.Initialize(newCompilation, analyzerExecutor, cancellationToken); return analyzerDriver; }