-
Notifications
You must be signed in to change notification settings - Fork 232
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix S4200 FN: Add support for LibraryImportAttribute; run only on non…
…-generated code (#6603)
- Loading branch information
1 parent
c1e03c5
commit fb0685b
Showing
5 changed files
with
161 additions
and
7 deletions.
There are no files selected for viewing
13 changes: 13 additions & 0 deletions
13
analyzers/its/sources/ManuallyAddedNoncompliantIssues.CS/Net7/S4200.cs
This file contains 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,13 @@ | ||
using System.Runtime.InteropServices; | ||
|
||
namespace Net7; | ||
|
||
public static partial class S4200 | ||
{ | ||
// Use Goto definition in VS on the methods to see the code generated by the Microsoft.Interop.LibraryImportGenerator | ||
[LibraryImport("foo.dll")] | ||
public static partial void DllImportAttributeAppliedToThisFunction(); // Noncompliant (S4200): Make this native method private and provide a wrapper. | ||
|
||
[LibraryImport("foo.dll", StringMarshalling = StringMarshalling.Utf8)] | ||
public static partial void DllImportAttributeAppliedToGeneratedLocalFunction(string p); // Noncompliant | ||
} |
This file contains 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 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
86 changes: 86 additions & 0 deletions
86
...SonarAnalyzer.UnitTest/TestCases/NativeMethodsShouldBeWrapped.CSharp11.SourceGenerator.cs
This file contains 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,86 @@ | ||
// <auto-generated/> | ||
namespace LibraryImportAttributeTests | ||
{ | ||
public static unsafe partial class ExternMethods | ||
{ | ||
[System.Runtime.InteropServices.DllImportAttribute("foo.dll", EntryPoint = "DllImportAttributeAppliedToThisFunction", ExactSpelling = true)] | ||
public static extern partial void DllImportAttributeAppliedToThisFunction(); | ||
} | ||
} | ||
namespace LibraryImportAttributeTests | ||
{ | ||
public static unsafe partial class ExternMethods | ||
{ | ||
[System.Runtime.InteropServices.DllImportAttribute("foo.dll", EntryPoint = "CompliantDllImportAttributeAppliedToThisFunction", ExactSpelling = true)] | ||
private static extern partial void CompliantDllImportAttributeAppliedToThisFunction(int i); | ||
} | ||
} | ||
namespace LibraryImportAttributeTests | ||
{ | ||
public static unsafe partial class ExternMethods | ||
{ | ||
[System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Interop.LibraryImportGenerator", "7.0.7.6804")] | ||
[System.Runtime.CompilerServices.SkipLocalsInitAttribute] | ||
public static partial void DllImportAttributeAppliedToGeneratedLocalFunction(string p) | ||
{ | ||
byte* __p_native = default; | ||
// Setup - Perform required setup. | ||
global::System.Runtime.InteropServices.Marshalling.Utf8StringMarshaller.ManagedToUnmanagedIn __p_native__marshaller = new(); | ||
try | ||
{ | ||
// Marshal - Convert managed data to native data. | ||
byte* __p_native__stackptr = stackalloc byte[global::System.Runtime.InteropServices.Marshalling.Utf8StringMarshaller.ManagedToUnmanagedIn.BufferSize]; | ||
__p_native__marshaller.FromManaged(p, new System.Span<byte>(__p_native__stackptr, global::System.Runtime.InteropServices.Marshalling.Utf8StringMarshaller.ManagedToUnmanagedIn.BufferSize)); | ||
{ | ||
// PinnedMarshal - Convert managed data to native data that requires the managed data to be pinned. | ||
__p_native = __p_native__marshaller.ToUnmanaged(); | ||
__PInvoke(__p_native); | ||
} | ||
} | ||
finally | ||
{ | ||
// Cleanup - Perform required cleanup. | ||
__p_native__marshaller.Free(); | ||
} | ||
|
||
// Local P/Invoke | ||
[System.Runtime.InteropServices.DllImportAttribute("foo.dll", EntryPoint = "DllImportAttributeAppliedToGeneratedLocalFunction", ExactSpelling = true)] | ||
static extern unsafe void __PInvoke(byte* p); | ||
} | ||
} | ||
} | ||
namespace LibraryImportAttributeTests | ||
{ | ||
public static unsafe partial class ExternMethods | ||
{ | ||
[System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Interop.LibraryImportGenerator", "7.0.7.6804")] | ||
[System.Runtime.CompilerServices.SkipLocalsInitAttribute] | ||
private static partial void CompliantDllImportAttributeAppliedToGeneratedLocalFunction(string p) | ||
{ | ||
byte* __p_native = default; | ||
// Setup - Perform required setup. | ||
global::System.Runtime.InteropServices.Marshalling.Utf8StringMarshaller.ManagedToUnmanagedIn __p_native__marshaller = new(); | ||
try | ||
{ | ||
// Marshal - Convert managed data to native data. | ||
byte* __p_native__stackptr = stackalloc byte[global::System.Runtime.InteropServices.Marshalling.Utf8StringMarshaller.ManagedToUnmanagedIn.BufferSize]; | ||
__p_native__marshaller.FromManaged(p, new System.Span<byte>(__p_native__stackptr, global::System.Runtime.InteropServices.Marshalling.Utf8StringMarshaller.ManagedToUnmanagedIn.BufferSize)); | ||
{ | ||
// PinnedMarshal - Convert managed data to native data that requires the managed data to be pinned. | ||
__p_native = __p_native__marshaller.ToUnmanaged(); | ||
__PInvoke(__p_native); | ||
} | ||
} | ||
finally | ||
{ | ||
// Cleanup - Perform required cleanup. | ||
__p_native__marshaller.Free(); | ||
} | ||
|
||
// Local P/Invoke | ||
[System.Runtime.InteropServices.DllImportAttribute("foo.dll", EntryPoint = "CompliantDllImportAttributeAppliedToGeneratedLocalFunction", ExactSpelling = true)] | ||
static extern unsafe void __PInvoke(byte* p); | ||
} | ||
} | ||
} | ||
|
This file contains 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