protected override void Analyze(IAttribute element, ElementProblemAnalyzerData data, IHighlightingConsumer consumer)
        {
            if (!(element.TypeReference?.Resolve().DeclaredElement is ITypeElement attributeTypeElement))
            {
                return;
            }

            // Otherwise we'll treat it as targeting a method
            if (element.Target == AttributeTarget.Return)
            {
                return;
            }

            if (ourAttributeLookups.TryGetValue(attributeTypeElement.GetClrName(), out var func))
            {
                var methodDeclaration = MethodDeclarationNavigator.GetByAttribute(element);
                if (methodDeclaration == null)
                {
                    return;
                }

                var predefinedType  = myPredefinedTypeCache.GetOrCreatePredefinedType(element.GetPsiModule());
                var methodSignature = func(predefinedType);

                var match = methodSignature.Match(methodDeclaration);
                AddMethodSignatureInspections(consumer, methodDeclaration, methodSignature, match);
            }
        }
        protected override void Analyze(IAttribute element, ElementProblemAnalyzerData data, IHighlightingConsumer consumer)
        {
            var attributeTypeElement = element.TypeReference?.Resolve().DeclaredElement as ITypeElement;

            if (attributeTypeElement == null)
            {
                return;
            }

            if (Equals(attributeTypeElement.GetClrName(), KnownTypes.InitializeOnLoadAttribute))
            {
                var classLikeDeclaration = ClassLikeDeclarationNavigator.GetByAttribute(element);
                if (classLikeDeclaration != null && classLikeDeclaration.ConstructorDeclarations.All(c => !c.IsStatic))
                {
                    // Unity doesn't report a warning if there isn't a static constructor, so just highlight
                    // the attribute as dead code.
                    consumer.AddHighlighting(new RedundantInitializeOnLoadAttributeWarning(element));
                }
            }
            else if (Equals(attributeTypeElement.GetClrName(), KnownTypes.InitializeOnLoadMethodAttribute) ||
                     Equals(attributeTypeElement.GetClrName(), KnownTypes.RuntimeInitializeOnLoadMethodAttribute))
            {
                var methodDeclaration = MethodDeclarationNavigator.GetByAttribute(element);
                if (methodDeclaration == null)
                {
                    return;
                }

                var predefinedType  = myPredefinedTypeCache.GetOrCreatePredefinedType(element.GetPsiModule());
                var methodSignature = new MethodSignature(predefinedType.Void, true);

                if (!methodDeclaration.IsStatic)
                {
                    consumer.AddHighlighting(new InvalidStaticModifierWarning(methodDeclaration, methodSignature));
                }
                if (!methodDeclaration.Type.IsVoid())
                {
                    consumer.AddHighlighting(new InvalidReturnTypeWarning(methodDeclaration, methodSignature));
                }
                if (methodDeclaration.ParameterDeclarationsEnumerable.Any())
                {
                    consumer.AddHighlighting(new InvalidSignatureWarning(methodDeclaration, methodSignature));
                }
            }
        }
Ejemplo n.º 3
0
        protected override void Analyze(IAttribute element, ElementProblemAnalyzerData data, IHighlightingConsumer consumer)
        {
            if (!(element.TypeReference?.Resolve().DeclaredElement is ITypeElement attributeTypeElement))
            {
                return;
            }

            if (element.Target == AttributeTarget.Return)
            {
                return;
            }

            var methodDeclaration = MethodDeclarationNavigator.GetByAttribute(element);

            if (methodDeclaration == null)
            {
                return;
            }

            var predefinedType           = myPredefinedTypeCache.GetOrCreatePredefinedType(element.GetPsiModule());
            var expectedMethodSignatures = GetExpectedMethodSignatures(attributeTypeElement, predefinedType);

            if (expectedMethodSignatures == null)
            {
                return;
            }

            if (expectedMethodSignatures.Length == 1)
            {
                var match = expectedMethodSignatures[0].Match(methodDeclaration);
                AddMethodSignatureInspections(consumer, methodDeclaration, expectedMethodSignatures[0], match);
            }
            else
            {
                foreach (var methodSignature in expectedMethodSignatures)
                {
                    if (methodSignature.Match(methodDeclaration) == MethodSignatureMatch.ExactMatch)
                    {
                        return;
                    }
                }

                AddMethodSignatureInspections(consumer, methodDeclaration, expectedMethodSignatures);
            }
        }
