Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve control flow decompilation (fixes #1133) #1176

Merged
merged 2 commits into from
Jun 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions ICSharpCode.Decompiler.Tests/PrettyTestRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ public void ShortCircuit([ValueSource("defaultOptions")] CSharpCompilerOptions c
public void ExceptionHandling([ValueSource("defaultOptions")] CSharpCompilerOptions cscOptions)
{
RunForLibrary(cscOptions: cscOptions, decompilerSettings: new DecompilerSettings {
NullPropagation = false
NullPropagation = false,
RemoveDeadCode = !cscOptions.HasFlag(CSharpCompilerOptions.UseRoslyn)
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RemoveDeadCode was introduced for F# (which emits much more dead code than C#).
It's not enabled by default, so it feels weird to have to use it in the C# tests.

As far as I can tell, it's only for while (true) loops compiled with legacy csc in debug mode?
I guess it's fine to special-case this for the test case... but maybe it would be better to have a transform that removes this specific kind of dead store independent of the RemoveDeadCode configuration option.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A transform would be better, I'll leave that one for you

}

Expand Down Expand Up @@ -161,7 +162,9 @@ public void Generics([ValueSource("defaultOptions")] CSharpCompilerOptions cscOp
[Test]
public void Loops([ValueSource("defaultOptionsWithMcs")] CSharpCompilerOptions cscOptions)
{
RunForLibrary(cscOptions: cscOptions);
RunForLibrary(cscOptions: cscOptions, decompilerSettings: new DecompilerSettings {
RemoveDeadCode = !cscOptions.HasFlag(CSharpCompilerOptions.Optimize)
});
}

[Test]
Expand Down
86 changes: 86 additions & 0 deletions ICSharpCode.Decompiler.Tests/TestCases/Pretty/ExceptionHandling.cs
Original file line number Diff line number Diff line change
Expand Up @@ -195,5 +195,91 @@ public void ThrowInFinally()
throw new Exception();
}
}

#if ROSLYN || !OPT
// TODO Non-Roslyn compilers create a second while loop inside the try, by inverting the if
// This is fixed in the non-optimised version by the enabling the RemoveDeadCode flag
//public bool EarlyExitInLoopTry()
//{
// while (true) {
// try {
// while (B(0)) {
// Console.WriteLine();
// }
//
// return false;
// } catch {
// }
// }
//}
public bool EarlyExitInLoopTry()
{
while (true) {
try {
if (!B(0)) {
return false;
}

Console.WriteLine();
} catch {
}
}
}
#endif

public bool ComplexConditionalReturnInThrow()
{
try {
if (B(0)) {
if (B(1)) {
Console.WriteLine("0 && 1");
return B(2);
}

if (B(3)) {
Console.WriteLine("0 && 3");
return !B(2);
}

Console.WriteLine("0");
}

Console.WriteLine("End Try");

} catch {
try {
try {
if (((B(0) || B(1)) && B(2)) || B(3)) {
return B(4) && !B(5);
}
if (B(6) || B(7)) {
return B(8) || B(9);
}
} catch {
Console.WriteLine("Catch2");
}
return B(10) && B(11);
} catch {
Console.WriteLine("Catch");
} finally {
Console.WriteLine("Finally");
}
}
return false;
}

public void AppropriateLockExit()
{
int num = 0;
lock (this) {
if (num <= 256) {
Console.WriteLine(0);
} else if (num <= 1024) {
Console.WriteLine(1);
} else if (num <= 16384) {
Console.WriteLine(2);
}
}
}
}
}
Loading