예제 #1
0
        public static AttributeData?FindAttributeData(this AttributeSyntax syntax, ISymbol targetSymbol)
        {
            AttributeTargetSpecifierSyntax attributeTarget = syntax.FirstAncestorOrSelf <AttributeListSyntax>().Target;

            if (attributeTarget is not null)
            {
                switch (attributeTarget.Identifier.Kind())
                {
                case SyntaxKind.ReturnKeyword:
                    return(((IMethodSymbol)targetSymbol).GetReturnTypeAttributes().First(attributeSyntaxLocationMatches));

                case SyntaxKind.AssemblyKeyword:
                    return(targetSymbol.ContainingAssembly.GetAttributes().First(attributeSyntaxLocationMatches));

                case SyntaxKind.ModuleKeyword:
                    return(targetSymbol.ContainingModule.GetAttributes().First(attributeSyntaxLocationMatches));

                default:
                    return(null);
                }
            }
            // Sometimes an attribute is put on a symbol that is nested within the containing symbol.
            // For example, the ContainingSymbol for an AttributeSyntax on a parameter have a ContainingSymbol of the method.
            // Since this method is internal and the callers don't care about attributes on parameters, we just allow
            // this method to return null in those cases.
            return(targetSymbol.GetAttributes().FirstOrDefault(attributeSyntaxLocationMatches));

            bool attributeSyntaxLocationMatches(AttributeData attrData)
            {
                return(attrData.ApplicationSyntaxReference !.SyntaxTree == syntax.SyntaxTree && attrData.ApplicationSyntaxReference.Span == syntax.Span);
            }
        }
예제 #2
0
        public override void VisitAttributeTargetSpecifier(AttributeTargetSpecifierSyntax node)
        {
            if (!PreVisit(node))
            {
                return;
            }

            base.VisitAttributeTargetSpecifier(node);

            PostVisit(node);
        }
 public override void AddChildren()
 {
     Kind = Node.Kind();
     _openBracketToken          = ((AttributeListSyntax)Node).OpenBracketToken;
     _openBracketTokenIsChanged = false;
     _target                     = ((AttributeListSyntax)Node).Target;
     _targetIsChanged            = false;
     _attributes                 = ((AttributeListSyntax)Node).Attributes;
     _attributesIsChanged        = false;
     _attributesCount            = _attributes.Count;
     _closeBracketToken          = ((AttributeListSyntax)Node).CloseBracketToken;
     _closeBracketTokenIsChanged = false;
 }
            private static SyntaxList <AttributeListSyntax> FixRemappedAttributes(
                SyntaxList <AttributeListSyntax> existingAttrList,
                List <AttributeSyntax> remappedListAttributes,
                AttributeTargetSpecifierSyntax target)
            {
                if (remappedListAttributes == null)
                {
                    return(existingAttrList);
                }

                foreach (var attrNode in remappedListAttributes)
                {
                    var attrName = attrNode.Name.ToString();
                    if (attrName.EndsWith(EncodeHelpers.AttributeToRemoveSuffix))
                    {
                        var attrNameToRemove = attrName.Substring(0, attrName.Length - EncodeHelpers.AttributeToRemoveSuffix.Length);

                        existingAttrList = SyntaxUtils.RemoveAttribute(existingAttrList, attrNameToRemove);
                    }
                    else
                    {
                        // Remove any existing attribute with the same name
                        existingAttrList = SyntaxUtils.RemoveAttribute(existingAttrList, attrName);

                        var attrListNode =
                            SyntaxFactory.AttributeList(
                                SyntaxFactory.SingletonSeparatedList <AttributeSyntax>(attrNode));
                        if (target != null)
                        {
                            attrListNode = attrListNode.WithTarget(target);
                        }

                        existingAttrList = existingAttrList.Add(attrListNode);
                    }
                }

                return(existingAttrList);
            }
예제 #5
0
 public override void VisitAttributeTargetSpecifier(AttributeTargetSpecifierSyntax node)
 {
     // not interested in supporting this!
     base.VisitAttributeTargetSpecifier(node);
 }