Ejemplo n.º 4
0
        protected override void Analyze(IAttribute element, ElementProblemAnalyzerData data, IHighlightingConsumer consumer)
        {
            if (!(element.TypeReference?.Resolve().DeclaredElement is ITypeElement attributeTypeElement))
            {
                return;
            }

            // Otherwise we'll treat it as targeting a method
            if (element.Target == AttributeTarget.Return)
            {
                return;
            }

            if (ourAttributeLookups.TryGetValue(attributeTypeElement.GetClrName(), out var func))
            {
                var methodDeclaration = MethodDeclarationNavigator.GetByAttribute(element);
                if (methodDeclaration == null)
                {
                    return;
                }

                var predefinedType  = myPredefinedTypeCache.GetOrCreatePredefinedType(element.GetPsiModule());
                var methodSignature = func(predefinedType);

                if (!methodSignature.HasMatchingStaticModifier(methodDeclaration))
                {
                    consumer.AddHighlighting(new InvalidStaticModifierWarning(methodDeclaration, methodSignature));
                }
                if (!methodSignature.HasMatchingReturnType(methodDeclaration))
                {
                    consumer.AddHighlighting(new InvalidReturnTypeWarning(methodDeclaration, methodSignature));
                }
                if (!methodSignature.HasMatchingTypeParameters(methodDeclaration))
                {
                    consumer.AddHighlighting(new InvalidTypeParametersWarning(methodDeclaration, methodSignature));
                }
                if (!methodSignature.HasMatchingParameters(methodDeclaration))
                {
                    consumer.AddHighlighting(new InvalidParametersWarning(methodDeclaration, methodSignature));
                }
            }
        }
        protected override void Analyze(IAttribute element, ElementProblemAnalyzerData data,
                                        IHighlightingConsumer consumer)
        {
            if (!(element.TypeReference?.Resolve().DeclaredElement is ITypeElement attributeTypeElement))
            {
                return;
            }

            // Otherwise we'll treat it as targeting a method
            if (element.Target == AttributeTarget.Return)
            {
                return;
            }

            if (attributeTypeElement.GetClrName().Equals(KnownTypes.DrawGizmo))
            {
                var methodDeclaration = MethodDeclarationNavigator.GetByAttribute(element);
                CheckMethodDeclaration(methodDeclaration, element, consumer);
            }
        }
Ejemplo n.º 6
0
        public IReference[] GetReferences(ITreeNode element, IReference[] oldReferences)
        {
            var literal = element as ILiteralExpression;

            if (literal != null && literal.ConstantValue.Value is string)
            {
                var agument   = literal.Parent as IVBArgument;
                var attribute = AttributeNavigator.GetByArgument(agument);
                if (attribute != null)
                {
                    var @class = attribute.AttributeType.Reference.Resolve().DeclaredElement as IClass;
                    if (@class != null && Equals(@class.GetClrName(), DataAttributeName))
                    {
                        var typeElement = (from a in attribute.Arguments
                                           where a is INamedArgument && a.ArgumentName == TypeMemberName
                                           select GetTypeof(a.Expression as IGetTypeExpression)).FirstOrDefault();

                        var member = MethodDeclarationNavigator.GetByAttribute(attribute) as ITypeMemberDeclaration;
                        if (member != null && member.DeclaredElement != null && typeElement == null)
                        {
                            typeElement = member.DeclaredElement.GetContainingType();
                        }

                        if (typeElement == null)
                        {
                            return(EmptyArray <IReference> .Instance);
                        }

                        var reference = CreateReference(typeElement, literal);

                        return(oldReferences != null && oldReferences.Length == 1 && Equals(oldReferences[0], reference)
                                   ? oldReferences
                                   : new[] { reference });
                    }
                }
            }

            return(EmptyArray <IReference> .Instance);
        }
Ejemplo n.º 7
0
        public IReference[] GetReferences(ITreeNode element, IReference[] oldReferences)
        {
            var literal = element as ILiteralExpression;

            if (literal != null && literal.ConstantValue.Value is string)
            {
                var attribute = AttributeNavigator.GetByConstructorArgumentExpression(literal as ICSharpExpression);
                if (attribute != null)
                {
                    var @class = attribute.Name.Reference.Resolve().DeclaredElement as IClass;
                    if (@class != null && Equals(@class.GetClrName(), XunitTestProvider.PropertyDataAttribute))
                    {
                        var typeElement = (from a in attribute.PropertyAssignments
                                           where a.PropertyNameIdentifier.Name == "PropertyType"
                                           select GetTypeof(a.Source as ITypeofExpression)).FirstOrDefault();

                        var member = MethodDeclarationNavigator.GetByAttribute(attribute);
                        if (member != null && member.DeclaredElement != null && typeElement == null)
                        {
                            typeElement = member.DeclaredElement.GetContainingType();
                        }

                        if (typeElement == null)
                        {
                            return(EmptyArray <IReference> .Instance);
                        }

                        var reference = new PropertyDataReference(typeElement, literal);

                        return(oldReferences != null && oldReferences.Length == 1 && Equals(oldReferences[0], reference)
                                   ? oldReferences
                                   : new IReference[] { reference });
                    }
                }
            }

            return(EmptyArray <IReference> .Instance);
        }