Skip to content

Commit 7beb737

Browse files
authored
Merge pull request github#16036 from tamasvajk/autobuilder/disposable
C#: Properly dispose diagnostic writer objects
2 parents 35f61d9 + 362a109 commit 7beb737

File tree

8 files changed

+92
-64
lines changed

8 files changed

+92
-64
lines changed

cpp/autobuilder/Semmle.Autobuild.Cpp.Tests/BuildScripts.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,8 @@ internal class TestDiagnosticWriter : IDiagnosticsWriter
203203
public IList<DiagnosticMessage> Diagnostics { get; } = new List<DiagnosticMessage>();
204204

205205
public void AddEntry(DiagnosticMessage message) => this.Diagnostics.Add(message);
206+
207+
public void Dispose() { }
206208
}
207209

208210
/// <summary>

cpp/autobuilder/Semmle.Autobuild.Cpp/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ static int Main()
1717
try
1818
{
1919
Console.WriteLine("CodeQL C++ autobuilder");
20-
var builder = new CppAutobuilder(actions, options);
20+
using var builder = new CppAutobuilder(actions, options);
2121
return builder.AttemptBuild();
2222
}
2323
catch (InvalidEnvironmentException ex)

csharp/autobuilder/Semmle.Autobuild.CSharp.Tests/BuildScripts.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,8 @@ internal class TestDiagnosticWriter : IDiagnosticsWriter
218218
public IList<DiagnosticMessage> Diagnostics { get; } = new List<DiagnosticMessage>();
219219

220220
public void AddEntry(DiagnosticMessage message) => this.Diagnostics.Add(message);
221+
222+
public void Dispose() { }
221223
}
222224

223225
/// <summary>

csharp/autobuilder/Semmle.Autobuild.CSharp/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public static int Main()
1717
try
1818
{
1919
Console.WriteLine("CodeQL C# autobuilder");
20-
var builder = new CSharpAutobuilder(actions, options);
20+
using var builder = new CSharpAutobuilder(actions, options);
2121
return builder.AttemptBuild();
2222
}
2323
catch (InvalidEnvironmentException ex)

csharp/autobuilder/Semmle.Autobuild.Shared/Autobuilder.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public interface IAutobuilder<out TAutobuildOptions> where TAutobuildOptions : A
9292
/// The overall design is intended to be extensible so that in theory,
9393
/// it should be possible to add new build rules without touching this code.
9494
/// </summary>
95-
public abstract class Autobuilder<TAutobuildOptions> : IAutobuilder<TAutobuildOptions> where TAutobuildOptions : AutobuildOptionsShared
95+
public abstract class Autobuilder<TAutobuildOptions> : IDisposable, IAutobuilder<TAutobuildOptions> where TAutobuildOptions : AutobuildOptionsShared
9696
{
9797
/// <summary>
9898
/// Full file paths of files found in the project directory, as well as
@@ -351,6 +351,20 @@ protected BuildScript AutobuildFailure() =>
351351
}
352352
});
353353

354+
public void Dispose()
355+
{
356+
Dispose(true);
357+
GC.SuppressFinalize(this);
358+
}
359+
360+
protected virtual void Dispose(bool disposing)
361+
{
362+
if (disposing)
363+
{
364+
diagnostics.Dispose();
365+
}
366+
}
367+
354368
/// <summary>
355369
/// Value of CODEQL_EXTRACTOR_<LANG>_ROOT environment variable.
356370
/// </summary>

csharp/extractor/Semmle.Util/ToolStatusPage.cs renamed to csharp/extractor/Semmle.Util/ToolStatusPage/DiagnosticMessage.cs

Lines changed: 0 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Globalization;
4-
using System.IO;
54
using Newtonsoft.Json;
65
using Newtonsoft.Json.Converters;
76
using Newtonsoft.Json.Serialization;
@@ -179,64 +178,4 @@ public DiagnosticMessage(
179178
this.PlaintextMessage = plaintextMessage;
180179
}
181180
}
182-
183-
/// <summary>
184-
/// Provides the ability to write diagnostic messages to some output.
185-
/// </summary>
186-
public interface IDiagnosticsWriter
187-
{
188-
/// <summary>
189-
/// Adds <paramref name="message" /> as a new diagnostics entry.
190-
/// </summary>
191-
/// <param name="message">The diagnostics entry to add.</param>
192-
void AddEntry(DiagnosticMessage message);
193-
}
194-
195-
/// <summary>
196-
/// A wrapper around an underlying <see cref="StreamWriter" /> which allows
197-
/// <see cref="DiagnosticMessage" /> objects to be serialized to it.
198-
/// </summary>
199-
public sealed class DiagnosticsStream : IDiagnosticsWriter, IDisposable
200-
{
201-
private readonly JsonSerializer serializer;
202-
private readonly StreamWriter writer;
203-
204-
/// <summary>
205-
/// Initialises a new <see cref="DiagnosticsStream" /> for a file at <paramref name="path" />.
206-
/// </summary>
207-
/// <param name="path">The path to the file that should be created.</param>
208-
public DiagnosticsStream(string path)
209-
{
210-
this.writer = File.CreateText(path);
211-
212-
var contractResolver = new DefaultContractResolver
213-
{
214-
NamingStrategy = new CamelCaseNamingStrategy()
215-
};
216-
217-
serializer = new JsonSerializer
218-
{
219-
ContractResolver = contractResolver,
220-
NullValueHandling = NullValueHandling.Ignore
221-
};
222-
}
223-
224-
/// <summary>
225-
/// Adds <paramref name="message" /> as a new diagnostics entry.
226-
/// </summary>
227-
/// <param name="message">The diagnostics entry to add.</param>
228-
public void AddEntry(DiagnosticMessage message)
229-
{
230-
serializer.Serialize(writer, message);
231-
writer.Flush();
232-
}
233-
234-
/// <summary>
235-
/// Releases all resources used by the <see cref="DiagnosticsStream" /> object.
236-
/// </summary>
237-
public void Dispose()
238-
{
239-
writer.Dispose();
240-
}
241-
}
242181
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using System;
2+
using System.IO;
3+
using Newtonsoft.Json;
4+
using Newtonsoft.Json.Serialization;
5+
6+
namespace Semmle.Util
7+
{
8+
/// <summary>
9+
/// A wrapper around an underlying <see cref="StreamWriter" /> which allows
10+
/// <see cref="DiagnosticMessage" /> objects to be serialized to it.
11+
/// </summary>
12+
public sealed class DiagnosticsStream : IDiagnosticsWriter
13+
{
14+
private readonly JsonSerializer serializer;
15+
private readonly StreamWriter writer;
16+
17+
/// <summary>
18+
/// Initialises a new <see cref="DiagnosticsStream" /> for a file at <paramref name="path" />.
19+
/// </summary>
20+
/// <param name="path">The path to the file that should be created.</param>
21+
public DiagnosticsStream(string path)
22+
{
23+
this.writer = File.CreateText(path);
24+
25+
var contractResolver = new DefaultContractResolver
26+
{
27+
NamingStrategy = new CamelCaseNamingStrategy()
28+
};
29+
30+
serializer = new JsonSerializer
31+
{
32+
ContractResolver = contractResolver,
33+
NullValueHandling = NullValueHandling.Ignore
34+
};
35+
}
36+
37+
/// <summary>
38+
/// Adds <paramref name="message" /> as a new diagnostics entry.
39+
/// </summary>
40+
/// <param name="message">The diagnostics entry to add.</param>
41+
public void AddEntry(DiagnosticMessage message)
42+
{
43+
serializer.Serialize(writer, message);
44+
writer.Flush();
45+
}
46+
47+
/// <summary>
48+
/// Releases all resources used by the <see cref="DiagnosticsStream" /> object.
49+
/// </summary>
50+
public void Dispose()
51+
{
52+
writer.Dispose();
53+
}
54+
}
55+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System;
2+
3+
namespace Semmle.Util
4+
{
5+
/// <summary>
6+
/// Provides the ability to write diagnostic messages to some output.
7+
/// </summary>
8+
public interface IDiagnosticsWriter : IDisposable
9+
{
10+
/// <summary>
11+
/// Adds <paramref name="message" /> as a new diagnostics entry.
12+
/// </summary>
13+
/// <param name="message">The diagnostics entry to add.</param>
14+
void AddEntry(DiagnosticMessage message);
15+
}
16+
}

0 commit comments

Comments
 (0)