-
Notifications
You must be signed in to change notification settings - Fork 485
Reduce excessive allocations in BannedSymbols analyzer. #6568
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
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
04c0545
Add parser
CyrusNajmabadi ea39a94
Defer work
CyrusNajmabadi 8db6370
ordering
CyrusNajmabadi a97be8b
Trimming
CyrusNajmabadi 23bc391
Cancellation
CyrusNajmabadi e692634
Simplify
CyrusNajmabadi 4c6dee7
Simplify
CyrusNajmabadi b994b17
Add docs
CyrusNajmabadi 39a5df0
Add docs
CyrusNajmabadi 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
108 changes: 108 additions & 0 deletions
108
src/Microsoft.CodeAnalysis.BannedApiAnalyzers/Core/DocumentationCommentIdParser.cs
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,108 @@ | ||
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the MIT license. See License.txt in the project root for license information. | ||
|
||
namespace Microsoft.CodeAnalysis.BannedApiAnalyzers | ||
{ | ||
/// <summary> | ||
/// Stripped down port of the code in roslyn. Responsible only for determining the <see cref="ISymbol.Name"/> and | ||
/// name of the <see cref="ISymbol.ContainingSymbol"/> for a given xml doc comment symbol id. | ||
/// </summary> | ||
internal static class DocumentationCommentIdParser | ||
{ | ||
private static readonly char[] s_nameDelimiters = { ':', '.', '(', ')', '{', '}', '[', ']', ',', '\'', '@', '*', '`', '~' }; | ||
|
||
public static (string ParentName, string SymbolName)? ParseDeclaredSymbolId(string id) | ||
{ | ||
if (id == null) | ||
return null; | ||
|
||
if (id.Length < 2) | ||
return null; | ||
|
||
int index = 0; | ||
return ParseDeclaredId(id, ref index); | ||
} | ||
|
||
private static (string ParentName, string SymbolName)? ParseDeclaredId(string id, ref int index) | ||
{ | ||
var kindChar = PeekNextChar(id, index); | ||
|
||
switch (kindChar) | ||
{ | ||
case 'E': // Events | ||
case 'F': // Fields | ||
case 'M': // Methods | ||
case 'P': // Properties | ||
case 'T': // Types | ||
break; | ||
case 'N': // Namespaces | ||
default: | ||
// Documentation comment id must start with E, F, M, N, P or T. Note: we don't support banning full | ||
// namespaces, so we bail in that case as well. | ||
return null; | ||
} | ||
|
||
index++; | ||
if (PeekNextChar(id, index) == ':') | ||
index++; | ||
|
||
string parentName = ""; | ||
|
||
// process dotted names | ||
while (true) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. honestly, this could be a lot better. HOwever, i don't care much simple the logic is:
|
||
{ | ||
var symbolName = ParseName(id, ref index); | ||
|
||
// has type parameters? | ||
if (PeekNextChar(id, index) == '`') | ||
{ | ||
index++; | ||
|
||
// method type parameters? | ||
if (PeekNextChar(id, index) == '`') | ||
index++; | ||
|
||
ReadNextInteger(id, ref index); | ||
} | ||
|
||
if (PeekNextChar(id, index) == '.') | ||
{ | ||
index++; | ||
parentName = symbolName; | ||
continue; | ||
} | ||
else | ||
{ | ||
return (parentName, symbolName); | ||
} | ||
} | ||
} | ||
|
||
private static char PeekNextChar(string id, int index) | ||
=> index >= id.Length ? '\0' : id[index]; | ||
|
||
private static string ParseName(string id, ref int index) | ||
{ | ||
string name; | ||
|
||
int delimiterOffset = id.IndexOfAny(s_nameDelimiters, index); | ||
if (delimiterOffset >= 0) | ||
{ | ||
name = id[index..delimiterOffset]; | ||
index = delimiterOffset; | ||
} | ||
else | ||
{ | ||
name = id[index..]; | ||
index = id.Length; | ||
} | ||
|
||
return name.Replace('#', '.'); | ||
} | ||
|
||
private static void ReadNextInteger(string id, ref int index) | ||
{ | ||
while (index < id.Length && char.IsDigit(id[index])) | ||
index++; | ||
} | ||
} | ||
} |
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.
copied from https://github.com/dotnet/roslyn/blob/45128e6b4cf49d63c734877c9552d6764831b9b7/src/Compilers/Core/Portable/DocumentationCommentId.cs#L691 with everything extraneous cut out.