当前位置: 首页>>代码示例>>C#>>正文


C# SymbolAnalysisContext.ReportDiagnostic方法代码示例

本文整理汇总了C#中SymbolAnalysisContext.ReportDiagnostic方法的典型用法代码示例。如果您正苦于以下问题:C# SymbolAnalysisContext.ReportDiagnostic方法的具体用法?C# SymbolAnalysisContext.ReportDiagnostic怎么用?C# SymbolAnalysisContext.ReportDiagnostic使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在SymbolAnalysisContext的用法示例。


在下文中一共展示了SymbolAnalysisContext.ReportDiagnostic方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Analyze

        public override void Analyze(SymbolAnalysisContext context, List<IncludeAttributeData> includeTags, List<ProtobufAttributeData> memberTags, List<ContractAttributeData> contractAttributes)
        {
            var grouped = memberTags.GroupBy(t => t.Symbol);

            foreach (var tagPerSymbol in grouped)
            {
                var list = tagPerSymbol.ToList();
                var protoMemberAttributes = list.Where(a => a is ProtoMemberAttributeData).Cast<ProtoMemberAttributeData>().ToList();
                var dataMemberAttributes = list.Where(a => a is DataMemberAttributeData).Cast<DataMemberAttributeData>().ToList();

                foreach (var protoMemberTag in protoMemberAttributes)
                {
                    foreach (var mismatchedDataMember in dataMemberAttributes.Where(d => d.Tag != protoMemberTag.Tag))
                    {
                        context.ReportDiagnostic(Diagnostic.Create(
                            GetDescriptor(), 
                            protoMemberTag.GetLocation(),
                            protoMemberTag.Symbol.Name,
                            protoMemberTag.Tag,
                            mismatchedDataMember.Tag));
                        context.ReportDiagnostic(Diagnostic.Create(
                            GetDescriptor(),
                            mismatchedDataMember.GetLocation(),
                            mismatchedDataMember.Symbol.Name,
                            protoMemberTag.Tag,
                            mismatchedDataMember.Tag));
                    }
                }
            }
        }
开发者ID:julienadam,项目名称:Protobuf.CodeFixes,代码行数:30,代码来源:ProtoMemberDataMemberTagMismatchDiagnosticAnalyzer.cs

示例2: AnalyzeType

        protected void AnalyzeType(SymbolAnalysisContext context)
        {
            var potentialController = (INamedTypeSymbol)context.Symbol;
            if (potentialController.AllInterfaces.Length == 0) return;

            var controllerTechnology = DetermineWebTechnology(potentialController.AllInterfaces);
            if (controllerTechnology == WebTechnology.None) return;

            var controllerAttributes = potentialController.GetAttributes();
            foreach (var controllerAttribute in controllerAttributes)
            {
                var attributeTechnology = DetermineWebTechnology(controllerAttribute.AttributeClass.AllInterfaces);
                if (attributeTechnology != WebTechnology.None && attributeTechnology != controllerTechnology)
                    context.ReportDiagnostic(Diagnostic.Create(webTechMixDiagnosticDescriptor, potentialController.Locations[0], 
                        controllerAttribute.AttributeClass.Name, attributeTechnology, potentialController.Name, controllerTechnology));
            }

            foreach (var method in potentialController.GetMembers().Where(m => m.Kind == SymbolKind.Method))
            {
                foreach (var methodAttribute in method.GetAttributes())
                {
                    var attributeTechnology = DetermineWebTechnology(methodAttribute.AttributeClass.AllInterfaces);
                    if (attributeTechnology != WebTechnology.None && attributeTechnology != controllerTechnology)
                    {
                        context.ReportDiagnostic(Diagnostic.Create(webTechMixDiagnosticDescriptor, method.Locations[0],
                            methodAttribute.AttributeClass.Name,
                            attributeTechnology, potentialController.Name, controllerTechnology));
                        break;
                    }
                }
            }
        }
开发者ID:damieng,项目名称:CSharpCodeFixes,代码行数:32,代码来源:WrongAttributeOnControllerAnalyzer.cs

示例3: AnalyzeSymbol

        private static void AnalyzeSymbol(SymbolAnalysisContext context)
        {
            var namedTypeSymbol = (INamedTypeSymbol)context.Symbol;

            if (HasStackOnlyAttribute(namedTypeSymbol)) {
                if (namedTypeSymbol.IsValueType) { return; }
                else { // reference types should not have StackOnlyAttribute applied
                    var diagnosticClass = Diagnostic.Create(RuleStackOnlyClass, namedTypeSymbol.Locations[0], namedTypeSymbol.Name);
                    context.ReportDiagnostic(diagnosticClass);
                }
            }

            // This is not a stack-only type, so ...
            // check if any fields are of stack-only types 
            foreach (var member in namedTypeSymbol.GetMembers())
            {
                var field = member as IFieldSymbol;
                if (field == null) { continue; }

                var fieldType = field.Type;
                if(HasStackOnlyAttribute(fieldType)) { 
                    var diagnostic = Diagnostic.Create(RuleStackOnlyField, field.Locations[0], field.Type.Name);
                    context.ReportDiagnostic(diagnostic);
                }
            }
        }
开发者ID:GrimDerp,项目名称:corefxlab,代码行数:26,代码来源:StackOnlyTypeAnalyzer.cs

示例4: AnalyzeField

 private static void AnalyzeField(SymbolAnalysisContext context)
 {
     if (context.IsGenerated()) return;
     var fieldSymbol = (IFieldSymbol)context.Symbol;
     if (!fieldSymbol.Type.AllInterfaces.Any(i => i.ToString() == "System.IDisposable") && fieldSymbol.Type.ToString() != "System.IDisposable") return;
     var fieldSyntaxRef = fieldSymbol.DeclaringSyntaxReferences.FirstOrDefault();
     var variableDeclarator = fieldSyntaxRef.GetSyntax() as VariableDeclaratorSyntax;
     if (variableDeclarator == null) return;
     if (ContainingTypeImplementsIDisposableAndCallsItOnTheField(context, fieldSymbol, fieldSymbol.ContainingType)) return;
     var props = new Dictionary<string, string> { { "variableIdentifier", variableDeclarator.Identifier.ValueText } }.ToImmutableDictionary();
     if (variableDeclarator.Initializer?.Value is InvocationExpressionSyntax)
         context.ReportDiagnostic(Diagnostic.Create(RuleForReturned, variableDeclarator.GetLocation(), props, fieldSymbol.Name));
     else if (variableDeclarator.Initializer?.Value is ObjectCreationExpressionSyntax)
         context.ReportDiagnostic(Diagnostic.Create(RuleForCreated, variableDeclarator.GetLocation(), props, fieldSymbol.Name));
 }
开发者ID:Cadums01,项目名称:code-cracker,代码行数:15,代码来源:DisposableFieldNotDisposedAnalyzer.cs

示例5: AnalyzeSymbol

        private void AnalyzeSymbol(SymbolAnalysisContext context, INamedTypeSymbol flagsAttribute)
        {
            var symbol = (INamedTypeSymbol)context.Symbol;

            if (symbol.TypeKind != TypeKind.Enum)
            {
                return;
            }

            SpecialType underlyingType = symbol.EnumUnderlyingType.SpecialType;
            if (underlyingType == SpecialType.System_Int32)
            {
                return;
            }

            // If accessibility of enum is not public exit
            if (symbol.GetResultantVisibility() != SymbolVisibility.Public)
            {
                return;
            }

            // If enum is Int64 and has Flags attributes then exit
            bool hasFlagsAttribute = symbol.GetAttributes().Any(a => a.AttributeClass.Equals(flagsAttribute));
            if (underlyingType == SpecialType.System_Int64 && hasFlagsAttribute)
            {
                return;
            }

            context.ReportDiagnostic(symbol.CreateDiagnostic(Rule, symbol.Name, symbol.EnumUnderlyingType));
        }
开发者ID:Anniepoh,项目名称:roslyn-analyzers,代码行数:30,代码来源:EnumStorageShouldBeInt32.cs

