Skip to content
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
56 changes: 29 additions & 27 deletions ICSharpCode.Decompiler.Tests/PrettyTestRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -233,22 +233,21 @@ public async Task CustomShortCircuitOperators([ValueSource(nameof(defaultOptions
[Test]
public async Task ExceptionHandling([ValueSource(nameof(defaultOptions))] CompilerOptions cscOptions)
{
await RunForLibrary(cscOptions: cscOptions, decompilerSettings: new DecompilerSettings {
NullPropagation = false,
await RunForLibrary(cscOptions: cscOptions, configureDecompiler: settings => {
settings.NullPropagation = false;
// legacy csc generates a dead store in debug builds
RemoveDeadStores = (cscOptions == CompilerOptions.None),
FileScopedNamespaces = false,
settings.RemoveDeadStores = (cscOptions == CompilerOptions.None);
settings.FileScopedNamespaces = false;
});
}

[Test]
public async Task Switch([ValueSource(nameof(defaultOptions))] CompilerOptions cscOptions)
{
await RunForLibrary(cscOptions: cscOptions, decompilerSettings: new DecompilerSettings {
await RunForLibrary(cscOptions: cscOptions, configureDecompiler: settings => {
// legacy csc generates a dead store in debug builds
RemoveDeadStores = (cscOptions == CompilerOptions.None),
SwitchExpressions = false,
FileScopedNamespaces = false,
settings.RemoveDeadStores = (cscOptions == CompilerOptions.None);
settings.SwitchExpressions = false;
});
}

Expand All @@ -267,7 +266,10 @@ public async Task ReduceNesting([ValueSource(nameof(defaultOptions))] CompilerOp
[Test]
public async Task DelegateConstruction([ValueSource(nameof(defaultOptionsWithMcs))] CompilerOptions cscOptions)
{
await RunForLibrary(cscOptions: cscOptions);
await RunForLibrary(cscOptions: cscOptions, configureDecompiler: settings => {
settings.QueryExpressions = false;
settings.NullableReferenceTypes = false;
});
}

[Test]
Expand All @@ -293,9 +295,9 @@ public async Task Using([ValueSource(nameof(defaultOptions))] CompilerOptions cs
{
await RunForLibrary(
cscOptions: cscOptions,
decompilerSettings: new DecompilerSettings {
UseEnhancedUsing = false,
FileScopedNamespaces = false,
configureDecompiler: settings => {
settings.UseEnhancedUsing = false;
settings.FileScopedNamespaces = false;
}
);
}
Expand Down Expand Up @@ -327,11 +329,11 @@ public async Task Generics([ValueSource(nameof(defaultOptions))] CompilerOptions
[Test]
public async Task Loops([ValueSource(nameof(defaultOptionsWithMcs))] CompilerOptions cscOptions)
{
DecompilerSettings settings = Tester.GetSettings(cscOptions);
// legacy csc generates a dead store in debug builds
settings.RemoveDeadStores = (cscOptions == CompilerOptions.None);
settings.UseExpressionBodyForCalculatedGetterOnlyProperties = false;
await RunForLibrary(cscOptions: cscOptions, decompilerSettings: settings);
await RunForLibrary(cscOptions: cscOptions, configureDecompiler: settings => {
// legacy csc generates a dead store in debug builds
settings.RemoveDeadStores = (cscOptions == CompilerOptions.None);
settings.UseExpressionBodyForCalculatedGetterOnlyProperties = false;
});
}

[Test]
Expand Down Expand Up @@ -440,9 +442,7 @@ public async Task VariableNaming([ValueSource(nameof(defaultOptions))] CompilerO
[Test]
public async Task VariableNamingWithoutSymbols([ValueSource(nameof(defaultOptions))] CompilerOptions cscOptions)
{
var settings = Tester.GetSettings(cscOptions);
settings.UseDebugSymbols = false;
await RunForLibrary(cscOptions: cscOptions, decompilerSettings: settings);
await RunForLibrary(cscOptions: cscOptions, configureDecompiler: settings => settings.UseDebugSymbols = false);
}

[Test]
Expand Down Expand Up @@ -474,7 +474,7 @@ public async Task AsyncUsing([ValueSource(nameof(roslyn3OrNewerOptions))] Compil
{
await RunForLibrary(
cscOptions: cscOptions,
decompilerSettings: new DecompilerSettings { UseEnhancedUsing = false, FileScopedNamespaces = false }
configureDecompiler: settings => { settings.UseEnhancedUsing = false; }
);
}

Expand All @@ -499,7 +499,7 @@ public async Task NativeInts([ValueSource(nameof(roslyn3OrNewerWithNet40Options)
[Test]
public async Task FileScopedNamespaces([ValueSource(nameof(roslyn4OrNewerOptions))] CompilerOptions cscOptions)
{
await RunForLibrary(cscOptions: cscOptions, decompilerSettings: new DecompilerSettings());
await RunForLibrary(cscOptions: cscOptions, configureDecompiler: settings => settings.FileScopedNamespaces = true);
}

[Test]
Expand Down Expand Up @@ -601,7 +601,7 @@ public async Task ConstantsTests([ValueSource(nameof(defaultOptions))] CompilerO
[Test]
public async Task Issue1080([ValueSource(nameof(roslynOnlyOptions))] CompilerOptions cscOptions)
{
await RunForLibrary(cscOptions: cscOptions, decompilerSettings: new DecompilerSettings(CSharp.LanguageVersion.CSharp6));
await RunForLibrary(cscOptions: cscOptions, configureDecompiler: settings => settings.SetLanguageVersion(CSharp.LanguageVersion.CSharp6));
}

[Test]
Expand Down Expand Up @@ -712,12 +712,12 @@ public async Task MetadataAttributes([ValueSource(nameof(defaultOptions))] Compi
await RunForLibrary(cscOptions: cscOptions);
}

async Task RunForLibrary([CallerMemberName] string testName = null, AssemblerOptions asmOptions = AssemblerOptions.None, CompilerOptions cscOptions = CompilerOptions.None, DecompilerSettings decompilerSettings = null)
async Task RunForLibrary([CallerMemberName] string testName = null, AssemblerOptions asmOptions = AssemblerOptions.None, CompilerOptions cscOptions = CompilerOptions.None, Action<DecompilerSettings> configureDecompiler = null)
{
await Run(testName, asmOptions | AssemblerOptions.Library, cscOptions | CompilerOptions.Library, decompilerSettings);
await Run(testName, asmOptions | AssemblerOptions.Library, cscOptions | CompilerOptions.Library, configureDecompiler);
}

async Task Run([CallerMemberName] string testName = null, AssemblerOptions asmOptions = AssemblerOptions.None, CompilerOptions cscOptions = CompilerOptions.None, DecompilerSettings decompilerSettings = null)
async Task Run([CallerMemberName] string testName = null, AssemblerOptions asmOptions = AssemblerOptions.None, CompilerOptions cscOptions = CompilerOptions.None, Action<DecompilerSettings> configureDecompiler = null)
{
var csFile = Path.Combine(TestCasePath, testName + ".cs");
var exeFile = TestsAssemblyOutput.GetFilePath(TestCasePath, testName, Tester.GetSuffix(cscOptions) + ".exe");
Expand All @@ -739,7 +739,9 @@ async Task Run([CallerMemberName] string testName = null, AssemblerOptions asmOp
}

// 2. Decompile
var decompiled = await Tester.DecompileCSharp(exeFile, decompilerSettings ?? Tester.GetSettings(cscOptions)).ConfigureAwait(false);
var settings = Tester.GetSettings(cscOptions);
configureDecompiler?.Invoke(settings);
var decompiled = await Tester.DecompileCSharp(exeFile, settings).ConfigureAwait(false);

// 3. Compile
CodeAssert.FilesAreEqual(csFile, decompiled, Tester.GetPreprocessorSymbols(cscOptions).Append("EXPECTED_OUTPUT").ToArray());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ public void MethodUnknownClass()
//IL_0007: Expected O, but got Unknown
UnknownClass val = new UnknownClass();
int? unknownProperty = val.UnknownProperty;
int? num2 = (val.UnknownProperty = unknownProperty.GetValueOrDefault());
int? num3 = num2;
int? num = (val.UnknownProperty = unknownProperty.GetValueOrDefault());
int? num3 = num;
List<object> list = new List<object> {
val[unknownProperty.Value] ?? "",
val.NotProperty,
Expand Down Expand Up @@ -50,9 +50,9 @@ public void MethodUnknownGenericClass()
//IL_00e1: Expected O, but got Unknown
//IL_00e1: Expected O, but got Unknown
UnknownGenericClass<UnknownEventArgs> val = new UnknownGenericClass<UnknownEventArgs>();
UnknownEventArgs val2 = (val.UnknownProperty = val.UnknownProperty);
UnknownEventArgs e = (val.UnknownProperty = val.UnknownProperty);
List<object> list = new List<object> {
val[((object)val2).GetHashCode()] ?? "",
val[((object)e).GetHashCode()] ?? "",
val.NotProperty,
val.get_NotPropertyWithGeneric<string>(42),
val[42],
Expand All @@ -61,10 +61,10 @@ public void MethodUnknownGenericClass()
};
val.OnEvent += Instance_OnEvent;
val.OnEvent -= Instance_OnEvent;
UnknownEventArgs val3 = val[(UnknownEventArgs)null];
val[new UnknownEventArgs()] = val3;
UnknownEventArgs val4 = val[new UnknownEventArgs(), new UnknownEventArgs()];
val[new UnknownEventArgs(), new UnknownEventArgs()] = val4;
UnknownEventArgs e2 = val[(UnknownEventArgs)null];
val[new UnknownEventArgs()] = e2;
UnknownEventArgs e3 = val[new UnknownEventArgs(), new UnknownEventArgs()];
val[new UnknownEventArgs(), new UnknownEventArgs()] = e3;
}

public void MethodUnknownStatic()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4943,8 +4943,8 @@ public void Issue1552StmtTwice(CustomStruct a, CustomStruct b)

public void Issue1779(int value)
{
CustomStruct2 @struct = GetStruct();
@struct.IntProp += value;
CustomStruct2 customStruct = GetStruct();
customStruct.IntProp += value;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,33 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading;
#if CS100
using System.Threading.Tasks;

#endif

namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty.DelegateConstruction
{
public static class DelegateConstruction
{
internal class Dummy
{
public int baz;

public List<Dummy> more;
}

[CompilerGenerated]
internal class Helper
{
internal bool HelpMe(Dummy dum)
{
return true;
}
}

private class InstanceTests
{
public struct SomeData
Expand Down Expand Up @@ -377,10 +395,10 @@ public static void NameConflict()
public static void NameConflict2(int j)
{
List<Action<int>> list = new List<Action<int>>();
for (int k = 0; k < 10; k++)
for (int i = 0; i < 10; i++)
{
list.Add(delegate (int i) {
Console.WriteLine(i);
list.Add(delegate (int k) {
Console.WriteLine(k);
});
}
}
Expand Down Expand Up @@ -643,6 +661,21 @@ private void Run(ParameterizedThreadStart del, object x)
{
del(x);
}

public void Issue1572(DelegateConstruction.Dummy dum)
{
#if EXPECTED_OUTPUT
DelegateConstruction.Helper CS_0024_003C_003E8__locals0 = new DelegateConstruction.Helper();
DelegateConstruction.Dummy dummy = dum.more.Where((DelegateConstruction.Dummy dummy2) => true).Where((DelegateConstruction.Dummy dummy2) => true).FirstOrDefault();
Console.WriteLine();
dummy.baz++;
#else
DelegateConstruction.Helper h = new DelegateConstruction.Helper();
DelegateConstruction.Dummy localDummy = dum.more.Where(h.HelpMe).Where(h.HelpMe).FirstOrDefault();
Console.WriteLine();
localDummy.baz++;
#endif
}
}

[AttributeUsage(AttributeTargets.All)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -418,9 +418,9 @@ public void GenericException<TException>(int input) where TException : Exception
{
Console.WriteLine(input);
}
catch (TException val)
catch (TException ex)
{
Console.WriteLine(val.Message);
Console.WriteLine(ex.Message);
throw;
}
}
Expand Down Expand Up @@ -452,9 +452,9 @@ public void GenericExceptionWithCondition<TException>(int input) where TExceptio
{
Console.WriteLine(input);
}
catch (TException val) when (val.Message.Contains("Test"))
catch (TException ex) when (ex.Message.Contains("Test"))
{
Console.WriteLine(val.Message);
Console.WriteLine(ex.Message);
throw;
}
}
Expand All @@ -465,9 +465,9 @@ public void GenericException2WithCondition<TException>(int input) where TExcepti
{
Console.WriteLine(input);
}
catch (TException val) when (val.Message.Contains("Test"))
catch (TException ex) when (ex.Message.Contains("Test"))
{
Console.WriteLine("{0} {1}", val, val.ToString());
Console.WriteLine("{0} {1}", ex, ex.ToString());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,11 +182,11 @@ public void CallOnRefReturn()
GetRef<ReadOnlyStruct>().Method();

// call on a copy, not the original ref:
NormalStruct @ref = GetRef<NormalStruct>();
@ref.Method();
NormalStruct normalStruct = GetRef<NormalStruct>();
normalStruct.Method();

ReadOnlyStruct ref2 = GetRef<ReadOnlyStruct>();
ref2.Method();
ReadOnlyStruct readOnlyStruct = GetRef<ReadOnlyStruct>();
readOnlyStruct.Method();
}

public void CallOnReadOnlyRefReturn()
Expand Down Expand Up @@ -293,13 +293,13 @@ public void CallSiteTests(NormalStruct s, ReadOnlyStruct r, ReadOnlyRefStruct rr

public void RefReassignment(ref NormalStruct s)
{
ref NormalStruct @ref = ref GetRef<NormalStruct>();
RefReassignment(ref @ref);
ref NormalStruct reference = ref GetRef<NormalStruct>();
RefReassignment(ref reference);
if (s.GetHashCode() == 0)
{
@ref = ref GetRef<NormalStruct>();
reference = ref GetRef<NormalStruct>();
}
RefReassignment(ref @ref.GetHashCode() == 4 ? ref @ref : ref s);
RefReassignment(ref reference.GetHashCode() == 4 ? ref reference : ref s);
}

public static void Main(string[] args)
Expand Down
12 changes: 6 additions & 6 deletions ICSharpCode.Decompiler.Tests/TestCases/VBPretty/Async.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ public async void AwaitYield()
public async void AwaitDefaultYieldAwaitable()
{
#if LEGACY_VBC || (OPTIMIZE && !ROSLYN4)
YieldAwaitable yieldAwaitable = default(YieldAwaitable);
YieldAwaitable yieldAwaitable2 = yieldAwaitable;
await yieldAwaitable2;
YieldAwaitable yieldAwaitable2 = default(YieldAwaitable);
YieldAwaitable yieldAwaitable = yieldAwaitable2;
await yieldAwaitable;
#else
await default(YieldAwaitable);
#endif
Expand All @@ -51,9 +51,9 @@ public async void AwaitDefaultYieldAwaitable()
public async void AwaitDefaultHopToThreadPool()
{
#if LEGACY_VBC || (OPTIMIZE && !ROSLYN4)
HopToThreadPoolAwaitable hopToThreadPoolAwaitable = default(HopToThreadPoolAwaitable);
HopToThreadPoolAwaitable hopToThreadPoolAwaitable2 = hopToThreadPoolAwaitable;
await hopToThreadPoolAwaitable2;
HopToThreadPoolAwaitable hopToThreadPoolAwaitable2 = default(HopToThreadPoolAwaitable);
HopToThreadPoolAwaitable hopToThreadPoolAwaitable = hopToThreadPoolAwaitable2;
await hopToThreadPoolAwaitable;
#else
await default(HopToThreadPoolAwaitable);
#endif
Expand Down
2 changes: 1 addition & 1 deletion ICSharpCode.Decompiler/CSharp/CSharpDecompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1282,7 +1282,7 @@ void FixParameterNames(EntityDeclaration entity)
{
if (string.IsNullOrWhiteSpace(parameter.Name) && !parameter.Type.IsArgList())
{
// needs to be consistent with logic in ILReader.CreateILVarable
// needs to be consistent with logic in ILReader.CreateILVariable
parameter.Name = "P_" + i;
}
i++;
Expand Down
9 changes: 6 additions & 3 deletions ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2547,15 +2547,18 @@ IEnumerable<ParameterDeclaration> MakeParameters(IReadOnlyList<IParameter> param
foreach (var parameter in parameters)
{
var pd = astBuilder.ConvertParameter(parameter);
if (variables.TryGetValue(i, out var v))
{
pd.AddAnnotation(new ILVariableResolveResult(v, parameters[i].Type));
pd.Name = v.Name;
}
if (string.IsNullOrEmpty(pd.Name) && !pd.Type.IsArgList())
{
// needs to be consistent with logic in ILReader.CreateILVarable(ParameterDefinition)
// needs to be consistent with logic in ILReader.CreateILVariable
pd.Name = "P_" + i;
}
if (settings.AnonymousTypes && parameter.Type.ContainsAnonymousType())
pd.Type = null;
if (variables.TryGetValue(i, out var v))
pd.AddAnnotation(new ILVariableResolveResult(v, parameters[i].Type));
yield return pd;
i++;
}
Expand Down
Loading
Loading