Browse Source

Added support for ArrayInitializers=false to suppress prettifier over array initializers (used by SharpLab).

pull/1051/head
Andrey Shchekin 7 years ago
parent
commit
6ba6b8c8f6
  1. 3
      ICSharpCode.Decompiler.Tests/ICSharpCode.Decompiler.Tests.csproj
  2. 4
      ICSharpCode.Decompiler.Tests/TestCases/Ugly/.gitignore
  3. 32
      ICSharpCode.Decompiler.Tests/TestCases/Ugly/NoArrayInitializers.Expected.cs
  4. 39
      ICSharpCode.Decompiler.Tests/TestCases/Ugly/NoArrayInitializers.cs
  5. 107
      ICSharpCode.Decompiler.Tests/TestCases/Ugly/NoArrayInitializers.opt.roslyn.il
  6. 120
      ICSharpCode.Decompiler.Tests/TestCases/Ugly/NoArrayInitializers.roslyn.il
  7. 106
      ICSharpCode.Decompiler.Tests/UglyTestRunner.cs
  8. 11
      ICSharpCode.Decompiler/CSharp/CSharpDecompiler.cs
  9. 19
      ICSharpCode.Decompiler/DecompilerSettings.cs
  10. 2
      ICSharpCode.Decompiler/IL/Transforms/TransformArrayInitializers.cs

3
ICSharpCode.Decompiler.Tests/ICSharpCode.Decompiler.Tests.csproj

@ -58,12 +58,15 @@ @@ -58,12 +58,15 @@
<ItemGroup>
<Compile Include="DataFlowTest.cs" />
<Compile Include="UglyTestRunner.cs" />
<Compile Include="TestCases\Correctness\ExpressionTrees.cs" />
<Compile Include="TestCases\Correctness\LINQRaytracer.cs" />
<Compile Include="TestCases\Correctness\MiniJSON.cs" />
<Compile Include="TestCases\Correctness\FloatingPointArithmetic.cs" />
<Compile Include="TestCases\ILPretty\Issue1038.cs" />
<None Include="TestCases\ILPretty\Issue1038.il" />
<Compile Include="TestCases\Ugly\NoArrayInitializers.cs" />
<None Include="TestCases\Ugly\NoArrayInitializers.Expected.cs" />
<None Include="TestCases\ILPretty\Issue1047.cs" />
<Compile Include="TestCases\ILPretty\Issue982.cs" />
<Compile Include="TestCases\Pretty\AsyncMain.cs" />

4
ICSharpCode.Decompiler.Tests/TestCases/Ugly/.gitignore vendored

@ -0,0 +1,4 @@ @@ -0,0 +1,4 @@
/*.res
/*.dll
/*.exe
/*.pdb

32
ICSharpCode.Decompiler.Tests/TestCases/Ugly/NoArrayInitializers.Expected.cs

@ -0,0 +1,32 @@ @@ -0,0 +1,32 @@
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace ICSharpCode.Decompiler.Tests.TestCases.Ugly
{
public class NoArrayInitializers
{
public int[] LiteralArray()
{
int[] obj = new int[3];
RuntimeHelpers.InitializeArray(obj, (RuntimeFieldHandle)/*OpCode not supported: LdMemberToken*/);
return obj;
}
public int[] VariableArray(int a, int b)
{
int[] obj = new int[2];
obj[0] = a;
obj[1] = b;
return obj;
}
}
}
[CompilerGenerated]
internal sealed class _003CPrivateImplementationDetails_003E
{
[StructLayout(LayoutKind.Explicit, Pack = 1, Size = 12)]
private struct __StaticArrayInitTypeSize_003D12
{
}
internal static readonly __StaticArrayInitTypeSize_003D12 E429CCA3F703A39CC5954A6572FEC9086135B34E/* Not supported: data(01 00 00 00 02 00 00 00 03 00 00 00) */;
}

39
ICSharpCode.Decompiler.Tests/TestCases/Ugly/NoArrayInitializers.cs

@ -0,0 +1,39 @@ @@ -0,0 +1,39 @@
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ICSharpCode.Decompiler.Tests.TestCases.Ugly
{
public class NoArrayInitializers
{
public int[] LiteralArray()
{
return new[] { 1, 2, 3 };
}
public int[] VariableArray(int a, int b)
{
return new[] { a, b };
}
}
}

