Skip to content

Extract diagnostic reporting out of COM class generator#125308

Merged
jkoritzinsky merged 8 commits intodotnet:mainfrom
DoctorKrolic:com-class-generator-diagnostics
Mar 10, 2026
Merged

Extract diagnostic reporting out of COM class generator#125308
jkoritzinsky merged 8 commits intodotnet:mainfrom
DoctorKrolic:com-class-generator-diagnostics

Conversation

@DoctorKrolic
Copy link
Contributor

No description provided.

@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label Mar 8, 2026
@dotnet-policy-service
Copy link
Contributor

Tagging subscribers to this area: @dotnet/interop-contrib
See info in area-owners.md if you want to be subscribed.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refactors COM class generation by moving diagnostic reporting responsibilities out of ComClassGenerator and into a dedicated Roslyn analyzer, keeping the generator focused on code emission.

Changes:

  • Introduced ComClassGeneratorDiagnosticsAnalyzer to report COM class diagnostics via analyzer infrastructure.
  • Updated COM class diagnostics tests to validate analyzer-reported diagnostics rather than generator-reported diagnostics.
  • Simplified generator plumbing by removing shared “diagnostic splitting/reporting” helper extensions and adjusting ComClassInfo to return nullable results instead of DiagnosticOr<T>.

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
src/libraries/System.Runtime.InteropServices/tests/ComInterfaceGenerator.Unit.Tests/ComClassGeneratorOutputShape.cs Small assertion refactor for type argument validation.
src/libraries/System.Runtime.InteropServices/tests/ComInterfaceGenerator.Unit.Tests/ComClassGeneratorDiagnostics.cs Switches verification to run the new diagnostics analyzer and expands diagnostic test cases.
src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/IncrementalGeneratorInitializationContextExtensions.cs Removes diagnostics registration helpers no longer used after diagnostics extraction.
src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/DiagnosticOr.cs Removes DiagnosticOr<T> incremental-provider helper extensions now unused.
src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/ComClassInfo.cs Converts from DiagnosticOr<ComClassInfo> to ComClassInfo? and defers diagnostics to the analyzer.
src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/ComClassGenerator.cs Updates generator pipeline to use TryGetFrom and filter null results.
src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/Analyzers/ComClassGeneratorDiagnosticsAnalyzer.cs New analyzer that reports COM class diagnostics and suppresses warnings when errors are present.
Comments suppressed due to low confidence (1)

src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/ComClassGenerator.cs:40

  • After filtering with .Where(info => info is not null), info is still a nullable reference type (ComClassInfo?) for subsequent stages. With Nullable=enable and TreatWarningsAsErrors=true in this repo, passing info to ItemAndSyntaxes<ComClassInfo> and dereferencing it will produce nullability warnings that fail the build. Consider unwrapping to a non-nullable provider (e.g., add a .Select(info => info!) after the Where, or use a helper that returns IncrementalValuesProvider<ComClassInfo>).
                .Where(static info => info is not null);

            var classInfoType = attributedClasses
                .Select(static (info, ct) => new ItemAndSyntaxes<ComClassInfo>(info,
                [
                    GenerateClassInfoType(info.ImplementedInterfacesNames.Array).NormalizeWhitespace(),
                    GenerateClassInfoAttributeOnUserType(info.ContainingSyntaxContext, info.ClassSyntax).NormalizeWhitespace()
                ]));

Copy link
Member

@jtschuster jtschuster left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a couple nits from me

Copy link
Member

@jtschuster jtschuster left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@jkoritzinsky jkoritzinsky merged commit 093df0c into dotnet:main Mar 10, 2026
100 of 102 checks passed
@DoctorKrolic DoctorKrolic deleted the com-class-generator-diagnostics branch March 11, 2026 05:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-System.Runtime.InteropServices community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants