Browse Source

Fix #1681: Use discard assignment, if possible.

pull/1707/head
Siegfried Pammer 6 years ago
parent
commit
d2f7258347
  1. 6
      ICSharpCode.Decompiler.Tests/ILPrettyTestRunner.cs
  2. 4
      ICSharpCode.Decompiler.Tests/PrettyTestRunner.cs
  3. 2
      ICSharpCode.Decompiler.Tests/TestCases/ILPretty/Issue1389.cs
  4. 15
      ICSharpCode.Decompiler.Tests/TestCases/ILPretty/Issue1681.cs
  5. 42
      ICSharpCode.Decompiler.Tests/TestCases/ILPretty/Issue1681.il
  6. 8
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/VariableNaming.cs
  7. 8
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/VariableNamingWithoutSymbols.cs
  8. 5
      ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs
  9. 27
      ICSharpCode.Decompiler/CSharp/Transforms/DeclareVariables.cs

6
ICSharpCode.Decompiler.Tests/ILPrettyTestRunner.cs

@ -154,6 +154,12 @@ namespace ICSharpCode.Decompiler.Tests @@ -154,6 +154,12 @@ namespace ICSharpCode.Decompiler.Tests
Run();
}
[Test]
public void Issue1681()
{
Run();
}
[Test]
public void Issue1454()
{

4
ICSharpCode.Decompiler.Tests/PrettyTestRunner.cs

@ -284,7 +284,9 @@ namespace ICSharpCode.Decompiler.Tests @@ -284,7 +284,9 @@ namespace ICSharpCode.Decompiler.Tests
[Test]
public void VariableNamingWithoutSymbols([ValueSource(nameof(defaultOptions))] CompilerOptions cscOptions)
{
RunForLibrary(cscOptions: cscOptions, decompilerSettings: new DecompilerSettings { UseDebugSymbols = false });
var settings = Tester.GetSettings(cscOptions);
settings.UseDebugSymbols = false;
RunForLibrary(cscOptions: cscOptions, decompilerSettings: settings);
}
[Test]

2
ICSharpCode.Decompiler.Tests/TestCases/ILPretty/Issue1389.cs

@ -12,7 +12,7 @@ namespace Issue1389 @@ -12,7 +12,7 @@ namespace Issue1389
private static void UnusedResultOfIsinst()
{
bool flag = GetObject() is TypeCode;
_ = (GetObject() is TypeCode);
}
private static bool BoolResultOfIsinst()

15
ICSharpCode.Decompiler.Tests/TestCases/ILPretty/Issue1681.cs

@ -0,0 +1,15 @@ @@ -0,0 +1,15 @@
namespace ICSharpCode.Decompiler.Tests.TestCases.ILPretty
{
internal class BaseClass
{
public int importsClausePosition;
}
internal class Issue1681 : BaseClass
{
public void Test()
{
_ = importsClausePosition;
}
}
}

42
ICSharpCode.Decompiler.Tests/TestCases/ILPretty/Issue1681.il

@ -0,0 +1,42 @@ @@ -0,0 +1,42 @@
// 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 ConsoleApp11
{
.ver 1:0:0:0
}
.module ConsoleApp11.exe
// MVID: {B973FCD6-A9C4-48A9-8291-26DDC248E208}
.imagebase 0x00400000
.file alignment 0x00000200
.stackreserve 0x00100000
.subsystem 0x0003 // WINDOWS_CUI
.corflags 0x00020003 // ILONLY 32BITPREFERRED
// Image base: 0x000001C4B6C90000
.class private auto ansi beforefieldinit ICSharpCode.Decompiler.Tests.TestCases.ILPretty.BaseClass
extends [mscorlib]System.Object
{
.field public int32 importsClausePosition
}
.class private auto ansi beforefieldinit ICSharpCode.Decompiler.Tests.TestCases.ILPretty.Issue1681
extends ICSharpCode.Decompiler.Tests.TestCases.ILPretty.BaseClass
{
.method public hidebysig instance void Test() cil managed
{
// Code size 18 (0x12)
.maxstack 8
ldarg.0
ldfld int32 class ICSharpCode.Decompiler.Tests.TestCases.ILPretty.BaseClass::importsClausePosition
pop
ret
} // end of method Issue1681::Test
}

8
ICSharpCode.Decompiler.Tests/TestCases/Pretty/VariableNaming.cs

@ -10,12 +10,20 @@ @@ -10,12 +10,20 @@
private void Test(string text, C c)
{
#if ROSLYN
_ = c.Name;
#else
string name = c.Name;
#endif
}
private void Test2(string text, C c)
{
#if ROSLYN
_ = c.Text;
#else
string text2 = c.Text;
#endif
}
}
}

8
ICSharpCode.Decompiler.Tests/TestCases/Pretty/VariableNamingWithoutSymbols.cs

@ -10,12 +10,20 @@ @@ -10,12 +10,20 @@
private void Test(string text, C c)
{
#if ROSLYN
_ = c.Name;
#else
string name = c.Name;
#endif
}
private void Test2(string text, C c)
{
#if ROSLYN
_ = c.Text;
#else
string text2 = c.Text;
#endif
}
}
}

5
ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs

@ -188,6 +188,11 @@ namespace ICSharpCode.Decompiler.CSharp @@ -188,6 +188,11 @@ namespace ICSharpCode.Decompiler.CSharp
}
internal bool HidesVariableWithName(string name)
{
return HidesVariableWithName(currentFunction, name);
}
internal static bool HidesVariableWithName(ILFunction currentFunction, string name)
{
return currentFunction.Ancestors.OfType<ILFunction>().Any(HidesVariableOrNestedFunction);

27
ICSharpCode.Decompiler/CSharp/Transforms/DeclareVariables.cs

@ -180,16 +180,23 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms @@ -180,16 +180,23 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms
if (!IsValidInStatementExpression(stmt.Expression)) {
// fetch ILFunction
var function = stmt.Ancestors.SelectMany(a => a.Annotations.OfType<ILFunction>()).First(f => f.Parent == null);
// assign result to dummy variable
var type = stmt.Expression.GetResolveResult().Type;
var v = function.RegisterVariable(
VariableKind.StackSlot,
type,
AssignVariableNames.GenerateVariableName(function, type, stmt.Expression.Annotations.OfType<ILInstruction>().Where(AssignVariableNames.IsSupportedInstruction).FirstOrDefault())
);
stmt.Expression = new AssignmentExpression(
new IdentifierExpression(v.Name).WithRR(new ILVariableResolveResult(v, v.Type)),
stmt.Expression.Detach());
// if possible use C# 7.0 discard-assignment
if (context.Settings.Discards && !ExpressionBuilder.HidesVariableWithName(function, "_")) {
stmt.Expression = new AssignmentExpression(
new IdentifierExpression("_"), // no ResolveResult
stmt.Expression.Detach());
} else {
// assign result to dummy variable
var type = stmt.Expression.GetResolveResult().Type;
var v = function.RegisterVariable(
VariableKind.StackSlot,
type,
AssignVariableNames.GenerateVariableName(function, type, stmt.Expression.Annotations.OfType<ILInstruction>().Where(AssignVariableNames.IsSupportedInstruction).FirstOrDefault())
);
stmt.Expression = new AssignmentExpression(
new IdentifierExpression(v.Name).WithRR(new ILVariableResolveResult(v, v.Type)),
stmt.Expression.Detach());
}
}
}
}

Loading…
Cancel
Save