Browse Source

Fixed failing attribute unit tests.

newNRvisualizers
Mike Krüger 15 years ago
parent
commit
79b9582964
  1. 8
      ICSharpCode.NRefactory.Tests/CSharp/Resolver/AttributeTests.cs
  2. 12
      ICSharpCode.NRefactory/CSharp/Resolver/CSharpAttribute.cs
  3. 28
      ICSharpCode.NRefactory/CSharp/Resolver/ResolveVisitor.cs

8
ICSharpCode.NRefactory.Tests/CSharp/Resolver/AttributeTests.cs

@ -36,21 +36,21 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver @@ -36,21 +36,21 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver
Assert.AreEqual("System.ObsoleteAttribute", result.Type.FullName);
}
[Test, Ignore("Fails due bug in cecil loader ?")]
[Test]
public void AttributeConstructor1()
{
string program = "using System; [$LoaderOptimization(3)$] class Test { }";
var mrr = Resolve<MemberResolveResult>(program);
Assert.AreEqual("System.LoaderOptimization..ctor", mrr.Member.FullName);
Assert.AreEqual("System.LoaderOptimizationAttribute..ctor", mrr.Member.FullName);
Assert.AreEqual("System.Byte", (mrr.Member as IMethod).Parameters[0].Type.Resolve(context).FullName);
}
[Test, Ignore("Fails due bug in cecil loader ?")]
[Test]
public void AttributeConstructor2()
{
string program = "using System; [$LoaderOptimization(LoaderOptimization.NotSpecified)$] class Test { }";
var mrr = Resolve<MemberResolveResult>(program);
Assert.AreEqual("System.LoaderOptimization..ctor", mrr.Member.FullName);
Assert.AreEqual("System.LoaderOptimizationAttribute..ctor", mrr.Member.FullName);
Assert.AreEqual("System.LoaderOptimization", (mrr.Member as IMethod).Parameters[0].Type.Resolve(context).FullName);
}

12
ICSharpCode.NRefactory/CSharp/Resolver/CSharpAttribute.cs

@ -107,10 +107,14 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver @@ -107,10 +107,14 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver
{
// If both types exist, C# considers that to be an ambiguity, but we are less strict.
IType type = withoutSuffix.Resolve(context);
if (type == SharedTypes.UnknownType)
return withSuffix.Resolve(context);
else
return type;
var attrType = context.GetTypeDefinition (typeof(System.Attribute));
if (attrType == null)
return SharedTypes.UnknownType;
if (type == SharedTypes.UnknownType || !(type.GetDefinition () != null && type.GetDefinition ().IsDerivedFrom (attrType, context)))
type = withSuffix.Resolve(context);
return type;
}
public override string ToString()

28
ICSharpCode.NRefactory/CSharp/Resolver/ResolveVisitor.cs

@ -1123,32 +1123,28 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver @@ -1123,32 +1123,28 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver
#endregion
#region Attributes
IType GetAttributeType (Attribute attribute)
{
var attributeType = attribute.Type;
var type = ResolveType(attributeType);
if (!type.Equals (SharedTypes.UnknownType))
return type;
AstType typeWithAttributeSuffix;
if (attributeType is SimpleType) {
var st = (SimpleType)attributeType;
typeWithAttributeSuffix = new SimpleType (st.Identifier + "Attribute");
} else if (attributeType is MemberType) {
var mt = (MemberType)attributeType;
typeWithAttributeSuffix = new MemberType (mt.Target.Clone (), mt.MemberName + "Attribute");
ITypeReference GetAttributeType (Attribute attribute)
{
var withoutSuffix = MakeTypeReference(attribute.Type);
ITypeReference withSuffix;
if (attribute.Type is SimpleType) {
var st = (SimpleType)attribute.Type;
withSuffix = MakeTypeReference(new SimpleType (st.Identifier + "Attribute"));
} else if (attribute.Type is MemberType) {
var mt = (MemberType)attribute.Type;
withSuffix = MakeTypeReference(new MemberType (mt.Target.Clone (), mt.MemberName + "Attribute"));
} else {
// unsupported type.
return SharedTypes.UnknownType;
}
return ResolveType(typeWithAttributeSuffix);
return new AttributeTypeReference(withoutSuffix, withSuffix);
}
public override ResolveResult VisitAttribute(Attribute attribute, object data)
{
ScanChildren(attribute);
if (resolverEnabled) {
var type = GetAttributeType (attribute);
var type = GetAttributeType (attribute).Resolve (resolver.Context);
if (!attribute.HasArgumentList)
return new TypeResolveResult (type);
// try if the attribute usage references a constructuor

Loading…
Cancel
Save