Browse Source

Fix pretty unit tests.

pull/927/head
Siegfried Pammer 8 years ago
parent
commit
0b6987f195
  1. 8
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/InitializerTests.il
  2. 8
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/InitializerTests.opt.il
  3. 2
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/InitializerTests.opt.roslyn.il
  4. 2
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/InitializerTests.roslyn.il
  5. 24
      ICSharpCode.Decompiler/IL/Transforms/AssignVariableNames.cs
  6. 13
      ICSharpCode.Decompiler/IL/Transforms/TransformCollectionAndObjectInitializers.cs

8
ICSharpCode.Decompiler.Tests/TestCases/Pretty/InitializerTests.il

@ -10,7 +10,7 @@ @@ -10,7 +10,7 @@
.publickeytoken = (B7 7A 5C 56 19 34 E0 89 ) // .z\V.4..
.ver 4:0:0:0
}
.assembly '0meyjl4r'
.assembly umgm00go
{
.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
@ -20,15 +20,15 @@ @@ -20,15 +20,15 @@
.hash algorithm 0x00008004
.ver 0:0:0:0
}
.module '0meyjl4r.dll'
// MVID: {DBAD72F7-978F-407F-9C77-44D3E99CD8AD}
.module umgm00go.dll
// MVID: {8C42504A-ACC6-453C-81BA-626AB6649E25}
.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: 0x02FE0000
// Image base: 0x02AE0000
// =============== CLASS MEMBERS DECLARATION ===================

8
ICSharpCode.Decompiler.Tests/TestCases/Pretty/InitializerTests.opt.il

@ -10,7 +10,7 @@ @@ -10,7 +10,7 @@
.publickeytoken = (B7 7A 5C 56 19 34 E0 89 ) // .z\V.4..
.ver 4:0:0:0
}
.assembly u2solxiw
.assembly ecxzpbak
{
.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
@ -20,15 +20,15 @@ @@ -20,15 +20,15 @@
.hash algorithm 0x00008004
.ver 0:0:0:0
}
.module u2solxiw.dll
// MVID: {D1507E89-2CB3-46B1-AEC7-B176088BA3F9}
.module ecxzpbak.dll
// MVID: {F67EEE6A-7EFF-4261-9FC7-3F2C4A0972E9}
.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: 0x028E0000
// Image base: 0x02430000
// =============== CLASS MEMBERS DECLARATION ===================

2
ICSharpCode.Decompiler.Tests/TestCases/Pretty/InitializerTests.opt.roslyn.il

@ -32,7 +32,7 @@ @@ -32,7 +32,7 @@
.stackreserve 0x00100000
.subsystem 0x0003 // WINDOWS_CUI
.corflags 0x00000001 // ILONLY
// Image base: 0x029A0000
// Image base: 0x00700000
// =============== CLASS MEMBERS DECLARATION ===================

2
ICSharpCode.Decompiler.Tests/TestCases/Pretty/InitializerTests.roslyn.il

@ -32,7 +32,7 @@ @@ -32,7 +32,7 @@
.stackreserve 0x00100000
.subsystem 0x0003 // WINDOWS_CUI
.corflags 0x00000001 // ILONLY
// Image base: 0x00C90000
// Image base: 0x005E0000
// =============== CLASS MEMBERS DECLARATION ===================

24
ICSharpCode.Decompiler/IL/Transforms/AssignVariableNames.cs

@ -196,6 +196,14 @@ namespace ICSharpCode.Decompiler.IL.Transforms @@ -196,6 +196,14 @@ namespace ICSharpCode.Decompiler.IL.Transforms
proposedName = proposedNameForLoads[0];
}
}
if (string.IsNullOrEmpty(proposedName)) {
var proposedNameForStoresFromNewObj = variable.StoreInstructions.OfType<StLoc>()
.Select(expr => GetNameByType(GuessType(expr.Value, context)))
.Except(currentFieldNames).ToList();
if (proposedNameForStoresFromNewObj.Count == 1) {
proposedName = proposedNameForStoresFromNewObj[0];
}
}
if (string.IsNullOrEmpty(proposedName)) {
proposedName = GetNameByType(variable.Type);
}
@ -349,6 +357,22 @@ namespace ICSharpCode.Decompiler.IL.Transforms @@ -349,6 +357,22 @@ namespace ICSharpCode.Decompiler.IL.Transforms
return char.ToLower(name[0]) + name.Substring(1);
}
internal static IType GuessType(ILInstruction inst, ILTransformContext context)
{
switch (inst) {
case NewObj newObj:
return newObj.Method.DeclaringType;
case Call call:
return call.Method.ReturnType;
case CallVirt callVirt:
return callVirt.Method.ReturnType;
case CallIndirect calli:
return calli.ReturnType;
default:
return context.TypeSystem.Compilation.FindType(inst.ResultType.ToKnownTypeCode());
}
}
internal static string GenerateForeachVariableName(ILFunction function, ILInstruction valueContext, ILVariable existingVariable = null)
{
if (function == null)

13
ICSharpCode.Decompiler/IL/Transforms/TransformCollectionAndObjectInitializers.cs

@ -97,6 +97,8 @@ namespace ICSharpCode.Decompiler.IL.Transforms @@ -97,6 +97,8 @@ namespace ICSharpCode.Decompiler.IL.Transforms
&& IsPartOfInitializer(body.Instructions, pos + initializerItemsCount + 1, v, instType, ref blockType)) {
initializerItemsCount++;
}
if (IsMethodCallOnVariable(body.Instructions[pos + initializerItemsCount + 1], v))
return false;
var index = possibleIndexVariables.Where(info => info.Value.Index > -1).Min(info => (int?)info.Value.Index);
if (index != null) {
initializerItemsCount = index.Value - pos - 1;
@ -144,6 +146,17 @@ namespace ICSharpCode.Decompiler.IL.Transforms @@ -144,6 +146,17 @@ namespace ICSharpCode.Decompiler.IL.Transforms
return true;
}
bool IsMethodCallOnVariable(ILInstruction inst, ILVariable variable)
{
if (inst.MatchLdLocRef(variable))
return true;
if (inst is CallInstruction call && call.Arguments.Count > 0 && !call.Method.IsStatic)
return IsMethodCallOnVariable(call.Arguments[0], variable);
if (inst.MatchLdFld(out var target, out _) || inst.MatchStFld(out target, out _, out _) || inst.MatchLdFlda(out target, out _))
return IsMethodCallOnVariable(target, variable);
return false;
}
Dictionary<ILVariable, (int Index, ILInstruction Value)> possibleIndexVariables;
List<AccessPathElement> currentPath;
bool isCollection;

Loading…
Cancel
Save