コード例 #1
0
        public void Analyze(string targetPath, Action <Diagnostic> reportDiagnostic, CancellationToken cancellationToken = default(CancellationToken))
        {
            // Create a Roslyn representation of the IL by constructing a MetadataReference against
            // the target path (as if we intended to reference this binary during compilation, instead
            // of analyzing it). Using this mechanism, we can scan types/members contained in the
            // binary. We cannot currently retrieve IL from method bodies.
            var reference   = MetadataReference.CreateFromFile(targetPath);
            var compilation = CSharpCompilation.Create("_", references: new[] { reference });
            var target      = compilation.GetAssemblyOrModuleSymbol(reference);

            // For each analysis target, we create a compilation start context, which may result
            // in symbol action registration. We need to capture and throw these registrations
            // away for each binary we inspect.
            var compilationStartAnalysisContext = new RoslynCompilationStartAnalysisContext(compilation, _options, cancellationToken);

            _perCompilationSymbolsActions = compilationStartAnalysisContext.SymbolActions;

            GlobalRoslynAnalysisContext.CompilationStartActions?.Invoke(compilationStartAnalysisContext);

            var visitor = new RoslynSymbolVisitor(symbol => Analyze(symbol, compilation, reportDiagnostic, cancellationToken));

            visitor.Visit(target);

            // Having finished analysis, we'll invoke any compilation end actions registered previously.
            // We also discard the per-compilation symbol actions we collected.
            var compilationAnalysisContext = new CompilationAnalysisContext(compilation, _options, reportDiagnostic, _isSupportedDiagnostic, cancellationToken);

            GlobalRoslynAnalysisContext.CompilationActions?.Invoke(compilationAnalysisContext);
            compilationStartAnalysisContext.CompilationEndActions?.Invoke(compilationAnalysisContext);

            _perCompilationSymbolsActions = null;
        }
コード例 #2
0
        private void Analyze(
            ISymbol symbol,
            Compilation compilation,
            ActionMap <SymbolAnalysisContext, SymbolKind> perCompilationSymbolActions,
            Action <Diagnostic> reportDiagnostic,
            CancellationToken cancellationToken)
        {
            var symbolContext = new SymbolAnalysisContext(symbol, compilation, _options, reportDiagnostic, _isSupportedDiagnostic, cancellationToken);

            this.GlobalRoslynAnalysisContext.SymbolActions.Invoke(symbol.Kind, symbolContext);
            perCompilationSymbolActions.Invoke(symbol.Kind, symbolContext);
        }
コード例 #3
0
 public RoslynCompilationStartAnalysisContext(Compilation compilation, AnalyzerOptions options, CancellationToken cancellationToken)
     : base(compilation, options, cancellationToken)
 {
     SymbolActions = new ActionMap <SymbolAnalysisContext, SymbolKind>();
 }