Skip to content

Commit

Permalink
Merge pull request #391 from tannergooding/single-file-helper-types
Browse files Browse the repository at this point in the history
Ensure generate-helper-types works with single-file mode
  • Loading branch information
tannergooding authored Sep 18, 2022
2 parents 8d55b99 + f05e35b commit f387011
Showing 1 changed file with 57 additions and 21 deletions.
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

0 comments on commit f387011

Please sign in to comment.