Skip to content

Commit 1ad5e9d

Browse files
Merge pull request #630 from tannergooding/readonly
Few more fixes to ensure existing codegen doesn't regress
2 parents db3c891 + 9d58e13 commit 1ad5e9d

File tree

8 files changed

+76
-21
lines changed

8 files changed

+76
-21
lines changed

Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
<PackageValidationBaselineVersion>17.0.0</PackageValidationBaselineVersion>
4848
<Product>ClangSharp</Product>
4949
<RootNamespace>ClangSharp</RootNamespace>
50-
<VersionPrefix>20.1.2.2</VersionPrefix>
50+
<VersionPrefix>20.1.2.3</VersionPrefix>
5151
<VersionSuffix Condition="'$(PACKAGE_PUBLISH_MODE)' != 'stable'">rc1</VersionSuffix>
5252
<VersionSuffix Condition="'$(GITHUB_EVENT_NAME)' == 'pull_request'">pr</VersionSuffix>
5353
</PropertyGroup>

sources/ClangSharp.PInvokeGenerator/CSharp/CSharpOutputBuilder.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,31 @@ public void WriteIndentedLine<T>(T value)
104104
WriteLine(value);
105105
}
106106

107+
public void WriteLabel(string name)
108+
{
109+
if (_currentLine.Length >= _indentationString.Length)
110+
{
111+
var match = true;
112+
113+
for (var i = 0; i < _indentationString.Length; i++)
114+
{
115+
if (_currentLine[_currentLine.Length - i - 1] != _indentationString[_indentationString.Length - 1 - i])
116+
{
117+
match = false;
118+
break;
119+
}
120+
}
121+
122+
if (match)
123+
{
124+
_ = _currentLine.Remove(_currentLine.Length - _indentationString.Length, _indentationString.Length);
125+
}
126+
}
127+
128+
Write(name);
129+
WriteLine(':');
130+
}
131+
107132
public void WriteLine<T>(T value)
108133
{
109134
Write(value);

sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitDecl.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,9 @@
77
using System.Linq;
88
using System.Runtime.InteropServices;
99
using System.Text;
10-
using System.Text.RegularExpressions;
1110
using ClangSharp.Abstractions;
1211
using ClangSharp.CSharp;
1312
using static ClangSharp.Interop.CX_CastKind;
14-
using static ClangSharp.Interop.CX_CharacterKind;
1513
using static ClangSharp.Interop.CX_DeclKind;
1614
using static ClangSharp.Interop.CX_StmtClass;
1715
using static ClangSharp.Interop.CX_StorageClass;
@@ -607,7 +605,7 @@ private void VisitFunctionDecl(FunctionDecl functionDecl)
607605
IsCxx = cxxMethodDecl is not null,
608606
IsStatic = isDllImport || (cxxMethodDecl is null) || cxxMethodDecl.IsStatic,
609607
NeedsNewKeyword = NeedsNewKeyword(escapedName, functionDecl.Parameters),
610-
IsReadOnly = (cxxMethodDecl is not null) && cxxMethodDecl.IsConst,
608+
IsReadOnly = IsReadonly(cxxMethodDecl),
611609
IsUnsafe = IsUnsafe(functionDecl),
612610
IsCtxCxxRecord = cxxRecordDecl is not null,
613611
IsCxxRecordCtxUnsafe = cxxRecordDecl is not null && IsUnsafe(cxxRecordDecl),
@@ -2228,7 +2226,7 @@ void OutputVtblHelperMethod(CXXRecordDecl cxxRecordDecl, CXXMethodDecl cxxMethod
22282226
HasFnPtrCodeGen = !_config.ExcludeFnptrCodegen,
22292227
IsCtxCxxRecord = true,
22302228
IsCxxRecordCtxUnsafe = IsUnsafe(cxxRecordDecl),
2231-
IsReadOnly = cxxMethodDecl.IsConst,
2229+
IsReadOnly = IsReadonly(cxxMethodDecl),
22322230
IsUnsafe = true,
22332231
NeedsReturnFixup = needsReturnFixup,
22342232
ReturnType = returnTypeName,
@@ -2353,7 +2351,7 @@ void OutputVtblHelperMethod(CXXRecordDecl cxxRecordDecl, CXXMethodDecl cxxMethod
23532351
body.Write(escapedCXXRecordDeclName);
23542352
body.Write("*)Unsafe.AsPointer(");
23552353

2356-
if (cxxMethodDecl.IsConst)
2354+
if (IsReadonly(cxxMethodDecl))
23572355
{
23582356
if (!_config.GenerateLatestCode)
23592357
{

sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitStmt.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2070,9 +2070,7 @@ private void VisitIntegerLiteral(IntegerLiteral integerLiteral)
20702070
private void VisitLabelStmt(LabelStmt labelStmt)
20712071
{
20722072
var outputBuilder = StartCSharpCode();
2073-
2074-
outputBuilder.Write(labelStmt.Decl.Name);
2075-
outputBuilder.WriteLine(':');
2073+
outputBuilder.WriteLabel(labelStmt.Decl.Name);
20762074

20772075
outputBuilder.WriteIndentation();
20782076
Visit(labelStmt.SubStmt);

sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.cs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1412,7 +1412,7 @@ static void GenerateTransparentStructs(PInvokeGenerator generator, Stream? strea
14121412
sw.Write(indentString);
14131413
sw.WriteLine(" {");
14141414
sw.Write(indentString);
1415-
sw.Write(" if (obj is ");
1415+
sw.Write(" if (obj is ");
14161416
sw.Write(name);
14171417
sw.WriteLine(" other)");
14181418
sw.Write(indentString);
@@ -3258,29 +3258,22 @@ private static int GetAnonymousRecordIndex(RecordDecl recordDecl, RecordDecl par
32583258
index++;
32593259
}
32603260

3261-
if (parentRecordDecl.Parent is RecordDecl grandParentRecordDecl)
3261+
if (parentRecordDecl.Parent is RecordDecl grandparentRecordDecl)
32623262
{
3263-
var parentIndex = GetAnonymousRecordIndex(parentRecordDecl, grandParentRecordDecl);
3263+
var parentIndex = GetAnonymousRecordIndex(parentRecordDecl, grandparentRecordDecl);
32643264

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

3270-
if (parentIndex == index)
3270+
if ((parentIndex == index) || ((parentIndex > 0) && (index > parentIndex)))
32713271
{
32723272
if (recordDecl.IsUnion == parentRecordDecl.IsUnion)
32733273
{
32743274
index++;
32753275
}
32763276
}
3277-
else if ((parentIndex > 0) && (index > parentIndex))
3278-
{
3279-
if (recordDecl.IsUnion == parentRecordDecl.AnonymousRecords[parentIndex].IsUnion)
3280-
{
3281-
index++;
3282-
}
3283-
}
32843277
}
32853278
}
32863279
}
@@ -5402,6 +5395,15 @@ private bool IsPrevContextStmt<T>([MaybeNullWhen(false)] out T cursor, out objec
54025395
}
54035396
}
54045397

5398+
private bool IsReadonly(CXXMethodDecl? cxxMethodDecl)
5399+
{
5400+
if (cxxMethodDecl is not null)
5401+
{
5402+
return cxxMethodDecl.IsConst || HasRemapping(cxxMethodDecl, _config._withReadonlys, matchStar: true);
5403+
}
5404+
return false;
5405+
}
5406+
54055407
private static bool IsStmtAsWritten<T>(Cursor cursor, [MaybeNullWhen(false)] out T value, bool removeParens = false)
54065408
where T : Stmt
54075409
{

sources/ClangSharp.PInvokeGenerator/PInvokeGeneratorConfiguration.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public sealed class PInvokeGeneratorConfiguration
3333
private readonly HashSet<string> _nativeTypeNamesToStrip;
3434
private readonly HashSet<string> _withManualImports;
3535
private readonly HashSet<string> _traversalNames;
36+
internal readonly HashSet<string> _withReadonlys;
3637
internal readonly HashSet<string> _withSetLastErrors;
3738
internal readonly HashSet<string> _withSuppressGCTransitions;
3839

@@ -87,6 +88,7 @@ public PInvokeGeneratorConfiguration(string language, string languageStandard, s
8788
_nativeTypeNamesToStrip = new HashSet<string>(StringComparer.Ordinal);
8889
_withManualImports = new HashSet<string>(StringComparer.Ordinal);
8990
_traversalNames = new HashSet<string>(StringComparer.Ordinal);
91+
_withReadonlys = new HashSet<string>(QualifiedNameComparer.Default);
9092
_withSetLastErrors = new HashSet<string>(QualifiedNameComparer.Default);
9193
_withSuppressGCTransitions = new HashSet<string>(QualifiedNameComparer.Default);
9294

@@ -516,6 +518,20 @@ public IReadOnlyDictionary<string, string> WithNamespaces
516518
}
517519
}
518520

521+
[AllowNull]
522+
public IReadOnlyCollection<string> WithReadonlys
523+
{
524+
get
525+
{
526+
return _withReadonlys;
527+
}
528+
529+
init
530+
{
531+
AddRange(_withReadonlys, value);
532+
}
533+
}
534+
519535
[AllowNull]
520536
public IReadOnlyCollection<string> WithSetLastErrors
521537
{

sources/ClangSharpPInvokeGenerator/Program.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ internal static class Program
5454
private static readonly string[] s_withManualImportOptionAliases = ["--with-manual-import", "-wmi"];
5555
private static readonly string[] s_withNamespaceOptionAliases = ["--with-namespace", "-wn"];
5656
private static readonly string[] s_withPackingOptionAliases = ["--with-packing", "-wp"];
57+
private static readonly string[] s_withReadonlyOptionAliases = ["--with-readonly", "-wro"];
5758
private static readonly string[] s_withSetLastErrorOptionAliases = ["--with-setlasterror", "-wsle"];
5859
private static readonly string[] s_withSuppressGCTransitionOptionAliases = ["--with-suppressgctransition", "-wsgct"];
5960
private static readonly string[] s_withTransparentStructOptionAliases = ["--with-transparent-struct", "-wts"];
@@ -92,6 +93,7 @@ internal static class Program
9293
private static readonly Option<string[]> s_withManualImports = GetWithManualImportOption();
9394
private static readonly Option<string[]> s_withNamespaceNameValuePairs = GetWithNamespaceOption();
9495
private static readonly Option<string[]> s_withPackingNameValuePairs = GetWithPackingOption();
96+
private static readonly Option<string[]> s_withReadonlys = GetWithReadonlyOption();
9597
private static readonly Option<string[]> s_withSetLastErrors = GetWithSetLastErrorOption();
9698
private static readonly Option<string[]> s_withSuppressGCTransitions = GetWithSuppressGCTransitionOption();
9799
private static readonly Option<string[]> s_withTransparentStructNameValuePairs = GetWithTransparentStructOption();
@@ -255,6 +257,7 @@ public static void Run(InvocationContext context)
255257
var withLibraryPathNameValuePairs = context.ParseResult.GetValueForOption(s_withLibraryPathNameValuePairs) ?? [];
256258
var withManualImports = context.ParseResult.GetValueForOption(s_withManualImports) ?? [];
257259
var withNamespaceNameValuePairs = context.ParseResult.GetValueForOption(s_withNamespaceNameValuePairs) ?? [];
260+
var withReadonlys = context.ParseResult.GetValueForOption(s_withReadonlys) ?? [];
258261
var withSetLastErrors = context.ParseResult.GetValueForOption(s_withSetLastErrors) ?? [];
259262
var withSuppressGCTransitions = context.ParseResult.GetValueForOption(s_withSuppressGCTransitions) ?? [];
260263
var withTransparentStructNameValuePairs = context.ParseResult.GetValueForOption(s_withTransparentStructNameValuePairs) ?? [];
@@ -729,6 +732,7 @@ public static void Run(InvocationContext context)
729732
WithLibraryPaths = withLibraryPaths,
730733
WithManualImports = withManualImports,
731734
WithNamespaces = withNamespaces,
735+
WithReadonlys = withReadonlys,
732736
WithSetLastErrors = withSetLastErrors,
733737
WithSuppressGCTransitions = withSuppressGCTransitions,
734738
WithTransparentStructs = withTransparentStructs,
@@ -1202,6 +1206,7 @@ private static RootCommand GetRootCommand()
12021206
s_withManualImports,
12031207
s_withNamespaceNameValuePairs,
12041208
s_withPackingNameValuePairs,
1209+
s_withReadonlys,
12051210
s_withSetLastErrors,
12061211
s_withSuppressGCTransitions,
12071212
s_withTransparentStructNameValuePairs,
@@ -1350,6 +1355,17 @@ private static Option<string[]> GetWithNamespaceOption()
13501355
};
13511356
}
13521357

1358+
private static Option<string[]> GetWithReadonlyOption()
1359+
{
1360+
return new Option<string[]>(
1361+
aliases: s_withReadonlyOptionAliases,
1362+
description: "Add the readonly modifier to a given instance method. Supports wildcards.",
1363+
getDefaultValue: Array.Empty<string>
1364+
) {
1365+
AllowMultipleArgumentsPerToken = true
1366+
};
1367+
}
1368+
13531369
private static Option<string[]> GetWithSetLastErrorOption()
13541370
{
13551371
return new Option<string[]>(

sources/ClangSharpPInvokeGenerator/Properties/launchSettings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"GenerateLocal": {
1313
"commandName": "Project",
1414
"commandLineArgs": "@generate.rsp",
15-
"workingDirectory": "D:\\repos\\terrafx.interop.windows\\generation\\DirectX\\um\\dcommon"
15+
"workingDirectory": "D:\\repos\\terrafx.interop.windows\\generation\\windows\\um\\winioctl"
1616
}
1717
}
1818
}

0 commit comments

Comments
 (0)