Skip to content
This repository has been archived by the owner on Sep 24, 2020. It is now read-only.

How to determine if an attribute is conditionally excluded? #140

Open
erik-kallen opened this issue Feb 7, 2013 · 4 comments
Open

How to determine if an attribute is conditionally excluded? #140

erik-kallen opened this issue Feb 7, 2013 · 4 comments

Comments

@erik-kallen
Copy link
Contributor

It is possible to specify a [ConditionalAttribute] on an attribute type to indicate that the attribute should only be present in the compiled assembly if a certain symbol is defined when the attribute is instantiated. I need access to this information, so how can we represent it?

I suggest a bool IsConditionallyExcluded flag on IAttribute. What do you think?

@dgrunwald
Copy link
Member

The IsConditionallyExcluded property in the type system is a good idea.

In SharpDevelop, we currently use this method in the semantic highlighter:

    bool IsInactiveConditional(IList<IAttribute> attributes)
    {
        bool hasConditionalAttribute = false;
        foreach (var attr in attributes) {
            if (attr.AttributeType.Name == "ConditionalAttribute" && attr.AttributeType.Namespace == "System.Diagnostics" && attr.PositionalArguments.Count == 1) {
                string symbol = attr.PositionalArguments[0].ConstantValue as string;
                if (symbol != null) {
                    hasConditionalAttribute = true;
                    var cu = this.resolver.RootNode as SyntaxTree;
                    if (cu != null) {
                        if (cu.ConditionalSymbols.Contains(symbol))
                            return false; // conditional is active
                    }
                }
            }
        }
        return hasConditionalAttribute;
    }

    public override void VisitAttribute(ICSharpCode.NRefactory.CSharp.Attribute attribute)
    {
        ITypeDefinition attrDef = resolver.Resolve(attribute.Type).Type.GetDefinition();
        if (attrDef != null && IsInactiveConditional(attrDef.Attributes)) {
            Colorize(attribute, inactiveCodeColor);
            return;
        }
        VisitChildren(attribute);
    }

To implement IsConditionallyExcluded, we'd just need to add a IList<string> reference to CSharpAttribute (+maybe cache the property value in CSharpResolvedAttribute).

@erik-kallen
Copy link
Contributor Author

I don't understand 100% what you mean with the IList<string>. Do you mean that the list should contain the list of defined symbols at the point of the attribute instantiation?

@dgrunwald
Copy link
Member

Yes, the unresolved attribute needs to store a reference to the cu.ConditionalSymbols list.

@erik-kallen
Copy link
Contributor Author

I'm thinking about starting to implement this feature.

What do you think about also adding members called ConditionalSymbols to ITypeDefinition and IMethod, and also IsConditionallyRemoved to InvocationResolveResult?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants