From 4698fa150892274cd14d3a9ae963dad89d82b89f Mon Sep 17 00:00:00 2001
From: Jackson Schuster <36744439+jtschuster@users.noreply.github.com>
Date: Fri, 25 Aug 2023 16:04:13 -0700
Subject: [PATCH] [ComInterfaceGenerator] Recommend [In] and [Out] attributes
on array parameters (#91094)
Recommend that methods with array parameters use [In] or [Out] attributes if there are none already, and the parameter is not in, ref, or out.
---
docs/project/list-of-diagnostics.md | 2 +-
.../DiagnosticDescriptorProvider.cs | 1 +
.../GeneratorDiagnostics.cs | 11 +
.../gen/Common/Resources/Strings.resx | 25 +-
.../gen/Common/Resources/xlf/Strings.cs.xlf | 41 ++-
.../gen/Common/Resources/xlf/Strings.de.xlf | 41 ++-
.../gen/Common/Resources/xlf/Strings.es.xlf | 41 ++-
.../gen/Common/Resources/xlf/Strings.fr.xlf | 39 ++-
.../gen/Common/Resources/xlf/Strings.it.xlf | 39 ++-
.../gen/Common/Resources/xlf/Strings.ja.xlf | 41 ++-
.../gen/Common/Resources/xlf/Strings.ko.xlf | 41 ++-
.../gen/Common/Resources/xlf/Strings.pl.xlf | 39 ++-
.../Common/Resources/xlf/Strings.pt-BR.xlf | 41 ++-
.../gen/Common/Resources/xlf/Strings.ru.xlf | 39 ++-
.../gen/Common/Resources/xlf/Strings.tr.xlf | 39 ++-
.../Common/Resources/xlf/Strings.zh-Hans.xlf | 41 ++-
.../Common/Resources/xlf/Strings.zh-Hant.xlf | 39 ++-
.../DiagnosticDescriptorProvider.cs | 1 +
.../GeneratorDiagnostics.cs | 10 +
.../IncrementalValuesProviderExtensions.cs | 2 +-
...ributedMarshallingModelGeneratorFactory.cs | 11 +-
.../ByValueContentsMarshalKindValidator.cs | 3 +-
.../ByValueMarshalKindSupportDescriptor.cs | 166 +++++-----
.../Marshalling/GeneratorDiagnostic.cs | 13 +
.../Marshalling/MarshallingGenerator.cs | 4 +
.../StaticPinnableManagedValueMarshaller.cs | 2 +-
.../ByValueContentsMarshalling.cs | 301 ++++++++++++++++++
.../CodeSnippets.cs | 60 ++++
.../ComInterfaceGenerator.Unit.Tests.csproj | 1 +
.../CompileFails.cs | 294 +----------------
.../tests/Common/TestUtils.cs | 14 +-
.../ByValueContentsMarshalling.cs | 135 ++++++++
.../CodeSnippets.cs | 16 +
.../CompileFails.cs | 57 ----
.../Compiles.cs | 24 --
.../TestAssets/SharedTypes/SharedTypes.csproj | 2 +
36 files changed, 1160 insertions(+), 516 deletions(-)
create mode 100644 src/libraries/System.Runtime.InteropServices/tests/ComInterfaceGenerator.Unit.Tests/ByValueContentsMarshalling.cs
create mode 100644 src/libraries/System.Runtime.InteropServices/tests/LibraryImportGenerator.UnitTests/ByValueContentsMarshalling.cs
diff --git a/docs/project/list-of-diagnostics.md b/docs/project/list-of-diagnostics.md
index 3c9a9afb9c4199..4b46d49f5813f0 100644
--- a/docs/project/list-of-diagnostics.md
+++ b/docs/project/list-of-diagnostics.md
@@ -208,7 +208,7 @@ The diagnostic id values reserved for .NET Libraries analyzer warnings are `SYSL
| __`SYSLIB1089`__ | _`SYSLIB1070`-`SYSLIB1089` reserved for System.Runtime.InteropServices.JavaScript.JSImportGenerator._ |
| __`SYSLIB1090`__ | Invalid 'GeneratedComInterfaceAttribute' usage |
| __`SYSLIB1091`__ | Method is declared in different partial declaration than the 'GeneratedComInterface' attribute. |
-| __`SYSLIB1092`__ | 'GenerateComInterfaceAttribute' usage not recommended. See aka.ms/GeneratedComInterfaceUsage for recommended usage. |
+| __`SYSLIB1092`__ | Usage of '[LibraryImport|GeneratedComInterface]' does not follow recommendation. See aka.ms/[LibraryImport|GeneratedComInterface]Usage for best practices. |
| __`SYSLIB1093`__ | Analysis for COM interface generation has failed |
| __`SYSLIB1094`__ | The base COM interface failed to generate source. Code will not be generated for this interface. |
| __`SYSLIB1095`__ | Invalid 'GeneratedComClassAttribute' usage |
diff --git a/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/DiagnosticDescriptorProvider.cs b/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/DiagnosticDescriptorProvider.cs
index c676f27cb9f401..fab2d39182c8b7 100644
--- a/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/DiagnosticDescriptorProvider.cs
+++ b/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/DiagnosticDescriptorProvider.cs
@@ -26,6 +26,7 @@ internal sealed class DiagnosticDescriptorProvider : IDiagnosticDescriptorProvid
GeneratorDiagnostic.NotSupported { NotSupportedDetails: not null, TypePositionInfo.IsManagedReturnPosition: false } => GeneratorDiagnostics.ParameterTypeNotSupportedWithDetails,
GeneratorDiagnostic.UnnecessaryData { TypePositionInfo.IsManagedReturnPosition: false } => GeneratorDiagnostics.UnnecessaryParameterMarshallingInfo,
GeneratorDiagnostic.UnnecessaryData { TypePositionInfo.IsManagedReturnPosition: true } => GeneratorDiagnostics.UnnecessaryReturnMarshallingInfo,
+ GeneratorDiagnostic.NotRecommended => GeneratorDiagnostics.GeneratedComInterfaceUsageDoesNotFollowBestPractices,
{ IsFatal: false } => null,
{ TypePositionInfo.IsManagedReturnPosition: true } => GeneratorDiagnostics.ReturnTypeNotSupported,
{ TypePositionInfo.IsManagedReturnPosition: false } => GeneratorDiagnostics.ParameterTypeNotSupported,
diff --git a/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/GeneratorDiagnostics.cs b/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/GeneratorDiagnostics.cs
index b213e3a9704f29..1e1849592f7ba4 100644
--- a/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/GeneratorDiagnostics.cs
+++ b/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/GeneratorDiagnostics.cs
@@ -485,6 +485,17 @@ public class Ids
DiagnosticSeverity.Info,
isEnabledByDefault: true);
+ ///
+ public static readonly DiagnosticDescriptor GeneratedComInterfaceUsageDoesNotFollowBestPractices =
+ new DiagnosticDescriptor(
+ Ids.NotRecommendedGeneratedComInterfaceUsage,
+ GetResourceString(nameof(SR.ComInterfaceUsageDoesNotFollowBestPracticesTitle)),
+ GetResourceString(nameof(SR.ComInterfaceUsageDoesNotFollowBestPracticesMessageWithDetails)),
+ Category,
+ DiagnosticSeverity.Info,
+ isEnabledByDefault: true,
+ helpLinkUri: "aka.ms/GeneratedComInterfaceUsage");
+
///
/// Report diagnostic for invalid configuration for string marshalling.
///
diff --git a/src/libraries/System.Runtime.InteropServices/gen/Common/Resources/Strings.resx b/src/libraries/System.Runtime.InteropServices/gen/Common/Resources/Strings.resx
index ebf0170e328752..2da298e8a7b0df 100644
--- a/src/libraries/System.Runtime.InteropServices/gen/Common/Resources/Strings.resx
+++ b/src/libraries/System.Runtime.InteropServices/gen/Common/Resources/Strings.resx
@@ -875,7 +875,13 @@
[In] and [Out] attributes
- The `[Out]` attribute is only supported on array parameters. Consider using 'out' or 'ref' keywords to make the parameter mutable.
+ The '[Out]' attribute is only supported on array parameters. Consider using 'out' or 'ref' keywords to make the parameter mutable.
+
+
+ The '[In]' attribute is only supported on array parameters. By-value parameters are considered read-only by default.
+
+
+ The '[In]' and '[Out]' attributes are only supported on array parameters. Consider using the 'ref' keyword to make the parameter mutable.The return value in the managed definition will be converted to an 'out' parameter when calling the unmanaged COM method. If the return value is intended to be the HRESULT code returned by the unmanaged COM method, use '[PreserveSig]' on the method.
@@ -889,4 +895,19 @@
This type will be treated as a struct in the native signature, not as a native HRESULT
-
\ No newline at end of file
+
+ It is recommended to use explicit '[In]' and '[Out]' attributes on array parameters.
+
+
+ The usage of 'GeneratedComInterfaceAttribute' does not follow recommendations.
+
+
+ The usage of 'GeneratedComInterfaceAttribute' does not follow recommendations. {0}
+
+
+ The usage of 'LibraryImportAttribute' does not follow recommendations.
+
+
+ The usage of 'LibraryImportAttribute' does not follow recommendations. {0}
+
+
diff --git a/src/libraries/System.Runtime.InteropServices/gen/Common/Resources/xlf/Strings.cs.xlf b/src/libraries/System.Runtime.InteropServices/gen/Common/Resources/xlf/Strings.cs.xlf
index e1b25c7690ffa4..829de99735954b 100644
--- a/src/libraries/System.Runtime.InteropServices/gen/Common/Resources/xlf/Strings.cs.xlf
+++ b/src/libraries/System.Runtime.InteropServices/gen/Common/Resources/xlf/Strings.cs.xlf
@@ -147,6 +147,16 @@
Hostování .NET COM s EnableComHosting nepodporuje rozhraní s generatedComInterfaceAttribute
+
+ The usage of 'GeneratedComInterfaceAttribute' does not follow recommendations. {0}
+ The usage of 'GeneratedComInterfaceAttribute' does not follow recommendations. {0}
+
+
+
+ The usage of 'GeneratedComInterfaceAttribute' does not follow recommendations.
+ The usage of 'GeneratedComInterfaceAttribute' does not follow recommendations.
+
+ The return value in the managed definition will be converted to an 'out' parameter when calling the unmanaged COM method. If the return value is intended to be the HRESULT code returned by the unmanaged COM method, use '[PreserveSig]' on the method.Vrácená hodnota ve spravované definici se při volání nespravované metody COM převede na parametr out. Pokud má být návratovou hodnotou kód HRESULT vrácený nespravovanou metodou COM, použijte u metody [PreserveSig].
@@ -447,6 +457,11 @@
Tento typ bude v nativním podpisu považován za strukturu, nikoli za nativní HRESULT.
+
+ The '[In]' attribute is only supported on array parameters. By-value parameters are considered read-only by default.
+ The '[In]' attribute is only supported on array parameters. By-value parameters are considered read-only by default.
+
+ The '[In]' attribute is not supported unless the '[Out]' attribute is also used. Blittable arrays cannot be marshalled as '[In]' only.Atribut [In] není podporován, pokud není použit také atribut [Out]. Blittable arrays nelze zařadit pouze jako [In].
@@ -467,6 +482,11 @@
Poskytnuté atributy „[In]“ a „[Out]“ u tohoto parametru se na tomto parametru nepodporují.
+
+ The '[In]' and '[Out]' attributes are only supported on array parameters. Consider using the 'ref' keyword to make the parameter mutable.
+ The '[In]' and '[Out]' attributes are only supported on array parameters. Consider using the 'ref' keyword to make the parameter mutable.
+
+ [In] and [Out] attributesatributy [In] a [Out]
@@ -702,6 +722,16 @@
Neplatné použití atributu VirtualMethodIndexAttribute
+
+ The usage of 'LibraryImportAttribute' does not follow recommendations. {0}
+ The usage of 'LibraryImportAttribute' does not follow recommendations. {0}
+
+
+
+ The usage of 'LibraryImportAttribute' does not follow recommendations.
+ The usage of 'LibraryImportAttribute' does not follow recommendations.
+
+ The element type of the 'ReadOnlySpan' returned by 'GetManagedValuesSource' must be the same as the element type returned by 'GetManagedValuesDestination'.Typ prvku ReadOnlySpan vrácený GetManagedValuesSource musí být stejný, jako typ prvku vrácený GetManagedValuesDestination.
@@ -903,8 +933,8 @@
- The `[Out]` attribute is only supported on array parameters. Consider using 'out' or 'ref' keywords to make the parameter mutable.
- Atribut „[Out]“ se podporuje pouze u parametrů pole. Zvažte použití klíčových slov „out“ nebo „ref“, aby se parametr dalo měnit.
+ The '[Out]' attribute is only supported on array parameters. Consider using 'out' or 'ref' keywords to make the parameter mutable.
+ The '[Out]' attribute is only supported on array parameters. Consider using 'out' or 'ref' keywords to make the parameter mutable.
@@ -917,6 +947,11 @@
Typ {0}určuje, že podporuje zařazování ve směru „Out“, ale neposkytuje metodu ToManaged, která vrací spravovaný typ
+
+ It is recommended to use explicit '[In]' and '[Out]' attributes on array parameters.
+ It is recommended to use explicit '[In]' and '[Out]' attributes on array parameters.
+
+ 'GeneratedComInterfaceAttribute' and 'GeneratedComClassAttribute' require unsafe code. Project must be updated with '<AllowUnsafeBlocks>true</AllowUnsafeBlocks>'.GeneratedComInterfaceAttribute a GeneratedComClassAttribute vyžadují nebezpečný kód. Projekt se musí aktualizovat na <AllowUnsafeBlocks>true</AllowUnsafeBlocks>.
@@ -1276,4 +1311,4 @@