Beispiel #1
0
        private void AnalyzeCompilation(CompilationEndAnalysisContext context)
        {
            if (AssemblyHasPublicTypes(context.Compilation.Assembly))
            {
                var comVisibleAttributeSymbol = WellKnownTypes.ComVisibleAttribute(context.Compilation);
                if (comVisibleAttributeSymbol == null)
                {
                    return;
                }

                var attributeInstance = context.Compilation.Assembly.GetAttributes().FirstOrDefault(a => a.AttributeClass.Equals(comVisibleAttributeSymbol));

                if (attributeInstance != null)
                {
                    if (attributeInstance.ConstructorArguments.Length > 0 &&
                        attributeInstance.ConstructorArguments[0].Kind == TypedConstantKind.Primitive &&
                        attributeInstance.ConstructorArguments[0].Value != null &
                        attributeInstance.ConstructorArguments[0].Value.Equals(true))
                    {
                        // Has the attribute, with the value 'true'.
                        context.ReportDiagnostic(Diagnostic.Create(Rule, Location.None, string.Format(FxCopRulesResources.CA1017_AttributeTrue, context.Compilation.Assembly.Name)));
                    }
                }
                else
                {
                    // No ComVisible attribute at all.
                    context.ReportDiagnostic(Diagnostic.Create(Rule, Location.None, string.Format(FxCopRulesResources.CA1017_NoAttribute, context.Compilation.Assembly.Name)));
                }
            }

            return;
        }
        private void AnalyzeCompilation(CompilationEndAnalysisContext context)
        {
            if (AssemblyHasPublicTypes(context.Compilation.Assembly))
            {
                var comVisibleAttributeSymbol = WellKnownTypes.ComVisibleAttribute(context.Compilation);
                if (comVisibleAttributeSymbol == null)
                {
                    return;
                }

                var attributeInstance = context.Compilation.Assembly.GetAttributes().FirstOrDefault(a => a.AttributeClass.Equals(comVisibleAttributeSymbol));

                if (attributeInstance != null)
                {
                    if (attributeInstance.ConstructorArguments.Length > 0 &&
                        attributeInstance.ConstructorArguments[0].Kind == TypedConstantKind.Primitive &&
                        attributeInstance.ConstructorArguments[0].Value != null &
                        attributeInstance.ConstructorArguments[0].Value.Equals(true))
                    {
                        // Has the attribute, with the value 'true'.
                        context.ReportDiagnostic(Diagnostic.Create(Rule, Location.None, string.Format(FxCopRulesResources.CA1017_AttributeTrue, context.Compilation.Assembly.Name)));
                    }
                }
                else
                {
                    // No ComVisible attribute at all.
                    context.ReportDiagnostic(Diagnostic.Create(Rule, Location.None, string.Format(FxCopRulesResources.CA1017_NoAttribute, context.Compilation.Assembly.Name)));
                }
            }

            return;
        }
 public void AnalyzeCompilation(CompilationEndAnalysisContext context)
 {
     foreach (var item in this.fieldDisposedMap)
     {
         if (!item.Value)
         {
             context.ReportDiagnostic(item.Key.CreateDiagnostic(Rule));
         }
     }
 }
 public void AnalyzeCompilation(CompilationEndAnalysisContext context)
 {
     foreach (var item in _fieldDisposedMap)
     {
         if (!item.Value)
         {
             context.ReportDiagnostic(item.Key.CreateDiagnostic(Rule));
         }
     }
 }
