|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.Collections.Immutable; |
| 6 | +using System.Linq; |
| 7 | +using Microsoft.CodeAnalysis; |
| 8 | +using Microsoft.CodeAnalysis.CSharp; |
| 9 | +using Microsoft.CodeAnalysis.CSharp.Syntax; |
| 10 | +using Microsoft.CodeAnalysis.Diagnostics; |
| 11 | +using Microsoft.CodeAnalysis.DotnetRuntime.Extensions; |
| 12 | + |
| 13 | +namespace Microsoft.Interop.Analyzers; |
| 14 | + |
| 15 | +[DiagnosticAnalyzer(LanguageNames.CSharp)] |
| 16 | +public sealed class ComClassGeneratorDiagnosticsAnalyzer : DiagnosticAnalyzer |
| 17 | +{ |
| 18 | + public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = |
| 19 | + ImmutableArray.Create( |
| 20 | + GeneratorDiagnostics.RequiresAllowUnsafeBlocks, |
| 21 | + GeneratorDiagnostics.InvalidAttributedClassMissingPartialModifier, |
| 22 | + GeneratorDiagnostics.ClassDoesNotImplementAnyGeneratedComInterface); |
| 23 | + |
| 24 | + public override void Initialize(AnalysisContext context) |
| 25 | + { |
| 26 | + context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); |
| 27 | + context.EnableConcurrentExecution(); |
| 28 | + |
| 29 | + context.RegisterCompilationStartAction(static context => |
| 30 | + { |
| 31 | + bool unsafeCodeIsEnabled = context.Compilation.Options is CSharpCompilationOptions { AllowUnsafe: true }; |
| 32 | + INamedTypeSymbol? generatedComClassAttributeType = context.Compilation.GetBestTypeByMetadataName(TypeNames.GeneratedComClassAttribute); |
| 33 | + |
| 34 | + // We use this type only to report warning diagnostic. We also don't report a warning if there is at least one error. |
| 35 | + // Given that with unsafe code disabled we will get an error on each declaration, we can skip |
| 36 | + // unnecessary work of getting this symbol here |
| 37 | + INamedTypeSymbol? generatedComInterfaceAttributeType = unsafeCodeIsEnabled |
| 38 | + ? context.Compilation.GetBestTypeByMetadataName(TypeNames.GeneratedComInterfaceAttribute) |
| 39 | + : null; |
| 40 | + |
| 41 | + context.RegisterSymbolAction(context => AnalyzeNamedType(context, unsafeCodeIsEnabled, generatedComClassAttributeType, generatedComInterfaceAttributeType), SymbolKind.NamedType); |
| 42 | + }); |
| 43 | + } |
| 44 | + |
| 45 | + private static void AnalyzeNamedType(SymbolAnalysisContext context, bool unsafeCodeIsEnabled, INamedTypeSymbol? generatedComClassAttributeType, INamedTypeSymbol? generatedComInterfaceAttributeType) |
| 46 | + { |
| 47 | + if (context.Symbol is not INamedTypeSymbol { TypeKind: TypeKind.Class } classToAnalyze) |
| 48 | + { |
| 49 | + return; |
| 50 | + } |
| 51 | + |
| 52 | + if (!classToAnalyze.GetAttributes().Any(a => SymbolEqualityComparer.Default.Equals(a.AttributeClass, generatedComClassAttributeType))) |
| 53 | + { |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + foreach (Diagnostic diagnostic in GetDiagnosticsForAnnotatedClass(classToAnalyze, unsafeCodeIsEnabled, generatedComInterfaceAttributeType)) |
| 58 | + { |
| 59 | + context.ReportDiagnostic(diagnostic); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + public static IEnumerable<Diagnostic> GetDiagnosticsForAnnotatedClass(INamedTypeSymbol annotatedClass, bool unsafeCodeIsEnabled, INamedTypeSymbol? generatedComInterfaceAttributeType) |
| 64 | + { |
| 65 | + Location location = annotatedClass.Locations.First(); |
| 66 | + bool hasErrors = false; |
| 67 | + |
| 68 | + if (!unsafeCodeIsEnabled) |
| 69 | + { |
| 70 | + yield return Diagnostic.Create(GeneratorDiagnostics.RequiresAllowUnsafeBlocks, location); |
| 71 | + hasErrors = true; |
| 72 | + } |
| 73 | + |
| 74 | + var declarationNode = (TypeDeclarationSyntax)location.SourceTree.GetRoot().FindNode(location.SourceSpan); |
| 75 | + |
| 76 | + if (!declarationNode.IsInPartialContext(out _)) |
| 77 | + { |
| 78 | + yield return Diagnostic.Create( |
| 79 | + GeneratorDiagnostics.InvalidAttributedClassMissingPartialModifier, |
| 80 | + location, |
| 81 | + annotatedClass); |
| 82 | + hasErrors = true; |
| 83 | + } |
| 84 | + |
| 85 | + if (hasErrors) |
| 86 | + { |
| 87 | + // If we already reported at least one error avoid stacking a warning on top of it |
| 88 | + yield break; |
| 89 | + } |
| 90 | + |
| 91 | + foreach (INamedTypeSymbol iface in annotatedClass.AllInterfaces) |
| 92 | + { |
| 93 | + if (iface.GetAttributes().FirstOrDefault(a => SymbolEqualityComparer.Default.Equals(a.AttributeClass, generatedComInterfaceAttributeType)) is { } generatedComInterfaceAttribute && |
| 94 | + GeneratedComInterfaceCompilationData.GetDataFromAttribute(generatedComInterfaceAttribute).Options.HasFlag(ComInterfaceOptions.ManagedObjectWrapper)) |
| 95 | + { |
| 96 | + yield break; |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + // Class doesn't implement any generated COM interface. Report a warning about that |
| 101 | + yield return Diagnostic.Create( |
| 102 | + GeneratorDiagnostics.ClassDoesNotImplementAnyGeneratedComInterface, |
| 103 | + location, |
| 104 | + annotatedClass); |
| 105 | + } |
| 106 | +} |
0 commit comments