From 9b533ae127be4e40e01719ee5dbacbe07debc94d Mon Sep 17 00:00:00 2001 From: Doug Bunting <6431421+dougbu@users.noreply.github.com> Date: Sun, 29 Dec 2019 15:56:44 -0800 Subject: [PATCH] Do not emit fake `[StructLayout]` attributes matching defaults - see dotnet/arcade#3144 discussions (not an exact fix for that request) - `LayoutKind.Auto` is _not_ the default for value types nit: sort `using`s --- .../Writers/CSharp/CSDeclarationWriter.Types.cs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/Microsoft.Cci.Extensions/Writers/CSharp/CSDeclarationWriter.Types.cs b/src/Microsoft.Cci.Extensions/Writers/CSharp/CSDeclarationWriter.Types.cs index 92c209ee4ed..b21bd2d92d1 100644 --- a/src/Microsoft.Cci.Extensions/Writers/CSharp/CSDeclarationWriter.Types.cs +++ b/src/Microsoft.Cci.Extensions/Writers/CSharp/CSDeclarationWriter.Types.cs @@ -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 { @@ -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()); @@ -96,7 +97,7 @@ private void WriteBaseTypes(ITypeDefinition type) { ITypeReference baseType = GetBaseType(type); IEnumerable interfaces = type.Interfaces.Where(IncludeBaseType).OrderBy(t => GetTypeName(t), StringComparer.OrdinalIgnoreCase); - + if (baseType == null && !interfaces.Any()) return;