107
ICSharpCode.Decompiler.Tests/TestCases/Ugly/NoArrayInitializers.opt.roslyn.il

@ -0,0 +1,107 @@ @@ -0,0 +1,107 @@
// Microsoft (R) .NET Framework IL Disassembler. Version 4.6.1055.0
// Copyright (c) Microsoft Corporation. All rights reserved.
// Metadata version: v4.0.30319
.assembly extern mscorlib
{
.publickeytoken = (B7 7A 5C 56 19 34 E0 89 ) // .z\V.4..
.ver 4:0:0:0
}
.assembly NoArrayInitializers
{
.custom instance void [mscorlib]System.Runtime.CompilerServices.CompilationRelaxationsAttribute::.ctor(int32) = ( 01 00 08 00 00 00 00 00 )
.custom instance void [mscorlib]System.Runtime.CompilerServices.RuntimeCompatibilityAttribute::.ctor() = ( 01 00 01 00 54 02 16 57 72 61 70 4E 6F 6E 45 78 // ....T..WrapNonEx
63 65 70 74 69 6F 6E 54 68 72 6F 77 73 01 ) // ceptionThrows.
// --- The following custom attribute is added automatically, do not uncomment -------
// .custom instance void [mscorlib]System.Diagnostics.DebuggableAttribute::.ctor(valuetype [mscorlib]System.Diagnostics.DebuggableAttribute/DebuggingModes) = ( 01 00 02 00 00 00 00 00 )
.permissionset reqmin
= {[mscorlib]System.Security.Permissions.SecurityPermissionAttribute = {property bool 'SkipVerification' = bool(true)}}
.hash algorithm 0x00008004
.ver 0:0:0:0
}
.module NoArrayInitializers.dll
// MVID: {C6AD59A2-7245-425B-A6BB-29F93FAB1116}
.custom instance void [mscorlib]System.Security.UnverifiableCodeAttribute::.ctor() = ( 01 00 00 00 )
.imagebase 0x10000000
.file alignment 0x00000200
.stackreserve 0x00100000
.subsystem 0x0003 // WINDOWS_CUI
.corflags 0x00000001 // ILONLY
// Image base: 0x00FD0000
// =============== CLASS MEMBERS DECLARATION ===================
.class public auto ansi beforefieldinit ICSharpCode.Decompiler.Tests.TestCases.Ugly.NoArrayInitializers
extends [mscorlib]System.Object
{
.method public hidebysig instance int32[]
LiteralArray() cil managed
{
// Code size 18 (0x12)
.maxstack 8
IL_0000: ldc.i4.3
IL_0001: newarr [mscorlib]System.Int32
IL_0006: dup
IL_0007: ldtoken field valuetype '<PrivateImplementationDetails>'/'__StaticArrayInitTypeSize=12' '<PrivateImplementationDetails>'::E429CCA3F703A39CC5954A6572FEC9086135B34E
IL_000c: call void [mscorlib]System.Runtime.CompilerServices.RuntimeHelpers::InitializeArray(class [mscorlib]System.Array,
valuetype [mscorlib]System.RuntimeFieldHandle)
IL_0011: ret
} // end of method NoArrayInitializers::LiteralArray
.method public hidebysig instance int32[]
VariableArray(int32 a,
int32 b) cil managed
{
// Code size 15 (0xf)
.maxstack 8
IL_0000: ldc.i4.2
IL_0001: newarr [mscorlib]System.Int32
IL_0006: dup
IL_0007: ldc.i4.0
IL_0008: ldarg.1
IL_0009: stelem.i4
IL_000a: dup
IL_000b: ldc.i4.1
IL_000c: ldarg.2
IL_000d: stelem.i4
IL_000e: ret
} // end of method NoArrayInitializers::VariableArray
.method public hidebysig specialname rtspecialname
instance void .ctor() cil managed
{
// Code size 7 (0x7)
.maxstack 8
IL_0000: ldarg.0
IL_0001: call instance void [mscorlib]System.Object::.ctor()
IL_0006: ret
} // end of method NoArrayInitializers::.ctor
} // end of class ICSharpCode.Decompiler.Tests.TestCases.Ugly.NoArrayInitializers
.class private auto ansi sealed '<PrivateImplementationDetails>'
extends [mscorlib]System.Object
{
.custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = ( 01 00 00 00 )
.class explicit ansi sealed nested private '__StaticArrayInitTypeSize=12'
extends [mscorlib]System.ValueType
{
.pack 1
.size 12
} // end of class '__StaticArrayInitTypeSize=12'
.field static assembly initonly valuetype '<PrivateImplementationDetails>'/'__StaticArrayInitTypeSize=12' E429CCA3F703A39CC5954A6572FEC9086135B34E at I_00002698
} // end of class '<PrivateImplementationDetails>'
// =============================================================
.data cil I_00002698 = bytearray (
01 00 00 00 02 00 00 00 03 00 00 00)
// *********** DISASSEMBLY COMPLETE ***********************

