Skip to content

Commit

Permalink
Replace uses of DiagnosticBag with IErrorListener where feasible in t…
Browse files Browse the repository at this point in the history
…he build process
  • Loading branch information
ds5678 committed Oct 16, 2022
1 parent f23314d commit c92533a
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 27 deletions.
8 changes: 4 additions & 4 deletions src/AsmResolver.DotNet/Builder/DotNetDirectoryFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,13 @@ public StrongNamePrivateKey? StrongNamePrivateKey
public virtual DotNetDirectoryBuildResult CreateDotNetDirectory(
ModuleDefinition module,
INativeSymbolsProvider symbolsProvider,
DiagnosticBag diagnosticBag)
IErrorListener errorListener)
{
// Find all members in the module.
var discoveryResult = DiscoverMemberDefinitionsInModule(module);

// Creat new .NET dir buffer.
var buffer = CreateDotNetDirectoryBuffer(module, symbolsProvider, diagnosticBag);
var buffer = CreateDotNetDirectoryBuffer(module, symbolsProvider, errorListener);
buffer.DefineModule(module);

// When specified, import existing AssemblyRef, ModuleRef, TypeRef and MemberRef prior to adding any other
Expand Down Expand Up @@ -148,10 +148,10 @@ private MemberDiscoveryResult DiscoverMemberDefinitionsInModule(ModuleDefinition
private DotNetDirectoryBuffer CreateDotNetDirectoryBuffer(
ModuleDefinition module,
INativeSymbolsProvider symbolsProvider,
DiagnosticBag diagnosticBag)
IErrorListener errorListener)
{
var metadataBuffer = CreateMetadataBuffer(module);
return new DotNetDirectoryBuffer(module, MethodBodySerializer, symbolsProvider, metadataBuffer, diagnosticBag);
return new DotNetDirectoryBuffer(module, MethodBodySerializer, symbolsProvider, metadataBuffer, errorListener);
}

private IMetadataBuffer CreateMetadataBuffer(ModuleDefinition module)
Expand Down
6 changes: 3 additions & 3 deletions src/AsmResolver.DotNet/Builder/IDotNetDirectoryFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ public interface IDotNetDirectoryFactory
/// </summary>
/// <param name="module">The module to serialize to a .NET data directory.</param>
/// <param name="symbolsProvider">The object responsible for providing references to native symbols.</param>
/// <param name="diagnosticBag">The bag that is used to collect all diagnostic information during the building process. </param>
/// <param name="errorListener">The listener that is used to collect all diagnostic information during the building process. </param>
/// <returns>The serialized data directory.</returns>
/// <exception cref="MetadataBuilderException">Occurs when the metadata builder encounters an error.</exception>
DotNetDirectoryBuildResult CreateDotNetDirectory(ModuleDefinition module, INativeSymbolsProvider symbolsProvider, DiagnosticBag diagnosticBag);
DotNetDirectoryBuildResult CreateDotNetDirectory(ModuleDefinition module, INativeSymbolsProvider symbolsProvider, IErrorListener errorListener);
}
}
}
10 changes: 5 additions & 5 deletions src/AsmResolver.DotNet/Builder/ManagedPEImageBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Linq;
using AsmResolver.DotNet.Code.Native;
using AsmResolver.PE;
Expand Down Expand Up @@ -74,7 +74,7 @@ public PEImageBuildResult CreateImage(ModuleDefinition module)
var result = DotNetDirectoryFactory.CreateDotNetDirectory(
module,
symbolProvider,
context.DiagnosticBag);
context.ErrorListener);
image.DotNetDirectory = result.Directory;
tokenMapping = result.TokenMapping;

Expand Down Expand Up @@ -108,12 +108,12 @@ public PEImageBuildResult CreateImage(ModuleDefinition module)
}
catch (Exception ex)
{
context.DiagnosticBag.RegisterException(ex);
context.DiagnosticBag.MarkAsFatal();
context.ErrorListener.RegisterException(ex);
context.ErrorListener.MarkAsFatal();
}

