Skip to content

Remove redundant configuration from [DllImport] declaration #33808

Closed

Description

@terrajobst

Immo proposal here: #33808 (comment)

Flag places where an attribute is set on a DllImport that's already the default value.

Category: Style

Activity

  1. added
    api-suggestion
    code-analyzer
    on Mar 19, 2020
  2. added
    untriaged
    on Mar 19, 2020
  3. added this to the Future milestone on Mar 20, 2020
  4. added
    code-fixer
    on Mar 21, 2020
  5. jeffhandley commented on Mar 21, 2020

    @jeffhandley
    Member

    Estimates:

    • Analyzer: Small
    • Fixer: Small
  6. removed
    untriaged
    on Mar 21, 2020
  7. carlossanlop commented on Dec 7, 2021

    @carlossanlop
    Contributor

    @terrajobst can you please clarify what you meant in the description? What does it mean "an attribute is set on a DllImport"?

    cc @AaronRobinsonMSFT since you linked #34831 and maybe you understand what the description means.

  8. alexrp commented on Dec 7, 2021

    @alexrp
    Contributor

    E.g. [DllImport(..., SetLastError = false)] or [DllImport(..., CharSet = CharSet.Ansi)] (etc) are pointless because those are already the default values.

  9. AaronRobinsonMSFT commented on Dec 7, 2021

    @AaronRobinsonMSFT
    Member

    @carlossanlop the response from @alexrp is what I interpreted as the intent of this issue.

  10. carlossanlop commented on Dec 7, 2021

    @carlossanlop
    Contributor

    Ok thanks. Then these are the default values set for the public fields of the DllImportAttribute class: https://docs.microsoft.com/en-us/dotnet/standard/native-interop/best-practices#dllimport-attribute-settings

    I'm curious about PreserveSig: it's a boolean, but the document says true is its default. Where is it set? I don't see any logic setting it to true in the ref file or in the src file.

  11. alexrp commented on Dec 7, 2021

    @alexrp
    Contributor

    Note that DllImportAttribute is one of a number of pseudo-attributes. It's actually understood by the compiler and emitted as special metadata. So Roslyn is really where you should be looking for the defaults.

    Example: https://sharplab.io/#v2:EYLgxg9gTgpgtADwGwBYA0AXEBLANgHwAEAGAAkIEYA6AJQFcA7DbAWxioEkmYoIAHAMo8AbtjAwAzgG4AsAChCAZnIAmUgGFSAb3mk9pANoARXLg4s+0DAAoARLmzAAZhAi20pAAqwJImAOwAc1IAXlIMKDoYAEoAXV19JXIKJFIYBAweBnIUUhcIa2jZOQBfIA

  12. AaronRobinsonMSFT commented on Dec 7, 2021

    @AaronRobinsonMSFT
    Member

    @carlossanlop The PreserveSig is a pseudo-attribute and is encoded directly in metadata. See section II.23.1.11 of ECMA-335.

  13. carlossanlop commented on Dec 7, 2021

    @carlossanlop
    Contributor

    Thanks.

    Should this analyzer also apply to the new GeneratedDllImportAttribute? Unlike DllImportAttribute, this new class uses properties, but they are similarly named.

  14. carlossanlop commented on Dec 7, 2021

    @carlossanlop
    Contributor

    Suggested severity: Info
    Suggested category: Style
    Suggested milestone: Future

    These are all the public fields in the DllImportAttribute class, and their documented default value, which the analyzer should flag:

    public sealed partial class DllImportAttribute : System.Attribute
    {
        // true by default
        // https://docs.microsoft.com/en-us/dotnet/api/system.runtime.interopservices.dllimportattribute.bestfitmapping?view=net-6.0#remarks
        public bool BestFitMapping;
    
        // The default value is Winapi on Windows, and Cdecl on all other platforms.
        // https://docs.microsoft.com/en-us/dotnet/api/system.runtime.interopservices.dllimportattribute.callingconvention?view=net-6.0#remarks
        public System.Runtime.InteropServices.CallingConvention CallingConvention;
    
        // If DefaultCharSetAttribute is applied on the module in C# or Visual Basic code,
        // then the C# or Visual Basic compiler will emit the provided CharSet by default instead of using CharSet.None.
        // F# does not support the DefaultCharSetAttribute, and always emits CharSet.None by default.
        // https://docs.microsoft.com/en-us/dotnet/standard/native-interop/charset
        public System.Runtime.InteropServices.CharSet CharSet;
    
        // Optional API name, the analyzer should ignore this field
        public string? EntryPoint;
    
        // false by default
        // https://docs.microsoft.com/en-us/dotnet/standard/native-interop/best-practices#dllimport-attribute-settings
        public bool ExactSpelling;
    
        // true by default
        // https://docs.microsoft.com/en-us/dotnet/standard/native-interop/best-practices#dllimport-attribute-settings
        public bool PreserveSig;
    
        // false by default
        // https://docs.microsoft.com/en-us/dotnet/standard/native-interop/best-practices#dllimport-attribute-settings
        public bool SetLastError;
    
        // false by default
        // https://docs.microsoft.com/en-us/dotnet/api/system.runtime.interopservices.dllimportattribute.throwonunmappablechar?view=net-6.0#remarks
        public bool ThrowOnUnmappableChar;
    
        // This is mandatory, the analyzer should ignore this field
        public string Value { get; }
    }

    If we want to do the same with GenerateDllImport, then these are the properties exposed by this attribute:

    #if DLLIMPORT_GENERATOR_TEST
        public
    #else
        internal
    #endif
    sealed class GeneratedDllImportAttribute : Attribute
    {
        public CharSet CharSet { get; set; }
        public string? EntryPoint { get; set; }
        public bool ExactSpelling { get; set; }
        public bool PreserveSig { get; set; }
        public bool SetLastError { get; set; }
    
        public string Value { get; private set; }
    }
  15. added
    api-ready-for-review
    and removed
    api-suggestion
    on Dec 7, 2021
  16. terrajobst commented on Dec 14, 2021

    @terrajobst
    ContributorAuthor

    Video

    • We're not convinced that over-specification is a bad practice; these are expert-level APIs and many experts like being explicit instead of relying on defaults.
    • However, there is value in P/Invoke analyzers, which, could flag when people are calling Unicode APIs (*W) but specify ANSI encoding, for instance.
    • Let's close this one for now. If there are other suggestions, we can open new issues.
  17. ghost locked as resolved and limited conversation to collaborators on Jan 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-ready-for-reviewarea-System.Runtime.InteropServicescode-analyzercode-fixer

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions