Skip to content
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

Allocations: Move stringliteral tokens array to static field #7663

Merged
merged 4 commits into from
Jul 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ namespace SonarAnalyzer.Extensions
internal static partial class SyntaxNodeExtensions
{
private static readonly ControlFlowGraphCache CfgCache = new();
private static readonly SyntaxKind[] ParenthesizedNodeKinds = new[] { SyntaxKind.ParenthesizedExpression, SyntaxKindEx.ParenthesizedPattern };

public static ControlFlowGraph CreateCfg(this SyntaxNode body, SemanticModel model, CancellationToken cancel) =>
CfgCache.FindOrCreate(body is CompilationUnitSyntax ? body : body.Parent, model, cancel);
Expand Down Expand Up @@ -116,7 +117,7 @@ public static ArrowExpressionClauseSyntax ArrowExpressionBody(this SyntaxNode no
public static SyntaxNode RemoveParentheses(this SyntaxNode expression)
{
var current = expression;
while (current is { } && current.IsAnyKind(SyntaxKind.ParenthesizedExpression, SyntaxKindEx.ParenthesizedPattern))
while (current is { } && current.IsAnyKind(ParenthesizedNodeKinds))
{
current = current.IsKind(SyntaxKindEx.ParenthesizedPattern)
? ((ParenthesizedPatternSyntaxWrapper)current).Pattern
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,24 @@ protected override TriviaClassifierBase GetTriviaClassifier() =>

private sealed class TokenClassifier : TokenClassifierBase
{
private static readonly SyntaxKind[] StringLiteralTokens = new[]
{
SyntaxKind.StringLiteralToken,
SyntaxKind.CharacterLiteralToken,
SyntaxKindEx.SingleLineRawStringLiteralToken,
SyntaxKindEx.MultiLineRawStringLiteralToken,
SyntaxKindEx.Utf8StringLiteralToken,
SyntaxKindEx.Utf8SingleLineRawStringLiteralToken,
SyntaxKindEx.Utf8MultiLineRawStringLiteralToken,
SyntaxKind.InterpolatedStringStartToken,
SyntaxKind.InterpolatedVerbatimStringStartToken,
SyntaxKindEx.InterpolatedSingleLineRawStringStartToken,
SyntaxKindEx.InterpolatedMultiLineRawStringStartToken,
SyntaxKind.InterpolatedStringTextToken,
SyntaxKind.InterpolatedStringEndToken,
SyntaxKindEx.InterpolatedRawStringEndToken,
};

public TokenClassifier(SemanticModel semanticModel, bool skipIdentifiers) : base(semanticModel, skipIdentifiers) { }

protected override SyntaxNode GetBindableParent(SyntaxToken token) =>
Expand All @@ -48,30 +66,28 @@ protected override bool IsNumericLiteral(SyntaxToken token) =>
token.IsKind(SyntaxKind.NumericLiteralToken);

protected override bool IsStringLiteral(SyntaxToken token) =>
token.IsAnyKind(
SyntaxKind.StringLiteralToken,
SyntaxKind.CharacterLiteralToken,
SyntaxKindEx.SingleLineRawStringLiteralToken,
SyntaxKindEx.MultiLineRawStringLiteralToken,
SyntaxKindEx.Utf8StringLiteralToken,
SyntaxKindEx.Utf8SingleLineRawStringLiteralToken,
SyntaxKindEx.Utf8MultiLineRawStringLiteralToken,
SyntaxKind.InterpolatedStringStartToken,
SyntaxKind.InterpolatedVerbatimStringStartToken,
SyntaxKindEx.InterpolatedSingleLineRawStringStartToken,
SyntaxKindEx.InterpolatedMultiLineRawStringStartToken,
SyntaxKind.InterpolatedStringTextToken,
SyntaxKind.InterpolatedStringEndToken,
SyntaxKindEx.InterpolatedRawStringEndToken);
token.IsAnyKind(StringLiteralTokens);
}

private sealed class TriviaClassifier : TriviaClassifierBase
{
private static readonly SyntaxKind[] RegularCommentToken = new[]
{
SyntaxKind.SingleLineCommentTrivia,
SyntaxKind.MultiLineCommentTrivia,
};

private static readonly SyntaxKind[] DocCommentToken = new[]
{
SyntaxKind.SingleLineDocumentationCommentTrivia,
SyntaxKind.MultiLineDocumentationCommentTrivia,
};

protected override bool IsRegularComment(SyntaxTrivia trivia) =>
trivia.IsAnyKind(SyntaxKind.SingleLineCommentTrivia, SyntaxKind.MultiLineCommentTrivia);
trivia.IsAnyKind(RegularCommentToken);

protected override bool IsDocComment(SyntaxTrivia trivia) =>
trivia.IsAnyKind(SyntaxKind.SingleLineDocumentationCommentTrivia, SyntaxKind.MultiLineDocumentationCommentTrivia);
trivia.IsAnyKind(DocCommentToken);
}
}
}