diff --git a/src/Neo.Compiler.CSharp/CompilationContext.cs b/src/Neo.Compiler.CSharp/CompilationContext.cs index c3b58ed13..5433b8866 100644 --- a/src/Neo.Compiler.CSharp/CompilationContext.cs +++ b/src/Neo.Compiler.CSharp/CompilationContext.cs @@ -17,6 +17,7 @@ using Neo.Cryptography.ECC; using Neo.IO; using Neo.Json; +using Neo.Optimizer; using Neo.SmartContract; using Neo.SmartContract.Manifest; using System; @@ -136,6 +137,27 @@ internal void Compile() } } + public (NefFile nef, ContractManifest manifest, JObject debugInfo) CreateResults(string folder) + { + NefFile nef = CreateExecutable(); + ContractManifest manifest = CreateManifest(); + JObject debugInfo = CreateDebugInformation(folder); + + if (!Options.NoOptimize) + { + try + { + (nef, manifest, debugInfo) = Reachability.RemoveUncoveredInstructions(nef, manifest, (JObject)debugInfo.Clone()); + } + catch (Exception ex) + { + Console.Error.WriteLine($"Failed to optimize: {ex}"); + } + } + + return (nef, manifest, debugInfo); + } + public NefFile CreateExecutable() { Assembly assembly = Assembly.GetExecutingAssembly(); diff --git a/src/Neo.Compiler.CSharp/Optimizer/Strategies/Reachability.cs b/src/Neo.Compiler.CSharp/Optimizer/Strategies/Reachability.cs index 17e0043ff..81164b32d 100644 --- a/src/Neo.Compiler.CSharp/Optimizer/Strategies/Reachability.cs +++ b/src/Neo.Compiler.CSharp/Optimizer/Strategies/Reachability.cs @@ -39,7 +39,7 @@ public enum BranchType } [Strategy(Priority = int.MaxValue)] - public static (NefFile, ContractManifest, JToken) RemoveUncoveredInstructions(NefFile nef, ContractManifest manifest, JToken debugInfo) + public static (NefFile, ContractManifest, JObject) RemoveUncoveredInstructions(NefFile nef, ContractManifest manifest, JObject debugInfo) { Dictionary coveredMap = FindCoveredInstructions(nef, manifest, debugInfo); Script oldScript = nef.Script; diff --git a/src/Neo.Compiler.CSharp/Program.cs b/src/Neo.Compiler.CSharp/Program.cs index 645aaa871..2fedeb759 100644 --- a/src/Neo.Compiler.CSharp/Program.cs +++ b/src/Neo.Compiler.CSharp/Program.cs @@ -133,7 +133,7 @@ private static int ProcessCsproj(Options options, string path) private static int ProcessSources(Options options, string folder, string[] sourceFiles) { - return ProcessOutputs(options, folder, new CompilationEngine(options).CompileSources(null, sourceFiles)); + return ProcessOutputs(options, folder, new CompilationEngine(options).CompileSources(sourceFiles)); } private static int ProcessOutputs(Options options, string folder, List contexts) @@ -171,20 +171,7 @@ private static int ProcessOutput(Options options, string folder, CompilationCont string path = outputFolder; string baseName = options.BaseName ?? context.ContractName!; - NefFile nef = context.CreateExecutable(); - ContractManifest manifest = context.CreateManifest(); - JToken debugInfo = context.CreateDebugInformation(folder); - if (!options.NoOptimize) - { - try - { - (nef, manifest, debugInfo) = Reachability.RemoveUncoveredInstructions(nef, manifest, debugInfo.Clone()); - } - catch (Exception ex) - { - Console.Error.WriteLine($"Failed to optimize: {ex}"); - } - } + (NefFile nef, ContractManifest manifest, JToken debugInfo) = context.CreateResults(folder); try { diff --git a/tests/Neo.SmartContract.Template.UnitTests/templates/TestCleanup.cs b/tests/Neo.SmartContract.Template.UnitTests/templates/TestCleanup.cs index 5223a1e94..e4d5c6fb8 100644 --- a/tests/Neo.SmartContract.Template.UnitTests/templates/TestCleanup.cs +++ b/tests/Neo.SmartContract.Template.UnitTests/templates/TestCleanup.cs @@ -33,6 +33,7 @@ public void EnsureArtifactsUpToDate() var result = new CompilationEngine(new CompilationOptions() { Debug = true, + NoOptimize = false, Nullable = Microsoft.CodeAnalysis.NullableContextOptions.Disable }) .CompileSources( @@ -66,9 +67,8 @@ public void EnsureArtifactsUpToDate() private static (string, NeoDebugInfo) CreateArtifact(CompilationContext context, string rootDebug) { - var manifest = context.CreateManifest(); - var nef = context.CreateExecutable(); - var debug = NeoDebugInfo.FromDebugInfoJson(context.CreateDebugInformation(rootDebug)); + (var nef, var manifest, var debugInfo) = context.CreateResults(rootDebug); + var debug = NeoDebugInfo.FromDebugInfoJson(debugInfo); return (manifest.GetArtifactsSource(typeof(T).Name, nef, generateProperties: true), debug); }