120
ICSharpCode.Decompiler.Tests/TestCases/Ugly/NoArrayInitializers.roslyn.il

@ -0,0 +1,120 @@ @@ -0,0 +1,120 @@
// Microsoft (R) .NET Framework IL Disassembler. Version 4.6.1055.0
// Copyright (c) Microsoft Corporation. All rights reserved.
// Metadata version: v4.0.30319
.assembly extern mscorlib
{
.publickeytoken = (B7 7A 5C 56 19 34 E0 89 ) // .z\V.4..
.ver 4:0:0:0
}
.assembly NoArrayInitializers
{
.custom instance void [mscorlib]System.Runtime.CompilerServices.CompilationRelaxationsAttribute::.ctor(int32) = ( 01 00 08 00 00 00 00 00 )
.custom instance void [mscorlib]System.Runtime.CompilerServices.RuntimeCompatibilityAttribute::.ctor() = ( 01 00 01 00 54 02 16 57 72 61 70 4E 6F 6E 45 78 // ....T..WrapNonEx
63 65 70 74 69 6F 6E 54 68 72 6F 77 73 01 ) // ceptionThrows.
// --- The following custom attribute is added automatically, do not uncomment -------
// .custom instance void [mscorlib]System.Diagnostics.DebuggableAttribute::.ctor(valuetype [mscorlib]System.Diagnostics.DebuggableAttribute/DebuggingModes) = ( 01 00 07 01 00 00 00 00 )
.permissionset reqmin
= {[mscorlib]System.Security.Permissions.SecurityPermissionAttribute = {property bool 'SkipVerification' = bool(true)}}
.hash algorithm 0x00008004
.ver 0:0:0:0
}
.module NoArrayInitializers.dll
// MVID: {C1B38171-55C8-4DDA-ACC8-78EFA1168D40}
.custom instance void [mscorlib]System.Security.UnverifiableCodeAttribute::.ctor() = ( 01 00 00 00 )
.imagebase 0x10000000
.file alignment 0x00000200
.stackreserve 0x00100000
.subsystem 0x0003 // WINDOWS_CUI
.corflags 0x00000001 // ILONLY
// Image base: 0x03D10000
// =============== CLASS MEMBERS DECLARATION ===================
.class public auto ansi beforefieldinit ICSharpCode.Decompiler.Tests.TestCases.Ugly.NoArrayInitializers
extends [mscorlib]System.Object
{
.method public hidebysig instance int32[]
LiteralArray() cil managed
{
// Code size 23 (0x17)
.maxstack 3
.locals init (int32[] V_0)
IL_0000: nop
IL_0001: ldc.i4.3
IL_0002: newarr [mscorlib]System.Int32
IL_0007: dup
IL_0008: ldtoken field valuetype '<PrivateImplementationDetails>'/'__StaticArrayInitTypeSize=12' '<PrivateImplementationDetails>'::E429CCA3F703A39CC5954A6572FEC9086135B34E
IL_000d: call void [mscorlib]System.Runtime.CompilerServices.RuntimeHelpers::InitializeArray(class [mscorlib]System.Array,
valuetype [mscorlib]System.RuntimeFieldHandle)
IL_0012: stloc.0
IL_0013: br.s IL_0015
IL_0015: ldloc.0
IL_0016: ret
} // end of method NoArrayInitializers::LiteralArray
.method public hidebysig instance int32[]
VariableArray(int32 a,
int32 b) cil managed
{
// Code size 20 (0x14)
.maxstack 4
.locals init (int32[] V_0)
IL_0000: nop
IL_0001: ldc.i4.2
IL_0002: newarr [mscorlib]System.Int32
IL_0007: dup
IL_0008: ldc.i4.0
IL_0009: ldarg.1
IL_000a: stelem.i4
IL_000b: dup
IL_000c: ldc.i4.1
IL_000d: ldarg.2
IL_000e: stelem.i4
IL_000f: stloc.0
IL_0010: br.s IL_0012
IL_0012: ldloc.0
IL_0013: ret
} // end of method NoArrayInitializers::VariableArray
.method public hidebysig specialname rtspecialname
instance void .ctor() cil managed
{
// Code size 8 (0x8)
.maxstack 8
IL_0000: ldarg.0
IL_0001: call instance void [mscorlib]System.Object::.ctor()
IL_0006: nop
IL_0007: ret
} // end of method NoArrayInitializers::.ctor
} // end of class ICSharpCode.Decompiler.Tests.TestCases.Ugly.NoArrayInitializers
.class private auto ansi sealed '<PrivateImplementationDetails>'
extends [mscorlib]System.Object
{
.custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = ( 01 00 00 00 )
.class explicit ansi sealed nested private '__StaticArrayInitTypeSize=12'
extends [mscorlib]System.ValueType
{
.pack 1
.size 12
} // end of class '__StaticArrayInitTypeSize=12'
.field static assembly initonly valuetype '<PrivateImplementationDetails>'/'__StaticArrayInitTypeSize=12' E429CCA3F703A39CC5954A6572FEC9086135B34E at I_000026C8
} // end of class '<PrivateImplementationDetails>'
// =============================================================
.data cil I_000026C8 = bytearray (
01 00 00 00 02 00 00 00 03 00 00 00)
// *********** DISASSEMBLY COMPLETE ***********************

106
ICSharpCode.Decompiler.Tests/UglyTestRunner.cs

@ -0,0 +1,106 @@ @@ -0,0 +1,106 @@
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using System.CodeDom.Compiler;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using ICSharpCode.Decompiler.Tests.Helpers;
using NUnit.Framework;
namespace ICSharpCode.Decompiler.Tests
{
public class UglyTestRunner
{
const string TestCasePath = DecompilerTestBase.TestCasePath + "/Ugly";
[Test]
public void AllFilesHaveTests()
{
var testNames = GetType().GetMethods()
.Where(m => m.GetCustomAttributes(typeof(TestAttribute), false).Any())
.Select(m => m.Name)
.ToArray();
foreach (var file in new DirectoryInfo(TestCasePath).EnumerateFiles()) {
if (file.Extension.Equals(".il", StringComparison.OrdinalIgnoreCase)
|| file.Extension.Equals(".cs", StringComparison.OrdinalIgnoreCase)) {
var testName = file.Name.Split('.')[0];
Assert.Contains(testName, testNames);
}
}
}
static readonly CompilerOptions[] noRoslynOptions =
{
CompilerOptions.None,
CompilerOptions.Optimize
};
static readonly CompilerOptions[] roslynOnlyOptions =
{
CompilerOptions.UseRoslyn,
CompilerOptions.Optimize | CompilerOptions.UseRoslyn
};
static readonly CompilerOptions[] defaultOptions =
{
CompilerOptions.None,
CompilerOptions.Optimize,
CompilerOptions.UseRoslyn,
CompilerOptions.Optimize | CompilerOptions.UseRoslyn
};
[Test]
public void NoArrayInitializers([ValueSource("roslynOnlyOptions")] CompilerOptions cscOptions)
{
RunForLibrary(cscOptions: cscOptions, decompilerSettings: new DecompilerSettings {
ArrayInitializers = false
});
}
void RunForLibrary([CallerMemberName] string testName = null, AssemblerOptions asmOptions = AssemblerOptions.None, CompilerOptions cscOptions = CompilerOptions.None, DecompilerSettings decompilerSettings = null)
{
Run(testName, asmOptions | AssemblerOptions.Library, cscOptions | CompilerOptions.Library, decompilerSettings);
}
void Run([CallerMemberName] string testName = null, AssemblerOptions asmOptions = AssemblerOptions.None, CompilerOptions cscOptions = CompilerOptions.None, DecompilerSettings decompilerSettings = null)
{
var ilFile = Path.Combine(TestCasePath, testName) + Tester.GetSuffix(cscOptions) + ".il";
var csFile = Path.Combine(TestCasePath, testName + ".cs");
var expectedFile = Path.Combine(TestCasePath, testName + ".Expected.cs");
if (!File.Exists(ilFile)) {
// re-create .il file if necessary
CompilerResults output = null;
try {
output = Tester.CompileCSharp(csFile, cscOptions);
Tester.Disassemble(output.PathToAssembly, ilFile, asmOptions);
} finally {
if (output != null)
output.TempFiles.Delete();
}
}
var executable = Tester.AssembleIL(ilFile, asmOptions);
var decompiled = Tester.DecompileCSharp(executable, decompilerSettings);
CodeAssert.FilesAreEqual(expectedFile, decompiled, Tester.GetPreprocessorSymbols(cscOptions).ToArray());
}
}
}

