Skip to content
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

A bunch of inline array improvements #699

Merged
merged 1 commit into from
Oct 1, 2022
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: 2 additions & 0 deletions src/Microsoft.Windows.CsWin32/FastSyntaxFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ internal static ForStatementSyntax ForStatement(VariableDeclarationSyntax? decla

internal static LocalDeclarationStatementSyntax LocalDeclarationStatement(VariableDeclarationSyntax declaration) => SyntaxFactory.LocalDeclarationStatement(TokenList(), declaration, Semicolon);

internal static DeclarationExpressionSyntax DeclarationExpression(TypeSyntax type, VariableDesignationSyntax designation) => SyntaxFactory.DeclarationExpression(type, designation);

internal static VariableDeclaratorSyntax VariableDeclarator(SyntaxToken identifier) => SyntaxFactory.VariableDeclarator(identifier);

internal static VariableDeclarationSyntax VariableDeclaration(TypeSyntax type) => SyntaxFactory.VariableDeclaration(type.WithTrailingTrivia(TriviaList(Space)));
Expand Down
959 changes: 595 additions & 364 deletions src/Microsoft.Windows.CsWin32/Generator.cs

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions src/Microsoft.Windows.CsWin32/GeneratorOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ public record GeneratorOptions
/// <value>The default value is <see langword="true"/>.</value>
public bool AllowMarshaling { get; init; } = true;

/// <summary>
/// Gets a value indicating whether to generate APIs judged to be unnecessary or redundant given the target framework
/// because the project multi-targets to frameworks that need the APIs consistently for easier coding.
/// </summary>
public bool MultiTargetingFriendlyAPIs { get; init; }

/// <summary>
/// Gets a value indicating whether friendly overloads should use safe handles.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.8.0" />
<PackageReference Include="Microsoft.Windows.SDK.Win32Metadata" Version="$(MetadataVersion)" GeneratePathProperty="true" PrivateAssets="none" />
<PackageReference Include="Microsoft.Windows.SDK.Win32Docs" Version="$(ApiDocsVersion)" GeneratePathProperty="true" PrivateAssets="none" />
<PackageReference Include="System.Memory" Version="4.5.4" PrivateAssets="none" />
<PackageReference Include="System.Memory" Version="4.5.5" PrivateAssets="none" />
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="6.0.0" PrivateAssets="none" />
<PackageReference Include="System.Reflection.Metadata" Version="5.0.0" />
<PackageReference Include="System.Text.Encodings.Web" Version="4.7.2" />
<PackageReference Include="System.Text.Json" Version="4.7.2" />
Expand Down
16 changes: 14 additions & 2 deletions src/Microsoft.Windows.CsWin32/Microsoft.Windows.CsWin32.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,24 @@
<group targetFramework="net45">
<dependency id="Microsoft.Windows.SDK.Win32Metadata" version="$MetadataVersion$" include="buildTransitive" />
<dependency id="Microsoft.Windows.SDK.Win32Docs" version="$ApiDocsVersion$" include="buildTransitive" />
<dependency id="System.Memory" version="4.5.4" include="All" />
</group>
<group targetFramework="net461">
<dependency id="Microsoft.Windows.SDK.Win32Metadata" version="$MetadataVersion$" include="buildTransitive" />
<dependency id="Microsoft.Windows.SDK.Win32Docs" version="$ApiDocsVersion$" include="buildTransitive" />
<dependency id="System.Memory" version="4.5.5" include="All" />
<dependency id="System.Runtime.CompilerServices.Unsafe" version="6.0.0" include="All" />
</group>
<group targetFramework=".NETStandard1.1">
<dependency id="Microsoft.Windows.SDK.Win32Metadata" version="$MetadataVersion$" include="buildTransitive" />
<dependency id="Microsoft.Windows.SDK.Win32Docs" version="$ApiDocsVersion$" include="buildTransitive" />
<dependency id="System.Memory" version="4.5.4" include="All" />
<dependency id="System.Memory" version="4.5.5" include="All" />
<dependency id="System.Runtime.CompilerServices.Unsafe" version="5.0.0" include="All" />
</group>
<group targetFramework=".NETStandard2.0">
<dependency id="Microsoft.Windows.SDK.Win32Metadata" version="$MetadataVersion$" include="buildTransitive" />
<dependency id="Microsoft.Windows.SDK.Win32Docs" version="$ApiDocsVersion$" include="buildTransitive" />
<dependency id="System.Memory" version="4.5.5" include="All" />
<dependency id="System.Runtime.CompilerServices.Unsafe" version="6.0.0" include="All" />
</group>
</dependencies>
</metadata>
Expand Down
5 changes: 5 additions & 0 deletions src/Microsoft.Windows.CsWin32/settings.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@
"type": "boolean",
"default": true
},
"multiTargetingFriendlyAPIs": {
"description": "A value indicating whether to generate APIs judged to be unnecessary or redundant given the target framework. This is useful for multi-targeting projects that need a consistent set of APIs across target frameworks to avoid too many conditional compilation regions.",
"type": "boolean",
"default": false
},
"useSafeHandles": {
"description": "A value indicating whether friendly overloads should use safe handles.",
"type": "boolean",
Expand Down
27 changes: 14 additions & 13 deletions test/GenerationSandbox.Tests/BasicTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -199,12 +199,12 @@ public void FixedLengthInlineArrayAccess()
header.dwReserved.AsSpan()[1] = 3;
Assert.Equal(3u, header.dwReserved.AsSpan()[1]);
Assert.Equal(3u, header.dwReserved[1]);
Assert.Equal(3u, header.dwReserved._1);
Assert.Equal(3u, header.dwReserved[1]);
#endif

header.dwReserved.ItemRef(2) = 4;
header.dwReserved[2] = 4;
Assert.Equal(4u, header.dwReserved.ReadOnlyItemRef(2));
Assert.Equal(4u, header.dwReserved._2);
Assert.Equal(4u, header.dwReserved[2]);
}