Beispiel #5
0
        private void AnalyzeCompilation(CompilationEndAnalysisContext context)
        {
            var assemblyVersionAttributeSymbol    = WellKnownTypes.AssemblyVersionAttribute(context.Compilation);
            var assemblyComplianceAttributeSymbol = WellKnownTypes.CLSCompliantAttribute(context.Compilation);

            if (assemblyVersionAttributeSymbol == null && assemblyComplianceAttributeSymbol == null)
            {
                return;
            }

            bool assemblyVersionAttributeFound    = false;
            bool assemblyComplianceAttributeFound = false;

            // Check all assembly level attributes for the target attribute
            foreach (var attribute in context.Compilation.Assembly.GetAttributes())
            {
                if (attribute.AttributeClass.Equals(assemblyVersionAttributeSymbol))
                {
                    // Mark the version attribute as found
                    assemblyVersionAttributeFound = true;
                }
                else if (attribute.AttributeClass.Equals(assemblyComplianceAttributeSymbol))
                {
                    // Mark the compliance attribute as found
                    assemblyComplianceAttributeFound = true;
                }
            }

            // Check for the case where we do not have the target attribute defined at all in our metadata references. If so, how can they reference it
            if (assemblyVersionAttributeSymbol == null)
            {
                assemblyVersionAttributeFound = false;
            }

            if (assemblyComplianceAttributeSymbol == null)
            {
                assemblyComplianceAttributeFound = false;
            }

            // If there's at least one diagnostic to report, let's report them
            if (!assemblyComplianceAttributeFound || !assemblyVersionAttributeFound)
            {
                if (!assemblyVersionAttributeFound)
                {
                    context.ReportDiagnostic(Diagnostic.Create(CA1016Rule, Location.None));
                }

                if (!assemblyComplianceAttributeFound)
                {
                    context.ReportDiagnostic(Diagnostic.Create(CA1014Rule, Location.None));
                }
            }
        }
Beispiel #6
0
        private void AnalyzeCompilation(CompilationEndAnalysisContext context)
        {
            var globalNamespaces = context.Compilation.GlobalNamespace.GetNamespaceMembers()
                                   .Where(item => item.ContainingAssembly == context.Compilation.Assembly);

            var globalTypes = context.Compilation.GlobalNamespace.GetTypeMembers().Where(item =>
                                                                                         item.ContainingAssembly == context.Compilation.Assembly &&
                                                                                         IsExternallyVisible(item));

            CheckTypeNames(globalTypes, context.ReportDiagnostic);
            CheckNamespaceMembers(globalNamespaces, context.Compilation, context.ReportDiagnostic);
        }
        private void AnalyzeCompilation(CompilationEndAnalysisContext context)
        {
            var assemblyVersionAttributeSymbol = WellKnownTypes.AssemblyVersionAttribute(context.Compilation);
            var assemblyComplianceAttributeSymbol = WellKnownTypes.CLSCompliantAttribute(context.Compilation);

            if (assemblyVersionAttributeSymbol == null && assemblyComplianceAttributeSymbol == null)
            {
                return;
            }

            bool assemblyVersionAttributeFound = false;
            bool assemblyComplianceAttributeFound = false;

            // Check all assembly level attributes for the target attribute
            foreach (var attribute in context.Compilation.Assembly.GetAttributes())
            {
                if (attribute.AttributeClass.Equals(assemblyVersionAttributeSymbol))
                {
                    // Mark the version attribute as found
                    assemblyVersionAttributeFound = true;
                }
                else if (attribute.AttributeClass.Equals(assemblyComplianceAttributeSymbol))
                {
                    // Mark the compliance attribute as found
                    assemblyComplianceAttributeFound = true;
                }
            }

            // Check for the case where we do not have the target attribute defined at all in our metadata references. If so, how can they reference it
            if (assemblyVersionAttributeSymbol == null)
            {
                assemblyVersionAttributeFound = false;
            }

            if (assemblyComplianceAttributeSymbol == null)
            {
                assemblyComplianceAttributeFound = false;
            }

            // If there's at least one diagnostic to report, let's report them
            if (!assemblyComplianceAttributeFound || !assemblyVersionAttributeFound)
            {
                if (!assemblyVersionAttributeFound)
                {
                    context.ReportDiagnostic(Diagnostic.Create(CA1016Rule, Location.None));
                }

                if (!assemblyComplianceAttributeFound)
                {
                    context.ReportDiagnostic(Diagnostic.Create(CA1014Rule, Location.None));
                }
            }
        }
        private void AnalyzeCompilation(CompilationEndAnalysisContext context)
        {
            var globalNamespaces = context.Compilation.GlobalNamespace.GetNamespaceMembers()
                .Where(item => item.ContainingAssembly == context.Compilation.Assembly);

            var globalTypes = context.Compilation.GlobalNamespace.GetTypeMembers().Where(item =>
                    item.ContainingAssembly == context.Compilation.Assembly &&
                    IsExternallyVisible(item));

            CheckTypeNames(globalTypes, context.ReportDiagnostic);
            CheckNamespaceMembers(globalNamespaces, context.Compilation, context.ReportDiagnostic);
        }
            internal void AnalyzeCompilationEnd(CompilationEndAnalysisContext context)
            {
                foreach (var sourceSymbol in _sourceSymbolsToCheck)
                {
                    var found = false;
                    foreach (var type in sourceSymbol.GetBaseTypesAndThis())
                    {
                        if (_typesWithSymbolDeclaredEventInvoked.Contains(type))
                        {
                            found = true;
                            break;
                        }
                    }

                    if (!found)
                    {
                        var diagnostic = Diagnostic.Create(SymbolDeclaredEventRule, sourceSymbol.Locations[0], sourceSymbol.Name, _compilationType.Name, SymbolDeclaredEventName);
                        context.ReportDiagnostic(diagnostic);
                    }
                }
            }
            public void OnCompilationEnd(CompilationEndAnalysisContext context)
            {
                foreach (var kv in _used.Where(kv => !kv.Value && (kv.Key.Locations.FirstOrDefault()?.IsInSource == true)))
                {
                    context.CancellationToken.ThrowIfCancellationRequested();
                    var symbol = kv.Key;

                    // report visible error only if symbol is not local symbol
                    if (!(symbol is ILocalSymbol))
                    {
                        context.ReportDiagnostic(Diagnostic.Create(s_rule, symbol.Locations[0], symbol.Locations.Skip(1), symbol.Name));
                    }

                    // where code fix works
                    foreach (var reference in symbol.DeclaringSyntaxReferences)
                    {
                        context.CancellationToken.ThrowIfCancellationRequested();

                        context.ReportDiagnostic(Diagnostic.Create(s_triggerRule, Location.Create(reference.SyntaxTree, reference.Span)));
                    }
                }
            }
 private void AnalyzeCompilation(CompilationEndAnalysisContext context)
 {
     OnAbstractMember("Compilation");
     OnOptions(context.Options);
 }