示例6: AnalyzeSymbol

        private void AnalyzeSymbol(SymbolAnalysisContext context)
        {
            var namedTypeSymbol = (INamedTypeSymbol)context.Symbol;

            var members = namedTypeSymbol.GetMembers().Where(x => !x.IsImplicitlyDeclared && x.CanBeReferencedByName).ToArray();

            var orderedMembers = members.OrderBy(x => x.DeclaredAccessibility).ThenBy(x => x.Kind).ThenBy(x => x.Name).ToArray();

            var alphabetized = true;

            for (var i = 0; i < members.Length; i++)
            {
                if(orderedMembers[i] != members[i])
                {
                    alphabetized = false;
                }
            }

            if (!alphabetized)
            {
                var diagnostic = Diagnostic.Create(Rule, namedTypeSymbol.Locations[0], namedTypeSymbol.Name);

                context.ReportDiagnostic(diagnostic);
            }
        }
开发者ID:Ophlan,项目名称:Ophlan.Analyzers,代码行数:25,代码来源:OrganizeMembersAnalyzer.cs

示例7: AnalyzeSymbol

        private static void AnalyzeSymbol(SymbolAnalysisContext context)
        {
            var namedSymbol = context.Symbol as INamedTypeSymbol;
            var iMvxViewType = context.Compilation.GetTypeByMetadataName(typeof(IMvxView).FullName);

            if (namedSymbol != null &&
                namedSymbol.ImplementsSymbol(iMvxViewType) &&
                namedSymbol.GetMembers(ViewModelPropertyName).Any())
            {
                var viewModelProperty = namedSymbol.GetMembers(ViewModelPropertyName).FirstOrDefault() as IPropertySymbol;
                var viewModelReturnType = viewModelProperty?.Type;

                if (!IsViewModelType(context, viewModelReturnType))
                    return;

                var syntax = namedSymbol.DeclaringSyntaxReferences.First().GetSyntax() as ClassDeclarationSyntax;

                var baseType = syntax?.BaseList.Types.First();

                if (baseType?.Type is GenericNameSyntax) return;

                context.ReportDiagnostic(
                    Diagnostic.Create(
                        Rule
                        , baseType?.GetLocation()
                        , baseType?.ToString())
                );
            }
        }
开发者ID:MvvmCross,项目名称:MvvmCross,代码行数:29,代码来源:ConsiderUsingGenericBaseViewAnalyzer.cs

示例8: HandleMethodDeclaration

        private void HandleMethodDeclaration(SymbolAnalysisContext context)
        {
            IMethodSymbol symbol = (IMethodSymbol)context.Symbol;
            if (symbol.IsAsync)
                return;

            if (!symbol.Name.EndsWith("Async", StringComparison.Ordinal))
                return;

            if (symbol.Locations.IsDefaultOrEmpty)
                return;

            Location location = symbol.Locations[0];
            if (!location.IsInSource || location.SourceTree.IsGeneratedDocument(context.CancellationToken))
                return;

            if (!symbol.ReturnsVoid)
            {
                if (string.Equals(nameof(Task), symbol.ReturnType?.Name, StringComparison.Ordinal)
                    && string.Equals(typeof(Task).Namespace, symbol.ReturnType?.ContainingNamespace?.ToString(), StringComparison.Ordinal))
                {
                    return;
                }
            }

            if (symbol.IsOverride)
                return;

            context.ReportDiagnostic(Diagnostic.Create(Descriptor, symbol.Locations[0], symbol.Name));
        }
开发者ID:ChrSteinert,项目名称:AsyncUsageAnalyzers,代码行数:30,代码来源:AvoidAsyncSuffixAnalyzer.cs

示例9: AnalyzeSymbol

        private void AnalyzeSymbol(SymbolAnalysisContext context)
        {
            var symbol = (IPropertySymbol)context.Symbol;
            if (symbol.IsIndexer && !symbol.IsOverride)
            {
                if (symbol.GetParameters().Length == 1)
                {
                    ITypeSymbol paramType = symbol.GetParameters()[0].Type;

                    if (paramType.TypeKind == TypeKind.TypeParameter)
                    {
                        return;
                    }

                    if (paramType.TypeKind == TypeKind.Enum)
                    {
                        paramType = ((INamedTypeSymbol)paramType).EnumUnderlyingType;
                    }

                    if (!s_allowedTypes.Contains(paramType.SpecialType))
                    {
                        context.ReportDiagnostic(symbol.CreateDiagnostic(Rule));
                    }
                }
            }
        }
