Skip to content

Commit

Permalink
C#: Properly dispose diagnostic writer objects
Browse files Browse the repository at this point in the history
  • Loading branch information
tamasvajk committed Mar 25, 2024
1 parent 55226c4 commit 410b1af
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 5 deletions.
2 changes: 2 additions & 0 deletions cpp/autobuilder/Semmle.Autobuild.Cpp.Tests/BuildScripts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,8 @@ internal class TestDiagnosticWriter : IDiagnosticsWriter
public IList<DiagnosticMessage> Diagnostics { get; } = new List<DiagnosticMessage>();

public void AddEntry(DiagnosticMessage message) => this.Diagnostics.Add(message);

public void Dispose() { }
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion cpp/autobuilder/Semmle.Autobuild.Cpp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ static int Main()
try
{
Console.WriteLine("CodeQL C++ autobuilder");
var builder = new CppAutobuilder(actions, options);
using var builder = new CppAutobuilder(actions, options);
return builder.AttemptBuild();
}
catch (InvalidEnvironmentException ex)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,8 @@ internal class TestDiagnosticWriter : IDiagnosticsWriter
public IList<DiagnosticMessage> Diagnostics { get; } = new List<DiagnosticMessage>();

public void AddEntry(DiagnosticMessage message) => this.Diagnostics.Add(message);

public void Dispose() { }
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion csharp/autobuilder/Semmle.Autobuild.CSharp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public static int Main()
try
{
Console.WriteLine("CodeQL C# autobuilder");
var builder = new CSharpAutobuilder(actions, options);
using var builder = new CSharpAutobuilder(actions, options);
return builder.AttemptBuild();
}
catch (InvalidEnvironmentException ex)
Expand Down
16 changes: 15 additions & 1 deletion csharp/autobuilder/Semmle.Autobuild.Shared/Autobuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public interface IAutobuilder<out TAutobuildOptions> where TAutobuildOptions : A
/// The overall design is intended to be extensible so that in theory,
/// it should be possible to add new build rules without touching this code.
/// </summary>
public abstract class Autobuilder<TAutobuildOptions> : IAutobuilder<TAutobuildOptions> where TAutobuildOptions : AutobuildOptionsShared
public abstract class Autobuilder<TAutobuildOptions> : IDisposable, IAutobuilder<TAutobuildOptions> where TAutobuildOptions : AutobuildOptionsShared
{
/// <summary>
/// Full file paths of files found in the project directory, as well as
Expand Down Expand Up @@ -370,6 +370,20 @@ protected BuildScript AutobuildFailure() =>
}
});

public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

protected virtual void Dispose(bool disposing)
{
if (disposing)
{
diagnostics?.Dispose();
}
}

/// <summary>
/// Value of CODEQL_EXTRACTOR_<LANG>_ROOT environment variable.
/// </summary>
Expand Down
4 changes: 2 additions & 2 deletions csharp/extractor/Semmle.Util/ToolStatusPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public DiagnosticMessage(
/// <summary>
/// Provides the ability to write diagnostic messages to some output.
/// </summary>
public interface IDiagnosticsWriter
public interface IDiagnosticsWriter : IDisposable
{
/// <summary>
/// Adds <paramref name="message" /> as a new diagnostics entry.
Expand All @@ -195,7 +195,7 @@ public interface IDiagnosticsWriter
/// A wrapper around an underlying <see cref="StreamWriter" /> which allows
/// <see cref="DiagnosticMessage" /> objects to be serialized to it.
/// </summary>
public sealed class DiagnosticsStream : IDiagnosticsWriter, IDisposable
public sealed class DiagnosticsStream : IDiagnosticsWriter
{
private readonly JsonSerializer serializer;
private readonly StreamWriter writer;
Expand Down

0 comments on commit 410b1af

Please sign in to comment.