Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Print method name and hexadecimal offsets in ILVerify output #59583

Merged
merged 2 commits into from
Feb 18, 2022
Merged
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
38 changes: 34 additions & 4 deletions src/Compilers/Test/Core/CompilationVerifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.IO;
using System.Linq;
using System.Reflection;
using System.Reflection.Metadata;
using System.Reflection.PortableExecutable;
using System.Runtime.CompilerServices;
using System.Xml.Linq;
Expand Down Expand Up @@ -323,15 +324,27 @@ private void ILVerify(Verification verification)

if (errorCount > 0)
{
return (false, printVerificationResult(result));
var metadataReader = mainModule.GetMetadataReader();
return (false, printVerificationResult(result, metadataReader));
}

return (true, string.Empty);
}

static string printVerificationResult(IEnumerable<ILVerify.VerificationResult> result)
static string printVerificationResult(IEnumerable<ILVerify.VerificationResult> result, MetadataReader metadataReader)
{
return string.Join("\r\n", result.Select(r => r.Message + printErrorArguments(r.ErrorArguments)));
return string.Join("\r\n", result.Select(r => printMethod(r.Method, metadataReader) + r.Message + printErrorArguments(r.ErrorArguments)));
}

static string printMethod(MethodDefinitionHandle method, MetadataReader metadataReader)
{
if (method.IsNil)
{
return "";
}

var methodName = metadataReader.GetString(metadataReader.GetMethodDefinition(method).Name);
return $"[{methodName}]: ";
}

static string printErrorArguments(ILVerify.ErrorArgument[] errorArguments)
Expand All @@ -345,7 +358,7 @@ static string printErrorArguments(ILVerify.ErrorArgument[] errorArguments)
var pooledBuilder = PooledStringBuilder.GetInstance();
var builder = pooledBuilder.Builder;
builder.Append(" { ");
var x = errorArguments.Select(a => a.Name + " = " + a.Value.ToString()).ToArray();
var x = errorArguments.Select(a => printErrorArgument(a)).ToArray();
for (int i = 0; i < x.Length; i++)
{
if (i > 0)
Expand All @@ -358,6 +371,23 @@ static string printErrorArguments(ILVerify.ErrorArgument[] errorArguments)

return pooledBuilder.ToStringAndFree();
}

static string printErrorArgument(ILVerify.ErrorArgument errorArgument)
{
var name = errorArgument.Name;

string value;
if (name == "Offset" && errorArgument.Value is int i)
{
value = "0x" + Convert.ToString(i, 16);
}
else
{
value = errorArgument.Value.ToString();
}

return name + " = " + value;
}
}

// TODO(tomat): Fold into CompileAndVerify.
Expand Down