11
ICSharpCode.Decompiler/CSharp/CSharpDecompiler.cs

@ -220,7 +220,7 @@ namespace ICSharpCode.Decompiler.CSharp @@ -220,7 +220,7 @@ namespace ICSharpCode.Decompiler.CSharp
if (settings.FixedBuffers && type.Name.StartsWith("<", StringComparison.Ordinal) && type.Name.Contains("__FixedBuffer"))
return true;
} else if (type.IsCompilerGenerated()) {
if (type.Name.StartsWith("<PrivateImplementationDetails>", StringComparison.Ordinal))
if (settings.ArrayInitializers && type.Name.StartsWith("<PrivateImplementationDetails>", StringComparison.Ordinal))
return true;
if (settings.AnonymousTypes && type.IsAnonymousType())
return true;
@ -241,7 +241,7 @@ namespace ICSharpCode.Decompiler.CSharp @@ -241,7 +241,7 @@ namespace ICSharpCode.Decompiler.CSharp
if (settings.AutomaticEvents && field.DeclaringType.Events.Any(ev => ev.Name == field.Name))
return true;
// HACK : only hide fields starting with '__StaticArrayInit'
if (field.DeclaringType.Name.StartsWith("<PrivateImplementationDetails>", StringComparison.Ordinal)) {
if (settings.ArrayInitializers && field.DeclaringType.Name.StartsWith("<PrivateImplementationDetails>", StringComparison.Ordinal)) {
if (field.Name.StartsWith("__StaticArrayInit", StringComparison.Ordinal))
return true;
if (field.FieldType.Name.StartsWith("__StaticArrayInit", StringComparison.Ordinal))
@ -873,6 +873,13 @@ namespace ICSharpCode.Decompiler.CSharp @@ -873,6 +873,13 @@ namespace ICSharpCode.Decompiler.CSharp
RemoveAttribute(fixedFieldDecl, fixedBufferAttributeTypeName);
return fixedFieldDecl;
}
if (fieldDefinition.InitialValue.Length > 0) {
// Field data as specified in II.16.3.2 of ECMA-335 6th edition:
// .data I_X = int32(123)
// .field public static int32 _x at I_X
var message = string.Format(" Not supported: data({0}) ", BitConverter.ToString(fieldDefinition.InitialValue).Replace('-', ' '));
((FieldDeclaration)fieldDecl).Variables.Single().AddChild(new Comment(message, CommentType.MultiLine), Roles.Comment);
}
return fieldDecl;
}

19
ICSharpCode.Decompiler/DecompilerSettings.cs

@ -303,6 +303,25 @@ namespace ICSharpCode.Decompiler @@ -303,6 +303,25 @@ namespace ICSharpCode.Decompiler
}
}
bool arrayInitializers = true;
/// <summary>
/// Gets/Sets whether to use array initializers.
/// If set to false, might produce non-compilable code.
/// </summary>
public bool ArrayInitializers
{
get { return arrayInitializers; }
set
{
if (arrayInitializers != value)
{
arrayInitializers = value;
OnPropertyChanged();
}
}
}
bool objectCollectionInitializers = true;
/// <summary>

2
ICSharpCode.Decompiler/IL/Transforms/TransformArrayInitializers.cs

@ -35,6 +35,8 @@ namespace ICSharpCode.Decompiler.IL.Transforms @@ -35,6 +35,8 @@ namespace ICSharpCode.Decompiler.IL.Transforms
void IStatementTransform.Run(Block block, int pos, StatementTransformContext context)
{
if (!context.Settings.ArrayInitializers)
return;
this.context = context;
try {
if (!DoTransform(block, pos))

Loading…
Cancel
Save