tokenMapping ??= new TokenMapping();
return new PEImageBuildResult(image, context.DiagnosticBag, tokenMapping);
return new PEImageBuildResult(image, context.ErrorListener, tokenMapping);
}
}
}
12 changes: 6 additions & 6 deletions src/AsmResolver.DotNet/Builder/PEImageBuildContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,24 @@ public class PEImageBuildContext
/// </summary>
public PEImageBuildContext()
{
DiagnosticBag = new DiagnosticBag();
ErrorListener = new DiagnosticBag();
}

/// <summary>
/// Creates a new build context.
/// </summary>
/// <param name="diagnosticBag">The diagnostic bag to use.</param>
public PEImageBuildContext(DiagnosticBag diagnosticBag)
public PEImageBuildContext(IErrorListener diagnosticBag)
{
DiagnosticBag = diagnosticBag ?? throw new ArgumentNullException(nameof(diagnosticBag));
ErrorListener = diagnosticBag ?? throw new ArgumentNullException(nameof(diagnosticBag));
}

/// <summary>
/// Gets the bag that collects all diagnostic information during the building process.
/// Gets the error listener that collects all diagnostic information during the building process.
/// </summary>
public DiagnosticBag DiagnosticBag
public IErrorListener ErrorListener
{
get;
}
}
}
}
10 changes: 5 additions & 5 deletions src/AsmResolver.DotNet/Builder/PEImageBuildResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ public class PEImageBuildResult
/// Creates a new instance of the <see cref="PEImageBuildResult"/> class.
/// </summary>
/// <param name="image">The constructed image, or <c>null</c> if the construction failed.</param>
/// <param name="diagnosticBag">The diagnostics that were collected during the construction of the image.</param>
/// <param name="errorListener">The diagnostics that were collected during the construction of the image.</param>
/// <param name="tokenMapping">An object that maps metadata members to their newly assigned tokens.</param>
public PEImageBuildResult(IPEImage? image, DiagnosticBag diagnosticBag, ITokenMapping tokenMapping)
public PEImageBuildResult(IPEImage? image, IErrorListener errorListener, ITokenMapping tokenMapping)
{
ConstructedImage = image;
DiagnosticBag = diagnosticBag ?? throw new ArgumentNullException(nameof(diagnosticBag));
ErrorListener = errorListener ?? throw new ArgumentNullException(nameof(errorListener));
TokenMapping = tokenMapping ?? throw new ArgumentNullException(nameof(tokenMapping));
}

Expand All @@ -34,12 +34,12 @@ public IPEImage? ConstructedImage
/// Gets a value indicating whether the image was constructed successfully or not.
/// </summary>
[MemberNotNullWhen(false, nameof(ConstructedImage))]
public bool HasFailed => DiagnosticBag.IsFatal;
public bool HasFailed => ConstructedImage is null;

/// <summary>
/// Gets the bag containing the diagnostics that were collected during the construction of the image.
/// </summary>
public DiagnosticBag DiagnosticBag
public IErrorListener ErrorListener
{
get;
}
Expand Down
10 changes: 7 additions & 3 deletions src/AsmResolver.DotNet/ModuleDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1231,14 +1231,18 @@ public void Write(IBinaryStreamWriter writer, IPEImageBuilder imageBuilder, IPEF
public IPEImage ToPEImage(IPEImageBuilder imageBuilder)
{
var result = imageBuilder.CreateImage(this);
if (result.DiagnosticBag.HasErrors)
if (result.ErrorListener is DiagnosticBag diagnosticBag && diagnosticBag.HasErrors)
{
throw new AggregateException(
"Construction of the PE image failed with one or more errors.",
result.DiagnosticBag.Exceptions);
diagnosticBag.Exceptions);
}
else if (result.HasFailed)
{
throw new Exception("Construction of the PE image failed.");
}

return result.ConstructedImage!;
return result.ConstructedImage;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ protected static ModuleDefinition RebuildAndReloadModule(ModuleDefinition module

var result = builder.CreateImage(module);
if (result.HasFailed)
throw new AggregateException(result.DiagnosticBag.Exceptions);
if (result.ErrorListener is DiagnosticBag diagnosticBag)
throw new AggregateException(diagnosticBag.Exceptions);
else
throw new Exception("Image creation failed.");

var newImage = result.ConstructedImage;

Expand Down

0 comments on commit c92533a

Please sign in to comment.