Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure generate-helper-types works with single-file mode #391

Merged
merged 1 commit into from
Sep 18, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 57 additions & 21 deletions sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -297,13 +297,22 @@ public void Close()

if (_config.GenerateHelperTypes && (_config.OutputMode == PInvokeGeneratorOutputMode.CSharp))
{
_ = Directory.CreateDirectory(_config.OutputLocation);
if (_config.GenerateMultipleFiles)
{
Debug.Assert(stream is null);
Debug.Assert(leaveStreamOpen is false);
}
else
{
Debug.Assert(stream is not null);
Debug.Assert(leaveStreamOpen is true);
}

GenerateNativeInheritanceAttribute(this);
GenerateNativeTypeNameAttribute(this);
GenerateSetsLastSystemErrorAttribute(this);
GenerateVtblIndexAttribute(this);
GenerateTransparentStructs(this);
GenerateNativeInheritanceAttribute(this, stream, leaveStreamOpen);
GenerateNativeTypeNameAttribute(this, stream, leaveStreamOpen);
GenerateSetsLastSystemErrorAttribute(this, stream, leaveStreamOpen);
GenerateVtblIndexAttribute(this, stream, leaveStreamOpen);
GenerateTransparentStructs(this, stream, leaveStreamOpen);
}

if (leaveStreamOpen && _outputBuilderFactory.OutputBuilders.Any())
Expand Down Expand Up @@ -345,12 +354,17 @@ public void Close()
_uuidsToGenerate.Clear();
_visitedFiles.Clear();

static void GenerateNativeInheritanceAttribute(PInvokeGenerator generator)
static void GenerateNativeInheritanceAttribute(PInvokeGenerator generator, Stream? stream, bool leaveStreamOpen)
{
var config = generator.Config;
var outputPath = Path.Combine(config.OutputLocation, "NativeInheritanceAttribute.cs");

using var sw = new StreamWriter(outputPath);
if (stream is null)
{
var outputPath = Path.Combine(config.OutputLocation, "NativeInheritanceAttribute.cs");
stream = generator._outputStreamFactory(outputPath);
}

using var sw = new StreamWriter(stream, s_defaultStreamWriterEncoding, DefaultStreamWriterBufferSize, leaveStreamOpen);
sw.NewLine = "\n";

if (config.HeaderText != string.Empty)
Expand Down Expand Up @@ -418,12 +432,17 @@ static void GenerateNativeInheritanceAttribute(PInvokeGenerator generator)
}
}

static void GenerateNativeTypeNameAttribute(PInvokeGenerator generator)
static void GenerateNativeTypeNameAttribute(PInvokeGenerator generator, Stream? stream, bool leaveStreamOpen)
{
var config = generator.Config;
var outputPath = Path.Combine(config.OutputLocation, "NativeTypeNameAttribute.cs");

using var sw = new StreamWriter(outputPath);
if (stream is null)
{
var outputPath = Path.Combine(config.OutputLocation, "NativeTypeNameAttribute.cs");
stream = generator._outputStreamFactory(outputPath);
}

using var sw = new StreamWriter(stream, s_defaultStreamWriterEncoding, DefaultStreamWriterBufferSize, leaveStreamOpen);
sw.NewLine = "\n";

if (config.HeaderText != string.Empty)
Expand Down Expand Up @@ -491,12 +510,18 @@ static void GenerateNativeTypeNameAttribute(PInvokeGenerator generator)
}
}

static void GenerateSetsLastSystemErrorAttribute(PInvokeGenerator generator)
static void GenerateSetsLastSystemErrorAttribute(PInvokeGenerator generator, Stream? stream, bool leaveStreamOpen)
{
var config = generator.Config;
var outputPath = Path.Combine(config.OutputLocation, "SetsLastSystemErrorAttribute.cs");

using var sw = new StreamWriter(outputPath);
if (stream is null)
{
Debug.Assert(stream is null);
var outputPath = Path.Combine(config.OutputLocation, "SetsLastSystemErrorAttribute.cs");
stream = generator._outputStreamFactory(outputPath);
}

using var sw = new StreamWriter(stream, s_defaultStreamWriterEncoding, DefaultStreamWriterBufferSize, leaveStreamOpen);
sw.NewLine = "\n";

if (config.HeaderText != string.Empty)
Expand Down Expand Up @@ -553,12 +578,18 @@ static void GenerateSetsLastSystemErrorAttribute(PInvokeGenerator generator)
}
}

static void GenerateVtblIndexAttribute(PInvokeGenerator generator)
static void GenerateVtblIndexAttribute(PInvokeGenerator generator, Stream? stream, bool leaveStreamOpen)
{
var config = generator.Config;
var outputPath = Path.Combine(config.OutputLocation, "VtblIndexAttribute.cs");

using var sw = new StreamWriter(outputPath);
if (stream is null)
{
Debug.Assert(stream is null);
var outputPath = Path.Combine(config.OutputLocation, "VtblIndexAttribute.cs");
stream = generator._outputStreamFactory(outputPath);
}

using var sw = new StreamWriter(stream, s_defaultStreamWriterEncoding, DefaultStreamWriterBufferSize, leaveStreamOpen);
sw.NewLine = "\n";

if (config.HeaderText != string.Empty)
Expand Down Expand Up @@ -626,7 +657,7 @@ static void GenerateVtblIndexAttribute(PInvokeGenerator generator)
}
}

static void GenerateTransparentStructs(PInvokeGenerator generator)
static void GenerateTransparentStructs(PInvokeGenerator generator, Stream? stream, bool leaveStreamOpen)
{
var config = generator.Config;

Expand All @@ -637,9 +668,14 @@ static void GenerateTransparentStructs(PInvokeGenerator generator)
var kind = transparentStruct.Value.Kind;

var isTypePointer = type.Contains('*');
var outputPath = Path.Combine(config.OutputLocation, $"{name}.cs");

using var sw = new StreamWriter(outputPath);
if (stream is null)
{
var outputPath = Path.Combine(config.OutputLocation, $"{name}.cs");
stream = generator._outputStreamFactory(outputPath);
}

using var sw = new StreamWriter(stream, s_defaultStreamWriterEncoding, DefaultStreamWriterBufferSize, leaveStreamOpen);
sw.NewLine = "\n";

if (config.HeaderText != string.Empty)
Expand Down