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>20.1.2.2</VersionPrefix>
<VersionPrefix>20.1.2.3</VersionPrefix>
<VersionSuffix Condition="'$(PACKAGE_PUBLISH_MODE)' != 'stable'">rc1</VersionSuffix>
<VersionSuffix Condition="'$(GITHUB_EVENT_NAME)' == 'pull_request'">pr</VersionSuffix>
</PropertyGroup>
Expand Down
25 changes: 25 additions & 0 deletions sources/ClangSharp.PInvokeGenerator/CSharp/CSharpOutputBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,31 @@ public void WriteIndentedLine<T>(T value)
WriteLine(value);
}

public void WriteLabel(string name)
{
if (_currentLine.Length >= _indentationString.Length)
{
var match = true;

for (var i = 0; i < _indentationString.Length; i++)
{
if (_currentLine[_currentLine.Length - i - 1] != _indentationString[_indentationString.Length - 1 - i])
{
match = false;
break;
}
}

if (match)
{
_ = _currentLine.Remove(_currentLine.Length - _indentationString.Length, _indentationString.Length);
}
}

Write(name);
WriteLine(':');
}

public void WriteLine<T>(T value)
{
Write(value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
using ClangSharp.Abstractions;
using ClangSharp.CSharp;
using static ClangSharp.Interop.CX_CastKind;
using static ClangSharp.Interop.CX_CharacterKind;
using static ClangSharp.Interop.CX_DeclKind;
using static ClangSharp.Interop.CX_StmtClass;
using static ClangSharp.Interop.CX_StorageClass;
Expand Down Expand Up @@ -607,7 +605,7 @@ private void VisitFunctionDecl(FunctionDecl functionDecl)
IsCxx = cxxMethodDecl is not null,
IsStatic = isDllImport || (cxxMethodDecl is null) || cxxMethodDecl.IsStatic,
NeedsNewKeyword = NeedsNewKeyword(escapedName, functionDecl.Parameters),
IsReadOnly = (cxxMethodDecl is not null) && cxxMethodDecl.IsConst,
IsReadOnly = IsReadonly(cxxMethodDecl),
IsUnsafe = IsUnsafe(functionDecl),
IsCtxCxxRecord = cxxRecordDecl is not null,
IsCxxRecordCtxUnsafe = cxxRecordDecl is not null && IsUnsafe(cxxRecordDecl),
Expand Down Expand Up @@ -2228,7 +2226,7 @@ void OutputVtblHelperMethod(CXXRecordDecl cxxRecordDecl, CXXMethodDecl cxxMethod
HasFnPtrCodeGen = !_config.ExcludeFnptrCodegen,
IsCtxCxxRecord = true,
IsCxxRecordCtxUnsafe = IsUnsafe(cxxRecordDecl),
IsReadOnly = cxxMethodDecl.IsConst,
IsReadOnly = IsReadonly(cxxMethodDecl),
IsUnsafe = true,
NeedsReturnFixup = needsReturnFixup,
ReturnType = returnTypeName,
Expand Down Expand Up @@ -2353,7 +2351,7 @@ void OutputVtblHelperMethod(CXXRecordDecl cxxRecordDecl, CXXMethodDecl cxxMethod
body.Write(escapedCXXRecordDeclName);
body.Write("*)Unsafe.AsPointer(");

if (cxxMethodDecl.IsConst)
if (IsReadonly(cxxMethodDecl))
{
if (!_config.GenerateLatestCode)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2070,9 +2070,7 @@ private void VisitIntegerLiteral(IntegerLiteral integerLiteral)
private void VisitLabelStmt(LabelStmt labelStmt)
{
var outputBuilder = StartCSharpCode();

outputBuilder.Write(labelStmt.Decl.Name);
outputBuilder.WriteLine(':');
outputBuilder.WriteLabel(labelStmt.Decl.Name);

outputBuilder.WriteIndentation();
Visit(labelStmt.SubStmt);
Expand Down
24 changes: 13 additions & 11 deletions sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1412,7 +1412,7 @@ static void GenerateTransparentStructs(PInvokeGenerator generator, Stream? strea
sw.Write(indentString);
sw.WriteLine(" {");
sw.Write(indentString);
sw.Write(" if (obj is ");
sw.Write(" if (obj is ");
sw.Write(name);
sw.WriteLine(" other)");
sw.Write(indentString);
Expand Down Expand Up @@ -3258,29 +3258,22 @@ private static int GetAnonymousRecordIndex(RecordDecl recordDecl, RecordDecl par
index++;
}

if (parentRecordDecl.Parent is RecordDecl grandParentRecordDecl)
if (parentRecordDecl.Parent is RecordDecl grandparentRecordDecl)
{
var parentIndex = GetAnonymousRecordIndex(parentRecordDecl, grandParentRecordDecl);
var parentIndex = GetAnonymousRecordIndex(parentRecordDecl, grandparentRecordDecl);

// We can't have the nested anonymous record have the same name as the parent
// so skip that index and just go one higher instead. This could still conflict
// with another anonymous record at a different level, but that is less likely
// and will still be unambiguous in total.

if (parentIndex == index)
if ((parentIndex == index) || ((parentIndex > 0) && (index > parentIndex)))
{
if (recordDecl.IsUnion == parentRecordDecl.IsUnion)
{
index++;
}
}
else if ((parentIndex > 0) && (index > parentIndex))
{
if (recordDecl.IsUnion == parentRecordDecl.AnonymousRecords[parentIndex].IsUnion)
{
index++;
}
}
}
}
}
Expand Down Expand Up @@ -5402,6 +5395,15 @@ private bool IsPrevContextStmt<T>([MaybeNullWhen(false)] out T cursor, out objec
}
}

private bool IsReadonly(CXXMethodDecl? cxxMethodDecl)
{
if (cxxMethodDecl is not null)
{
return cxxMethodDecl.IsConst || HasRemapping(cxxMethodDecl, _config._withReadonlys, matchStar: true);
}
return false;
}

private static bool IsStmtAsWritten<T>(Cursor cursor, [MaybeNullWhen(false)] out T value, bool removeParens = false)
where T : Stmt
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public sealed class PInvokeGeneratorConfiguration
private readonly HashSet<string> _nativeTypeNamesToStrip;
private readonly HashSet<string> _withManualImports;
private readonly HashSet<string> _traversalNames;
internal readonly HashSet<string> _withReadonlys;
internal readonly HashSet<string> _withSetLastErrors;
internal readonly HashSet<string> _withSuppressGCTransitions;

Expand Down Expand Up @@ -87,6 +88,7 @@ public PInvokeGeneratorConfiguration(string language, string languageStandard, s
_nativeTypeNamesToStrip = new HashSet<string>(StringComparer.Ordinal);
_withManualImports = new HashSet<string>(StringComparer.Ordinal);
_traversalNames = new HashSet<string>(StringComparer.Ordinal);
_withReadonlys = new HashSet<string>(QualifiedNameComparer.Default);
_withSetLastErrors = new HashSet<string>(QualifiedNameComparer.Default);
_withSuppressGCTransitions = new HashSet<string>(QualifiedNameComparer.Default);

Expand Down Expand Up @@ -516,6 +518,20 @@ public IReadOnlyDictionary<string, string> WithNamespaces
}
}

[AllowNull]
public IReadOnlyCollection<string> WithReadonlys
{
get
{
return _withReadonlys;
}

init
{
AddRange(_withReadonlys, value);
}
}

[AllowNull]
public IReadOnlyCollection<string> WithSetLastErrors
{
Expand Down
16 changes: 16 additions & 0 deletions sources/ClangSharpPInvokeGenerator/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ internal static class Program
private static readonly string[] s_withManualImportOptionAliases = ["--with-manual-import", "-wmi"];
private static readonly string[] s_withNamespaceOptionAliases = ["--with-namespace", "-wn"];
private static readonly string[] s_withPackingOptionAliases = ["--with-packing", "-wp"];
private static readonly string[] s_withReadonlyOptionAliases = ["--with-readonly", "-wro"];
private static readonly string[] s_withSetLastErrorOptionAliases = ["--with-setlasterror", "-wsle"];
private static readonly string[] s_withSuppressGCTransitionOptionAliases = ["--with-suppressgctransition", "-wsgct"];
private static readonly string[] s_withTransparentStructOptionAliases = ["--with-transparent-struct", "-wts"];
Expand Down Expand Up @@ -92,6 +93,7 @@ internal static class Program
private static readonly Option<string[]> s_withManualImports = GetWithManualImportOption();
private static readonly Option<string[]> s_withNamespaceNameValuePairs = GetWithNamespaceOption();
private static readonly Option<string[]> s_withPackingNameValuePairs = GetWithPackingOption();
private static readonly Option<string[]> s_withReadonlys = GetWithReadonlyOption();
private static readonly Option<string[]> s_withSetLastErrors = GetWithSetLastErrorOption();
private static readonly Option<string[]> s_withSuppressGCTransitions = GetWithSuppressGCTransitionOption();
private static readonly Option<string[]> s_withTransparentStructNameValuePairs = GetWithTransparentStructOption();
Expand Down Expand Up @@ -255,6 +257,7 @@ public static void Run(InvocationContext context)
var withLibraryPathNameValuePairs = context.ParseResult.GetValueForOption(s_withLibraryPathNameValuePairs) ?? [];
var withManualImports = context.ParseResult.GetValueForOption(s_withManualImports) ?? [];
var withNamespaceNameValuePairs = context.ParseResult.GetValueForOption(s_withNamespaceNameValuePairs) ?? [];
var withReadonlys = context.ParseResult.GetValueForOption(s_withReadonlys) ?? [];
var withSetLastErrors = context.ParseResult.GetValueForOption(s_withSetLastErrors) ?? [];
var withSuppressGCTransitions = context.ParseResult.GetValueForOption(s_withSuppressGCTransitions) ?? [];
var withTransparentStructNameValuePairs = context.ParseResult.GetValueForOption(s_withTransparentStructNameValuePairs) ?? [];
Expand Down Expand Up @@ -729,6 +732,7 @@ public static void Run(InvocationContext context)
WithLibraryPaths = withLibraryPaths,
WithManualImports = withManualImports,
WithNamespaces = withNamespaces,
WithReadonlys = withReadonlys,
WithSetLastErrors = withSetLastErrors,
WithSuppressGCTransitions = withSuppressGCTransitions,
WithTransparentStructs = withTransparentStructs,
Expand Down Expand Up @@ -1202,6 +1206,7 @@ private static RootCommand GetRootCommand()
s_withManualImports,
s_withNamespaceNameValuePairs,
s_withPackingNameValuePairs,
s_withReadonlys,
s_withSetLastErrors,
s_withSuppressGCTransitions,
s_withTransparentStructNameValuePairs,
Expand Down Expand Up @@ -1350,6 +1355,17 @@ private static Option<string[]> GetWithNamespaceOption()
};
}

private static Option<string[]> GetWithReadonlyOption()
{
return new Option<string[]>(
aliases: s_withReadonlyOptionAliases,
description: "Add the readonly modifier to a given instance method. Supports wildcards.",
getDefaultValue: Array.Empty<string>
) {
AllowMultipleArgumentsPerToken = true
};
}

private static Option<string[]> GetWithSetLastErrorOption()
{
return new Option<string[]>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"GenerateLocal": {
"commandName": "Project",
"commandLineArgs": "@generate.rsp",
"workingDirectory": "D:\\repos\\terrafx.interop.windows\\generation\\DirectX\\um\\dcommon"
"workingDirectory": "D:\\repos\\terrafx.interop.windows\\generation\\windows\\um\\winioctl"
}
}
}
Loading