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

Open
opened 2013-02-07 02:28:22 -06:00 by erik-kallen · 4 comments
erik-kallen commented 2013-02-07 02:28:22 -06:00 (Migrated from github.com)

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?

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 commented 2013-02-21 15:22:51 -06:00 (Migrated from github.com)

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).

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 commented 2013-02-26 04:07:51 -06:00 (Migrated from github.com)

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?

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 commented 2013-02-26 16:48:25 -06:00 (Migrated from github.com)

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

Yes, the unresolved attribute needs to store a reference to the `cu.ConditionalSymbols` list.
erik-kallen commented 2013-03-07 16:24:50 -06:00 (Migrated from github.com)

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?

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`?
This discussion has been locked. Commenting is limited to contributors.
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
gh-icsharpcode/NRefactory#140
No description provided.