From 1217c5b488981851e19aa620166ffe864729198d Mon Sep 17 00:00:00 2001 From: Siegfried Pammer Date: Fri, 1 Jul 2016 16:34:44 +0900 Subject: [PATCH] Add some more options to Tester.CompileCSharp and Tester.AssembleIL --- .../Tests/Helpers/Tester.cs | 29 ++++++++++++++++--- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/ICSharpCode.Decompiler/Tests/Helpers/Tester.cs b/ICSharpCode.Decompiler/Tests/Helpers/Tester.cs index bd6a3c280..e2548f9c4 100644 --- a/ICSharpCode.Decompiler/Tests/Helpers/Tester.cs +++ b/ICSharpCode.Decompiler/Tests/Helpers/Tester.cs @@ -39,18 +39,36 @@ namespace ICSharpCode.Decompiler.Tests.Helpers { None, Optimize, - UseDebug + UseDebug, + Force32Bit + } + + [Flags] + public enum AssemblerOptions + { + None, + UseDebug, + Force32Bit } public static class Tester { - public static string AssembleIL(string sourceFileName) + public static string AssembleIL(string sourceFileName, AssemblerOptions options = AssemblerOptions.UseDebug) { string ilasmPath = Path.Combine(Environment.GetEnvironmentVariable("windir"), @"Microsoft.NET\Framework\v4.0.30319\ilasm.exe"); string outputFile = Path.GetTempFileName(); + string otherOptions = " "; + + if (options.HasFlag(AssemblerOptions.UseDebug)) { + otherOptions += "/debug "; + } + if (options.HasFlag(AssemblerOptions.Force32Bit)) { + otherOptions += "/32BitPreferred "; + } + ProcessStartInfo info = new ProcessStartInfo(ilasmPath); - info.Arguments = $"/nologo /exe /output=\"{outputFile}\" \"{sourceFileName}\""; + info.Arguments = $"/nologo /exe{otherOptions}/output=\"{outputFile}\" \"{sourceFileName}\""; info.RedirectStandardError = true; info.RedirectStandardOutput = true; info.UseShellExecute = false; @@ -79,7 +97,7 @@ namespace ICSharpCode.Decompiler.Tests.Helpers CSharpCodeProvider provider = new CSharpCodeProvider(new Dictionary { { "CompilerVersion", "v4.0" } }); CompilerParameters options = new CompilerParameters(); options.GenerateExecutable = true; - options.CompilerOptions = "/unsafe /o" + (flags.HasFlag(CompilerOptions.Optimize) ? "+" : "-") + (flags.HasFlag(CompilerOptions.UseDebug) ? " /debug" : ""); + options.CompilerOptions = "/unsafe /o" + (flags.HasFlag(CompilerOptions.Optimize) ? "+" : "-") + (flags.HasFlag(CompilerOptions.UseDebug) ? " /debug" : "") + (flags.HasFlag(CompilerOptions.Force32Bit) ? " /platform:anycpu32bitpreferred" : ""); options.ReferencedAssemblies.Add("System.Core.dll"); CompilerResults results = provider.CompileAssemblyFromFile(options, sourceFileNames.ToArray()); if (results.Errors.Cast().Any(e => !e.IsWarning)) { @@ -138,6 +156,9 @@ namespace ICSharpCode.Decompiler.Tests.Helpers int result2 = Tester.Run(decompiledOutputFile, out output2, out error2); if (result1 != result2 || output1 != output2 || error1 != error2) { + Console.WriteLine("original: " + outputFile); + Console.WriteLine("decompiled: " + decompiledOutputFile); + Console.WriteLine("Test {0} failed.", testFileName); if (decompiledCodeFile != null) Console.WriteLine("Decompiled code in {0}:line 1", decompiledCodeFile);