Skip to content

Add DontUseUsingStaticsForGuidMember #594

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

Merged
merged 2 commits into from
Apr 4, 2025
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
36 changes: 32 additions & 4 deletions sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitDecl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1411,7 +1411,15 @@ private void VisitRecordDecl(RecordDecl recordDecl)
var className = GetClass(uuidName);

_testOutputBuilder.AddUsingDirective("System");
_testOutputBuilder.AddUsingDirective($"static {GetNamespace(className)}.{className}");

if (_config.DontUseUsingStaticsForGuidMember)
{
_testOutputBuilder.AddUsingDirective($"{GetNamespace(className)}");
}
else
{
_testOutputBuilder.AddUsingDirective($"static {GetNamespace(className)}.{className}");
}

_testOutputBuilder.WriteIndented("/// <summary>Validates that the <see cref=\"Guid\" /> of the <see cref=\"");
_testOutputBuilder.Write(escapedName);
Expand Down Expand Up @@ -1440,7 +1448,13 @@ private void VisitRecordDecl(RecordDecl recordDecl)
_testOutputBuilder.Write("Is.EqualTo(");
}

_testOutputBuilder.Write(uuidName);
var usableUuidName = uuidName;
if (_config.DontUseUsingStaticsForGuidMember)
{
usableUuidName = $"{className}.{usableUuidName}";
}

_testOutputBuilder.Write(usableUuidName);

if (_config.GenerateTestsNUnit)
{
Expand Down Expand Up @@ -1626,12 +1640,26 @@ private void VisitRecordDecl(RecordDecl recordDecl)
_outputBuilder.EmitUsingDirective("System");
_outputBuilder.EmitUsingDirective("System.Runtime.CompilerServices");

_outputBuilder.EmitUsingDirective($"static {GetNamespace(uuidClassName)}.{uuidClassName}");
if (_config.DontUseUsingStaticsForGuidMember)
{
_outputBuilder.EmitUsingDirective($"{GetNamespace(uuidClassName)}");
}
else
{
_outputBuilder.EmitUsingDirective($"static {GetNamespace(uuidClassName)}.{uuidClassName}");
}

var usableUuidName = uuidName;
if (_config.DontUseUsingStaticsForGuidMember)
{
usableUuidName = $"{uuidClassName}.{usableUuidName}";
}

_outputBuilder.BeginValue(in valueDesc);

var code = _outputBuilder.BeginCSharpCode();
code.Write("(Guid*)Unsafe.AsPointer(ref Unsafe.AsRef(in ");
code.Write(uuidName);
code.Write(usableUuidName);
code.Write("))");
_outputBuilder.EndCSharpCode(code);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,8 @@ public IReadOnlyCollection<string> ExcludedNames

public bool StripEnumMemberTypeName => _options.HasFlag(PInvokeGeneratorConfigurationOptions.StripEnumMemberTypeName);

public bool DontUseUsingStaticsForGuidMember => _options.HasFlag(PInvokeGeneratorConfigurationOptions.DontUseUsingStaticsForGuidMember);
Copy link
Member

Choose a reason for hiding this comment

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

There is some corresponding code in Program.cs of ClangSharpPInvokeGenerator that needs updating to actually set this flag (Silk.NET's ResponseFileHandler is a manually maintained subset of this Program.cs file)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

good catch, fixed it


public string HeaderText => _headerText;

[AllowNull]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,6 @@ public enum PInvokeGeneratorConfigurationOptions : long
GenerateGenericPointerWrapper = 1L << 38,

StripEnumMemberTypeName = 1L << 39,

DontUseUsingStaticsForGuidMember = 1L << 40,
}
7 changes: 7 additions & 0 deletions sources/ClangSharpPInvokeGenerator/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,13 @@ public static void Run(InvocationContext context)
break;
}

case "exclude-using-statics-for-guid-members":
case "dont-use-using-statics-for-guid-members":
{
configOptions |= PInvokeGeneratorConfigurationOptions.DontUseUsingStaticsForGuidMember;
break;
}

// VTBL Options

case "explicit-vtbls":
Expand Down