Skip to content

Commit

Permalink
Fix bug in MetadataMethod.GetAttributes and add tests for PreserveSig…
Browse files Browse the repository at this point in the history
… in DllImportAttribute and PreserveSigAttribute.
  • Loading branch information
siegfriedpammer committed Oct 27, 2018
1 parent 83759ee commit 1212284
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
22 changes: 20 additions & 2 deletions ICSharpCode.Decompiler.Tests/TypeSystem/TypeSystemLoaderTests.cs
Expand Up @@ -22,12 +22,10 @@
using System.IO;
using System.Linq;
using System.Reflection.Metadata;
using System.Reflection.PortableExecutable;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using ICSharpCode.Decompiler.Metadata;
using ICSharpCode.Decompiler.Semantics;
using ICSharpCode.Decompiler.TypeSystem;
using ICSharpCode.Decompiler.TypeSystem.Implementation;
using NUnit.Framework;
Expand Down Expand Up @@ -677,6 +675,26 @@ public void DllImportAttribute()
Assert.AreEqual((int)CharSet.Unicode, dllImport.NamedArguments.Single().Value);
}

[Test]
public void DllImportAttributeWithPreserveSigFalse()
{
IMethod method = GetTypeDefinition(typeof(NonCustomAttributes)).Methods.Single(m => m.Name == "DoNotPreserveSig");
IAttribute dllImport = method.GetAttributes().Single();
Assert.AreEqual("System.Runtime.InteropServices.DllImportAttribute", dllImport.AttributeType.FullName);
Assert.AreEqual("unmanaged.dll", dllImport.FixedArguments[0].Value);
Assert.AreEqual(false, dllImport.NamedArguments.Single().Value);
}

[Test]
public void PreserveSigAttribute()
{
IMethod method = GetTypeDefinition(typeof(NonCustomAttributes)).Methods.Single(m => m.Name == "PreserveSigAsAttribute");
IAttribute preserveSig = method.GetAttributes().Single();
Assert.AreEqual("System.Runtime.InteropServices.PreserveSigAttribute", preserveSig.AttributeType.FullName);
Assert.IsTrue(preserveSig.FixedArguments.Length == 0);
Assert.IsTrue(preserveSig.NamedArguments.Length == 0);
}

[Test]
public void InOutParametersOnRefMethod()
{
Expand Down
8 changes: 8 additions & 0 deletions ICSharpCode.Decompiler.Tests/TypeSystem/TypeSystemTestCase.cs
Expand Up @@ -153,6 +153,14 @@ public class NonCustomAttributes
[DllImport("unmanaged.dll", CharSet = CharSet.Unicode)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool DllMethod([In, Out] ref int p);

[DllImport("unmanaged.dll", PreserveSig = false)]
public static extern bool DoNotPreserveSig();

[PreserveSig]
public static void PreserveSigAsAttribute()
{
}
}

[StructLayout(LayoutKind.Explicit, CharSet = CharSet.Unicode, Pack = 8)]
Expand Down
Expand Up @@ -332,7 +332,7 @@ public IEnumerable<IAttribute> GetAttributes()
if ((implAttributes & MethodImplAttributes.PreserveSig) == MethodImplAttributes.PreserveSig) {
implAttributes &= ~MethodImplAttributes.PreserveSig;
} else {
dllImport.AddNamedArg("PreserveSig", KnownTypeCode.Boolean, true);
dllImport.AddNamedArg("PreserveSig", KnownTypeCode.Boolean, false);
}

if ((info.Attributes & MethodImportAttributes.SetLastError) == MethodImportAttributes.SetLastError)
Expand Down

0 comments on commit 1212284

Please sign in to comment.