public FxCopNullabilityRuleValidatorBuilder ExternallyAnnotated([NotNull] ExternalAnnotationsBuilder builder)
        {
            Guard.NotNull(builder, "builder");

            externalAnnotationsResolver = new SimpleExternalAnnotationsResolver(builder.Build());
            return(this);
        }
Example #2
0
        private void StartAnalyzeCompilation([NotNull] CompilationStartAnalysisContext context)
        {
            Guard.NotNull(context, nameof(context));

            AnalyzerSettings settings = SettingsProvider.LoadSettings(context.Options, context.CancellationToken);

            NullabilityAttributeSymbols nullSymbols = NullabilityAttributeProvider.GetCached()
                                                      .GetSymbols(context.Compilation, context.CancellationToken);

            if (nullSymbols == null)
            {
                // Nullability attributes not found; keep silent.
                return;
            }

            IExternalAnnotationsResolver resolver = ExternalAnnotationsResolver.GetCached();

            resolver.EnsureScanned();

            var generatedCodeCache = new GeneratedCodeDocumentCache();
            var typeCache          = new FrameworkTypeCache(context.Compilation);

            var nullabilityContext = new AnalysisScope(resolver, generatedCodeCache, typeCache, settings,
                                                       disableReportOnNullableValueTypesRule, appliesToItem);

            var factory = new SymbolAnalyzerFactory(nullabilityContext);

            ImmutableDictionary <string, string> properties = nullSymbols.GetMetadataNamesAsProperties();

            context.RegisterSymbolAction(c => AnalyzeField(c, factory, properties), SymbolKind.Field);
            context.RegisterSymbolAction(c => AnalyzeProperty(c, factory, properties), SymbolKind.Property);
            context.RegisterSymbolAction(c => AnalyzeMethod(c, factory, properties), SymbolKind.Method);
            context.RegisterSyntaxNodeAction(c => AnalyzeParameter(SyntaxToSymbolContext(c), factory, properties),
                                             SyntaxKind.Parameter);
        }
        public AnalyzerFactory([NotNull] IExternalAnnotationsResolver externalAnnotations, bool appliesToItem)
        {
            Guard.NotNull(externalAnnotations, "externalAnnotations");

            this.externalAnnotations = externalAnnotations;
            this.appliesToItem       = appliesToItem;
        }
Example #4
0
 public MethodReturnValueAnalyzer(SymbolAnalysisContext context,
                                  [NotNull] IExternalAnnotationsResolver externalAnnotations,
                                  [NotNull] GeneratedCodeDocumentCache generatedCodeCache, [NotNull] FrameworkTypeCache typeCache,
                                  bool appliesToItem)
     : base(context, externalAnnotations, generatedCodeCache, typeCache, appliesToItem)
 {
 }
Example #5
0
        protected AnalyzerTestContext CreateTestContext([NotNull] ParsedSourceCode source)
        {
            externalAnnotationsResolver = new SimpleExternalAnnotationsResolver(source.ExternalAnnotationsMap);

            AnalyzerOptions options = AnalyzerSettingsBuilder.ToOptions(source.Settings);

            return(new AnalyzerTestContext(source.GetText(), LanguageNames.CSharp, options)
                   .WithReferences(source.References)
                   .InFileNamed(source.Filename));
        }
        protected void VerifyNullabilityDiagnostic([NotNull] ParsedSourceCode source)
        {
            Guard.NotNull(source, nameof(source));

            externalAnnotationsResolver = new SimpleExternalAnnotationsResolver(source.ExternalAnnotationsMap);

            AnalyzerTestContext analyzerTextContext = new AnalyzerTestContext(source.GetText(), LanguageNames.CSharp)
                .WithReferences(source.References)
                .WithFileName(source.Filename);

            AssertDiagnostics(analyzerTextContext);
        }
Example #7
0
        protected void VerifyNullabilityDiagnostic([NotNull] ParsedSourceCode source)
        {
            Guard.NotNull(source, nameof(source));

            externalAnnotationsResolver = new SimpleExternalAnnotationsResolver(source.ExternalAnnotationsMap);

            AnalyzerTestContext analyzerTextContext = new AnalyzerTestContext(source.GetText(), LanguageNames.CSharp)
                                                      .WithReferences(source.References)
                                                      .WithFileName(source.Filename);

            AssertDiagnostics(analyzerTextContext);
        }
        public SymbolAnalyzerFactory([NotNull] IExternalAnnotationsResolver externalAnnotations,
            [NotNull] GeneratedCodeDocumentCache generatedCodeCache, [NotNull] FrameworkTypeCache typeCache,
            bool appliesToItem)
        {
            Guard.NotNull(externalAnnotations, nameof(externalAnnotations));
            Guard.NotNull(generatedCodeCache, nameof(generatedCodeCache));
            Guard.NotNull(typeCache, nameof(typeCache));

            this.externalAnnotations = externalAnnotations;
            this.generatedCodeCache = generatedCodeCache;
            this.typeCache = typeCache;
            this.appliesToItem = appliesToItem;
        }
Example #9
0
        public SymbolAnalyzerFactory([NotNull] IExternalAnnotationsResolver externalAnnotations,
                                     [NotNull] GeneratedCodeDocumentCache generatedCodeCache, [NotNull] FrameworkTypeCache typeCache,
                                     bool appliesToItem)
        {
            Guard.NotNull(externalAnnotations, nameof(externalAnnotations));
            Guard.NotNull(generatedCodeCache, nameof(generatedCodeCache));
            Guard.NotNull(typeCache, nameof(typeCache));

            this.externalAnnotations = externalAnnotations;
            this.generatedCodeCache  = generatedCodeCache;
            this.typeCache           = typeCache;
            this.appliesToItem       = appliesToItem;
        }
        protected void VerifyNullabilityDiagnostic([NotNull] ParsedSourceCode source)
        {
            Guard.NotNull(source, nameof(source));

            externalAnnotationsResolver = new SimpleExternalAnnotationsResolver(source.ExternalAnnotationsMap);

            string text = source.GetText();
            AnalyzerOptions options = AnalyzerSettingsBuilder.ToOptions(source.Settings);

            AnalyzerTestContext analyzerContext = new AnalyzerTestContext(text, LanguageNames.CSharp, options)
                .WithReferences(source.References)
                .WithFileName(source.Filename);

            AssertDiagnostics(analyzerContext);
        }
Example #11
0
        protected BaseSymbolAnalyzer(SymbolAnalysisContext context,
                                     [NotNull] IExternalAnnotationsResolver externalAnnotations,
                                     [NotNull] GeneratedCodeDocumentCache generatedCodeCache, [NotNull] FrameworkTypeCache typeCache,
                                     bool appliesToItem)
        {
            Guard.NotNull(externalAnnotations, nameof(externalAnnotations));
            Guard.NotNull(generatedCodeCache, nameof(generatedCodeCache));
            Guard.NotNull(typeCache, nameof(typeCache));

            this.context             = context;
            this.externalAnnotations = externalAnnotations;
            this.generatedCodeCache  = generatedCodeCache;
            this.typeCache           = typeCache;
            AppliesToItem            = appliesToItem;

            Symbol = (TSymbol)context.Symbol;
        }
        private ProblemCollection CheckSymbol([CanBeNull] ISymbol symbol)
        {
            if (symbol != null)
            {
                // When unable to load external annotations, the rule would likely report lots
                // of false positives. This is prevented by letting it throw here and report nothing.
                IExternalAnnotationsResolver resolver = ExternalAnnotationsResolver.GetCached();
                resolver.EnsureScanned();

                var analyzerFactory = new AnalyzerFactory(resolver, appliesToItem);

                BaseAnalyzer analyzer = analyzerFactory.CreateFor(symbol);
                analyzer.Analyze(ReportProblem);
            }

            return(Problems);
        }
Example #13
0
        public AnalysisScope([NotNull] IExternalAnnotationsResolver externalAnnotations,
                             [NotNull] GeneratedCodeDocumentCache generatedCodeCache, [NotNull] FrameworkTypeCache typeCache,
                             [NotNull] AnalyzerSettings settings, [NotNull] DiagnosticDescriptor disableReportOnNullableValueTypesRule,
                             bool appliesToItem)
        {
            Guard.NotNull(externalAnnotations, nameof(externalAnnotations));
            Guard.NotNull(generatedCodeCache, nameof(generatedCodeCache));
            Guard.NotNull(typeCache, nameof(typeCache));
            Guard.NotNull(settings, nameof(settings));
            Guard.NotNull(disableReportOnNullableValueTypesRule, nameof(disableReportOnNullableValueTypesRule));

            ExternalAnnotations = externalAnnotations;
            GeneratedCodeCache  = generatedCodeCache;
            TypeCache           = typeCache;
            Settings            = settings;
            DisableReportOnNullableValueTypesRule = disableReportOnNullableValueTypesRule;
            AppliesToItem = appliesToItem;
        }
        protected virtual void VerifyNullabilityFix([NotNull] ParsedSourceCode source)
        {
            Guard.NotNull(source, nameof(source));

            externalAnnotationsResolver = new SimpleExternalAnnotationsResolver(source.ExternalAnnotationsMap);

            string fixNotNull = source.GetExpectedTextForAttribute(NotNullAttributeName);
            string fixCanBeNull = source.GetExpectedTextForAttribute(CanBeNullAttributeName);

            string text = source.GetText();
            AnalyzerOptions options = AnalyzerSettingsBuilder.ToOptions(source.Settings);

            AnalyzerTestContext analyzerContext = new AnalyzerTestContext(text, LanguageNames.CSharp, options)
                .WithReferences(source.References)
                .WithFileName(source.Filename);

            var fixContext = new FixProviderTestContext(analyzerContext, new[] { fixNotNull, fixCanBeNull },
                source.ReIndentExpected);

            AssertDiagnosticsWithCodeFixes(fixContext);
        }
 public PropertyAnalyzer([NotNull] PropertySymbol symbol,
                         [NotNull] IExternalAnnotationsResolver externalAnnotations, bool appliesToItem)
     : base(symbol, externalAnnotations, appliesToItem)
 {
 }
Example #16
0
 public FieldAnalyzer([NotNull] FieldSymbol field, [NotNull] IExternalAnnotationsResolver externalAnnotations,
                      bool appliesToItem)
     : base(field, externalAnnotations, appliesToItem)
 {
     heuristics = new GeneratedCodeHeuristics(field);
 }
Example #17
0
 public ParameterAnalyzer([NotNull] ParameterSymbol symbol,
                          [NotNull] IExternalAnnotationsResolver externalAnnotations, bool appliesToItem)
     : base(symbol, externalAnnotations, appliesToItem)
 {
 }
Example #18
0
 public MethodReturnValueAnalyzer([NotNull] MethodSymbol symbol,
                                  [NotNull] IExternalAnnotationsResolver externalAnnotations, bool appliesToItem)
     : base(symbol, externalAnnotations, appliesToItem)
 {
 }