Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#nullable disable

#pragma warning disable 436 // SuppressUnmanagedCodeSecurityAttribute defined in source and mscorlib

using System;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Runtime.InteropServices;

namespace Microsoft.DiaSymReader
{
[ComImport]
[Guid("2ae6a06a-92ba-4c2d-a64e-7e9fa421a330")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[ComVisible(false)]
internal interface ISymUnmanagedCompilerInfoWriter
{
/// <summary>
/// Adds compiler version number and name.
/// </summary>
[PreserveSig]
int AddCompilerInfo(ushort major, ushort minor, ushort build, ushort revision, [MarshalAs(UnmanagedType.LPWStr)] string name);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -208,5 +208,20 @@ public abstract void SetAsyncInfo(
/// <exception cref="InvalidOperationException">Writes are not allowed to the underlying stream.</exception>
/// <exception cref="SymUnmanagedWriterException">Error occurred while writing PDB data.</exception>
public abstract void CloseTokensToSourceSpansMap();

/// <summary>
/// Writes compiler version and name to the PDB.
/// </summary>
/// <param name="major">Major version</param>
/// <param name="minor">Minor version</param>
/// <param name="build">Build</param>
/// <param name="revision">Revision</param>
/// <param name="name">Compiler name</param>
/// <exception cref="ObjectDisposedException">Object has been disposed.</exception>
/// <exception cref="SymUnmanagedWriterException">Error occurred while writing PDB data.</exception>
/// <exception cref="NotSupportedException">The PDB writer does not support adding compiler info.</exception>
/// <exception cref="ArgumentNullException"><paramref name="name"/> is null.</exception>
public virtual void AddCompilerInfo(ushort major, ushort minor, ushort build, ushort revision, string name)
=> throw new NotSupportedException();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -749,5 +749,28 @@ public override unsafe void GetSignature(out Guid guid, out uint stamp, out int
// we need to go through IPdbWriter interface to get it.
((IPdbWriter)symWriter).GetSignatureAge(out stamp, out age);
}

public override void AddCompilerInfo(ushort major, ushort minor, ushort build, ushort revision, string name)
{
if (name == null)
{
throw new ArgumentNullException(nameof(name));
}

var symWriter = GetSymWriter();
if (symWriter is not ISymUnmanagedCompilerInfoWriter infoWriter)
{
return;
}

try
{
infoWriter.AddCompilerInfo(major, minor, build, revision, name);
}
catch (Exception ex)
{
throw PdbWritingException(ex);
}
}
}
}
9 changes: 9 additions & 0 deletions src/Compilers/Core/Portable/NativePdbWriter/PdbWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Reflection.Metadata;
using System.Reflection.Metadata.Ecma335;
using System.Security.Cryptography;
Expand Down Expand Up @@ -768,5 +769,13 @@ public void WriteRemainingDebugDocuments(IReadOnlyDictionary<string, DebugSource
AddDocumentIndex(kvp.Value);
}
}

public void WriteCompilerVersion(string language)
{
var compilerAssembly = typeof(Compilation).Assembly;
var fileVersion = Version.Parse(compilerAssembly.GetCustomAttribute<AssemblyFileVersionAttribute>().Version);
var versionString = compilerAssembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>().InformationalVersion;
_symWriter.AddCompilerInfo((ushort)fileVersion.Major, (ushort)fileVersion.Minor, (ushort)fileVersion.Build, (ushort)fileVersion.Revision, $"{language} - {versionString}");
}
}
}
2 changes: 2 additions & 0 deletions src/Compilers/Core/Portable/PEWriter/PeWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ internal static bool WritePeToStream(
}

nativePdbWriterOpt.WriteRemainingDebugDocuments(mdWriter.Module.DebugDocumentsBuilder.DebugDocuments);

nativePdbWriterOpt.WriteCompilerVersion(context.Module.CommonCompilation.Language);
}

Stream peStream = getPeStream();
Expand Down
5 changes: 5 additions & 0 deletions src/Test/PdbUtilities/Writer/MockSymUnmanagedWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,5 +126,10 @@ public override void WriteTo(Stream stream)
{
throw MakeException();
}

public override void AddCompilerInfo(ushort major, ushort minor, ushort build, ushort revision, string name)
{
throw MakeException();
}
}
}