-
Notifications
You must be signed in to change notification settings - Fork 128
Lambda and local function suppressions #2689
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
Merged
sbomer
merged 21 commits into
dotnet:main
from
sbomer:lambdaAndLocalFunctionSuppressions
Mar 30, 2022
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
e0ec141
Detect lambdas for suppressions
sbomer 9f4e410
Detect local functions for suppressions
sbomer fc17e5c
Clean up tests and add lambda suppression tests
sbomer df7e531
Respect attribute on outer method in analyzer
sbomer 7bb1d64
Don't early exit for lambdas and local functions
sbomer 6a3e9f5
Also find local functions in display classes
sbomer afd92f3
Check owning member for suppressions recursively
sbomer 3b4871b
Prototype: track calls to lambdas, local functions
sbomer 5904761
Don't fail for duplicate methods for lambda
sbomer 74cfb28
Fix testcase
sbomer 994cc86
Add tests for unused lambdas and local functions
sbomer bc61bc3
Implement call graph discovery of nested functions
sbomer fd38b07
Adjust tests for analyzer
sbomer 4634dcd
Scan nested state machines
sbomer db9398e
Add more tests for nested functions
sbomer 2453b08
Check for suppressions on reflection access
sbomer 8719341
Fix formatting
sbomer 8cf7af2
Remove comment
sbomer d76a3fe
PR feedback
sbomer 4ce04bc
PR feedback
sbomer d90a22a
PR feedback
sbomer 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
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,43 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.Collections.Generic; | ||
| using Mono.Cecil; | ||
|
|
||
| namespace Mono.Linker | ||
| { | ||
| class CallGraph | ||
| { | ||
| readonly Dictionary<MethodDefinition, HashSet<MethodDefinition>> callGraph; | ||
|
|
||
| public CallGraph () => callGraph = new Dictionary<MethodDefinition, HashSet<MethodDefinition>> (); | ||
|
|
||
| public void TrackCall (MethodDefinition fromMethod, MethodDefinition toMethod) | ||
| { | ||
| if (!callGraph.TryGetValue (fromMethod, out HashSet<MethodDefinition>? toMethods)) { | ||
| toMethods = new HashSet<MethodDefinition> (); | ||
| callGraph.Add (fromMethod, toMethods); | ||
| } | ||
| toMethods.Add (toMethod); | ||
| } | ||
|
|
||
| public IEnumerable<MethodDefinition> GetReachableMethods (MethodDefinition start) | ||
| { | ||
| Queue<MethodDefinition> queue = new (); | ||
| HashSet<MethodDefinition> visited = new (); | ||
| visited.Add (start); | ||
| queue.Enqueue (start); | ||
| while (queue.TryDequeue (out MethodDefinition? method)) { | ||
| if (!callGraph.TryGetValue (method, out HashSet<MethodDefinition>? callees)) | ||
| continue; | ||
|
|
||
| foreach (var callee in callees) { | ||
| if (visited.Add (callee)) { | ||
| queue.Enqueue (callee); | ||
| yield return callee; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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,56 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| namespace Mono.Linker | ||
| { | ||
| class CompilerGeneratedNames | ||
| { | ||
| internal static bool IsGeneratedMemberName (string memberName) | ||
| { | ||
| return memberName.Length > 0 && memberName[0] == '<'; | ||
| } | ||
|
|
||
| internal static bool IsLambdaDisplayClass (string className) | ||
| { | ||
| if (!IsGeneratedMemberName (className)) | ||
| return false; | ||
|
|
||
| // This is true for static lambdas (which are emitted into a class like <>c) | ||
| // and for instance lambdas (which are emitted into a class like <>c__DisplayClass1_0) | ||
| return className.StartsWith ("<>c"); | ||
| } | ||
|
|
||
| internal static bool IsLambdaOrLocalFunction (string methodName) => IsLambdaMethod (methodName) || IsLocalFunction (methodName); | ||
|
|
||
| // Lambda methods have generated names like "<UserMethod>b__0_1" where "UserMethod" is the name | ||
| // of the original user code that contains the lambda method declaration. | ||
sbomer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| internal static bool IsLambdaMethod (string methodName) | ||
| { | ||
| if (!IsGeneratedMemberName (methodName)) | ||
| return false; | ||
|
|
||
| int i = methodName.IndexOf ('>', 1); | ||
| if (i == -1) | ||
| return false; | ||
|
|
||
| // Ignore the method ordinal/generation and lambda ordinal/generation. | ||
| return methodName[i + 1] == 'b'; | ||
| } | ||
|
|
||
| // Local functions have generated names like "<UserMethod>g__LocalFunction|0_1" where "UserMethod" is the name | ||
| // of the original user code that contains the lambda method declaration, and "LocalFunction" is the name of | ||
| // the local function. | ||
| internal static bool IsLocalFunction (string methodName) | ||
| { | ||
| if (!IsGeneratedMemberName (methodName)) | ||
| return false; | ||
|
|
||
| int i = methodName.IndexOf ('>', 1); | ||
| if (i == -1) | ||
| return false; | ||
|
|
||
| // Ignore the method ordinal/generation and local function ordinal/generation. | ||
| return methodName[i + 1] == 'g'; | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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
Oops, something went wrong.
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.
Is there a reason we don't want this while loop in
IsMethodInRequiresUnreferencedCodeScope?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 considered it, but think eventually we might want to pull this out so that it applies not only to methods but also to fields and other members - so I left it here for now, and when I add dataflow for nested functions I'll see what makes more sense.