开发者ID:duracellko,项目名称:roslyn-analyzers,代码行数:26,代码来源:UseIntegralOrStringArgumentForIndexers.cs

示例10: AnalyzeField

        private static void AnalyzeField(SymbolAnalysisContext symbolAnalysisContext)
        {
            var fieldDeclarationSyntax = (IFieldSymbol)symbolAnalysisContext.Symbol;
            if (!IsFieldPrivate(fieldDeclarationSyntax) &&
                !IsStaticReadonly(fieldDeclarationSyntax) &&
                IsParentAClass(fieldDeclarationSyntax) &&
                !fieldDeclarationSyntax.IsConst)
            {
                foreach (var location in symbolAnalysisContext.Symbol.Locations)
                {
                    if (!location.IsInSource)
                    {
                        // assume symbols not defined in a source document are "out of reach"
                        return;
                    }

                    if (location.SourceTree.IsGeneratedDocument(symbolAnalysisContext.Compilation, symbolAnalysisContext.CancellationToken))
                    {
                        return;
                    }
                }

                symbolAnalysisContext.ReportDiagnostic(Diagnostic.Create(Descriptor, fieldDeclarationSyntax.Locations[0]));
            }
        }
开发者ID:Noryoko,项目名称:StyleCopAnalyzers,代码行数:25,代码来源:SA1401FieldsMustBePrivate.cs

示例11: AnalyzeSymbol

        /// <summary>
        /// Implementation for CA1044: Properties should not be write only
        /// </summary>
        private static void AnalyzeSymbol(SymbolAnalysisContext context)
        {
            var property = context.Symbol as IPropertySymbol;
            if (property == null)
            {
                return;
            }
            
            // not raising a violation for when: 
            //     property is overridden because the issue can only be fixed in the base type 
            //     property is the implementaton of any interface member 
            if (property.IsOverride || property.IsImplementationOfAnyInterfaceMember())
            {
                return;
            }

            // If property is not visible outside the assembly
            if (property.GetResultantVisibility() != SymbolVisibility.Public)
            {
                return;
            }

            // We handled the non-CA1044 cases earlier.  Now, we handle CA1044 cases
            // If there is no getter then it is not accessible
            if (property.IsWriteOnly)
            {
                context.ReportDiagnostic(property.CreateDiagnostic(AddGetterRule, property.Name));
            }
            // Otherwise if there is a setter, check for its relative accessibility
            else if (!(property.IsReadOnly) && (property.GetMethod.DeclaredAccessibility < property.SetMethod.DeclaredAccessibility))
            {
                context.ReportDiagnostic(property.CreateDiagnostic(MakeMoreAccessibleRule, property.Name));
            }
        }
开发者ID:bkoelman,项目名称:roslyn-analyzers,代码行数:37,代码来源:PropertiesShouldNotBeWriteOnly.cs

示例12: Analyze

        private static void Analyze(SymbolAnalysisContext context)
        {
            if (context.IsGenerated()) return;
            var symbol = (INamedTypeSymbol)context.Symbol;
            if (symbol.TypeKind != TypeKind.Class) return;
            if (!symbol.Interfaces.Any(i => i.SpecialType == SpecialType.System_IDisposable)) return;
            if (symbol.IsSealed && !ContainsUserDefinedFinalizer(symbol)) return;
            if (!ContainsNonPrivateConstructors(symbol)) return;
            var disposeMethod = FindDisposeMethod(symbol);
            if (disposeMethod == null) return;
            var syntaxTree = disposeMethod.DeclaringSyntaxReferences[0]?.GetSyntax();

            var statements = ((MethodDeclarationSyntax)syntaxTree)?.Body?.Statements.OfType<ExpressionStatementSyntax>();
            if (statements != null)
            {
                foreach (var statement in statements)
                {
                    var invocation = statement.Expression as InvocationExpressionSyntax;
                    var method = invocation?.Expression as MemberAccessExpressionSyntax;
                    var identifierSyntax = method?.Expression as IdentifierNameSyntax;
                    if (identifierSyntax != null && identifierSyntax.Identifier.ToString() == "GC" && method.Name.ToString() == "SuppressFinalize")
                        return;
                }
            }
            context.ReportDiagnostic(Diagnostic.Create(Rule, disposeMethod.Locations[0], symbol.Name));
        }
