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

Add analyzer to flag converting from DllImport to GeneratedDllImport #367

Merged

Conversation

elinor-fung
Copy link
Member

@elinor-fung elinor-fung commented Nov 19, 2020

This does a coarse check to flag DllImport methods where switching to GeneratedDllImport can be useful. Flags the DllImport method if it has one of:

  • SetLastError=true
  • PreserveSig=false
  • by-ref parameters
  • parameter or return type that is not considered blittable

Disabled by default. To enable, add to editorconfig:
dotnet_diagnostic.DLLIMPORTGENANALYZER015.severity = <severity>

Was able to run against a local build of libraries (not just NetCoreApp) on Windows (using https://github.com/elinor-fung/runtime/tree/useDllImportGenerator)

  • flagged 1564 methods with warnings
  • -os Linux : 1460 warnings
  • -os OSX: 1210 warnings

cc @AaronRobinsonMSFT @jkoritzinsky

@elinor-fung elinor-fung added the area-DllImportGenerator Source Generated stubs for P/Invokes in C# label Nov 19, 2020
@jkoritzinsky
Copy link
Member

Do we want to add a code fix that does the conversion as well?

@elinor-fung
Copy link
Member Author

I think we want a fixer eventually. For our current target of the libraries, since they are used for many target frameworks and we only support net5.0+, we'd have to use the right target framework preprocessor defines (or add a define specifically for when the P/Invoke generator is enabled). I didn't add a fixer here, since I honestly didn't want to deal with that yet and I think just flagging them provides some value right now.

@elinor-fung elinor-fung merged commit a7ccdd8 into dotnet:feature/DllImportGenerator Dec 1, 2020
@elinor-fung elinor-fung deleted the convertAnalyzer branch December 1, 2020 20:03
jkoritzinsky pushed a commit to jkoritzinsky/runtime that referenced this pull request Sep 20, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-DllImportGenerator Source Generated stubs for P/Invokes in C#
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants