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
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
<PackageValidationBaselineVersion>17.0.0</PackageValidationBaselineVersion>
<Product>ClangSharp</Product>
<RootNamespace>ClangSharp</RootNamespace>
<VersionPrefix>17.0.0</VersionPrefix>
<VersionPrefix>17.0.1</VersionPrefix>
<VersionSuffix Condition="'$(EXCLUDE_SUFFIX_FROM_VERSION)' != 'true'">rc1</VersionSuffix>
<VersionSuffix Condition="'$(GITHUB_EVENT_NAME)' == 'pull_request'">pr</VersionSuffix>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public void WriteDivider(bool force = false)

public void WriteIid(string name, Guid value)
{
if (_config.GenerateUnmanagedConstants)
if (_generator.Config.GenerateUnmanagedConstants)
{
AddUsingDirective("System");
AddUsingDirective("System.Diagnostics");
Expand All @@ -89,7 +89,7 @@ public void WriteIid(string name, Guid value)

WriteIndented("ReadOnlySpan<byte> data = ");

if (_config.GenerateLatestCode)
if (_generator.Config.GenerateLatestCode)
{
WriteLine('[');
}
Expand Down Expand Up @@ -124,7 +124,7 @@ public void WriteIid(string name, Guid value)
WriteNewline();
DecreaseIndentation();

if (_config.GenerateLatestCode)
if (_generator.Config.GenerateLatestCode)
{
WriteIndented(']');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public void EndUnchecked()

public void BeginValue(in ValueDesc desc)
{
if (_config.GenerateDocIncludes && (desc.Kind == ValueKind.Enumerator))
if (_generator.Config.GenerateDocIncludes && (desc.Kind == ValueKind.Enumerator))
{
WriteIndented("/// <include file='");
Write(desc.ParentName);
Expand Down Expand Up @@ -63,7 +63,7 @@ public void BeginValue(in ValueDesc desc)
}
else if (desc.IsConstant)
{
if (_config.GenerateUnmanagedConstants && (desc.Kind != ValueKind.Enumerator))
if (_generator.Config.GenerateUnmanagedConstants && (desc.Kind != ValueKind.Enumerator))
{
if (desc.IsCopy)
{
Expand All @@ -88,7 +88,7 @@ public void BeginValue(in ValueDesc desc)
{
Write(" static ");

if (!_config.GenerateUnmanagedConstants)
if (!_generator.Config.GenerateUnmanagedConstants)
{
Write("readonly ");
}
Expand All @@ -111,7 +111,7 @@ public void BeginValue(in ValueDesc desc)
Write(GetAccessSpecifierString(desc.AccessSpecifier, isNested: true));
Write(" static ");

if (_config.GenerateUnmanagedConstants && desc.IsConstant)
if (_generator.Config.GenerateUnmanagedConstants && desc.IsConstant)
{
if (desc.IsArray)
{
Expand Down Expand Up @@ -177,7 +177,7 @@ public void BeginValue(in ValueDesc desc)
{
WriteNewline();
WriteBlockStart();
BeginGetter(desc.IsConstant && _config.GenerateAggressiveInlining, isReadOnly: false);
BeginGetter(desc.IsConstant && _generator.Config.GenerateAggressiveInlining, isReadOnly: false);
}
else
{
Expand All @@ -203,7 +203,7 @@ public void EndValue(in ValueDesc desc)
{
WriteLine(',');

if (_config.GenerateDocIncludes)
if (_generator.Config.GenerateDocIncludes)
{
NeedsNewline = true;
}
Expand All @@ -214,7 +214,7 @@ public void EndValue(in ValueDesc desc)
{
if (desc.IsConstant)
{
if (_config.GenerateUnmanagedConstants && !desc.IsCopy)
if (_generator.Config.GenerateUnmanagedConstants && !desc.IsCopy)
{
EndGetter();
WriteBlockEnd();
Expand Down Expand Up @@ -244,7 +244,7 @@ public void EndValue(in ValueDesc desc)

public void BeginEnum(in EnumDesc desc)
{
if (_config.GenerateDocIncludes)
if (_generator.Config.GenerateDocIncludes)
{
WriteIndented("/// <include file='");
Write(desc.EscapedName);
Expand Down Expand Up @@ -283,7 +283,7 @@ public void BeginEnum(in EnumDesc desc)

public void BeginField(in FieldDesc desc)
{
if (_config.GenerateDocIncludes && !string.IsNullOrWhiteSpace(desc.ParentName))
if (_generator.Config.GenerateDocIncludes && !string.IsNullOrWhiteSpace(desc.ParentName))
{
WriteIndented("/// <include file='");
Write(desc.ParentName);
Expand Down Expand Up @@ -327,7 +327,7 @@ public void BeginField(in FieldDesc desc)

public void WriteFixedCountField(string typeName, string escapedName, string fixedName, string count)
{
if (PInvokeGenerator.IsSupportedFixedSizedBufferType(typeName))
if (_generator.IsSupportedFixedSizedBufferType(typeName))
{
Write("fixed ");
Write(typeName);
Expand Down Expand Up @@ -364,7 +364,7 @@ public void EndField(in FieldDesc desc)

public void BeginFunctionOrDelegate(in FunctionOrDelegateDesc desc, ref bool isMethodClassUnsafe)
{
if (_config.GenerateDocIncludes && !string.IsNullOrEmpty(desc.ParentName))
if (_generator.Config.GenerateDocIncludes && !string.IsNullOrEmpty(desc.ParentName))
{
if (desc.IsInherited)
{
Expand Down Expand Up @@ -430,7 +430,7 @@ public void BeginFunctionOrDelegate(in FunctionOrDelegateDesc desc, ref bool isM

Write("ExactSpelling = true");

if (desc.SetLastError && !_config.GenerateSetsLastSystemErrorAttribute)
if (desc.SetLastError && !_generator.Config.GenerateSetsLastSystemErrorAttribute)
{
Write(", SetLastError = true");
}
Expand All @@ -443,7 +443,7 @@ public void BeginFunctionOrDelegate(in FunctionOrDelegateDesc desc, ref bool isM
WriteSourceLocation(location, false);
}

if (desc.SetLastError && _config.GenerateSetsLastSystemErrorAttribute)
if (desc.SetLastError && _generator.Config.GenerateSetsLastSystemErrorAttribute)
{
WriteIndentedLine("[SetsLastSystemError]");
}
Expand Down Expand Up @@ -732,7 +732,7 @@ public void EndFunctionOrDelegate(in FunctionOrDelegateDesc desc)

public void BeginStruct(in StructDesc desc)
{
if (_config.GenerateDocIncludes)
if (_generator.Config.GenerateDocIncludes)
{
WriteIndented("/// <include file='");
Write(desc.EscapedName);
Expand Down Expand Up @@ -793,7 +793,7 @@ public void BeginStruct(in StructDesc desc)
Write("partial struct ");
Write(desc.EscapedName);

if (_config.GenerateMarkerInterfaces)
if (_generator.Config.GenerateMarkerInterfaces)
{
if (desc.HasVtbl)
{
Expand All @@ -802,7 +802,7 @@ public void BeginStruct(in StructDesc desc)
Write(".Interface");
}

if ((desc.Uuid is not null) && _config.GenerateGuidMember && _config.GenerateLatestCode)
if ((desc.Uuid is not null) && _generator.Config.GenerateGuidMember && _generator.Config.GenerateLatestCode)
{
Write(desc.HasVtbl ? ", " : " : ");
Write("INativeGuid");
Expand Down Expand Up @@ -840,7 +840,7 @@ public void BeginExplicitVtbl()
{
WriteIndented("public partial struct Vtbl");

if (_config.GenerateMarkerInterfaces && !_config.ExcludeFnptrCodegen)
if (_generator.Config.GenerateMarkerInterfaces && !_generator.Config.ExcludeFnptrCodegen)
{
WriteLine("<TSelf>");
IncreaseIndentation();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@

namespace ClangSharp.CSharp;

internal sealed partial class CSharpOutputBuilder(string name, PInvokeGeneratorConfiguration config, string indentationString = CSharpOutputBuilder.DefaultIndentationString,
internal sealed partial class CSharpOutputBuilder(string name, PInvokeGenerator generator, string indentationString = CSharpOutputBuilder.DefaultIndentationString,
bool isTestOutput = false, MarkerMode markerMode = MarkerMode.None,
bool writeSourceLocation = false)
{
public const string DefaultIndentationString = " ";

private readonly string _name = name;
private readonly PInvokeGeneratorConfiguration _config = config;
private readonly PInvokeGenerator _generator = generator;
private readonly List<string> _contents = [];
private readonly StringBuilder _currentLine = new StringBuilder();
private readonly SortedSet<string> _usingDirectives = [];
Expand Down Expand Up @@ -301,7 +301,7 @@ private void AddVtblIndexAttribute(long vtblIndex, string? prefix = null, string

private void AddNativeTypeNameAttribute(string nativeTypeName, string? prefix = null, string? postfix = null, string? attributePrefix = null)
{
foreach (var entry in _config.NativeTypeNamesToStrip)
foreach (var entry in _generator.Config.NativeTypeNamesToStrip)
{
nativeTypeName = nativeTypeName.Replace(entry, "", StringComparison.Ordinal);
}
Expand Down
16 changes: 8 additions & 8 deletions sources/ClangSharp.PInvokeGenerator/OutputBuilderFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ namespace ClangSharp;

internal sealed class OutputBuilderFactory
{
private readonly PInvokeGeneratorConfiguration _config;
private readonly PInvokeGenerator _generator;
private readonly bool _writeSourceLocation;
private readonly Dictionary<string, IOutputBuilder> _outputBuilders;

public OutputBuilderFactory(PInvokeGeneratorConfiguration config)
public OutputBuilderFactory(PInvokeGenerator generator)
{
_config = config;
_writeSourceLocation = config.GenerateSourceLocationAttribute;
_generator = generator;
_writeSourceLocation = generator.Config.GenerateSourceLocationAttribute;
_outputBuilders = [];
}

Expand All @@ -33,10 +33,10 @@ public IOutputBuilder Create(string name)
throw new ArgumentNullException(nameof(name));
}

var outputBuilder = _config.OutputMode switch
var outputBuilder = _generator.Config.OutputMode switch
{
PInvokeGeneratorOutputMode.CSharp => (IOutputBuilder) new CSharpOutputBuilder(name, _config, writeSourceLocation: _writeSourceLocation),
PInvokeGeneratorOutputMode.Xml => new XmlOutputBuilder(name, _config),
PInvokeGeneratorOutputMode.CSharp => (IOutputBuilder) new CSharpOutputBuilder(name, _generator, writeSourceLocation: _writeSourceLocation),
PInvokeGeneratorOutputMode.Xml => new XmlOutputBuilder(name, _generator),
_ => throw new InvalidOperationException()
};

Expand All @@ -51,7 +51,7 @@ public CSharpOutputBuilder CreateTests(string name)
throw new ArgumentNullException(nameof(name));
}

var outputBuilder = new CSharpOutputBuilder(name, _config, isTestOutput: true, writeSourceLocation: _writeSourceLocation);
var outputBuilder = new CSharpOutputBuilder(name, _generator, isTestOutput: true, writeSourceLocation: _writeSourceLocation);

_outputBuilders.Add(name, outputBuilder);
return outputBuilder;
Expand Down
18 changes: 10 additions & 8 deletions sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitDecl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1087,19 +1087,18 @@ private void VisitIndirectFieldDecl(IndirectFieldDecl indirectFieldDecl)
{
code.Write("[0], ");
code.Write(Math.Max(IsType<ConstantArrayType>(indirectFieldDecl, type, out var constantArrayType) ? constantArrayType.Size : 0, 1));
code.Write(')');
}
else
else if (!_config.GenerateLatestCode)
{
code.Write(".AsSpan(");
code.Write(".AsSpan()");
}
}
else
{
code.Write(", 1)");
code.Write(", 1))");
}

code.Write(')');

if (isIndirectPointerField)
{
code.Write('.');
Expand Down Expand Up @@ -3046,14 +3045,17 @@ void VisitConstantOrIncompleteArrayFieldDecl(RecordDecl recordDecl, FieldDecl co
code.AddUsingDirective("System");
code.AddUsingDirective("System.Runtime.InteropServices");

code.WriteIndented("return ref AsSpan(");
code.WriteIndented("return ref ");

if (arraySize == 1)
{
code.Write("int.MaxValue");
code.Write("Unsafe.Add(ref e0, index)");
}
else
{
code.Write("AsSpan()[index]");
}

code.Write(")[index]");
code.WriteSemicolon();
code.WriteNewline();
_outputBuilder.EndCSharpCode(code);
Expand Down
11 changes: 6 additions & 5 deletions sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,9 @@ public PInvokeGenerator(PInvokeGeneratorConfiguration config, Func<string, Strea
throw new InvalidOperationException($"Invalid libClang version. Returned string '{clangSharpVersion}' does not contain '{ExpectedClangSharpVersion}'");
}

_config = config;
_index = CXIndex.Create();
_outputBuilderFactory = new OutputBuilderFactory(config);
_outputBuilderFactory = new OutputBuilderFactory(this);
_outputStreamFactory = outputStreamFactory ?? ((path) => {
var directoryPath = Path.GetDirectoryName(path) ?? "";
_ = Directory.CreateDirectory(directoryPath);
Expand All @@ -138,7 +139,6 @@ public PInvokeGenerator(PInvokeGeneratorConfiguration config, Func<string, Strea
_visitedFiles = [];
_diagnostics = [];
_context = new LinkedList<(Cursor, object?)>();
_config = config;
_uuidsToGenerate = [];
_generatedUuids = [];
_cursorNames = [];
Expand Down Expand Up @@ -3690,7 +3690,7 @@ private string GetTypeName(Cursor? cursor, Cursor? context, Type rootType, Type
case CXTemplateArgumentKind_Expression:
{
var oldOutputBuilder = _outputBuilder;
_outputBuilder = new CSharpOutputBuilder("ClangSharp_TemplateSpecializationType_AsExpr", _config);
_outputBuilder = new CSharpOutputBuilder("ClangSharp_TemplateSpecializationType_AsExpr", this);

Visit(arg.AsExpr);
typeName = _outputBuilder.ToString() ?? "";
Expand Down Expand Up @@ -5326,7 +5326,7 @@ private bool IsTypeVoid(Cursor? cursor, Type type)
=> IsType<BuiltinType>(cursor, type, out var builtinType)
&& (builtinType.Kind == CXType_Void);

internal static bool IsSupportedFixedSizedBufferType(string typeName)
internal bool IsSupportedFixedSizedBufferType(string typeName)
{
switch (typeName)
{
Expand All @@ -5343,7 +5343,8 @@ internal static bool IsSupportedFixedSizedBufferType(string typeName)
case "uint":
case "ulong":
{
return true;
// We want to prefer InlineArray in modern code, as it is safer and supports more features
return !Config.GenerateLatestCode;
}

default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ public void EmitSystemSupport()
public CSharpOutputBuilder BeginCSharpCode()
{
_ = _sb.Append("<code>");
return new CSharpOutputBuilder("__Internal", _config, markerMode: MarkerMode.Xml);
return new CSharpOutputBuilder("__Internal", _generator, markerMode: MarkerMode.Xml);
}

public void EndCSharpCode(CSharpOutputBuilder output)
Expand Down Expand Up @@ -396,7 +396,7 @@ private void AddNativeTypeNameAttribute(string? nativeTypeName)
return;
}

foreach (var entry in _config.NativeTypeNamesToStrip)
foreach (var entry in _generator.Config.NativeTypeNamesToStrip)
{
nativeTypeName = nativeTypeName.Replace(entry, "", StringComparison.Ordinal);
}
Expand Down
4 changes: 2 additions & 2 deletions sources/ClangSharp.PInvokeGenerator/XML/XmlOutputBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@

namespace ClangSharp.XML;

internal partial class XmlOutputBuilder(string name, PInvokeGeneratorConfiguration config) : IOutputBuilder
internal partial class XmlOutputBuilder(string name, PInvokeGenerator generator) : IOutputBuilder
{
private readonly PInvokeGeneratorConfiguration _config = config;
private readonly PInvokeGenerator _generator = generator;

public string Name { get; } = name;
public string Extension { get; } = ".xml";
Expand Down
Loading