From 7ce5bf93e5881a5be1cf5daf8c7e7001f202a0bc Mon Sep 17 00:00:00 2001 From: Jackson Schuster <36744439+jtschuster@users.noreply.github.com> Date: Mon, 12 Jun 2023 11:36:56 -0700 Subject: [PATCH] Clean up code, use DiagnosticInfo in LibraryImportGenerator, Cache well known attribute types in StubEnvironment (#87348) Remove unused usings and simplify access to CodeAnalysis.Location. Cache the attributes searched for in incremental stub calculations. Add warning if unsafe blocks are not allowed. Removes assert in ComMethodInfo that was throwing when editing files in the test project. Co-authored-by: Jeremy Koritzinsky --- .../ComInterfaceAndMethodsContext.cs | 4 -- .../ComInterfaceGenerator/ComInterfaceGenerator.cs | 16 +++--- .../gen/ComInterfaceGenerator/ComInterfaceInfo.cs | 58 ++++++++++--------- .../ComInterfaceGenerator/GeneratorDiagnostics.cs | 13 ++++- .../ComInterfaceGenerator/Resources/Strings.resx | 9 +++ .../Resources/xlf/Strings.cs.xlf | 15 +++++ .../Resources/xlf/Strings.de.xlf | 15 +++++ .../Resources/xlf/Strings.es.xlf | 15 +++++ .../Resources/xlf/Strings.fr.xlf | 15 +++++ .../Resources/xlf/Strings.it.xlf | 15 +++++ .../Resources/xlf/Strings.ja.xlf | 15 +++++ .../Resources/xlf/Strings.ko.xlf | 15 +++++ .../Resources/xlf/Strings.pl.xlf | 15 +++++ .../Resources/xlf/Strings.pt-BR.xlf | 15 +++++ .../Resources/xlf/Strings.ru.xlf | 15 +++++ .../Resources/xlf/Strings.tr.xlf | 15 +++++ .../Resources/xlf/Strings.zh-Hans.xlf | 15 +++++ .../Resources/xlf/Strings.zh-Hant.xlf | 15 +++++ .../Analyzers/CustomMarshallerAttributeFixer.cs | 8 +-- .../Analyzers/DiagnosticReporter.cs | 2 +- .../LibraryImportGenerator/GeneratorDiagnostics.cs | 5 +- .../LibraryImportGenerator.cs | 31 +++++----- .../DiagnosticInfo.cs | 7 +-- .../DiagnosticOr.cs | 5 -- .../MethodSignatureDiagnosticLocations.cs | 2 +- .../StubEnvironment.cs | 60 +++++++++++++++++++- .../CompileFails.cs | 66 +++++++++++++++++++--- 27 files changed, 392 insertions(+), 89 deletions(-) diff --git a/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/ComInterfaceAndMethodsContext.cs b/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/ComInterfaceAndMethodsContext.cs index dd6797f..13f91ff 100644 --- a/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/ComInterfaceAndMethodsContext.cs +++ b/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/ComInterfaceAndMethodsContext.cs @@ -12,10 +12,6 @@ namespace Microsoft.Interop /// internal sealed record ComInterfaceAndMethodsContext(ComInterfaceContext Interface, SequenceEqualImmutableArray Methods) { - // Change Calc all methods to return an ordered list of all the methods and the data in comInterfaceandMethodsContext - // Have a step that runs CalculateMethodStub on each of them. - // Call GroupMethodsByInterfaceForGeneration - /// /// COM methods that are declared on the attributed interface declaration. /// diff --git a/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/ComInterfaceGenerator.cs b/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/ComInterfaceGenerator.cs index d0334a4..4b25e43 100644 --- a/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/ComInterfaceGenerator.cs +++ b/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/ComInterfaceGenerator.cs @@ -42,10 +42,10 @@ namespace Microsoft.Interop : null) .Where( static modelData => modelData is not null); - - var interfaceSymbolOrDiagnostics = attributedInterfaces.Select(static (data, ct) => + var stubEnvironment = context.CreateStubEnvironmentProvider(); + var interfaceSymbolOrDiagnostics = attributedInterfaces.Combine(stubEnvironment).Select(static (data, ct) => { - return ComInterfaceInfo.From(data.Symbol, data.Syntax, ct); + return ComInterfaceInfo.From(data.Left.Symbol, data.Left.Syntax, data.Right, ct); }); var interfaceSymbolsWithoutDiagnostics = context.FilterAndReportDiagnostics(interfaceSymbolOrDiagnostics); @@ -59,7 +59,7 @@ namespace Microsoft.Interop var comMethodsAndSymbolsOrDiagnostics = interfaceSymbolsWithoutDiagnostics.Select(ComMethodInfo.GetMethodsFromInterface); var methodInfoAndSymbolGroupedByInterface = context - .FilterAndReportDiagnostics<(ComMethodInfo MethodInfo, IMethodSymbol Symbol)> (comMethodsAndSymbolsOrDiagnostics); + .FilterAndReportDiagnostics<(ComMethodInfo MethodInfo, IMethodSymbol Symbol)>(comMethodsAndSymbolsOrDiagnostics); var methodInfosGroupedByInterface = methodInfoAndSymbolGroupedByInterface .Select(static (methods, ct) => @@ -84,7 +84,7 @@ namespace Microsoft.Interop .Select((data, ct) => data.ToDictionary(static x => x.MethodInfo, static x => x.Symbol)); var comMethodContexts = comMethodContextBuilders .Combine(methodInfoToSymbolMap) - .Combine(context.CreateStubEnvironmentProvider()) + .Combine(stubEnvironment) .Select((param, ct) => { var ((data, symbolMap), env) = param; @@ -212,9 +212,9 @@ namespace Microsoft.Interop private static IncrementalMethodStubGenerationContext CalculateStubInformation(MethodDeclarationSyntax syntax, IMethodSymbol symbol, int index, StubEnvironment environment, ManagedTypeInfo owningInterface, CancellationToken ct) { ct.ThrowIfCancellationRequested(); - INamedTypeSymbol? lcidConversionAttrType = environment.Compilation.GetTypeByMetadataName(TypeNames.LCIDConversionAttribute); - INamedTypeSymbol? suppressGCTransitionAttrType = environment.Compilation.GetTypeByMetadataName(TypeNames.SuppressGCTransitionAttribute); - INamedTypeSymbol? unmanagedCallConvAttrType = environment.Compilation.GetTypeByMetadataName(TypeNames.UnmanagedCallConvAttribute); + INamedTypeSymbol? lcidConversionAttrType = environment.LcidConversionAttrType; + INamedTypeSymbol? suppressGCTransitionAttrType = environment.SuppressGCTransitionAttrType; + INamedTypeSymbol? unmanagedCallConvAttrType = environment.UnmanagedCallConvAttrType; // Get any attributes of interest on the method AttributeData? lcidConversionAttr = null; AttributeData? suppressGCTransitionAttribute = null; diff --git a/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/ComInterfaceInfo.cs b/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/ComInterfaceInfo.cs index 0c18dca..b7314b7 100644 --- a/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/ComInterfaceInfo.cs +++ b/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/ComInterfaceInfo.cs @@ -8,8 +8,7 @@ using System.Threading; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Syntax; -using Microsoft.Interop; -using DiagnosticOrInterfaceInfo = Microsoft.Interop.DiagnosticOr<(Microsoft.Interop.ComInterfaceInfo InterfaceInfo, Microsoft.CodeAnalysis.INamedTypeSymbol Symbol) >; +using DiagnosticOrInterfaceInfo = Microsoft.Interop.DiagnosticOr<(Microsoft.Interop.ComInterfaceInfo InterfaceInfo, Microsoft.CodeAnalysis.INamedTypeSymbol Symbol)>; namespace Microsoft.Interop { @@ -26,37 +25,23 @@ namespace Microsoft.Interop Guid InterfaceId, Location DiagnosticLocation) { - public static DiagnosticOrInterfaceInfo From(INamedTypeSymbol symbol, InterfaceDeclarationSyntax syntax, CancellationToken _) + public static DiagnosticOrInterfaceInfo From(INamedTypeSymbol symbol, InterfaceDeclarationSyntax syntax, StubEnvironment env, CancellationToken _) { + if (env.Compilation.Options is not CSharpCompilationOptions { AllowUnsafe: true }) // Unsafe code enabled + return DiagnosticOrInterfaceInfo.From(DiagnosticInfo.Create(GeneratorDiagnostics.RequiresAllowUnsafeBlocks, syntax.Identifier.GetLocation())); // Verify the method has no generic types or defined implementation // and is not marked static or sealed if (syntax.TypeParameterList is not null) { - // Verify the interface has no generic types or defined implementation - // and is not marked static or sealed - if (syntax.TypeParameterList is not null) - { - return DiagnosticOrInterfaceInfo.From( - DiagnosticInfo.Create( - GeneratorDiagnostics.InvalidAttributedInterfaceGenericNotSupported, - syntax.Identifier.GetLocation(), - symbol.Name)); - } + return DiagnosticOrInterfaceInfo.From( + DiagnosticInfo.Create( + GeneratorDiagnostics.InvalidAttributedInterfaceGenericNotSupported, + syntax.Identifier.GetLocation(), + symbol.Name)); } - // Verify that the types the interface is declared in are marked partial. - for (SyntaxNode? parentNode = syntax; parentNode is TypeDeclarationSyntax typeDecl; parentNode = parentNode.Parent) - { - if (!typeDecl.Modifiers.Any(SyntaxKind.PartialKeyword)) - { - return DiagnosticOrInterfaceInfo.From( - DiagnosticInfo.Create( - GeneratorDiagnostics.InvalidAttributedInterfaceMissingPartialModifiers, - syntax.Identifier.GetLocation(), - symbol.Name, - typeDecl.Identifier)); - } - } + if (!IsInPartialContext(symbol, syntax, out DiagnosticInfo? partialContextDiagnostic)) + return DiagnosticOrInterfaceInfo.From(partialContextDiagnostic); if (!TryGetGuid(symbol, syntax, out Guid? guid, out DiagnosticInfo? guidDiagnostic)) return DiagnosticOrInterfaceInfo.From(guidDiagnostic); @@ -76,10 +61,29 @@ namespace Microsoft.Interop new ContainingSyntaxContext(syntax), new ContainingSyntax(syntax.Modifiers, syntax.Kind(), syntax.Identifier, syntax.TypeParameterList), guid ?? Guid.Empty, - symbol.Locations[0]), + syntax.Identifier.GetLocation()), symbol)); } + private static bool IsInPartialContext(INamedTypeSymbol symbol, InterfaceDeclarationSyntax syntax, [NotNullWhen(false)] out DiagnosticInfo? diagnostic) + { + // Verify that the types the interface is declared in are marked partial. + for (SyntaxNode? parentNode = syntax; parentNode is TypeDeclarationSyntax typeDecl; parentNode = parentNode.Parent) + { + if (!typeDecl.Modifiers.Any(SyntaxKind.PartialKeyword)) + { + diagnostic = DiagnosticInfo.Create( + GeneratorDiagnostics.InvalidAttributedInterfaceMissingPartialModifiers, + syntax.Identifier.GetLocation(), + symbol.Name, + typeDecl.Identifier); + return false; + } + } + diagnostic = null; + return true; + } + private static bool StringMarshallingIsValid(INamedTypeSymbol symbol, InterfaceDeclarationSyntax syntax, INamedTypeSymbol? baseSymbol, [NotNullWhen(false)] out DiagnosticInfo? stringMarshallingDiagnostic) { var attrInfo = GeneratedComInterfaceData.From(GeneratedComInterfaceCompilationData.GetAttributeDataFromInterfaceSymbol(symbol)); diff --git a/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/GeneratorDiagnostics.cs b/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/GeneratorDiagnostics.cs index 9d77ff7..d40f7a2 100644 --- a/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/GeneratorDiagnostics.cs +++ b/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/GeneratorDiagnostics.cs @@ -20,6 +20,7 @@ namespace Microsoft.Interop public const string InvalidLibraryImportAttributeUsage = Prefix + "1050"; public const string TypeNotSupported = Prefix + "1051"; public const string ConfigurationNotSupported = Prefix + "1052"; + public const string RequiresAllowUnsafeBlocks = Prefix + "1062"; public const string InvalidGeneratedComInterfaceAttributeUsage = Prefix + "1090"; public const string MethodNotDeclaredInAttributedInterface = Prefix + "1091"; public const string MultipleComInterfaceBaseTypes = Prefix + "1092"; @@ -30,6 +31,16 @@ namespace Microsoft.Interop private const string Category = "ComInterfaceGenerator"; + public static readonly DiagnosticDescriptor RequiresAllowUnsafeBlocks = + new DiagnosticDescriptor( + Ids.RequiresAllowUnsafeBlocks, + GetResourceString(nameof(SR.RequiresAllowUnsafeBlocksTitle)), + GetResourceString(nameof(SR.RequiresAllowUnsafeBlocksMessage)), + Category, + DiagnosticSeverity.Error, + isEnabledByDefault: true, + description: GetResourceString(nameof(SR.RequiresAllowUnsafeBlocksDescription))); + /// public static readonly DiagnosticDescriptor InvalidAttributedMethodSignature = new DiagnosticDescriptor( @@ -396,7 +407,7 @@ namespace Microsoft.Interop TypePositionInfo info, string? notSupportedDetails) { - CodeAnalysis.Location diagnosticLocation = CodeAnalysis.Location.None; + Location diagnosticLocation = CodeAnalysis.Location.None; string elementName = string.Empty; if (info.IsManagedReturnPosition) diff --git a/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/Resources/Strings.resx b/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/Resources/Strings.resx index 9977853..5bbe8eb 100644 --- a/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/Resources/Strings.resx +++ b/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/Resources/Strings.resx @@ -267,6 +267,15 @@ The configuration of 'StringMarshalling' and 'StringMarshallingCustomType' on interface '{0}' is invalid. {1} + + GeneratedComInterfaceAttribute requires unsafe code. Project must be updated with '<AllowUnsafeBlocks>true</AllowUnsafeBlocks>'. + + + GeneratedComInterfaceAttribute requires unsafe code. Project must be updated with '<AllowUnsafeBlocks>true</AllowUnsafeBlocks>'. + + + GeneratedComInterfaceAttribute requires unsafe code. + The interface '{0}' or one of its containing types is missing the 'partial' keyword. Code will not be generated for '{0}'. diff --git a/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/Resources/xlf/Strings.cs.xlf b/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/Resources/xlf/Strings.cs.xlf index 640637e..ab079fb 100644 --- a/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/Resources/xlf/Strings.cs.xlf +++ b/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/Resources/xlf/Strings.cs.xlf @@ -297,6 +297,21 @@ Zadané rozhraní je odvozeno ze dvou nebo více rozhraní s atributem GeneratedComInterfaceAttribute. + + GeneratedComInterfaceAttribute requires unsafe code. Project must be updated with '<AllowUnsafeBlocks>true</AllowUnsafeBlocks>'. + GeneratedComInterfaceAttribute requires unsafe code. Project must be updated with '<AllowUnsafeBlocks>true</AllowUnsafeBlocks>'. + + + + GeneratedComInterfaceAttribute requires unsafe code. Project must be updated with '<AllowUnsafeBlocks>true</AllowUnsafeBlocks>'. + GeneratedComInterfaceAttribute requires unsafe code. Project must be updated with '<AllowUnsafeBlocks>true</AllowUnsafeBlocks>'. + + + + GeneratedComInterfaceAttribute requires unsafe code. + GeneratedComInterfaceAttribute requires unsafe code. + + COM Interop APIs on 'System.Runtime.InteropServices.Marshal' do not support source-generated COM and will fail at runtime COM Interop APIs on 'System.Runtime.InteropServices.Marshal' do not support source-generated COM and will fail at runtime diff --git a/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/Resources/xlf/Strings.de.xlf b/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/Resources/xlf/Strings.de.xlf index c0d3124..1e9474a 100644 --- a/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/Resources/xlf/Strings.de.xlf +++ b/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/Resources/xlf/Strings.de.xlf @@ -297,6 +297,21 @@ Die angegebene Schnittstelle wird von mindestens zwei Schnittstellen abgeleitet, die "GeneratedComInterfaceAttribute" zugeordnet sind. + + GeneratedComInterfaceAttribute requires unsafe code. Project must be updated with '<AllowUnsafeBlocks>true</AllowUnsafeBlocks>'. + GeneratedComInterfaceAttribute requires unsafe code. Project must be updated with '<AllowUnsafeBlocks>true</AllowUnsafeBlocks>'. + + + + GeneratedComInterfaceAttribute requires unsafe code. Project must be updated with '<AllowUnsafeBlocks>true</AllowUnsafeBlocks>'. + GeneratedComInterfaceAttribute requires unsafe code. Project must be updated with '<AllowUnsafeBlocks>true</AllowUnsafeBlocks>'. + + + + GeneratedComInterfaceAttribute requires unsafe code. + GeneratedComInterfaceAttribute requires unsafe code. + + COM Interop APIs on 'System.Runtime.InteropServices.Marshal' do not support source-generated COM and will fail at runtime COM Interop APIs on 'System.Runtime.InteropServices.Marshal' do not support source-generated COM and will fail at runtime diff --git a/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/Resources/xlf/Strings.es.xlf b/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/Resources/xlf/Strings.es.xlf index fb2702b..40d06bb 100644 --- a/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/Resources/xlf/Strings.es.xlf +++ b/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/Resources/xlf/Strings.es.xlf @@ -297,6 +297,21 @@ La interfaz especificada deriva de dos o más interfaces con atributos "GeneratedComInterfaceAttribute". + + GeneratedComInterfaceAttribute requires unsafe code. Project must be updated with '<AllowUnsafeBlocks>true</AllowUnsafeBlocks>'. + GeneratedComInterfaceAttribute requires unsafe code. Project must be updated with '<AllowUnsafeBlocks>true</AllowUnsafeBlocks>'. + + + + GeneratedComInterfaceAttribute requires unsafe code. Project must be updated with '<AllowUnsafeBlocks>true</AllowUnsafeBlocks>'. + GeneratedComInterfaceAttribute requires unsafe code. Project must be updated with '<AllowUnsafeBlocks>true</AllowUnsafeBlocks>'. + + + + GeneratedComInterfaceAttribute requires unsafe code. + GeneratedComInterfaceAttribute requires unsafe code. + + COM Interop APIs on 'System.Runtime.InteropServices.Marshal' do not support source-generated COM and will fail at runtime COM Interop APIs on 'System.Runtime.InteropServices.Marshal' do not support source-generated COM and will fail at runtime diff --git a/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/Resources/xlf/Strings.fr.xlf b/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/Resources/xlf/Strings.fr.xlf index 9fb27aa..aead2aef 100644 --- a/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/Resources/xlf/Strings.fr.xlf +++ b/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/Resources/xlf/Strings.fr.xlf @@ -297,6 +297,21 @@ L’interface spécifiée dérive de deux ou plusieurs interfaces avec attribut 'GeneratedComInterfaceAttribute'. + + GeneratedComInterfaceAttribute requires unsafe code. Project must be updated with '<AllowUnsafeBlocks>true</AllowUnsafeBlocks>'. + GeneratedComInterfaceAttribute requires unsafe code. Project must be updated with '<AllowUnsafeBlocks>true</AllowUnsafeBlocks>'. + + + + GeneratedComInterfaceAttribute requires unsafe code. Project must be updated with '<AllowUnsafeBlocks>true</AllowUnsafeBlocks>'. + GeneratedComInterfaceAttribute requires unsafe code. Project must be updated with '<AllowUnsafeBlocks>true</AllowUnsafeBlocks>'. + + + + GeneratedComInterfaceAttribute requires unsafe code. + GeneratedComInterfaceAttribute requires unsafe code. + + COM Interop APIs on 'System.Runtime.InteropServices.Marshal' do not support source-generated COM and will fail at runtime COM Interop APIs on 'System.Runtime.InteropServices.Marshal' do not support source-generated COM and will fail at runtime diff --git a/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/Resources/xlf/Strings.it.xlf b/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/Resources/xlf/Strings.it.xlf index 39f5e4b..811c87b 100644 --- a/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/Resources/xlf/Strings.it.xlf +++ b/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/Resources/xlf/Strings.it.xlf @@ -297,6 +297,21 @@ L'interfaccia specificata deriva da due o più interfacce con attributi 'GeneratedComInterfaceAttribute'. + + GeneratedComInterfaceAttribute requires unsafe code. Project must be updated with '<AllowUnsafeBlocks>true</AllowUnsafeBlocks>'. + GeneratedComInterfaceAttribute requires unsafe code. Project must be updated with '<AllowUnsafeBlocks>true</AllowUnsafeBlocks>'. + + + + GeneratedComInterfaceAttribute requires unsafe code. Project must be updated with '<AllowUnsafeBlocks>true</AllowUnsafeBlocks>'. + GeneratedComInterfaceAttribute requires unsafe code. Project must be updated with '<AllowUnsafeBlocks>true</AllowUnsafeBlocks>'. + + + + GeneratedComInterfaceAttribute requires unsafe code. + GeneratedComInterfaceAttribute requires unsafe code. + + COM Interop APIs on 'System.Runtime.InteropServices.Marshal' do not support source-generated COM and will fail at runtime COM Interop APIs on 'System.Runtime.InteropServices.Marshal' do not support source-generated COM and will fail at runtime diff --git a/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/Resources/xlf/Strings.ja.xlf b/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/Resources/xlf/Strings.ja.xlf index 7e1a209..8e01e4c 100644 --- a/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/Resources/xlf/Strings.ja.xlf +++ b/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/Resources/xlf/Strings.ja.xlf @@ -297,6 +297,21 @@ 指定されたインターフェイスは、2 つ以上の 'GeneratedComInterfaceAttribute' 属性インターフェイスから派生しています。 + + GeneratedComInterfaceAttribute requires unsafe code. Project must be updated with '<AllowUnsafeBlocks>true</AllowUnsafeBlocks>'. + GeneratedComInterfaceAttribute requires unsafe code. Project must be updated with '<AllowUnsafeBlocks>true</AllowUnsafeBlocks>'. + + + + GeneratedComInterfaceAttribute requires unsafe code. Project must be updated with '<AllowUnsafeBlocks>true</AllowUnsafeBlocks>'. + GeneratedComInterfaceAttribute requires unsafe code. Project must be updated with '<AllowUnsafeBlocks>true</AllowUnsafeBlocks>'. + + + + GeneratedComInterfaceAttribute requires unsafe code. + GeneratedComInterfaceAttribute requires unsafe code. + + COM Interop APIs on 'System.Runtime.InteropServices.Marshal' do not support source-generated COM and will fail at runtime COM Interop APIs on 'System.Runtime.InteropServices.Marshal' do not support source-generated COM and will fail at runtime diff --git a/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/Resources/xlf/Strings.ko.xlf b/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/Resources/xlf/Strings.ko.xlf index 56e0539..318be73 100644 --- a/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/Resources/xlf/Strings.ko.xlf +++ b/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/Resources/xlf/Strings.ko.xlf @@ -297,6 +297,21 @@ 지정한 인터페이스는 두 개 이상의 'GeneratedComInterfaceAttribute' 특성 인터페이스에서 파생됩니다. + + GeneratedComInterfaceAttribute requires unsafe code. Project must be updated with '<AllowUnsafeBlocks>true</AllowUnsafeBlocks>'. + GeneratedComInterfaceAttribute requires unsafe code. Project must be updated with '<AllowUnsafeBlocks>true</AllowUnsafeBlocks>'. + + + + GeneratedComInterfaceAttribute requires unsafe code. Project must be updated with '<AllowUnsafeBlocks>true</AllowUnsafeBlocks>'. + GeneratedComInterfaceAttribute requires unsafe code. Project must be updated with '<AllowUnsafeBlocks>true</AllowUnsafeBlocks>'. + + + + GeneratedComInterfaceAttribute requires unsafe code. + GeneratedComInterfaceAttribute requires unsafe code. + + COM Interop APIs on 'System.Runtime.InteropServices.Marshal' do not support source-generated COM and will fail at runtime COM Interop APIs on 'System.Runtime.InteropServices.Marshal' do not support source-generated COM and will fail at runtime diff --git a/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/Resources/xlf/Strings.pl.xlf b/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/Resources/xlf/Strings.pl.xlf index cbe9032..85eff7c 100644 --- a/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/Resources/xlf/Strings.pl.xlf +++ b/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/Resources/xlf/Strings.pl.xlf @@ -297,6 +297,21 @@ Określony interfejs pochodzi z co najmniej dwóch interfejsów z atrybutem „GeneratedComInterfaceAttribute”. + + GeneratedComInterfaceAttribute requires unsafe code. Project must be updated with '<AllowUnsafeBlocks>true</AllowUnsafeBlocks>'. + GeneratedComInterfaceAttribute requires unsafe code. Project must be updated with '<AllowUnsafeBlocks>true</AllowUnsafeBlocks>'. + + + + GeneratedComInterfaceAttribute requires unsafe code. Project must be updated with '<AllowUnsafeBlocks>true</AllowUnsafeBlocks>'. + GeneratedComInterfaceAttribute requires unsafe code. Project must be updated with '<AllowUnsafeBlocks>true</AllowUnsafeBlocks>'. + + + + GeneratedComInterfaceAttribute requires unsafe code. + GeneratedComInterfaceAttribute requires unsafe code. + + COM Interop APIs on 'System.Runtime.InteropServices.Marshal' do not support source-generated COM and will fail at runtime COM Interop APIs on 'System.Runtime.InteropServices.Marshal' do not support source-generated COM and will fail at runtime diff --git a/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/Resources/xlf/Strings.pt-BR.xlf b/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/Resources/xlf/Strings.pt-BR.xlf index 48c7fdb..4c4ffbf 100644 --- a/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/Resources/xlf/Strings.pt-BR.xlf +++ b/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/Resources/xlf/Strings.pt-BR.xlf @@ -297,6 +297,21 @@ A interface especificada deriva de duas ou mais interfaces atribuídas a 'GeneratedComInterfaceAttribute'. + + GeneratedComInterfaceAttribute requires unsafe code. Project must be updated with '<AllowUnsafeBlocks>true</AllowUnsafeBlocks>'. + GeneratedComInterfaceAttribute requires unsafe code. Project must be updated with '<AllowUnsafeBlocks>true</AllowUnsafeBlocks>'. + + + + GeneratedComInterfaceAttribute requires unsafe code. Project must be updated with '<AllowUnsafeBlocks>true</AllowUnsafeBlocks>'. + GeneratedComInterfaceAttribute requires unsafe code. Project must be updated with '<AllowUnsafeBlocks>true</AllowUnsafeBlocks>'. + + + + GeneratedComInterfaceAttribute requires unsafe code. + GeneratedComInterfaceAttribute requires unsafe code. + + COM Interop APIs on 'System.Runtime.InteropServices.Marshal' do not support source-generated COM and will fail at runtime COM Interop APIs on 'System.Runtime.InteropServices.Marshal' do not support source-generated COM and will fail at runtime diff --git a/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/Resources/xlf/Strings.ru.xlf b/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/Resources/xlf/Strings.ru.xlf index f2721f1..0fc1b09 100644 --- a/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/Resources/xlf/Strings.ru.xlf +++ b/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/Resources/xlf/Strings.ru.xlf @@ -297,6 +297,21 @@ Указанный интерфейс является производным от двух или более интерфейсов с атрибутом "GeneratedComInterfaceAttribute". + + GeneratedComInterfaceAttribute requires unsafe code. Project must be updated with '<AllowUnsafeBlocks>true</AllowUnsafeBlocks>'. + GeneratedComInterfaceAttribute requires unsafe code. Project must be updated with '<AllowUnsafeBlocks>true</AllowUnsafeBlocks>'. + + + + GeneratedComInterfaceAttribute requires unsafe code. Project must be updated with '<AllowUnsafeBlocks>true</AllowUnsafeBlocks>'. + GeneratedComInterfaceAttribute requires unsafe code. Project must be updated with '<AllowUnsafeBlocks>true</AllowUnsafeBlocks>'. + + + + GeneratedComInterfaceAttribute requires unsafe code. + GeneratedComInterfaceAttribute requires unsafe code. + + COM Interop APIs on 'System.Runtime.InteropServices.Marshal' do not support source-generated COM and will fail at runtime COM Interop APIs on 'System.Runtime.InteropServices.Marshal' do not support source-generated COM and will fail at runtime diff --git a/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/Resources/xlf/Strings.tr.xlf b/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/Resources/xlf/Strings.tr.xlf index 34a1e34..6c35945 100644 --- a/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/Resources/xlf/Strings.tr.xlf +++ b/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/Resources/xlf/Strings.tr.xlf @@ -297,6 +297,21 @@ Belirtilen arabirim 'GeneratedComInterfaceAttribute' özniteliğine sahip iki veya daha fazla arabirimden türetildi. + + GeneratedComInterfaceAttribute requires unsafe code. Project must be updated with '<AllowUnsafeBlocks>true</AllowUnsafeBlocks>'. + GeneratedComInterfaceAttribute requires unsafe code. Project must be updated with '<AllowUnsafeBlocks>true</AllowUnsafeBlocks>'. + + + + GeneratedComInterfaceAttribute requires unsafe code. Project must be updated with '<AllowUnsafeBlocks>true</AllowUnsafeBlocks>'. + GeneratedComInterfaceAttribute requires unsafe code. Project must be updated with '<AllowUnsafeBlocks>true</AllowUnsafeBlocks>'. + + + + GeneratedComInterfaceAttribute requires unsafe code. + GeneratedComInterfaceAttribute requires unsafe code. + + COM Interop APIs on 'System.Runtime.InteropServices.Marshal' do not support source-generated COM and will fail at runtime COM Interop APIs on 'System.Runtime.InteropServices.Marshal' do not support source-generated COM and will fail at runtime diff --git a/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/Resources/xlf/Strings.zh-Hans.xlf b/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/Resources/xlf/Strings.zh-Hans.xlf index bf91ed4..f67a6f5 100644 --- a/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/Resources/xlf/Strings.zh-Hans.xlf +++ b/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/Resources/xlf/Strings.zh-Hans.xlf @@ -297,6 +297,21 @@ 指定的接口派生自两个或更多“GeneratedComInterfaceAttribute”特性化接口。 + + GeneratedComInterfaceAttribute requires unsafe code. Project must be updated with '<AllowUnsafeBlocks>true</AllowUnsafeBlocks>'. + GeneratedComInterfaceAttribute requires unsafe code. Project must be updated with '<AllowUnsafeBlocks>true</AllowUnsafeBlocks>'. + + + + GeneratedComInterfaceAttribute requires unsafe code. Project must be updated with '<AllowUnsafeBlocks>true</AllowUnsafeBlocks>'. + GeneratedComInterfaceAttribute requires unsafe code. Project must be updated with '<AllowUnsafeBlocks>true</AllowUnsafeBlocks>'. + + + + GeneratedComInterfaceAttribute requires unsafe code. + GeneratedComInterfaceAttribute requires unsafe code. + + COM Interop APIs on 'System.Runtime.InteropServices.Marshal' do not support source-generated COM and will fail at runtime COM Interop APIs on 'System.Runtime.InteropServices.Marshal' do not support source-generated COM and will fail at runtime diff --git a/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/Resources/xlf/Strings.zh-Hant.xlf b/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/Resources/xlf/Strings.zh-Hant.xlf index a68f8e0..12fd284 100644 --- a/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/Resources/xlf/Strings.zh-Hant.xlf +++ b/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/Resources/xlf/Strings.zh-Hant.xlf @@ -297,6 +297,21 @@ 指定的介面衍生自兩個或兩個以上的 'GeneratedComInterfaceAttribute'-屬性介面。 + + GeneratedComInterfaceAttribute requires unsafe code. Project must be updated with '<AllowUnsafeBlocks>true</AllowUnsafeBlocks>'. + GeneratedComInterfaceAttribute requires unsafe code. Project must be updated with '<AllowUnsafeBlocks>true</AllowUnsafeBlocks>'. + + + + GeneratedComInterfaceAttribute requires unsafe code. Project must be updated with '<AllowUnsafeBlocks>true</AllowUnsafeBlocks>'. + GeneratedComInterfaceAttribute requires unsafe code. Project must be updated with '<AllowUnsafeBlocks>true</AllowUnsafeBlocks>'. + + + + GeneratedComInterfaceAttribute requires unsafe code. + GeneratedComInterfaceAttribute requires unsafe code. + + COM Interop APIs on 'System.Runtime.InteropServices.Marshal' do not support source-generated COM and will fail at runtime COM Interop APIs on 'System.Runtime.InteropServices.Marshal' do not support source-generated COM and will fail at runtime diff --git a/src/libraries/System.Runtime.InteropServices/gen/LibraryImportGenerator/Analyzers/CustomMarshallerAttributeFixer.cs b/src/libraries/System.Runtime.InteropServices/gen/LibraryImportGenerator/Analyzers/CustomMarshallerAttributeFixer.cs index cf78e8a..231514a 100644 --- a/src/libraries/System.Runtime.InteropServices/gen/LibraryImportGenerator/Analyzers/CustomMarshallerAttributeFixer.cs +++ b/src/libraries/System.Runtime.InteropServices/gen/LibraryImportGenerator/Analyzers/CustomMarshallerAttributeFixer.cs @@ -6,19 +6,13 @@ using System.Collections.Generic; using System.Collections.Immutable; using System.Composition; using System.Linq; -using System.Runtime.InteropServices; -using System.Text; using System.Threading; using System.Threading.Tasks; -using System.Xml.Linq; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CodeActions; using Microsoft.CodeAnalysis.CodeFixes; -using Microsoft.CodeAnalysis.Differencing; using Microsoft.CodeAnalysis.DotnetRuntime.Extensions; using Microsoft.CodeAnalysis.Editing; -using Microsoft.CodeAnalysis.Simplification; -using Microsoft.CodeAnalysis.Text; using static Microsoft.Interop.Analyzers.CustomMarshallerAttributeAnalyzer; namespace Microsoft.Interop.Analyzers @@ -184,7 +178,7 @@ namespace Microsoft.Interop.Analyzers // Get the managed type from the CustomMarshallerAttribute located at the provided location in source on the provided type. // As we only get fixable diagnostics for types that have valid non-null managed types in the CustomMarshallerAttribute applications, // we do not need to worry about the returned symbol being null. - private static ITypeSymbol GetManagedTypeInAttributeSyntax(CodeAnalysis.Location locationInAttribute, INamedTypeSymbol attributedTypeSymbol) + private static ITypeSymbol GetManagedTypeInAttributeSyntax(Location locationInAttribute, INamedTypeSymbol attributedTypeSymbol) => (ITypeSymbol)attributedTypeSymbol.GetAttributes().First(attr => attr.ApplicationSyntaxReference.SyntaxTree == locationInAttribute.SourceTree && attr.ApplicationSyntaxReference.Span.Contains(locationInAttribute.SourceSpan)).ConstructorArguments[0].Value!; diff --git a/src/libraries/System.Runtime.InteropServices/gen/LibraryImportGenerator/Analyzers/DiagnosticReporter.cs b/src/libraries/System.Runtime.InteropServices/gen/LibraryImportGenerator/Analyzers/DiagnosticReporter.cs index b128616..e964365 100644 --- a/src/libraries/System.Runtime.InteropServices/gen/LibraryImportGenerator/Analyzers/DiagnosticReporter.cs +++ b/src/libraries/System.Runtime.InteropServices/gen/LibraryImportGenerator/Analyzers/DiagnosticReporter.cs @@ -16,7 +16,7 @@ namespace Microsoft.Interop.Analyzers _diagnosticFactory = createAndReportDiagnostic; } - public static DiagnosticReporter CreateForLocation(CodeAnalysis.Location location, Action reportDiagnostic) => new((descriptor, properties, args) => reportDiagnostic(location.CreateDiagnosticInfo(descriptor, properties, args).ToDiagnostic())); + public static DiagnosticReporter CreateForLocation(Location location, Action reportDiagnostic) => new((descriptor, properties, args) => reportDiagnostic(location.CreateDiagnosticInfo(descriptor, properties, args).ToDiagnostic())); public void CreateAndReportDiagnostic(DiagnosticDescriptor descriptor, params object[] messageArgs) => _diagnosticFactory(descriptor, ImmutableDictionary.Empty, messageArgs); diff --git a/src/libraries/System.Runtime.InteropServices/gen/LibraryImportGenerator/GeneratorDiagnostics.cs b/src/libraries/System.Runtime.InteropServices/gen/LibraryImportGenerator/GeneratorDiagnostics.cs index 1054687..491c799 100644 --- a/src/libraries/System.Runtime.InteropServices/gen/LibraryImportGenerator/GeneratorDiagnostics.cs +++ b/src/libraries/System.Runtime.InteropServices/gen/LibraryImportGenerator/GeneratorDiagnostics.cs @@ -1,12 +1,11 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -using Microsoft.CodeAnalysis; -using Microsoft.CodeAnalysis.CSharp.Syntax; using System; using System.Collections.Generic; using System.Collections.Immutable; using System.Diagnostics; +using Microsoft.CodeAnalysis; namespace Microsoft.Interop { @@ -231,7 +230,7 @@ namespace Microsoft.Interop string? notSupportedDetails, ImmutableDictionary diagnosticProperties) { - CodeAnalysis.Location diagnosticLocation = CodeAnalysis.Location.None; + Location diagnosticLocation = Location.None; string elementName = string.Empty; if (info.IsManagedReturnPosition) diff --git a/src/libraries/System.Runtime.InteropServices/gen/LibraryImportGenerator/LibraryImportGenerator.cs b/src/libraries/System.Runtime.InteropServices/gen/LibraryImportGenerator/LibraryImportGenerator.cs index 8bc3457..2da767b 100644 --- a/src/libraries/System.Runtime.InteropServices/gen/LibraryImportGenerator/LibraryImportGenerator.cs +++ b/src/libraries/System.Runtime.InteropServices/gen/LibraryImportGenerator/LibraryImportGenerator.cs @@ -52,18 +52,13 @@ namespace Microsoft.Interop // Validate if attributed methods can have source generated var methodsWithDiagnostics = attributedMethods.Select(static (data, ct) => { - Diagnostic? diagnostic = GetDiagnosticIfInvalidMethodForGeneration(data.Syntax, data.Symbol); - return new { Syntax = data.Syntax, Symbol = data.Symbol, Diagnostic = diagnostic }; + DiagnosticInfo? diagnostic = GetDiagnosticIfInvalidMethodForGeneration(data.Syntax, data.Symbol); + return diagnostic is not null + ? DiagnosticOr<(MethodDeclarationSyntax Syntax, IMethodSymbol Symbol)>.From(diagnostic) + : DiagnosticOr<(MethodDeclarationSyntax Syntax, IMethodSymbol Symbol)>.From((data.Syntax, data.Symbol)); }); - var methodsToGenerate = methodsWithDiagnostics.Where(static data => data.Diagnostic is null); - var invalidMethodDiagnostics = methodsWithDiagnostics.Where(static data => data.Diagnostic is not null); - - // Report diagnostics for invalid methods - context.RegisterSourceOutput(invalidMethodDiagnostics, static (context, invalidMethod) => - { - context.ReportDiagnostic(invalidMethod.Diagnostic); - }); + var methodsToGenerate = context.FilterAndReportDiagnostics(methodsWithDiagnostics); // Compute generator options IncrementalValueProvider stubOptions = context.AnalyzerConfigOptionsProvider @@ -220,10 +215,10 @@ namespace Microsoft.Interop CancellationToken ct) { ct.ThrowIfCancellationRequested(); - INamedTypeSymbol? lcidConversionAttrType = environment.Compilation.GetTypeByMetadataName(TypeNames.LCIDConversionAttribute); - INamedTypeSymbol? suppressGCTransitionAttrType = environment.Compilation.GetTypeByMetadataName(TypeNames.SuppressGCTransitionAttribute); - INamedTypeSymbol? unmanagedCallConvAttrType = environment.Compilation.GetTypeByMetadataName(TypeNames.UnmanagedCallConvAttribute); - INamedTypeSymbol? defaultDllImportSearchPathsAttrType = environment.Compilation.GetTypeByMetadataName(TypeNames.DefaultDllImportSearchPathsAttribute); + INamedTypeSymbol? lcidConversionAttrType = environment.LcidConversionAttrType; + INamedTypeSymbol? suppressGCTransitionAttrType = environment.SuppressGCTransitionAttrType; + INamedTypeSymbol? unmanagedCallConvAttrType = environment.UnmanagedCallConvAttrType; + INamedTypeSymbol? defaultDllImportSearchPathsAttrType = environment.DefaultDllImportSearchPathsAttrType; // Get any attributes of interest on the method AttributeData? generatedDllImportAttr = null; AttributeData? lcidConversionAttr = null; @@ -520,7 +515,7 @@ namespace Microsoft.Interop } } - private static Diagnostic? GetDiagnosticIfInvalidMethodForGeneration(MethodDeclarationSyntax methodSyntax, IMethodSymbol method) + private static DiagnosticInfo? GetDiagnosticIfInvalidMethodForGeneration(MethodDeclarationSyntax methodSyntax, IMethodSymbol method) { // Verify the method has no generic types or defined implementation // and is marked static and partial. @@ -529,7 +524,7 @@ namespace Microsoft.Interop || !methodSyntax.Modifiers.Any(SyntaxKind.StaticKeyword) || !methodSyntax.Modifiers.Any(SyntaxKind.PartialKeyword)) { - return Diagnostic.Create(GeneratorDiagnostics.InvalidAttributedMethodSignature, methodSyntax.Identifier.GetLocation(), method.Name); + return DiagnosticInfo.Create(GeneratorDiagnostics.InvalidAttributedMethodSignature, methodSyntax.Identifier.GetLocation(), method.Name); } // Verify that the types the method is declared in are marked partial. @@ -537,14 +532,14 @@ namespace Microsoft.Interop { if (!typeDecl.Modifiers.Any(SyntaxKind.PartialKeyword)) { - return Diagnostic.Create(GeneratorDiagnostics.InvalidAttributedMethodContainingTypeMissingModifiers, methodSyntax.Identifier.GetLocation(), method.Name, typeDecl.Identifier); + return DiagnosticInfo.Create(GeneratorDiagnostics.InvalidAttributedMethodContainingTypeMissingModifiers, methodSyntax.Identifier.GetLocation(), method.Name, typeDecl.Identifier); } } // Verify the method does not have a ref return if (method.ReturnsByRef || method.ReturnsByRefReadonly) { - return Diagnostic.Create(GeneratorDiagnostics.ReturnConfigurationNotSupported, methodSyntax.Identifier.GetLocation(), "ref return", method.ToDisplayString()); + return DiagnosticInfo.Create(GeneratorDiagnostics.ReturnConfigurationNotSupported, methodSyntax.Identifier.GetLocation(), "ref return", method.ToDisplayString()); } return null; diff --git a/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/DiagnosticInfo.cs b/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/DiagnosticInfo.cs index 91072e6..995de2c 100644 --- a/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/DiagnosticInfo.cs +++ b/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/DiagnosticInfo.cs @@ -1,7 +1,6 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -using System.Collections; using System.Collections.Generic; using System.Collections.Immutable; using System.Linq; @@ -24,7 +23,7 @@ namespace Microsoft.Interop properties: Properties?.Map, messageArgs: MessageArgs.Array.ToArray()); - public static DiagnosticInfo Create(DiagnosticDescriptor descriptor, CodeAnalysis.Location location, params object?[] messageArgs) + public static DiagnosticInfo Create(DiagnosticDescriptor descriptor, Location location, params object?[] messageArgs) { return new DiagnosticInfo() { @@ -36,7 +35,7 @@ namespace Microsoft.Interop }; } - public static DiagnosticInfo Create(DiagnosticDescriptor descriptor, CodeAnalysis.Location location, ImmutableDictionary? properties, params object?[] messageArgs) + public static DiagnosticInfo Create(DiagnosticDescriptor descriptor, Location location, ImmutableDictionary? properties, params object?[] messageArgs) { return new DiagnosticInfo() { @@ -48,7 +47,7 @@ namespace Microsoft.Interop }; } - public static DiagnosticInfo Create(DiagnosticDescriptor descriptor, CodeAnalysis.Location location, IEnumerable? additionalLocations, ImmutableDictionary? properties, params object?[] messageArgs) + public static DiagnosticInfo Create(DiagnosticDescriptor descriptor, Location location, IEnumerable? additionalLocations, ImmutableDictionary? properties, params object?[] messageArgs) { return new DiagnosticInfo() { diff --git a/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/DiagnosticOr.cs b/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/DiagnosticOr.cs index caa15d3..6ad78b0 100644 --- a/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/DiagnosticOr.cs +++ b/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/DiagnosticOr.cs @@ -3,14 +3,9 @@ using System; using System.Collections.Generic; -using System.Collections.Immutable; using System.Diagnostics; -using System.Diagnostics.CodeAnalysis; using System.Linq; -using System.Runtime.InteropServices.ComTypes; -using System.Text; using Microsoft.CodeAnalysis; -using Microsoft.CodeAnalysis.CSharp.Syntax; namespace Microsoft.Interop { diff --git a/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/MethodSignatureDiagnosticLocations.cs b/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/MethodSignatureDiagnosticLocations.cs index 3295ec3..80607c5 100644 --- a/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/MethodSignatureDiagnosticLocations.cs +++ b/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/MethodSignatureDiagnosticLocations.cs @@ -9,7 +9,7 @@ using Microsoft.CodeAnalysis.CSharp.Syntax; namespace Microsoft.Interop { - public sealed record MethodSignatureDiagnosticLocations(string MethodIdentifier, ImmutableArray ManagedParameterLocations, CodeAnalysis.Location FallbackLocation) + public sealed record MethodSignatureDiagnosticLocations(string MethodIdentifier, ImmutableArray ManagedParameterLocations, Location FallbackLocation) { public MethodSignatureDiagnosticLocations(MethodDeclarationSyntax syntax) : this(syntax.Identifier.Text, syntax.ParameterList.Parameters.Select(p => p.Identifier.GetLocation()).ToImmutableArray(), syntax.Identifier.GetLocation()) diff --git a/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/StubEnvironment.cs b/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/StubEnvironment.cs index 769c391..e6319f1 100644 --- a/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/StubEnvironment.cs +++ b/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/StubEnvironment.cs @@ -2,7 +2,6 @@ // The .NET Foundation licenses this file to you under the MIT license. using System; - using Microsoft.CodeAnalysis; namespace Microsoft.Interop @@ -11,5 +10,62 @@ namespace Microsoft.Interop Compilation Compilation, TargetFramework TargetFramework, Version TargetFrameworkVersion, - bool ModuleSkipLocalsInit); + bool ModuleSkipLocalsInit) + { + private Optional _lcidConversionAttrType; + public INamedTypeSymbol? LcidConversionAttrType + { + get + { + if (_lcidConversionAttrType.HasValue) + { + return _lcidConversionAttrType.Value; + } + _lcidConversionAttrType = new Optional(Compilation.GetTypeByMetadataName(TypeNames.LCIDConversionAttribute)); + return _lcidConversionAttrType.Value; + } + } + + private Optional _suppressGCTransitionAttrType; + public INamedTypeSymbol? SuppressGCTransitionAttrType + { + get + { + if (_suppressGCTransitionAttrType.HasValue) + { + return _suppressGCTransitionAttrType.Value; + } + _suppressGCTransitionAttrType = new Optional(Compilation.GetTypeByMetadataName(TypeNames.SuppressGCTransitionAttribute)); + return _suppressGCTransitionAttrType.Value; + } + } + + private Optional _unmanagedCallConvAttrType; + public INamedTypeSymbol? UnmanagedCallConvAttrType + { + get + { + if (_unmanagedCallConvAttrType.HasValue) + { + return _unmanagedCallConvAttrType.Value; + } + _unmanagedCallConvAttrType = new Optional(Compilation.GetTypeByMetadataName(TypeNames.UnmanagedCallConvAttribute)); + return _unmanagedCallConvAttrType.Value; + } + } + + private Optional _defaultDllImportSearchPathsAttrType; + public INamedTypeSymbol? DefaultDllImportSearchPathsAttrType + { + get + { + if (_defaultDllImportSearchPathsAttrType.HasValue) + { + return _defaultDllImportSearchPathsAttrType.Value; + } + _defaultDllImportSearchPathsAttrType = new Optional(Compilation.GetTypeByMetadataName(TypeNames.DefaultDllImportSearchPathsAttribute)); + return _defaultDllImportSearchPathsAttrType.Value; + } + } + } } diff --git a/src/libraries/System.Runtime.InteropServices/tests/ComInterfaceGenerator.Unit.Tests/CompileFails.cs b/src/libraries/System.Runtime.InteropServices/tests/ComInterfaceGenerator.Unit.Tests/CompileFails.cs index c7fc7cd..21b3d8c 100644 --- a/src/libraries/System.Runtime.InteropServices/tests/ComInterfaceGenerator.Unit.Tests/CompileFails.cs +++ b/src/libraries/System.Runtime.InteropServices/tests/ComInterfaceGenerator.Unit.Tests/CompileFails.cs @@ -3,23 +3,19 @@ using System; using System.Collections.Generic; -using System.Collections.Immutable; -using System.IO; +using System.Diagnostics; using System.Linq; using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; +using System.Runtime.InteropServices.Marshalling; using System.Threading.Tasks; using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.Testing; using Microsoft.Interop; using Microsoft.Interop.UnitTests; using Xunit; -using System.Diagnostics; - -using VerifyComInterfaceGenerator = Microsoft.Interop.UnitTests.Verifiers.CSharpSourceGeneratorVerifier; using StringMarshalling = System.Runtime.InteropServices.StringMarshalling; -using System.Runtime.InteropServices.Marshalling; -using Microsoft.CodeAnalysis.CSharp; +using VerifyComInterfaceGenerator = Microsoft.Interop.UnitTests.Verifiers.CSharpSourceGeneratorVerifier; namespace ComInterfaceGenerator.Unit.Tests { @@ -521,5 +517,59 @@ namespace ComInterfaceGenerator.Unit.Tests await test.RunAsync(); } + + [Fact] + public async Task VerifyDiagnosticIsOnAttributedSyntax() + { + string source = $$""" + using System.Runtime.InteropServices; + using System.Runtime.InteropServices.Marshalling; + + partial interface J + { + } + + [GeneratedComInterface] + partial interface {|#0:J|} + { + void Method(); + } + + partial interface J + { + } + """; + DiagnosticResult expectedDiagnostic = VerifyComInterfaceGenerator.Diagnostic(GeneratorDiagnostics.InvalidAttributedInterfaceMissingGuidAttribute) + .WithLocation(0).WithArguments("J"); + await VerifyComInterfaceGenerator.VerifySourceGeneratorAsync(source, expectedDiagnostic); + } + + internal class UnsafeBlocksNotAllowedTest : VerifyComInterfaceGenerator.Test + { + internal UnsafeBlocksNotAllowedTest(bool referenceAncillaryInterop) : base(referenceAncillaryInterop) { } + protected override CompilationOptions CreateCompilationOptions() + => new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary, allowUnsafe: false); + } + + [Fact] + public async Task VerifyGeneratedComInterfaceWithoutAllowUnsafeBlocksWarns() + { + string source = $$""" + using System.Runtime.InteropServices; + using System.Runtime.InteropServices.Marshalling; + + [GeneratedComInterface] + partial interface {|#0:J|} + { + void Method(); + } + """; + DiagnosticResult expectedDiagnostic = VerifyComInterfaceGenerator.Diagnostic(GeneratorDiagnostics.RequiresAllowUnsafeBlocks) + .WithLocation(0); + var test = new UnsafeBlocksNotAllowedTest(false); + test.TestState.Sources.Add(source); + test.ExpectedDiagnostics.Add(expectedDiagnostic); + await test.RunAsync(); + } } } -- 2.7.4