Skip to content

Commit

Permalink
Do not emit fake [StructLayout] attributes matching defaults
Browse files Browse the repository at this point in the history
- see #3144 discussions (not an exact fix for that request)
- `LayoutKind.Auto` is _not_ the default for value types

nit: sort `using`s
  • Loading branch information
dougbu committed Jan 3, 2020
1 parent 037576c commit 9b533ae
Showing 1 changed file with 4 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;
using Microsoft.Cci.Extensions.CSharp;
using Microsoft.Cci.Extensions;
using Microsoft.Cci.Extensions.CSharp;

namespace Microsoft.Cci.Writers.CSharp
{
Expand All @@ -25,7 +25,8 @@ public void WriteTypeDeclaration(ITypeDefinition type)
// But we need also consider if this attribute is filtered out or not but I guess
// we have the same problem with all the fake attributes at this point.

if ((type.IsStruct || type.IsClass) && type.Layout != LayoutKind.Auto)
if (type.IsClass && type.Layout != LayoutKind.Auto ||
type.IsStruct && (type.Layout != LayoutKind.Sequential || type.Alignment != 0 || type.SizeOf != 0 || type.StringFormat != StringFormatKind.Ansi))
{
FakeCustomAttribute structLayout = new FakeCustomAttribute("System.Runtime.InteropServices", "StructLayoutAttribute");
string layoutKind = string.Format("System.Runtime.InteropServices.LayoutKind.{0}", type.Layout.ToString());
Expand Down Expand Up @@ -96,7 +97,7 @@ private void WriteBaseTypes(ITypeDefinition type)
{
ITypeReference baseType = GetBaseType(type);
IEnumerable<ITypeReference> interfaces = type.Interfaces.Where(IncludeBaseType).OrderBy(t => GetTypeName(t), StringComparer.OrdinalIgnoreCase);

if (baseType == null && !interfaces.Any())
return;

Expand Down

0 comments on commit 9b533ae

Please sign in to comment.