开发者ID:Bemjamin,项目名称:code-cracker,代码行数:26,代码来源:DisposablesShouldCallSuppressFinalizeAnalyzer.cs

示例13: Analyze

        public override void Analyze(SymbolAnalysisContext context, List<IncludeAttributeData> includeTags, List<ProtobufAttributeData> memberTags, List<ContractAttributeData> contractAttributes)
        {
            if (!includeTags.Any())
            {
                return;
            }

            var allTags = includeTags.Concat(memberTags);
           
            // Group it by tag
            var groupedByTag = allTags
                .GroupBy(m => m.Tag)
                .ToList();

            // Any group with more than one element is suspicious
            foreach (var group in groupedByTag.Where(g => g.Count() > 1))
            {
                // Any group with an include means an error
                if (group.Any(a => a is ProtoIncludeAttributeData))
                {
                    var symbolList = string.Join(", ", group.Select(g => g.GetRelevantSymbolName()));
                    foreach (var a in group)
                    {
                        var diagnostic = Diagnostic.Create(GetDescriptor(), a.GetLocation(), a.Tag, context.Symbol.Name, symbolList);
                        context.ReportDiagnostic(diagnostic);
                    }
                }
            }
        }
开发者ID:julienadam,项目名称:Protobuf.CodeFixes,代码行数:29,代码来源:DuplicateTagBetweenMemberAndIncludeDiagnosticAnalyzer.cs

示例14: HandleMethodDeclaration

        private void HandleMethodDeclaration(SymbolAnalysisContext context)
        {
            IMethodSymbol symbol = (IMethodSymbol)context.Symbol;
            if (symbol.Name.EndsWith("Async", StringComparison.Ordinal))
                return;

            if (symbol.Locations.IsDefaultOrEmpty)
                return;

            Location location = symbol.Locations[0];
            if (!location.IsInSource || location.SourceTree.IsGeneratedDocument(context.CancellationToken))
                return;

            // void-returning methods are not asynchronous according to their signature, even if they use `async`
            if (symbol.ReturnsVoid)
                return;

            if (!string.Equals(nameof(Task), symbol.ReturnType?.Name, StringComparison.Ordinal))
                return;

            if (!string.Equals(typeof(Task).Namespace, symbol.ReturnType?.ContainingNamespace?.ToString(), StringComparison.Ordinal))
                return;

            if (symbol.MethodKind == MethodKind.PropertyGet || symbol.MethodKind == MethodKind.PropertySet)
                return;

            context.ReportDiagnostic(Diagnostic.Create(Descriptor, symbol.Locations[0], symbol.Name));
        }
开发者ID:RichiCoder1,项目名称:AsyncUsageAnalyzers,代码行数:28,代码来源:UseAsyncSuffixAnalyzer.cs

示例15: AnalyzeSymbol

        private static void AnalyzeSymbol(SymbolAnalysisContext context, INamedTypeSymbol equatableType)
        {
            var namedType = context.Symbol as INamedTypeSymbol;
            if (namedType == null || !(namedType.TypeKind == TypeKind.Struct || namedType.TypeKind == TypeKind.Class))
            {
                return;
            }

            bool overridesObjectEquals = namedType.OverridesEquals();

            INamedTypeSymbol constructedEquatable = equatableType.Construct(namedType);
            INamedTypeSymbol implementation = namedType
                .Interfaces
                .Where(x => x.Equals(constructedEquatable))
                .FirstOrDefault();
            bool implementsEquatable = implementation != null;

            if (overridesObjectEquals && !implementsEquatable && namedType.TypeKind == TypeKind.Struct)
            {
                context.ReportDiagnostic(namedType.CreateDiagnostic(s_implementIEquatableDescriptor, namedType));
            }

            if (!overridesObjectEquals && implementsEquatable)
            {
                context.ReportDiagnostic(namedType.CreateDiagnostic(s_overridesObjectEqualsDescriptor, namedType));
            }
        }
开发者ID:bkoelman,项目名称:roslyn-analyzers,代码行数:27,代码来源:EquatableAnalyzer.cs


注:本文中的SymbolAnalysisContext.ReportDiagnostic方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。