forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create analyzer in ComInterfaceGenerator to enforce the generator is …
…used with IUnknown only (dotnet#78189) Creates an analyzer that warns if the GeneratedComInterfaceAttribute is used with InterfaceTypeAttribute that has an argument that is not 'InterfaceIsIUnknown' Co-authored-by: Jeremy Koritzinsky <[email protected]> Co-authored-by: Elinor Fung <[email protected]>
- Loading branch information
1 parent
6e1ca64
commit 1630725
Showing
26 changed files
with
954 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
...System.Runtime.InteropServices/gen/ComInterfaceGenerator/Analyzers/AnalyzerDiagnostics.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System; | ||
using Microsoft.CodeAnalysis; | ||
namespace Microsoft.Interop.Analyzers | ||
{ | ||
public static class AnalyzerDiagnostics | ||
{ | ||
public static class Ids | ||
{ | ||
public const string Prefix = "SYSLIB"; | ||
public const string InvalidGeneratedComAttributeUsage = Prefix + "1090"; | ||
} | ||
|
||
private const string Category = "ComInterfaceGenerator"; | ||
|
||
private static LocalizableResourceString GetResourceString(string resourceName) | ||
{ | ||
return new LocalizableResourceString(resourceName, SR.ResourceManager, typeof(FxResources.Microsoft.Interop.ComInterfaceGenerator.SR)); | ||
} | ||
|
||
public static readonly DiagnosticDescriptor InterfaceTypeNotSupported = | ||
new DiagnosticDescriptor( | ||
Ids.InvalidGeneratedComAttributeUsage, | ||
GetResourceString(nameof(SR.InterfaceTypeNotSupportedTitle)), | ||
GetResourceString(nameof(SR.InterfaceTypeNotSupportedMessage)), | ||
Category, | ||
DiagnosticSeverity.Error, | ||
isEnabledByDefault: true, | ||
description: GetResourceString(nameof(SR.InterfaceTypeNotSupportedMessage))); | ||
} | ||
} |
91 changes: 91 additions & 0 deletions
91
...ropServices/gen/ComInterfaceGenerator/Analyzers/GeneratedComInterfaceAttributeAnalyzer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// 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.Immutable; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Runtime.InteropServices; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
|
||
namespace Microsoft.Interop.Analyzers | ||
{ | ||
/// <summary> | ||
/// Validates that if an interface has GeneratedComInterfaceAttribute and <see cref="InterfaceTypeAttribute"/>, | ||
/// the <see cref="InterfaceTypeAttribute"/> is given a <see cref="ComInterfaceType"/> that is supported by the generator. | ||
/// </summary> | ||
[DiagnosticAnalyzer(LanguageNames.CSharp)] | ||
public class GeneratedComInterfaceAttributeAnalyzer : DiagnosticAnalyzer | ||
{ | ||
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } | ||
= ImmutableArray.Create(AnalyzerDiagnostics.InterfaceTypeNotSupported); | ||
|
||
public static readonly ImmutableArray<ComInterfaceType> SupportedComInterfaceTypes = ImmutableArray.Create(ComInterfaceType.InterfaceIsIUnknown); | ||
|
||
public override void Initialize(AnalysisContext context) | ||
{ | ||
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); | ||
context.EnableConcurrentExecution(); | ||
context.RegisterSymbolAction((context) => | ||
{ | ||
INamedTypeSymbol typeSymbol = (INamedTypeSymbol)context.Symbol; | ||
if (typeSymbol.TypeKind != TypeKind.Interface) | ||
return; | ||
|
||
ImmutableArray<AttributeData> customAttributes = typeSymbol.GetAttributes(); | ||
if (customAttributes.Length == 0) | ||
return; | ||
|
||
// Interfaces with both GeneratedComInterfaceAttribute and InterfaceTypeAttribute should only have [InterfaceTypeAttribute(InterfaceIsIUnknown)] | ||
if (GetAttribute(typeSymbol, TypeNames.GeneratedComInterfaceAttribute, out _) | ||
&& GetAttribute(typeSymbol, TypeNames.InterfaceTypeAttribute, out AttributeData? comInterfaceAttribute) | ||
&& !InterfaceTypeAttributeIsSupported(comInterfaceAttribute, out string unsupportedValue)) | ||
{ | ||
context.ReportDiagnostic(comInterfaceAttribute.CreateDiagnostic(AnalyzerDiagnostics.InterfaceTypeNotSupported, unsupportedValue)); | ||
} | ||
}, SymbolKind.NamedType); | ||
} | ||
|
||
private static bool InterfaceTypeAttributeIsSupported(AttributeData comInterfaceAttribute, out string argument) | ||
{ | ||
if (comInterfaceAttribute.ConstructorArguments.IsEmpty) | ||
{ | ||
argument = "<empty>"; | ||
return false; | ||
} | ||
TypedConstant ctorArg0 = comInterfaceAttribute.ConstructorArguments[0]; | ||
ComInterfaceType interfaceType; | ||
|
||
argument = ctorArg0.ToCSharpString(); | ||
switch (ctorArg0.Type.ToDisplayString()) | ||
{ | ||
case TypeNames.ComInterfaceTypeAttribute: | ||
interfaceType = (ComInterfaceType)ctorArg0.Value; | ||
break; | ||
case TypeNames.System_Int16: | ||
case TypeNames.@short: | ||
interfaceType = (ComInterfaceType)(short)ctorArg0.Value; | ||
break; | ||
default: | ||
return false; | ||
} | ||
|
||
return SupportedComInterfaceTypes.Contains(interfaceType); | ||
} | ||
|
||
private static bool GetAttribute(ISymbol symbol, string attributeDisplayName, [NotNullWhen(true)] out AttributeData? attribute) | ||
{ | ||
foreach (AttributeData attr in symbol.GetAttributes()) | ||
{ | ||
if (attr.AttributeClass?.ToDisplayString() == attributeDisplayName) | ||
{ | ||
attribute = attr; | ||
return true; | ||
} | ||
} | ||
|
||
attribute = null; | ||
return false; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.