-
Notifications
You must be signed in to change notification settings - Fork 232
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 S4200 FN: Add support for LibraryImportAttribute; run only on non-generated code #6603
Merged
andrei-epure-sonarsource
merged 11 commits into
master
from
Martin/S4200_LibraryImportAttribute
Jan 17, 2023
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
c44e7e6
Add IT for S4200
martin-strecker-sonarsource d13b33d
Add test case for DllImport applied to the method by the generator
martin-strecker-sonarsource 8778236
Fix auto-generated code
martin-strecker-sonarsource c963d32
Add test case for DllImportAttributeAppliedToGeneratedLocalFunction
martin-strecker-sonarsource 456fbb0
Support LibraryImportAttribute
martin-strecker-sonarsource 38cdf29
Add wrapper tests and fix support for LibraryImpoerAttribute
martin-strecker-sonarsource 0142790
Add message to assertion
martin-strecker-sonarsource 3e0a781
Use expression body function in one test case
martin-strecker-sonarsource 7165f43
Add compliant wrapper
martin-strecker-sonarsource 804ef7e
Add a comment about re-generating the SG code.
martin-strecker-sonarsource 88283c0
Grammar
martin-strecker-sonarsource File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 | ||
{ | ||
andrei-epure-sonarsource marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[education] why is this necessary to not be raised on generated code?
I get why it's necessary for the case of
LibraryImport
(to not raise the issue twice + users don't control the source generator) - is it the case that in general we should not raise on generated code ever?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. It was requested by Pavel at one point that we should fix this here. I added it as item 4 on the todo list here #6488 when he suggested it. It is also needed to properly support the LibraryImport attribute.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the PR name should reflect that IMO because it's a change of functionality for the whole rule and it's not only related to adding support for a new attribute