-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Extract diagnostic reporting out of COM class generator #125308
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
Merged
jkoritzinsky
merged 8 commits into
dotnet:main
from
DoctorKrolic:com-class-generator-diagnostics
Mar 10, 2026
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
3cb6384
Move disgnostic reporting out from COM class generator
DoctorKrolic 083004a
Compare symbols instead of relying on `ToString` representation
DoctorKrolic ed0c42d
Show that analyzer reports multiple diagnostics at once
DoctorKrolic 50897e7
Avoid reporting a warning if an error is already present
DoctorKrolic bb21bcb
Simplify
DoctorKrolic 2e4c827
Merge branch 'main' into com-class-generator-diagnostics
AaronRobinsonMSFT 34a5007
Remove redundant verification arguments from diagnostics without any …
DoctorKrolic 7f3bce9
Reuse analysis code
DoctorKrolic File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
106 changes: 106 additions & 0 deletions
106
...teropServices/gen/ComInterfaceGenerator/Analyzers/ComClassGeneratorDiagnosticsAnalyzer.cs
This file contains hidden or 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,106 @@ | ||
| // 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.Generic; | ||
| using System.Collections.Immutable; | ||
| using System.Linq; | ||
| using Microsoft.CodeAnalysis; | ||
| using Microsoft.CodeAnalysis.CSharp; | ||
| using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
| using Microsoft.CodeAnalysis.Diagnostics; | ||
| using Microsoft.CodeAnalysis.DotnetRuntime.Extensions; | ||
|
|
||
| namespace Microsoft.Interop.Analyzers; | ||
|
|
||
| [DiagnosticAnalyzer(LanguageNames.CSharp)] | ||
| public sealed class ComClassGeneratorDiagnosticsAnalyzer : DiagnosticAnalyzer | ||
| { | ||
| public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = | ||
| ImmutableArray.Create( | ||
| GeneratorDiagnostics.RequiresAllowUnsafeBlocks, | ||
| GeneratorDiagnostics.InvalidAttributedClassMissingPartialModifier, | ||
| GeneratorDiagnostics.ClassDoesNotImplementAnyGeneratedComInterface); | ||
|
|
||
| public override void Initialize(AnalysisContext context) | ||
| { | ||
| context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); | ||
| context.EnableConcurrentExecution(); | ||
|
|
||
| context.RegisterCompilationStartAction(static context => | ||
| { | ||
| bool unsafeCodeIsEnabled = context.Compilation.Options is CSharpCompilationOptions { AllowUnsafe: true }; | ||
| INamedTypeSymbol? generatedComClassAttributeType = context.Compilation.GetBestTypeByMetadataName(TypeNames.GeneratedComClassAttribute); | ||
|
|
||
| // We use this type only to report warning diagnostic. We also don't report a warning if there is at least one error. | ||
| // Given that with unsafe code disabled we will get an error on each declaration, we can skip | ||
| // unnecessary work of getting this symbol here | ||
| INamedTypeSymbol? generatedComInterfaceAttributeType = unsafeCodeIsEnabled | ||
| ? context.Compilation.GetBestTypeByMetadataName(TypeNames.GeneratedComInterfaceAttribute) | ||
| : null; | ||
|
|
||
| context.RegisterSymbolAction(context => AnalyzeNamedType(context, unsafeCodeIsEnabled, generatedComClassAttributeType, generatedComInterfaceAttributeType), SymbolKind.NamedType); | ||
| }); | ||
| } | ||
|
|
||
| private static void AnalyzeNamedType(SymbolAnalysisContext context, bool unsafeCodeIsEnabled, INamedTypeSymbol? generatedComClassAttributeType, INamedTypeSymbol? generatedComInterfaceAttributeType) | ||
| { | ||
| if (context.Symbol is not INamedTypeSymbol { TypeKind: TypeKind.Class } classToAnalyze) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| if (!classToAnalyze.GetAttributes().Any(a => SymbolEqualityComparer.Default.Equals(a.AttributeClass, generatedComClassAttributeType))) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| foreach (Diagnostic diagnostic in GetDiagnosticsForAnnotatedClass(classToAnalyze, unsafeCodeIsEnabled, generatedComInterfaceAttributeType)) | ||
| { | ||
| context.ReportDiagnostic(diagnostic); | ||
| } | ||
| } | ||
|
|
||
| public static IEnumerable<Diagnostic> GetDiagnosticsForAnnotatedClass(INamedTypeSymbol annotatedClass, bool unsafeCodeIsEnabled, INamedTypeSymbol? generatedComInterfaceAttributeType) | ||
| { | ||
| Location location = annotatedClass.Locations.First(); | ||
| bool hasErrors = false; | ||
|
|
||
| if (!unsafeCodeIsEnabled) | ||
| { | ||
| yield return Diagnostic.Create(GeneratorDiagnostics.RequiresAllowUnsafeBlocks, location); | ||
| hasErrors = true; | ||
| } | ||
DoctorKrolic marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| var declarationNode = (TypeDeclarationSyntax)location.SourceTree.GetRoot().FindNode(location.SourceSpan); | ||
|
|
||
| if (!declarationNode.IsInPartialContext(out _)) | ||
| { | ||
| yield return Diagnostic.Create( | ||
| GeneratorDiagnostics.InvalidAttributedClassMissingPartialModifier, | ||
| location, | ||
| annotatedClass); | ||
| hasErrors = true; | ||
| } | ||
|
|
||
| if (hasErrors) | ||
| { | ||
| // If we already reported at least one error avoid stacking a warning on top of it | ||
| yield break; | ||
| } | ||
|
|
||
| foreach (INamedTypeSymbol iface in annotatedClass.AllInterfaces) | ||
| { | ||
| if (iface.GetAttributes().FirstOrDefault(a => SymbolEqualityComparer.Default.Equals(a.AttributeClass, generatedComInterfaceAttributeType)) is { } generatedComInterfaceAttribute && | ||
| GeneratedComInterfaceCompilationData.GetDataFromAttribute(generatedComInterfaceAttribute).Options.HasFlag(ComInterfaceOptions.ManagedObjectWrapper)) | ||
| { | ||
| yield break; | ||
| } | ||
| } | ||
|
|
||
| // Class doesn't implement any generated COM interface. Report a warning about that | ||
| yield return Diagnostic.Create( | ||
| GeneratorDiagnostics.ClassDoesNotImplementAnyGeneratedComInterface, | ||
| location, | ||
| annotatedClass); | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.