Ejemplo n.º 1
0
 protected AbstractCodeQualityDiagnosticAnalyzer(
     ImmutableArray <DiagnosticDescriptor> descriptors,
     GeneratedCodeAnalysisFlags generatedCodeAnalysisFlags)
 {
     SupportedDiagnostics        = descriptors;
     _generatedCodeAnalysisFlags = generatedCodeAnalysisFlags;
 }
 public async Task TestCSharpAnalyzerWithExclusionPasses(GeneratedCodeAnalysisFlags generatedCodeAnalysisFlags)
 {
     await new CSharpReplaceThisWithBaseTest(generatedCodeAnalysisFlags)
     {
         TestCode = ReplaceThisWithBaseTestCode,
     }.RunAsync();
 }
 public async Task TestVisualBasicAnalyzerWithExclusionPasses(GeneratedCodeAnalysisFlags generatedCodeAnalysisFlags)
 {
     await new VisualBasicReplaceThisWithBaseTest(generatedCodeAnalysisFlags)
     {
         TestCode = ReplaceMyClassWithMyBaseTestCode,
     }.RunAsync();
 }
Ejemplo n.º 4
0
        internal SourcebergAnalyzerHost(Type type)
        {
            // create the setup instance
            AnalyzerGroupType = type;
            var analyzerSetup     = (SourcebergAnalyzerGroup)Activator.CreateInstance(AnalyzerGroupType);
            var serviceCollection = new ServiceCollection().AddDefaultSourbergServices();

            analyzerSetup.ConfigureAnalyzerServices(new ServiceCollectionAdapter(serviceCollection));
            serviceCollection = serviceCollection.ExpandAnalyzerCollection();

            _generatorFlags      = analyzerSetup.GeneratedCodeAnalysisFlags;
            _provider            = serviceCollection.BuildServiceProvider();
            SupportedDiagnostics = GetSupportedDiagnostics(serviceCollection);
        }
Ejemplo n.º 5
0
        internal SourcebergAnalyzerHost(SourcebergAnalyzerGroup analyzer, IServiceProvider origianlProvider, IEnumerable <ServiceDescriptor> supportedService)
        {
            // create the setup instance
            AnalyzerGroupType = analyzer.GetType();
            // This allows us to add singleton service to the new service collection that points back to the original provider.
            // We don't add default sourceberg services because we already have it in the original provider.
            IServiceCollection serviceCollection = new ServiceCollection {
                supportedService.Select(x => new ServiceDescriptor(x.ServiceType, origianlProvider.GetRequiredService(x.ServiceType)))
            };

            var customServiceCollection = new ServiceCollection();

            analyzer.ConfigureAnalyzerServices(new ServiceCollectionAdapter(customServiceCollection));

            // Add back the custom service collection so it can override some preset service inherit from the generator
            serviceCollection = serviceCollection.Add(customServiceCollection.ExpandAnalyzerCollection());

            _generatorFlags      = analyzer.GeneratedCodeAnalysisFlags;
            _provider            = serviceCollection.BuildServiceProvider();
            SupportedDiagnostics = GetSupportedDiagnostics(serviceCollection);
        }
Ejemplo n.º 6
0
 public override void ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags analysisMode)
 {
 }
Ejemplo n.º 7
0
 public void ConfigureGeneratedCodeAnalysis(DiagnosticAnalyzer analyzer, GeneratedCodeAnalysisFlags mode)
 {
     _generatedCodeConfigurationMap.AddOrUpdate(analyzer, addValue: mode, updateValueFactory: (a, c) => mode);
 }
Ejemplo n.º 8
0
 public override void ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags mode)
 {
     _scope.ConfigureGeneratedCodeAnalysis(_analyzer, mode);
 }
        private static void AddExpectedLocalDiagnostics(
            ArrayBuilder<DiagnosticDescription> builder,
            bool isGeneratedCode,
            string squiggledText,
            int line,
            int column,
            GeneratedCodeAnalysisFlags? generatedCodeAnalysisFlagsOpt,
            params string[] arguments)
        {
            // Always report diagnostics in generated code, unless explicitly suppressed or we are not even analyzing generated code.
            var reportInGeneratedCode = generatedCodeAnalysisFlagsOpt == null ||
                ((generatedCodeAnalysisFlagsOpt & GeneratedCodeAnalysisFlags.ReportDiagnostics) != 0 &&
                 (generatedCodeAnalysisFlagsOpt & GeneratedCodeAnalysisFlags.Analyze) != 0);

            if (!isGeneratedCode || reportInGeneratedCode)
            {
                var diagnostic = Diagnostic(GeneratedCodeAnalyzer.Warning.Id, squiggledText).WithArguments(arguments).WithLocation(line, column);
                builder.Add(diagnostic);

                diagnostic = Diagnostic(GeneratedCodeAnalyzer.Error.Id, squiggledText).WithArguments(arguments).WithLocation(line, column);
                builder.Add(diagnostic);
            }
        }
        private static DiagnosticDescription[] GetExpectedGeneratedCodeAnalyzerDiagnostics(Compilation compilation, Func<string, bool> isGeneratedFileName, GeneratedCodeAnalysisFlags? generatedCodeAnalysisFlagsOpt)
        {
            var analyzers = new DiagnosticAnalyzer[] { new GeneratedCodeAnalyzer(generatedCodeAnalysisFlagsOpt) };
            var files = compilation.SyntaxTrees.Select(t => t.FilePath).ToImmutableArray();
            var sortedCallbackSymbolNames = new SortedSet<string>();
            var sortedCallbackTreePaths = new SortedSet<string>();
            var builder = ArrayBuilder<DiagnosticDescription>.GetInstance();
            for (int i = 0; i < compilation.SyntaxTrees.Count(); i++)
            {
                var file = files[i];
                var isGeneratedFile = isGeneratedFileName(file);

                // Type "GeneratedCode{0}"
                var squiggledText = string.Format("GeneratedCode{0}", i);
                var diagnosticArgument = squiggledText;
                var line = 3;
                var column = 7;
                var isGeneratedCode = true;
                AddExpectedLocalDiagnostics(builder, isGeneratedCode, squiggledText, line, column, generatedCodeAnalysisFlagsOpt, diagnosticArgument);

                // Type "Nested{0}"
                squiggledText = string.Format("Nested{0}", i);
                diagnosticArgument = squiggledText;
                line = 5;
                column = 19;
                isGeneratedCode = true;
                AddExpectedLocalDiagnostics(builder, isGeneratedCode, squiggledText, line, column, generatedCodeAnalysisFlagsOpt, diagnosticArgument);

                // Type "NonGeneratedCode{0}"
                squiggledText = string.Format("NonGeneratedCode{0}", i);
                diagnosticArgument = squiggledText;
                line = 8;
                column = 7;
                isGeneratedCode = isGeneratedFile;
                AddExpectedLocalDiagnostics(builder, isGeneratedCode, squiggledText, line, column, generatedCodeAnalysisFlagsOpt, diagnosticArgument);

                // Type "NestedGeneratedCode{0}"
                squiggledText = string.Format("NestedGeneratedCode{0}", i);
                diagnosticArgument = squiggledText;
                line = 11;
                column = 19;
                isGeneratedCode = true;
                AddExpectedLocalDiagnostics(builder, isGeneratedCode, squiggledText, line, column, generatedCodeAnalysisFlagsOpt, diagnosticArgument);

                // File diagnostic
                squiggledText = "}"; // last token in file.
                diagnosticArgument = file;
                line = 12;
                column = 1;
                isGeneratedCode = isGeneratedFile;
                AddExpectedLocalDiagnostics(builder, isGeneratedCode, squiggledText, line, column, generatedCodeAnalysisFlagsOpt, diagnosticArgument);

                // Compilation end summary diagnostic (verify callbacks into analyzer)
                // Analyzer always called for generated code, unless generated code analysis is explicitly disabled.
                if (generatedCodeAnalysisFlagsOpt == null || (generatedCodeAnalysisFlagsOpt & GeneratedCodeAnalysisFlags.Analyze) != 0)
                {
                    sortedCallbackSymbolNames.Add(string.Format("GeneratedCode{0}", i));
                    sortedCallbackSymbolNames.Add(string.Format("Nested{0}", i));
                    sortedCallbackSymbolNames.Add(string.Format("NonGeneratedCode{0}", i));
                    sortedCallbackSymbolNames.Add(string.Format("NestedGeneratedCode{0}", i));

                    sortedCallbackTreePaths.Add(file);
                }
                else if (!isGeneratedFile)
                {
                    // Analyzer always called for non-generated code.
                    sortedCallbackSymbolNames.Add(string.Format("NonGeneratedCode{0}", i));
                    sortedCallbackTreePaths.Add(file);
                }
            }

            // Compilation end summary diagnostic (verify callbacks into analyzer)
            var arg1 = sortedCallbackSymbolNames.Join(",");
            var arg2 = sortedCallbackTreePaths.Join(",");
            AddExpectedNonLocalDiagnostic(builder, arguments: new[] { arg1, arg2 });

            if (compilation.Options.GeneralDiagnosticOption == ReportDiagnostic.Error)
            {
                for(int i = 0; i < builder.Count; i++)
                {
                    if (((string)builder[i].Code) != GeneratedCodeAnalyzer.Error.Id)
                    {
                        builder[i] = builder[i].WithWarningAsError(true);
                    }
                }
            }

            return builder.ToArrayAndFree();
        }
 private static void VerifyGeneratedCodeAnalyzerDiagnostics(Compilation compilation, DiagnosticDescription[] expected, GeneratedCodeAnalysisFlags? generatedCodeAnalysisFlagsOpt)
 {
     var analyzers = new DiagnosticAnalyzer[] { new GeneratedCodeAnalyzer(generatedCodeAnalysisFlagsOpt) };
     compilation.VerifyAnalyzerDiagnostics(analyzers, null, null, logAnalyzerExceptionAsDiagnostics: false, expected: expected);
 }
 private static void VerifyGeneratedCodeAnalyzerDiagnostics(Compilation compilation, Func<string, bool> isGeneratedFileName, GeneratedCodeAnalysisFlags? generatedCodeAnalysisFlagsOpt)
 {
     var expected = GetExpectedGeneratedCodeAnalyzerDiagnostics(compilation, isGeneratedFileName, generatedCodeAnalysisFlagsOpt);
     VerifyGeneratedCodeAnalyzerDiagnostics(compilation, expected, generatedCodeAnalysisFlagsOpt);
 }
Ejemplo n.º 13
0
 public VisualBasicReplaceThisWithBaseTest(GeneratedCodeAnalysisFlags generatedCodeAnalysisFlags)
 {
     _generatedCodeAnalysisFlags = generatedCodeAnalysisFlags;
 }
Ejemplo n.º 14
0
 public CSharpReplaceThisWithBaseTest(GeneratedCodeAnalysisFlags generatedCodeAnalysisFlags)
 {
     _generatedCodeAnalysisFlags = generatedCodeAnalysisFlags;
 }
Ejemplo n.º 15
0
 public ReplaceThisWithBaseAnalyzer(GeneratedCodeAnalysisFlags generatedCodeAnalysisFlags)
 {
     _generatedCodeAnalysisFlags = generatedCodeAnalysisFlags;
 }