示例#1
0
        private void AnalyzeManagedAssembly(string assemblyFilePath, IEnumerable <string> roslynAnalyzerFilePaths, BinaryAnalyzerContext context)
        {
            if (_globalRoslynAnalysisContext == null)
            {
                _globalRoslynAnalysisContext = new RoslynAnalysisContext();

                // We could use the ILDiagnosticsAnalyzer factory method that initializes
                // an object instance from an enumerable collection of analyzer paths. We
                // initialize a context object from each path one-by-one instead, in order
                // to make an attempt to load each specified analyzer. We will therefore
                // collect information on each analyzer that fails to load. We will also
                // proceed with performing as much analysis as possible. Ultimately, a
                // single analyzer load failure will return in BinSkim returning a non-zero
                // failure code from the run.
                foreach (string analyzerFilePath in roslynAnalyzerFilePaths)
                {
                    InvokeCatchingRelevantIOExceptions
                    (
                        action: () => { ILDiagnosticsAnalyzer.LoadAnalyzer(analyzerFilePath, _globalRoslynAnalysisContext); },
                        exceptionHandler: (ex) =>
                    {
                        LogExceptionLoadingRoslynAnalyzer(analyzerFilePath, context, ex);
                    }
                    );
                }
            }

            ILDiagnosticsAnalyzer roslynAnalyzer = ILDiagnosticsAnalyzer.Create(_globalRoslynAnalysisContext);

            roslynAnalyzer.Analyze(assemblyFilePath, diagnostic =>
            {
                MessageKind messageKind = diagnostic.Severity.ConvertToMessageKind();
                context.Logger.Log(messageKind, context, diagnostic.GetMessage(CultureInfo.CurrentCulture));
            });
        }
        public void AnalysisContext_Simple()
        {
            var context = new RoslynAnalysisContext();
            int symbolActionInvocationCount = 0;
            int compilationStartInvocationCount = 0;

            // The relevant work
            context.RegisterSymbolAction((c) => { symbolActionInvocationCount += 13; }, SymbolKind.NamedType);
            context.RegisterCompilationStartAction((c) => { compilationStartInvocationCount += 7; });

            // No-ops
            context.RegisterCodeBlockAction(null);
            context.RegisterCodeBlockStartAction<int>(null);
            context.RegisterSemanticModelAction(null);
            context.RegisterSyntaxNodeAction<int>(null);
            context.RegisterSyntaxTreeAction(null);

            Assert.NotNull(context.SymbolActions);
            Assert.NotNull(context.CompilationStartActions);

            context.CompilationStartActions.Invoke(null);
            Assert.Equal(7, compilationStartInvocationCount);

            context.SymbolActions.Invoke(SymbolKind.NamedType, new SymbolAnalysisContext());
            Assert.Equal(13, symbolActionInvocationCount);
        }
        public static ILDiagnosticsAnalyzer Create(IEnumerable<string> analyzerFilePaths)
        {
            var analysisContext = new RoslynAnalysisContext();

            foreach(string analyzerFilePath in analyzerFilePaths)
            {
                LoadAnalyzer(analyzerFilePath, analysisContext);
            }

            return Create(analysisContext);
        }
        public static RoslynAnalysisContext LoadAnalyzer(string path, RoslynAnalysisContext analysisContext = null)
        {
            analysisContext = analysisContext ?? new RoslynAnalysisContext();

            Assembly assembly = Assembly.LoadFrom(path);

            foreach (Type type in assembly.GetTypes())
            {
                if (typeof(DiagnosticAnalyzer).IsAssignableFrom(type) && !type.IsAbstract)
                {
                    var analyzer = (DiagnosticAnalyzer)Activator.CreateInstance(type);
                    analyzer.Initialize(analysisContext);
                }
            }
            return analysisContext;
        }
 public static ILDiagnosticsAnalyzer Create(RoslynAnalysisContext analysisContext)
 {
     return new ILDiagnosticsAnalyzer(analysisContext);
 }
 private ILDiagnosticsAnalyzer(RoslynAnalysisContext analysisContext)
 {
     GlobalRoslynAnalysisContext = analysisContext;
 }