-
Notifications
You must be signed in to change notification settings - Fork 470
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
Prefer ExactSpelling=true for known extern methods #4130
Prefer ExactSpelling=true for known extern methods #4130
Conversation
Codecov Report
@@ Coverage Diff @@
## master #4130 +/- ##
==========================================
+ Coverage 95.78% 95.80% +0.01%
==========================================
Files 1163 1173 +10
Lines 262725 266697 +3972
Branches 15829 16077 +248
==========================================
+ Hits 251655 255508 +3853
- Misses 9064 9130 +66
- Partials 2006 2059 +53 |
Another question left open is if we want to include name manged functions. excluding them should be easy, and in my analysis no name mangled function exhibited behaviour which we want to catch with this analyzer @jkotas |
...s/Microsoft.NetCore.Analyzers/InteropServices/PreferIsExactSpellingIsTrueForKnownApisTest.cs
Outdated
Show resolved
Hide resolved
...Microsoft.NetCore.Analyzers/InteropServices/PreferIsExactSpellingIsTrueForKnownApis.Fixer.cs
Outdated
Show resolved
Hide resolved
...Microsoft.NetCore.Analyzers/InteropServices/PreferIsExactSpellingIsTrueForKnownApis.Fixer.cs
Outdated
Show resolved
Hide resolved
...Microsoft.NetCore.Analyzers/InteropServices/PreferIsExactSpellingIsTrueForKnownApis.Fixer.cs
Outdated
Show resolved
Hide resolved
...Microsoft.NetCore.Analyzers/InteropServices/PreferIsExactSpellingIsTrueForKnownApis.Fixer.cs
Outdated
Show resolved
Hide resolved
...Microsoft.NetCore.Analyzers/InteropServices/PreferIsExactSpellingIsTrueForKnownApis.Fixer.cs
Outdated
Show resolved
Hide resolved
...Microsoft.NetCore.Analyzers/InteropServices/PreferIsExactSpellingIsTrueForKnownApis.Fixer.cs
Outdated
Show resolved
Hide resolved
...Microsoft.NetCore.Analyzers/InteropServices/PreferIsExactSpellingIsTrueForKnownApis.Fixer.cs
Outdated
Show resolved
Hide resolved
...Microsoft.NetCore.Analyzers/InteropServices/PreferIsExactSpellingIsTrueForKnownApis.Fixer.cs
Outdated
Show resolved
Hide resolved
...Microsoft.NetCore.Analyzers/InteropServices/PreferIsExactSpellingIsTrueForKnownApis.Fixer.cs
Outdated
Show resolved
Hide resolved
...Microsoft.NetCore.Analyzers/InteropServices/PreferIsExactSpellingIsTrueForKnownApis.Fixer.cs
Outdated
Show resolved
Hide resolved
@Evangelink thank you for the review, I believe I adressed all points which were not directly responded to and unclear. |
…nto feature/ExactSpellingAnalyzer # Conflicts: # src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/MicrosoftNetCoreAnalyzersResources.resx
...Microsoft.NetCore.Analyzers/InteropServices/PreferIsExactSpellingIsTrueForKnownApis.Fixer.cs
Outdated
Show resolved
Hide resolved
...Microsoft.NetCore.Analyzers/InteropServices/PreferIsExactSpellingIsTrueForKnownApis.Fixer.cs
Outdated
Show resolved
Hide resolved
...Microsoft.NetCore.Analyzers/InteropServices/PreferIsExactSpellingIsTrueForKnownApis.Fixer.cs
Outdated
Show resolved
Hide resolved
...Microsoft.NetCore.Analyzers/InteropServices/PreferIsExactSpellingIsTrueForKnownApis.Fixer.cs
Outdated
Show resolved
Hide resolved
...Microsoft.NetCore.Analyzers/InteropServices/PreferIsExactSpellingIsTrueForKnownApis.Fixer.cs
Outdated
Show resolved
Hide resolved
src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/MicrosoftNetCoreAnalyzersResources.resx
Outdated
Show resolved
Hide resolved
...s/Microsoft.NetCore.Analyzers/InteropServices/PreferIsExactSpellingIsTrueForKnownApisTest.cs
Outdated
Show resolved
Hide resolved
...s/Microsoft.NetCore.Analyzers/InteropServices/PreferIsExactSpellingIsTrueForKnownApisTest.cs
Outdated
Show resolved
Hide resolved
...s/Microsoft.NetCore.Analyzers/InteropServices/PreferIsExactSpellingIsTrueForKnownApisTest.cs
Outdated
Show resolved
Hide resolved
.../Core/Microsoft.NetCore.Analyzers/InteropServices/PreferIsExactSpellingIsTrueForKnownApis.cs
Outdated
Show resolved
Hide resolved
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.
A couple more cosmetic changes and then it's ok on my side.
Tagging @mavasani for extra review.
...oft.NetCore.Analyzers/InteropServices/CSharpPreferIsExactSpellingIsTrueForKnownApis.Fixer.cs
Outdated
Show resolved
Hide resolved
.../Core/Microsoft.NetCore.Analyzers/InteropServices/PreferIsExactSpellingIsTrueForKnownApis.cs
Outdated
Show resolved
Hide resolved
src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/MicrosoftNetCoreAnalyzersResources.resx
Outdated
Show resolved
Hide resolved
src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/MicrosoftNetCoreAnalyzersResources.resx
Outdated
Show resolved
Hide resolved
Thank you @Evangelink for the detailed review. For future PR's I have a way better understanding of desired formatting and can adapt accordingly. The remaining issues should be fixed now |
var str = MicrosoftNetCoreAnalyzersResources.ResourceManager.GetObject("KnownApiList", CultureInfo.InvariantCulture); | ||
using var stringReader = new StringReader(str.ToString()); | ||
using var reader = XmlReader.Create(stringReader); | ||
var knownApis = (KnownApi[])serializer.Deserialize(reader); |
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.
Why are we doing all these XML reader shenanigans? Can we please hard code the known APIs in source, just like all other analyzers do?
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.
Well there are currently 160k entries for known methods. I initially used the new generator feature to generate a compile time initialised dictionary with the constants, but the resulting compile time was an order of magnitude larger than compiling without the generated dictionary
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.
Well there are currently 160k entries.
We would absolutely need performance measurements with this analyzer enabled then. It seems the initial load be pretty expensive?
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 the initial load is the heaviest part, it is below half a second though and once its done it is very cheap to query.
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.
Excluding name mangled symbols this would shring to 96k method symbols
{ | ||
[Serializable] | ||
#pragma warning disable CA1034 // Nested types should not be visible | ||
public class KnownApi //Only made public as XmlSerializer requires it |
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.
Please delete and hard code the dictionary in source.
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.
See above
...crosoft.NetCore.Analyzers/InteropServices/PreferIsExactSpellingIsTrueForKnownApisAnalyzer.cs
Outdated
Show resolved
Hide resolved
...crosoft.NetCore.Analyzers/InteropServices/PreferIsExactSpellingIsTrueForKnownApisAnalyzer.cs
Outdated
Show resolved
Hide resolved
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.
Marking as request changes till we figure out the branching for post .NET5 analyzers. Tagging @jmarolf and @jeffhandley
Once this is established I have branches already implemented for all analyers marked as api-approved in the dotnet/runtime repository with the feedback from this branch already ported. I would hold of sending a pr until this is figured out. |
@mavasani which release is this intended for? .NET 5 or .NET 6 Preview 1? |
@jmarolf I believe all new .NET SDK API analyzers are now for post .NET5. @jeffhandley to confirm. |
That is correct. New analyzers should target 6.0 at this point. |
alright then this getting merged into master is the correct thing. @mavasani can you unblock this merging? |
Dismissing my block, I am hoping to review the PR later this week.
...crosoft.NetCore.Analyzers/InteropServices/PreferIsExactSpellingIsTrueForKnownApisAnalyzer.cs
Show resolved
Hide resolved
...crosoft.NetCore.Analyzers/InteropServices/PreferIsExactSpellingIsTrueForKnownApisAnalyzer.cs
Outdated
Show resolved
Hide resolved
...crosoft.NetCore.Analyzers/InteropServices/PreferIsExactSpellingIsTrueForKnownApisAnalyzer.cs
Outdated
Show resolved
Hide resolved
...crosoft.NetCore.Analyzers/InteropServices/PreferIsExactSpellingIsTrueForKnownApisAnalyzer.cs
Show resolved
Hide resolved
...crosoft.NetCore.Analyzers/InteropServices/PreferIsExactSpellingIsTrueForKnownApisAnalyzer.cs
Outdated
Show resolved
Hide resolved
return; | ||
} | ||
|
||
if (methods.Contains(methodName)) |
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.
@jeffhandley I think someone from the runtime team should review the core analyzer semantics in terms of the analyzer implementation matching the recommended API usage.
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.
It seems missing some scenarios which i commented here , but i would defer to @elinor-fung for final expected scenarios
@mavasani I integrated your formatting and spelling changes. The null checks you suggested are all redundant though, as we would check non-nullable value types for null. We have to check on their reference type properties, but these checks are already there |
...oft.NetCore.Analyzers/InteropServices/CSharpPreferIsExactSpellingIsTrueForKnownApis.Fixer.cs
Outdated
Show resolved
Hide resolved
...oft.NetCore.Analyzers/InteropServices/CSharpPreferIsExactSpellingIsTrueForKnownApis.Fixer.cs
Outdated
Show resolved
Hide resolved
...oft.NetCore.Analyzers/InteropServices/CSharpPreferIsExactSpellingIsTrueForKnownApis.Fixer.cs
Outdated
Show resolved
Hide resolved
...oft.NetCore.Analyzers/InteropServices/CSharpPreferIsExactSpellingIsTrueForKnownApis.Fixer.cs
Show resolved
Hide resolved
...oft.NetCore.Analyzers/InteropServices/CSharpPreferIsExactSpellingIsTrueForKnownApis.Fixer.cs
Outdated
Show resolved
Hide resolved
...crosoft.NetCore.Analyzers/InteropServices/PreferIsExactSpellingIsTrueForKnownApisAnalyzer.cs
Outdated
Show resolved
Hide resolved
...crosoft.NetCore.Analyzers/InteropServices/PreferIsExactSpellingIsTrueForKnownApisAnalyzer.cs
Outdated
Show resolved
Hide resolved
...crosoft.NetCore.Analyzers/InteropServices/PreferIsExactSpellingIsTrueForKnownApisAnalyzer.cs
Outdated
Show resolved
Hide resolved
...s/Microsoft.NetCore.Analyzers/InteropServices/PreferIsExactSpellingIsTrueForKnownApisTest.cs
Outdated
Show resolved
Hide resolved
src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/MicrosoftNetCoreAnalyzersResources.resx
Outdated
Show resolved
Hide resolved
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.
Please retarget this PR to https://github.com/dotnet/roslyn-analyzers/tree/release/6.0.1xx-preview1
{ | ||
return; | ||
} | ||
|
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.
if (isExactSpelling) // Bail out | |
return; | |
} |
if (isExactSpelling) //Bail out if known method has parameter set to true | ||
return; |
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.
if (isExactSpelling) //Bail out if known method has parameter set to true | |
return; |
return; | ||
} | ||
|
||
if (!isCharSetUnicode && !isExactSpelling && methodName.EndsWith("A", StringComparison.Ordinal)) |
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.
if (!isCharSetUnicode && !isExactSpelling && methodName.EndsWith("A", StringComparison.Ordinal)) | |
if (!isCharSetUnicode && methodName.EndsWith("A", StringComparison.Ordinal)) |
} | ||
} | ||
|
||
if (methods.Contains(methodName + "A") && !isCharSetUnicode) |
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.
I think this should be within the search condition above (row 138 if (methods.Contains(methodName))
) and seems it is missing scenarios with W
suffix @jkotas suggested :
When we see "Xxxx" in the entrypoint, the ExactSpelling autofixer needs to decide whether to suggest:
"XxxxW" + ExactSpelling=true. This should be suggested when we see "XxxxW" in the database.
"Xxxx" + ExactSpelling=true. This should be suggested when we see "Xxxx" and no "XxxxW" in the database
Leave the API alone. This should be the case when neither "Xxxx" nor "XxxxW" is in the database
- If
methodName
exist in DB (already covered withif (methods.Contains(methodName))
condition): - If
methodName + "W"
exist we suggest adding"W"
suffix and settingExactSpelling=true
- If
methodName + "W"
does not exist we suggest only settingExactSpelling=true
Probably we want to do same for "A" suffix but from from @elinor-fung's comment seems suggesting ExactSpelling=true
when CharSet=CharSet.Ansi
might not important or might not much helpful, if we want to cover it the rule should be same
- If
methodName + "A"
exist we suggest adding"A"
suffix and settingExactSpelling=true
- If
methodName + "A"
does not exist we suggest only settingExactSpelling=true
|
||
if (methods.Contains(methodName)) | ||
{ | ||
if (isCharSetUnicode && !isExactSpelling && methodName.EndsWith("W", StringComparison.Ordinal)) |
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.
!isExactSpelling
better to be moved before DB search ( before if (methods.Contains(methodName))
), here and all other occurrences below
if (isCharSetUnicode && !isExactSpelling && methodName.EndsWith("W", StringComparison.Ordinal)) | |
if (isCharSetUnicode && methodName.EndsWith("W", StringComparison.Ordinal)) |
PR needs to be retargeted to |
Closing this PR as there were no activity for a while, @Mrnikbobjeff feel free to reopen the PR after the feedbacks addressed and when its ready for review |
Implementation of #35695 the warning level was not explicitely stated as far as I can tell. I also extended the proposal to look at the EntryPoint parameter, as we would probably not want to flag
[DllImport("user32.dll", EntryPoint = "CallMsgFilterA", ExactSpelling = true)] public XXX CallMsgFilter(...)