Skip to content
Merged
Show file tree
Hide file tree
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
7 changes: 5 additions & 2 deletions sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.Close.cs
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,10 @@ public void Close()

if (_config.OutputMode == PInvokeGeneratorOutputMode.CSharp)
{
sw.WriteLine('}');
if (!_config.GenerateFileScopedNamespaces)
{
sw.WriteLine('}');
}
}
else if (_config.OutputMode == PInvokeGeneratorOutputMode.Xml)
{
Expand All @@ -299,7 +302,7 @@ public void Close()
using var tsw = new StreamWriter(testStream, s_defaultStreamWriterEncoding, DefaultStreamWriterBufferSize, leaveStreamOpen);
tsw.NewLine = "\n";

if (_config.OutputMode == PInvokeGeneratorOutputMode.CSharp)
if ((_config.OutputMode == PInvokeGeneratorOutputMode.CSharp) && !_config.GenerateFileScopedNamespaces)
{
tsw.WriteLine('}');
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright (c) .NET Foundation and Contributors. All Rights Reserved. Licensed under the MIT License (MIT). See License.md in the repository root for more information.

using System.Threading.Tasks;
using NUnit.Framework;

namespace ClangSharp.UnitTests;

/// <summary>
/// Regression test for https://github.com/dotnet/ClangSharp/issues/555.
/// When <c>generate-file-scoped-namespaces</c> is used with a single output file, no namespace
/// opening brace is emitted, so no closing brace must be emitted either.
/// </summary>
[Platform("win")]
public sealed class FileScopedNamespaceTest : PInvokeGeneratorTest
{
[Test]
public Task MethodClassHasNoTrailingBrace()
{
var inputContents = @"extern ""C"" void MyFunction();
";

var expectedOutputContents = @"using System.Runtime.InteropServices;

namespace ClangSharp.Test;

public static partial class Methods
{
[DllImport(""ClangSharpPInvokeGenerator"", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern void MyFunction();
}
";

return ValidateGeneratedCSharpLatestWindowsBindingsAsync(inputContents, expectedOutputContents, PInvokeGeneratorConfigurationOptions.GenerateFileScopedNamespaces);
}

[Test]
public Task StructHasNoTrailingBrace()
{
var inputContents = @"struct Point
{
int x;
int y;
};
";

var expectedOutputContents = @"namespace ClangSharp.Test;

public partial struct Point
{
public int x;

public int y;
}
";

return ValidateGeneratedCSharpLatestWindowsBindingsAsync(inputContents, expectedOutputContents, PInvokeGeneratorConfigurationOptions.GenerateFileScopedNamespaces);
}
}
Loading