コード例 #1
0
        public void ObjectsShouldNotBeDisposedMoreThanOnce()
        {
            // Symbolic execution analyzers are run by the SymbolicExecutionRunner
            var analyzers = ImmutableArray.Create <ISymbolicExecutionAnalyzer>(new ObjectsShouldNotBeDisposedMoreThanOnce());
            var runner    = new SymbolicExecutionRunner(new SymbolicExecutionAnalyzerFactory(analyzers));

            Verifier.VerifyAnalyzer(@"TestCases\ObjectsShouldNotBeDisposedMoreThanOnce.cs",
                                    runner,
                                    ParseOptionsHelper.FromCSharp8,
                                    additionalReferences: NuGetMetadataReference.NETStandardV2_1_0);
        }
コード例 #2
0
        public void EmptyNullableValueAccess()
        {
            // Symbolic execution analyzers are run by the SymbolicExecutionRunner
            var analyzers = ImmutableArray.Create <ISymbolicExecutionAnalyzer>(new EmptyNullableValueAccess());
            var runner    = new SymbolicExecutionRunner(new SymbolicExecutionAnalyzerFactory(analyzers));

            Verifier.VerifyAnalyzer(@"TestCases\EmptyNullableValueAccess.cs",
                                    runner,
                                    ParseOptionsHelper.FromCSharp8,
                                    additionalReferences: NuGetMetadataReference.NETStandardV2_1_0);
        }
コード例 #3
0
        public void PublicMethodArgumentsShouldBeCheckedForNull()
        {
            // Symbolic execution analyzers are run by the SymbolicExecutionRunner
            var analyzers = ImmutableArray.Create <ISymbolicExecutionAnalyzer>(new PublicMethodArgumentsShouldBeCheckedForNull());
            var runner    = new SymbolicExecutionRunner(new SymbolicExecutionAnalyzerFactory(analyzers));

            Verifier.VerifyAnalyzer(@"TestCases\PublicMethodArgumentsShouldBeCheckedForNull.cs",
                                    runner,
                                    ParseOptionsHelper.FromCSharp8,
                                    additionalReferences: NuGetMetadataReference.NETStandardV2_1_0);
        }
        public void EmptyCollectionsShouldNotBeEnumerated()
        {
            // Symbolic execution analyzers are run by the SymbolicExecutionRunner
            var analyzers = ImmutableArray.Create <ISymbolicExecutionAnalyzer>(new EmptyCollectionsShouldNotBeEnumerated());
            var runner    = new SymbolicExecutionRunner(new SymbolicExecutionAnalyzerFactory(analyzers));

            Verifier.VerifyAnalyzer(
                @"TestCases\EmptyCollectionsShouldNotBeEnumerated.cs",
                runner,
                ParseOptionsHelper.CSharp8,
                additionalReferences: NuGetMetadataReference.NETStandardV2_1_0);
        }
コード例 #5
0
        public override void ReportSuppressions(SuppressionAnalysisContext context)
        {
            var options = Options.Read(context.Options.AnalyzerConfigOptionsProvider.GlobalOptions);

            if (options.DisableSuppressions)
            {
                return;
            }

            var logger = Logger.Get(options.LogFile);

            logger.Log(() => $"ReportSuppressions: {context.ReportedDiagnostics.Length}={string.Join("|", context.ReportedDiagnostics)}");

            var cancellationToken = context.CancellationToken;

            var cachedDiagnostics = new Dictionary <SyntaxNode, IList <Diagnostic> >();

            var index = 0;

            foreach (var diagnostic in context.ReportedDiagnostics)
            {
                try
                {
                    var location   = diagnostic.Location;
                    var sourceTree = location.SourceTree;
                    if (sourceTree == null)
                    {
                        continue;
                    }

                    var root = sourceTree.GetRoot(cancellationToken);

                    var sourceSpan          = location.SourceSpan;
                    var elementNode         = root.FindNode(sourceSpan);
                    var elementNodeLocation = elementNode.GetLocation();

                    var analysisContext = new AnalysisContext(cachedDiagnostics);
                    var runner          = new SymbolicExecutionRunner(new NullPointerDereference(), context.CancellationToken, options.MaxSteps ?? 5000);
                    runner.Initialize(analysisContext);

                    var stopwatch = Stopwatch.StartNew();

                    var detectedDiagnostics = analysisContext.Analyze(elementNode, context, sourceTree);

                    var elapsed = stopwatch.ElapsedMilliseconds;
                    stopwatch.Stop();

                    logger.Log(() => $"  Analyzing {++index}: {elapsed} ms, {runner.Steps} steps, {detectedDiagnostics.Count} diagnostics");

                    var detected = detectedDiagnostics.FirstOrDefault(d => elementNodeLocation == d.Location);
                    if (detected?.Id != NullPointerDereference.NotNullDiagnosticId)
                    {
                        continue;
                    }

                    var suppression = SupportedSuppressions.Single(item => item.SuppressedDiagnosticId == diagnostic.Id);
                    logger.Log(() => $"    ReportSuppression: {diagnostic}");
                    context.ReportSuppression(Suppression.Create(suppression, diagnostic));
                }
                catch (Exception ex)
                {
                    // could not analyze the full graph, so just do not suppress anything.
                    logger.Log($"  Error: {ex}");
                }
            }
        }