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
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace ClangSharp.Abstractions
// Copyright (c) Microsoft and Contributors. All rights reserved. Licensed under the University of Illinois/NCSA Open Source License. See LICENSE.txt in the project root for license information.

namespace ClangSharp.Abstractions
{
public enum AccessSpecifier : byte
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace ClangSharp.Abstractions
// Copyright (c) Microsoft and Contributors. All rights reserved. Licensed under the University of Illinois/NCSA Open Source License. See LICENSE.txt in the project root for license information.

namespace ClangSharp.Abstractions
{
internal struct ConstantDesc
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Copyright (c) Microsoft and Contributors. All rights reserved. Licensed under the University of Illinois/NCSA Open Source License. See LICENSE.txt in the project root for license information.

using System;

namespace ClangSharp.Abstractions
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace ClangSharp.Abstractions
// Copyright (c) Microsoft and Contributors. All rights reserved. Licensed under the University of Illinois/NCSA Open Source License. See LICENSE.txt in the project root for license information.

namespace ClangSharp.Abstractions
{
internal struct FieldDesc
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
// Copyright (c) Microsoft and Contributors. All rights reserved. Licensed under the University of Illinois/NCSA Open Source License. See LICENSE.txt in the project root for license information.

using System;
using System.Runtime.InteropServices;

namespace ClangSharp.Abstractions
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Copyright (c) Microsoft and Contributors. All rights reserved. Licensed under the University of Illinois/NCSA Open Source License. See LICENSE.txt in the project root for license information.

namespace ClangSharp.Abstractions
{
internal partial interface IOutputBuilder
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
// Copyright (c) Microsoft and Contributors. All rights reserved. Licensed under the University of Illinois/NCSA Open Source License. See LICENSE.txt in the project root for license information.

using System;
using System.Collections.Generic;

namespace ClangSharp.Abstractions
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
// Copyright (c) Microsoft and Contributors. All rights reserved. Licensed under the University of Illinois/NCSA Open Source License. See LICENSE.txt in the project root for license information.

using System;
using System.Runtime.InteropServices;

namespace ClangSharp.Abstractions
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
// Copyright (c) Microsoft and Contributors. All rights reserved. Licensed under the University of Illinois/NCSA Open Source License. See LICENSE.txt in the project root for license information.

using System;

namespace ClangSharp.Abstractions
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace ClangSharp.CSharp
// Copyright (c) Microsoft and Contributors. All rights reserved. Licensed under the University of Illinois/NCSA Open Source License. See LICENSE.txt in the project root for license information.

namespace ClangSharp.CSharp
{
internal partial class CSharpOutputBuilder
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Copyright (c) Microsoft and Contributors. All rights reserved. Licensed under the University of Illinois/NCSA Open Source License. See LICENSE.txt in the project root for license information.

using System.Diagnostics;
using System.Globalization;
using System.Runtime.InteropServices;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public CSharpOutputBuilder(string name, string indentationString = DefaultIndent

public IEnumerable<string> Contents => _contents;

public bool HasPendingLine => _currentLine.Length > 0;

public string IndentationString => _indentationString;

public bool IsTestOutput => _isTestOutput;
Expand Down Expand Up @@ -146,7 +148,7 @@ public void WriteNewlineIfNeeded()

public void WritePendingLine()
{
if (_currentLine.Length > 0)
if (HasPendingLine)
{
WriteNewline();
}
Expand Down
2 changes: 2 additions & 0 deletions sources/ClangSharp.PInvokeGenerator/CSharp/MarkerMode.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Copyright (c) Microsoft and Contributors. All rights reserved. Licensed under the University of Illinois/NCSA Open Source License. See LICENSE.txt in the project root for license information.

namespace ClangSharp.CSharp
{
internal enum MarkerMode
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
// Copyright (c) Microsoft and Contributors. All rights reserved. Licensed under the University of Illinois/NCSA Open Source License. See LICENSE.txt in the project root for license information.

using System;
using System.Runtime.InteropServices;
using ClangSharp.Abstractions;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@ private void VisitFunctionDecl(FunctionDecl functionDecl)

var needsReturnFixup = isVirtual && NeedsReturnFixup(cxxMethodDecl);

if (!(functionDecl is CXXConstructorDecl))
if ((functionDecl is not CXXConstructorDecl) || (_config.OutputMode == PInvokeGeneratorOutputMode.Xml))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess we should add a boolean to WriteReturnType indicating whether it's for a ctor or not to avoid this being baked into VisitDecl, that does nothing if that boolean is true on the C# backend.

{
_outputBuilder.WriteReturnType(needsReturnFixup ? $"{returnTypeName}*" : returnTypeName);
}
Expand Down
88 changes: 61 additions & 27 deletions sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,19 @@ public void Close()

var usingDirectives = Enumerable.Empty<string>();
var staticUsingDirectives = Enumerable.Empty<string>();
var hasAnyContents = false;

foreach (var outputBuilder in _outputBuilderFactory.OutputBuilders)
{
if (outputBuilder is CSharpOutputBuilder csharpOutputBuilder)
{
usingDirectives = usingDirectives.Concat(csharpOutputBuilder.UsingDirectives);
staticUsingDirectives = staticUsingDirectives.Concat(csharpOutputBuilder.StaticUsingDirectives);
hasAnyContents = csharpOutputBuilder.Contents.Any();
}
else if (outputBuilder is XmlOutputBuilder xmlOutputBuilder)
{
hasAnyContents = xmlOutputBuilder.Contents.Any();
}
}

Expand All @@ -131,15 +137,20 @@ public void Close()

usingDirectives = usingDirectives.Concat(staticUsingDirectives);

if (usingDirectives.Any())
if (hasAnyContents)
{
using var sw = new StreamWriter(stream, defaultStreamWriterEncoding, DefaultStreamWriterBufferSize, leaveStreamOpen);
sw.NewLine = "\n";

if (_config.OutputMode == PInvokeGeneratorOutputMode.CSharp)
{
sw.NewLine = "\n";
if (_config.OutputMode == PInvokeGeneratorOutputMode.CSharp)
if (_config.HeaderText != string.Empty)
{
sw.Write(_config.HeaderText);
sw.WriteLine(_config.HeaderText);
}

if (usingDirectives.Any())
{
foreach (var usingDirective in usingDirectives)
{
sw.Write("using ");
Expand All @@ -149,18 +160,26 @@ public void Close()

sw.WriteLine();
}
else if (_config.OutputMode == PInvokeGeneratorOutputMode.Xml)
}
else if (_config.OutputMode == PInvokeGeneratorOutputMode.Xml)
{
sw.WriteLine("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\" ?>");
sw.WriteLine("<bindings>");

if (_config.HeaderText != string.Empty)
{
sw.WriteLine("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\" ?>");
sw.WriteLine("<bindings>");
sw.WriteLine(" <comment>");
foreach (var ln in _config.HeaderText.Split('\n'))
sw.WriteLine(" <comment>");

if (_config.HeaderText != string.Empty)
{
sw.Write(" ");
sw.WriteLine(ln);
foreach (var ln in _config.HeaderText.Split('\n'))
{
sw.Write(" ");
sw.WriteLine(ln);
}
}

sw.WriteLine(" </comment>");
sw.WriteLine(" </comment>");
}
}
}
Expand Down Expand Up @@ -198,7 +217,15 @@ public void Close()
using var sw = new StreamWriter(stream, defaultStreamWriterEncoding, DefaultStreamWriterBufferSize, leaveStreamOpen);
sw.NewLine = "\n";

sw.WriteLine('}');
if (_config.OutputMode == PInvokeGeneratorOutputMode.CSharp)
{
sw.WriteLine('}');
}
else if (_config.OutputMode == PInvokeGeneratorOutputMode.Xml)
{
sw.WriteLine(" </namespace>");
sw.WriteLine("</bindings>");
}
}

_context.Clear();
Expand Down Expand Up @@ -349,18 +376,23 @@ private void CloseOutputBuilder(Stream stream, IOutputBuilder outputBuilder, boo
sw.WriteLine();
}
}
else if (outputBuilder is XmlOutputBuilder xmlOutputBuilder)
else if ((outputBuilder is XmlOutputBuilder xmlOutputBuilder) && xmlOutputBuilder.Contents.Any())
{
sw.WriteLine("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\" ?>");
sw.WriteLine("<bindings>");
sw.WriteLine(" <comment>");
foreach (var ln in _config.HeaderText.Split('\n'))
{
sw.Write(" ");
sw.WriteLine(ln);
}
sw.WriteLine("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\" ?>");
sw.WriteLine("<bindings>");

if (_config.HeaderText != string.Empty)
{
sw.WriteLine(" <comment>");

foreach (var ln in _config.HeaderText.Split('\n'))
{
sw.Write(" ");
sw.WriteLine(ln);
}

sw.WriteLine(" </comment>");
sw.WriteLine(" </comment>");
}
}
}

Expand Down Expand Up @@ -440,23 +472,26 @@ void ForCSharp(CSharpOutputBuilder csharpOutputBuilder)

void ForXml(XmlOutputBuilder xmlOutputBuilder)
{
const string indent = " ";
const string indent = " ";
var indentationString = indent;

if (emitNamespaceDeclaration)
{
sw.Write(indentationString);
sw.Write("<namespace name=\"");
sw.Write(Config.Namespace);
sw.WriteLine("\">");
indentationString += indent;
}

indentationString += indent;

if (isMethodClass)
{
sw.Write(indentationString);
sw.Write("<class name=\"");
sw.Write(Config.MethodClassName);
sw.Write("\" access=\"public\" static=\"true\"");

if (_isMethodClassUnsafe)
{
sw.Write(" unsafe=\"true\"");
Expand Down Expand Up @@ -491,9 +526,8 @@ void ForXml(XmlOutputBuilder xmlOutputBuilder)
indentationString = indentationString.Substring(0, indentationString.Length - indent.Length);
sw.Write(indentationString);
sw.WriteLine("</namespace>");
sw.WriteLine("</bindings>");
}

sw.WriteLine("</bindings>");
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Copyright (c) Microsoft and Contributors. All rights reserved. Licensed under the University of Illinois/NCSA Open Source License. See LICENSE.txt in the project root for license information.

namespace ClangSharp.XML
{
internal partial class XmlOutputBuilder
Expand Down
Loading