From ec3d6f87a8693d94aff34340cf720be9a840218d Mon Sep 17 00:00:00 2001 From: Sven Boemer Date: Thu, 6 Jun 2024 00:09:37 +0000 Subject: [PATCH] PR feedback - Use static lambdas - Check HasCustomDebugInformation - Test writing modified CustomDebugInformation - Formatting fixes --- Directory.Build.props | 1 + Mono.Cecil.Cil/Symbols.cs | 12 +++--- Mono.Cecil/AssemblyWriter.cs | 2 +- Test/Mono.Cecil.Tests/BaseTestFixture.cs | 49 ++++++++++++++++++++++ Test/Mono.Cecil.Tests/ILProcessorTests.cs | 50 ----------------------- Test/Mono.Cecil.Tests/PortablePdbTests.cs | 16 ++++++++ 6 files changed, 74 insertions(+), 56 deletions(-) diff --git a/Directory.Build.props b/Directory.Build.props index 5b9f0da9d..a37508076 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -9,6 +9,7 @@ $(MSBuildThisFileDirectory)\cecil.snk $(DefineConstants);NET_CORE + latest true diff --git a/Mono.Cecil.Cil/Symbols.cs b/Mono.Cecil.Cil/Symbols.cs index c6b7d4332..294eb16db 100644 --- a/Mono.Cecil.Cil/Symbols.cs +++ b/Mono.Cecil.Cil/Symbols.cs @@ -1232,13 +1232,14 @@ public static bool GetHasCustomDebugInformations ( ref Collection collection, ModuleDefinition module) { - if (module.HasImage ()) - module.Read (ref collection, self, (provider, reader) => { + if (module.HasImage ()) { + module.Read (ref collection, self, static (provider, reader) => { var symbol_reader = reader.module.symbol_reader; if (symbol_reader != null) return symbol_reader.Read (provider); return null; }); + } return !collection.IsNullOrEmpty (); } @@ -1248,13 +1249,14 @@ public static Collection GetCustomDebugInformations ( ref Collection collection, ModuleDefinition module) { - if (module.HasImage ()) - module.Read (ref collection, self, (provider, reader) => { + if (module.HasImage ()) { + module.Read (ref collection, self, static (provider, reader) => { var symbol_reader = reader.module.symbol_reader; if (symbol_reader != null) - return symbol_reader.Read (self); + return symbol_reader.Read (provider); return null; }); + } Interlocked.CompareExchange (ref collection, new Collection (), null); return collection; diff --git a/Mono.Cecil/AssemblyWriter.cs b/Mono.Cecil/AssemblyWriter.cs index 3cc0569e1..ee234c81f 100644 --- a/Mono.Cecil/AssemblyWriter.cs +++ b/Mono.Cecil/AssemblyWriter.cs @@ -1487,7 +1487,7 @@ void AddType (TypeDefinition type) if (type.HasNestedTypes) AddNestedTypes (type); - if (symbol_writer != null && type.custom_infos != null) + if (symbol_writer != null && type.HasCustomDebugInformations) symbol_writer.Write (type); WindowsRuntimeProjections.ApplyProjection (type, treatment); diff --git a/Test/Mono.Cecil.Tests/BaseTestFixture.cs b/Test/Mono.Cecil.Tests/BaseTestFixture.cs index 046791bfa..12de4227a 100644 --- a/Test/Mono.Cecil.Tests/BaseTestFixture.cs +++ b/Test/Mono.Cecil.Tests/BaseTestFixture.cs @@ -2,6 +2,7 @@ using System.IO; using System.Runtime.CompilerServices; using Mono.Cecil.Cil; +using Mono.Cecil.Pdb; using NUnit.Framework; using Mono.Cecil.PE; @@ -154,6 +155,54 @@ static void Run (TestCase testCase) using (var runner = new TestRunner (testCase, TestCaseType.WriteFromImmediate)) runner.RunTest (); } + + public enum RoundtripType { + None, + Pdb, + PortablePdb + } + + protected static ModuleDefinition RoundtripModule(ModuleDefinition module, RoundtripType roundtripType) + { + if (roundtripType == RoundtripType.None) + return module; + + var file = Path.Combine (Path.GetTempPath (), "RoundtripModule.dll"); + if (File.Exists (file)) + File.Delete (file); + + ISymbolWriterProvider symbolWriterProvider; + switch (roundtripType) { + case RoundtripType.Pdb when Platform.HasNativePdbSupport: + symbolWriterProvider = new PdbWriterProvider (); + break; + case RoundtripType.PortablePdb: + default: + symbolWriterProvider = new PortablePdbWriterProvider (); + break; + } + + module.Write (file, new WriterParameters { + SymbolWriterProvider = symbolWriterProvider, + }); + module.Dispose (); + + ISymbolReaderProvider symbolReaderProvider; + switch (roundtripType) { + case RoundtripType.Pdb when Platform.HasNativePdbSupport: + symbolReaderProvider = new PdbReaderProvider (); + break; + case RoundtripType.PortablePdb: + default: + symbolReaderProvider = new PortablePdbReaderProvider (); + break; + } + + return ModuleDefinition.ReadModule (file, new ReaderParameters { + SymbolReaderProvider = symbolReaderProvider, + InMemory = true + }); + } } abstract class TestCase { diff --git a/Test/Mono.Cecil.Tests/ILProcessorTests.cs b/Test/Mono.Cecil.Tests/ILProcessorTests.cs index fd462c365..93ca078c8 100644 --- a/Test/Mono.Cecil.Tests/ILProcessorTests.cs +++ b/Test/Mono.Cecil.Tests/ILProcessorTests.cs @@ -3,10 +3,8 @@ using System.IO; using System.Linq; -using Mono.Cecil; using Mono.Cecil.Cil; using Mono.Cecil.Mdb; -using Mono.Cecil.Pdb; using NUnit.Framework; namespace Mono.Cecil.Tests { @@ -499,12 +497,6 @@ static MethodBody CreateTestMethodWithLocalScopes (RoundtripType roundtripType, return methodBody; } - public enum RoundtripType { - None, - Pdb, - PortablePdb - } - static MethodBody RoundtripMethodBody(MethodBody methodBody, RoundtripType roundtripType, bool forceUnresolvedScopes = false, bool reverseScopeOrder = false) { var newModule = RoundtripModule (methodBody.Method.Module, roundtripType); @@ -540,47 +532,5 @@ static void ReverseScopeOrder(ScopeDebugInformation scope) foreach (var subScope in scope.Scopes) ReverseScopeOrder (subScope); } - - static ModuleDefinition RoundtripModule(ModuleDefinition module, RoundtripType roundtripType) - { - if (roundtripType == RoundtripType.None) - return module; - - var file = Path.Combine (Path.GetTempPath (), "TestILProcessor.dll"); - if (File.Exists (file)) - File.Delete (file); - - ISymbolWriterProvider symbolWriterProvider; - switch (roundtripType) { - case RoundtripType.Pdb when Platform.HasNativePdbSupport: - symbolWriterProvider = new PdbWriterProvider (); - break; - case RoundtripType.PortablePdb: - default: - symbolWriterProvider = new PortablePdbWriterProvider (); - break; - } - - module.Write (file, new WriterParameters { - SymbolWriterProvider = symbolWriterProvider, - }); - module.Dispose (); - - ISymbolReaderProvider symbolReaderProvider; - switch (roundtripType) { - case RoundtripType.Pdb when Platform.HasNativePdbSupport: - symbolReaderProvider = new PdbReaderProvider (); - break; - case RoundtripType.PortablePdb: - default: - symbolReaderProvider = new PortablePdbReaderProvider (); - break; - } - - return ModuleDefinition.ReadModule (file, new ReaderParameters { - SymbolReaderProvider = symbolReaderProvider, - InMemory = true - }); - } } } diff --git a/Test/Mono.Cecil.Tests/PortablePdbTests.cs b/Test/Mono.Cecil.Tests/PortablePdbTests.cs index bae1436bc..126d23ef4 100644 --- a/Test/Mono.Cecil.Tests/PortablePdbTests.cs +++ b/Test/Mono.Cecil.Tests/PortablePdbTests.cs @@ -666,6 +666,22 @@ public void TypeDefinitionDebugInformation () }, symbolReaderProvider: typeof (PortablePdbReaderProvider), symbolWriterProvider: typeof (PortablePdbWriterProvider)); } + [Test] + public void ModifyTypeDefinitionDebugInformation () + { + using (var module = GetResourceModule ("TypeDefinitionDebugInformation.dll", new ReaderParameters { SymbolReaderProvider = new PortablePdbReaderProvider () })) { + var enum_type = module.GetType ("TypeDefinitionDebugInformation.Enum"); + var binary_custom_debug_info = enum_type.CustomDebugInformations.OfType ().FirstOrDefault (); + binary_custom_debug_info.Data = new byte [] { 0x2 }; + + var outputModule = RoundtripModule (module, RoundtripType.None); + enum_type = outputModule.GetType ("TypeDefinitionDebugInformation.Enum"); + binary_custom_debug_info = enum_type.CustomDebugInformations.OfType ().FirstOrDefault (); + Assert.IsNotNull (binary_custom_debug_info); + Assert.AreEqual (new byte [] { 0x2 }, binary_custom_debug_info.Data); + } + } + public sealed class SymbolWriterProvider : ISymbolWriterProvider { readonly DefaultSymbolWriterProvider writer_provider = new DefaultSymbolWriterProvider ();