Skip to content

Commit

Permalink
PR feedback
Browse files Browse the repository at this point in the history
- Use static lambdas
- Check HasCustomDebugInformation
- Test writing modified CustomDebugInformation
- Formatting fixes
  • Loading branch information
sbomer committed Jun 6, 2024
1 parent babace9 commit ec3d6f8
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 56 deletions.
1 change: 1 addition & 0 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<AssemblyOriginatorKeyFile>$(MSBuildThisFileDirectory)\cecil.snk</AssemblyOriginatorKeyFile>
<DefineConstants Condition=" '$(TargetFramework)' == 'netstandard2.0' OR '$(TargetFramework)' == 'net8.0' ">$(DefineConstants);NET_CORE</DefineConstants>
<RootNamespace></RootNamespace>
<LangVersion>latest</LangVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(TargetFramework)' == 'net40' ">
<DisableImplicitFrameworkReferences>true</DisableImplicitFrameworkReferences>
Expand Down
12 changes: 7 additions & 5 deletions Mono.Cecil.Cil/Symbols.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1232,13 +1232,14 @@ public static bool GetHasCustomDebugInformations (
ref Collection<CustomDebugInformation> 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 ();
}
Expand All @@ -1248,13 +1249,14 @@ public static Collection<CustomDebugInformation> GetCustomDebugInformations (
ref Collection<CustomDebugInformation> 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<CustomDebugInformation> (), null);
return collection;
Expand Down
2 changes: 1 addition & 1 deletion Mono.Cecil/AssemblyWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
49 changes: 49 additions & 0 deletions Test/Mono.Cecil.Tests/BaseTestFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down
50 changes: 0 additions & 50 deletions Test/Mono.Cecil.Tests/ILProcessorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
});
}
}
}
16 changes: 16 additions & 0 deletions Test/Mono.Cecil.Tests/PortablePdbTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<BinaryCustomDebugInformation> ().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<BinaryCustomDebugInformation> ().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 ();
Expand Down

0 comments on commit ec3d6f8

Please sign in to comment.