Beispiel #12
0
 public void AnalyzeCompilation(CompilationEndAnalysisContext context)
 {
 }
 public void AnalyzeCompilation(CompilationEndAnalysisContext context)
 {
 }
 public static void AnalyzeCompilation(CompilationEndAnalysisContext context)
 {
     var diagnostics = context.Compilation.GetDeclarationDiagnostics(cancellationToken: context.CancellationToken);
     ReportDiagnostics(diagnostics, context.ReportDiagnostic, location => !IsSourceLocation(location));
 }
Beispiel #15
0
        private async Task ProcessCompilationCompletedAsync(CompilationCompletedEvent endEvent, CancellationToken cancellationToken)
        {
            try
            {
                // Execute analyzers in parallel.
                var tasks = ArrayBuilder<Task>.GetInstance();
                foreach (var analyzerAndActions in _compilationEndActionsMap)
                {
                    var task = Task.Run(() =>
                    {
                        // Execute actions for a given analyzer sequentially.
                        foreach (var endAction in analyzerAndActions.Value)
                        {
                            Debug.Assert(endAction.Analyzer == analyzerAndActions.Key);

                            // Catch Exception from endAction
                            AnalyzerDriverHelper.ExecuteAndCatchIfThrows(endAction.Analyzer, _addDiagnostic, continueOnAnalyzerException, () =>
                                {
                                    cancellationToken.ThrowIfCancellationRequested();
                                    var compilationContext = new CompilationEndAnalysisContext(_compilation, this.analyzerOptions, _addDiagnostic, cancellationToken);
                                    endAction.Action(compilationContext);
                                }, cancellationToken);
                        }
                    }, cancellationToken);

                    tasks.Add(task);
                }

                await Task.WhenAll(tasks.ToArrayAndFree()).ConfigureAwait(false);
            }
            finally
            {
                endEvent.FlushCache();
            }
        }