Skip to content

Commit

Permalink
Print method name and hexadecimal offsets in ILVerify output (#59583)
Browse files Browse the repository at this point in the history
  • Loading branch information
jcouv authored Feb 18, 2022
1 parent ed5caf4 commit 36dc033
Showing 1 changed file with 34 additions and 4 deletions.
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

0 comments on commit 36dc033

Please sign in to comment.