dotnet / roslyn

The Roslyn .NET compiler provides C# and Visual Basic languages with rich code analysis APIs.
https://docs.microsoft.com/dotnet/csharp/roslyn-sdk/
MIT License
18.99k stars 4.03k forks source link

Please provide API to serialize `EmbeddedText` to blob suitable for PDB #24047

Open jnm2 opened 6 years ago

jnm2 commented 6 years ago

Roslyn 2.6.1

Problem

I need a way to add embedded source to a MetadataBuilder. Currently this requires reflection:

// https://github.com/dotnet/roslyn/blob/ee2a411ff9aeb2f78da962e2abaa0b69da3f925c/src/Dependencies/CodeAnalysis.Debugging/PortableCustomDebugInfoKinds.cs#L17
private static readonly Guid PortableCustomDebugInfoKinds_EmbeddedSource = new Guid("0E8A571B-6926-466E-B4AD-8AB04611F5FE");

// https://github.com/dotnet/roslyn/blob/ee2a411ff9aeb2f78da962e2abaa0b69da3f925c/src/Compilers/Core/Portable/EmbeddedText.cs#L79
private static readonly Func<EmbeddedText, ImmutableArray<byte>> get_Blob = (Func<EmbeddedText, ImmutableArray<byte>>)
    typeof(EmbeddedText)
    .GetProperty("Blob", BindingFlags.Instance | BindingFlags.NonPublic)
    .GetGetMethod(true)
    .CreateDelegate(typeof(Func<EmbeddedText, ImmutableArray<byte>>));

// ... (MetadataBuilder builder)

builder.AddCustomDebugInformation(
    parent: documentHandle,
    kind: builder.GetOrAddGuid(PortableCustomDebugInfoKinds_EmbeddedSource),
    value: builder.GetOrAddBlob(get_Blob.Invoke(embeddedText)));

Proposal

Please make these internal APIs public (or provide an equivalent alternative which returns both the serialized blob and the checksum):

namespace Microsoft.CodeAnalysis
{
    public sealed class EmbeddedText
    {
+       public ImmutableArray<byte> Blob { get; } 
    }
}

namespace Microsoft.CodeAnalysis.Debugging
{
+   public static class PortableCustomDebugInfoKinds
+   {
+       public static readonly Guid EmbeddedSource;
+   }
}

Existing API

https://github.com/dotnet/roslyn/blob/a2f028526a329950a891d5c74a5b8d703168297c/src/Compilers/Core/Portable/EmbeddedText.cs#L79

https://github.com/dotnet/roslyn/blob/ee2a411ff9aeb2f78da962e2abaa0b69da3f925c/src/Dependencies/CodeAnalysis.Debugging/PortableCustomDebugInfoKinds.cs#L17

jcouv commented 6 years ago

Tagging @tmat