[Fact]
Expand Down Expand Up @@ -243,26 +243,25 @@ public void GetAllWindowsInfo()
public void FixedCharArrayToString_Length()
{
Windows.Win32.System.RestartManager.RM_PROCESS_INFO info = default;
info.strServiceShortName._0 = 'H';
info.strServiceShortName._1 = 'i';
info.strServiceShortName[0] = 'H';
info.strServiceShortName[1] = 'i';
Assert.Equal("Hi", info.strServiceShortName.ToString(2));
Assert.Equal("Hi\0\0", info.strServiceShortName.ToString(4));
}

[Fact]
public unsafe void FixedCharArray_ToString()
public void FixedCharArray_ToString()
{
Windows.Win32.System.RestartManager.RM_PROCESS_INFO.__char_64 fixedCharArray = default;
Assert.Equal(string.Empty, fixedCharArray.ToString());
fixedCharArray._0 = 'H';
fixedCharArray[0] = 'H';
Assert.Equal("H", fixedCharArray.ToString());
fixedCharArray._1 = 'i';
fixedCharArray[1] = 'i';
Assert.Equal("Hi", fixedCharArray.ToString());

char* p = &fixedCharArray._0;
for (int i = 0; i < fixedCharArray.Length; i++)
{
*(p + i) = 'x';
fixedCharArray[i] = 'x';
}

Assert.Equal(new string('x', fixedCharArray.Length), fixedCharArray.ToString());
Expand All @@ -274,8 +273,8 @@ public void FixedLengthArray_ToArray()
Windows.Win32.System.RestartManager.RM_PROCESS_INFO.__char_64 fixedCharArray = default;
fixedCharArray = "hi";
char[] expected = new char[fixedCharArray.Length];
expected[0] = fixedCharArray._0;
expected[1] = fixedCharArray._1;
expected[0] = fixedCharArray[0];
expected[1] = fixedCharArray[1];
char[] actual = fixedCharArray.ToArray();
Assert.Equal<char>(expected, actual);

Expand Down Expand Up @@ -319,7 +318,9 @@ public void FixedLengthArray_Equals()
buffer[1] = 'i';
Assert.True(fixedCharArray.Equals(buffer));
Assert.True(fixedCharArray.Equals(buffer.AsSpan(0, 2)));
Assert.True(fixedCharArray.Equals(buffer.AsSpan(0, 3)));
Assert.False(fixedCharArray.Equals(buffer.AsSpan(0, 3)));

Assert.True(fixedCharArray.Equals(fixedCharArray.ToArray()));

// This should be false because the remainder of the fixed length array is non-default.
Assert.False(fixedCharArray.Equals(buffer.AsSpan(0, 1)));
Expand Down
3 changes: 2 additions & 1 deletion test/GenerationSandbox.Tests/GenerationSandbox.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
<!-- <PackageReference Include="Microsoft.Dia.Win32Metadata" Version="$(DiaMetadataVersion)" PrivateAssets="none" /> -->
<PackageReference Include="coverlet.msbuild" Version="3.1.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.1" />
<PackageReference Include="System.Memory" Version="4.5.4" />
<PackageReference Include="System.Memory" Version="4.5.5" />
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="6.0.0" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5" />
</ItemGroup>
Expand Down
1 change: 1 addition & 0 deletions test/GenerationSandbox.Tests/NativeMethods.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"$schema": "..\\..\\src\\Microsoft.Windows.CsWin32\\settings.schema.json",
"emitSingleFile": true,
"multiTargetingFriendlyAPIs": true,
"comInterop": {
"preserveSigMethods": [
"IEnumDebugPropertyInfo.Next"
Expand Down
Loading