예제 #6
0
 public override Evaluation VisitAttributeTargetSpecifier(AttributeTargetSpecifierSyntax node)
 {
     return(base.VisitAttributeTargetSpecifier(node));
 }
예제 #7
0
        private static bool MatchAttributeTarget(IAttributeTargetSymbol attributeTarget, AttributeLocation symbolPart, AttributeTargetSpecifierSyntax targetOpt, DiagnosticBag diagnostics)
        {
            IAttributeTargetSymbol attributesOwner = attributeTarget.AttributesOwner;

            // Determine if the target symbol owns the attribute declaration.
            // We need to report diagnostics only once, so do it when visiting attributes for the owner.
            bool isOwner = symbolPart == AttributeLocation.None && ReferenceEquals(attributesOwner, attributeTarget);

            if (targetOpt == null)
            {
                // only attributes with an explicit target match if the symbol doesn't own the attributes:
                return(isOwner);
            }

            AttributeLocation allowedTargets = attributesOwner.AllowedAttributeLocations;

            AttributeLocation explicitTarget = targetOpt.GetAttributeLocation();

            if (explicitTarget == AttributeLocation.None)
            {
                // error: unknown attribute location
                if (isOwner)
                {
                    //NOTE: ValueText so that we accept targets like "@return", to match dev10 (DevDiv #2591).
                    diagnostics.Add(ErrorCode.WRN_InvalidAttributeLocation,
                                    targetOpt.Identifier.GetLocation(), targetOpt.Identifier.ValueText, allowedTargets.ToDisplayString());
                }

                return(false);
            }

            if ((explicitTarget & allowedTargets) == 0)
            {
                // error: invalid target for symbol
                if (isOwner)
                {
                    if (allowedTargets == AttributeLocation.None)
                    {
                        switch (attributeTarget.DefaultAttributeLocation)
                        {
                        case AttributeLocation.Assembly:
                        case AttributeLocation.Module:
                            // global attributes are disallowed in interactive code:
                            diagnostics.Add(ErrorCode.ERR_GlobalAttributesNotAllowed, targetOpt.Identifier.GetLocation());
                            break;

                        default:
                            // currently this can't happen
                            throw ExceptionUtilities.UnexpectedValue(attributeTarget.DefaultAttributeLocation);
                        }
                    }
                    else
                    {
                        diagnostics.Add(ErrorCode.WRN_AttributeLocationOnBadDeclaration,
                                        targetOpt.Identifier.GetLocation(), targetOpt.Identifier.ToString(), allowedTargets.ToDisplayString());
                    }
                }

                return(false);
            }

            if (symbolPart == AttributeLocation.None)
            {
                return(explicitTarget == attributeTarget.DefaultAttributeLocation);
            }
            else
            {
                return(explicitTarget == symbolPart);
            }
        }
예제 #8
0
 public override void VisitAttributeTargetSpecifier(AttributeTargetSpecifierSyntax node)
 {
     Debug.Fail(node.ToString());
     base.VisitAttributeTargetSpecifier(node);
 }
예제 #9
0
파일: Node.cs 프로젝트: binarybird/Cascade
 public override void VisitAttributeTargetSpecifier(AttributeTargetSpecifierSyntax node)
 {
     base.VisitAttributeTargetSpecifier(node);
 }
예제 #10
0
 public override void VisitAttributeTargetSpecifier(AttributeTargetSpecifierSyntax node)
 {
     throw new NotImplementedException();
 }
예제 #11
0
 public override SyntaxNode VisitAttributeTargetSpecifier(AttributeTargetSpecifierSyntax node)
 {
     node = (AttributeTargetSpecifierSyntax)base.VisitAttributeTargetSpecifier(node);
     Classes.Add(node);
     return(node);
 }
예제 #12
0
 public TameAttributeTargetSpecifierSyntax(AttributeTargetSpecifierSyntax node)
 {
     Node = node;
     AddChildren();
 }
 //
 // Summary:
 //     Called when the visitor visits a AttributeTargetSpecifierSyntax node.
 public virtual void VisitAttributeTargetSpecifier(AttributeTargetSpecifierSyntax node);