Skip to content
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

Fix method counting for interfaces when scanning references #1427

Merged
merged 1 commit into from
Jan 13, 2023

Conversation

riverar
Copy link
Collaborator

@riverar riverar commented Jan 13, 2023

ClangSharpSourceWinmdGenerator maintains a cache of encountered interface types and their method counts. This information is currently obtained during two phases:

  1. a walk of input syntax trees
  2. a walk of compilation references

The input syntax trees (1) contain interface types with all inherited and local methods already emitted. When ClangSharpSourceWinmdGenerator performs a MethodDeclarationSyntax count, it caches the correct value.

Example input fragment:

[NativeInheritance("IInspectable")]
public unsafe partial struct IDragDropManagerInterop
{
  public delegate int _QueryInterface(IDragDropManagerInterop* pThis, [NativeTypeName("const IID &"),Const]Guid* riid, [ComOutPtr]void** ppvObject);
  public delegate uint _AddRef(IDragDropManagerInterop* pThis);
  public delegate uint _Release(IDragDropManagerInterop* pThis);
  ...
}

The compilation references (2) (e.g. Windows.Win32.winmd) however contain interface types that do not have these methods emitted. ClangSharpSourceWinmdGenerator instead counts the number of methods on the type specified by NativeInheritance and moves on, resulting in an inaccurate count. This count is used later to skip over inherited methods when emitting interface method metadata, resulting in interfaces with invalid method members.

Example output metadata:

public interface IFoo : IInspectable
{
  // 3 IUnknown methods skipped over

  // 3 IInspectable methods that didn't get skipped over
  unsafe HRESULT GetIids(...);
  unsafe HRESULT GetRuntimeClassName(...);
  unsafe HRESULT GetTrustLevel(...);
  unsafe HRESULT IFooFirstMethod(...);
}

This impacts all projects with references to Windows.Win32.winmd and the Microsoft.Windows.WinmdGenerator. (Generally everyone outside of win32metadata.)

This PR fixes method counting during the compilation reference walk by also counting the methods for each interface implemented by an interface type. This information is obtained via the InterfaceImpl table (II.22.23).

Fixes: #1426

@BenJKuhn
Copy link
Member

Change looks great, thanks for including new test collateral, and for tackling this issue!

@mikebattista mikebattista merged commit 589461f into microsoft:main Jan 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Generating metadata for IInspectable-derived interfaces is broken
3 participants