Skip to content

Commit

Permalink
Improve the error output that occurs when saving the module (#447)
Browse files Browse the repository at this point in the history
* Improve the error output that occurs when saving the module

* Update

* rollback only one '.' changes

* rollback deleted MethodBodyWriter constructors

* change internal errorContext to private

* fix MetadataErrorContext.Source overwritten
  • Loading branch information
wwh1004 authored Mar 24, 2022
1 parent 2fae02e commit dafe486
Show file tree
Hide file tree
Showing 10 changed files with 175 additions and 66 deletions.
6 changes: 3 additions & 3 deletions src/DotNet/Pdb/Portable/ImportScopeBlobWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ void Write(DataWriter writer, IList<PdbImport> imports) {
for (int i = 0; i < count; i++) {
var import = imports[i];
if (!ImportDefinitionKindUtils.ToImportDefinitionKind(import.Kind, out uint rawKind)) {
helper.Error("Unknown import definition kind: " + import.Kind.ToString());
helper.Error2("Unknown import definition kind: {0}.", import.Kind);
return;
}
writer.WriteCompressedUInt32(rawKind);
Expand Down Expand Up @@ -94,7 +94,7 @@ void Write(DataWriter writer, IList<PdbImport> imports) {
break;

default:
helper.Error("Unknown import definition kind: " + import.Kind.ToString());
helper.Error2("Unknown import definition kind: {0}.", import.Kind);
return;
}
}
Expand All @@ -108,7 +108,7 @@ uint GetTypeDefOrRefEncodedToken(ITypeDefOrRef tdr) {
var token = systemMetadata.GetToken(tdr);
if (MD.CodedToken.TypeDefOrRef.Encode(token, out uint codedToken))
return codedToken;
helper.Error($"Could not encode token 0x{token.Raw:X8}");
helper.Error2("Could not encode token 0x{0:X8}.", token.Raw);
return 0;
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/DotNet/Pdb/Portable/LocalConstantSigBlobWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ void Write(DataWriter writer, TypeSig type, object value) {
var tdr = ((ValueTypeSig)type).TypeDefOrRef;
var td = tdr.ResolveTypeDef();
if (td is null)
helper.Error($"Couldn't resolve type 0x{tdr?.MDToken.Raw ?? 0:X8}");
helper.Error2("Couldn't resolve type 0x{0:X8}.", tdr?.MDToken.Raw ?? 0);
else if (td.IsEnum) {
var underlyingType = td.GetEnumUnderlyingType().RemovePinnedAndModifiers();
switch (underlyingType.GetElementType()) {
Expand Down Expand Up @@ -135,7 +135,7 @@ void Write(DataWriter writer, TypeSig type, object value) {
if (value is byte[])
writer.WriteBytes((byte[])value);
else if (value is not null) {
helper.Error("Unsupported constant: " + value.GetType().FullName);
helper.Error2("Unsupported constant: {0}.", value.GetType().FullName);
return;
}
}
Expand Down Expand Up @@ -176,7 +176,7 @@ void Write(DataWriter writer, TypeSig type, object value) {
case ElementType.Sentinel:
case ElementType.Pinned:
default:
helper.Error("Unsupported element type in LocalConstant sig blob: " + et.ToString());
helper.Error2("Unsupported element type in LocalConstant sig blob: {0}.", et);
return;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/DotNet/Pdb/Portable/PortablePdbCustomDebugInfoWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ void WriteUTF8Z(string s) {

void WriteStateMachineHoistedLocalScopes(PdbStateMachineHoistedLocalScopesCustomDebugInfo cdi) {
if (!methodContext.HasBody) {
helper.Error("Method has no body, can't write custom debug info: " + cdi.Kind);
helper.Error2("Method has no body, can't write custom debug info: {0}.", cdi.Kind);
return;
}
var cdiScopes = cdi.Scopes;
Expand Down Expand Up @@ -222,7 +222,7 @@ void WriteSourceLink(PdbSourceLinkCustomDebugInfo cdi) {

void WriteAsyncMethodSteppingInformation(PdbAsyncMethodCustomDebugInfo cdi) {
if (!methodContext.HasBody) {
helper.Error("Method has no body, can't write custom debug info: " + cdi.Kind);
helper.Error2("Method has no body, can't write custom debug info: {0}.", cdi.Kind);
return;
}

Expand Down
16 changes: 16 additions & 0 deletions src/DotNet/Writer/IWriterError.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,20 @@ public interface IWriterError2 : IWriterError {
/// <param name="args">Optional message arguments</param>
void Error(string message, params object[] args);
}

static partial class Extensions {
/// <summary>
/// Called when an error is detected (eg. a null pointer or other invalid value). The error
/// can be ignored but the written data won't be valid.
/// </summary>
/// <param name="helper">The instance of <see cref="IWriterError"/></param>
/// <param name="message">Error message</param>
/// <param name="args">Optional message arguments</param>
internal static void Error2(this IWriterError helper, string message, params object[] args) {
if (helper is IWriterError2 helper2)
helper2.Error(message, args);
else
helper.Error(string.Format(message, args));
}
}
}
2 changes: 1 addition & 1 deletion src/DotNet/Writer/MarshalBlobWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ byte[] Write(MarshalType marshalType) {
bool UpdateCanWrite(bool isValid, string field, ref bool canWriteMore) {
if (!canWriteMore) {
if (isValid)
helper.Error($"MarshalType field {field} is valid even though a previous field was invalid");
helper.Error2("MarshalType field {0} is valid even though a previous field was invalid.", field);
return canWriteMore;
}

Expand Down
Loading

0 comments on commit dafe486

Please sign in to comment.