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

Classify string fields with embedded languages if we can see their values passed to a StringSyntax api #77199

Merged
merged 3 commits into from
Feb 13, 2025
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 @@ -1312,4 +1312,70 @@ void Goo()
Namespace("RegularExpressions"),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

view with whitespace off.

Keyword("var"));
}

[Theory, CombinatorialData]
[WorkItem("https://github.com/dotnet/roslyn/issues/77189")]
public async Task TestStringFieldUsedLater_ProperModifiers(
TestHost testHost,
[CombinatorialValues("const", "static readonly")] string modifiers)
{
await TestAsync(
$$"""
using System.Diagnostics.CodeAnalysis;
using System.Text.RegularExpressions;

class Program
{
private {{modifiers}} string regexValue = [|@"$(\a\t\u0020)"|];

void Goo()
{
Bar(regexValue);
}

void Bar([StringSyntax(StringSyntaxAttribute.Regex)] string p)
{
}
}
""" + EmbeddedLanguagesTestConstants.StringSyntaxAttributeCodeCSharp,
testHost,
Regex.Anchor("$"),
Regex.Grouping("("),
Regex.OtherEscape("\\"),
Regex.OtherEscape("a"),
Regex.OtherEscape("\\"),
Regex.OtherEscape("t"),
Regex.OtherEscape("\\"),
Regex.OtherEscape("u"),
Regex.OtherEscape("0020"),
Regex.Grouping(")"));
}

[Theory, CombinatorialData]
[WorkItem("https://github.com/dotnet/roslyn/issues/77189")]
public async Task TestStringFieldUsedLater_ImproperModifiers(
TestHost testHost,
[CombinatorialValues("", "static", "readonly")] string modifiers)
{
await TestAsync(
$$"""
using System.Diagnostics.CodeAnalysis;
using System.Text.RegularExpressions;

class Program
{
private {{modifiers}} string regexValue = [|@"$(\a\t\u0020)"|];

void Goo()
{
Bar(regexValue);
}

void Bar([StringSyntax(StringSyntaxAttribute.Regex)] string p)
{
}
}
""" + EmbeddedLanguagesTestConstants.StringSyntaxAttributeCodeCSharp,
testHost);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ internal abstract class AbstractLanguageDetector<TOptions>(
where TOptions : struct, Enum
{
protected readonly EmbeddedLanguageInfo Info = info;
protected readonly EmbeddedLanguageDetector Detector = new EmbeddedLanguageDetector(info, languageIdentifiers, commentDetector);
protected readonly EmbeddedLanguageDetector Detector = new(info, languageIdentifiers, commentDetector);

/// <summary>
/// Whether or not this is an argument to a well known api for this language (like Regex.Match or JToken.Parse).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,8 @@ private bool IsEmbeddedLanguageStringLiteralToken(
semanticModel.GetDeclaredSymbol(variableDeclarator, cancellationToken) ??
semanticModel.GetDeclaredSymbol(syntaxFacts.GetIdentifierOfVariableDeclarator(variableDeclarator).GetRequiredParent(), cancellationToken);

return IsLocalConsumedByApiWithStringSyntaxAttribute(symbol, container, semanticModel, cancellationToken, out identifier);
return IsLocalConsumedByApiWithStringSyntaxAttribute(symbol, container, semanticModel, cancellationToken, out identifier) ||
IsFieldConsumedByApiWithStringSyntaxAttribute(symbol, container, semanticModel, cancellationToken, out identifier);
}

return false;
Expand All @@ -336,46 +337,83 @@ private bool IsLocalConsumedByApiWithStringSyntaxAttribute(
[NotNullWhen(true)] out string? identifier)
{
identifier = null;
if (symbol is not ILocalSymbol localSymbol)
if (symbol is not ILocalSymbol { Name: not "" } localSymbol)
return false;

var blockFacts = this.Info.BlockFacts;
var syntaxFacts = this.Info.SyntaxFacts;

var block = tokenParent.AncestorsAndSelf().FirstOrDefault(blockFacts.IsExecutableBlock);
if (block is null)
return false;

var localName = localSymbol.Name;
if (localName == "")
return false;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pulled into pattern match above.


// Now look at the next statements that follow for usages of this local variable.
foreach (var statement in blockFacts.GetExecutableBlockStatements(block))
{
foreach (var descendent in statement.DescendantNodesAndSelf())
{
cancellationToken.ThrowIfCancellationRequested();
if (CheckDescendants(localSymbol, semanticModel, statement, cancellationToken, out identifier))
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

extracted into helper. we use it for the field case as well.

return true;
}

if (!syntaxFacts.IsIdentifierName(descendent))
continue;
return false;
}

var identifierToken = syntaxFacts.GetIdentifierOfIdentifierName(descendent);
if (identifierToken.ValueText != localName)
continue;
private bool IsFieldConsumedByApiWithStringSyntaxAttribute(
ISymbol? symbol,
SyntaxNode tokenParent,
SemanticModel semanticModel,
CancellationToken cancellationToken,
[NotNullWhen(true)] out string? identifier)
{
identifier = null;
if (symbol is not IFieldSymbol { Name: not "" } fieldSymbol)
return false;

var otherSymbol = semanticModel.GetSymbolInfo(descendent, cancellationToken).GetAnySymbol();
var isConst = fieldSymbol.IsConst;
var isStaticReadonly = fieldSymbol.IsStatic && fieldSymbol.IsReadOnly;
if (!isConst && !isStaticReadonly)
return false;

// Only do a direct check here. We don't want to continually do indirect checks where a string literal
// is assigned to one local, assigned to another local, assigned to another local, and so on.
if (localSymbol.Equals(otherSymbol) &&
IsEmbeddedLanguageStringLiteralToken_Direct(identifierToken, semanticModel, cancellationToken, out identifier))
{
return true;
}
var syntaxFacts = this.Info.SyntaxFacts;

var typeDeclaration = tokenParent.AncestorsAndSelf().FirstOrDefault(syntaxFacts.IsTypeDeclaration);
if (typeDeclaration is null)
return false;

return CheckDescendants(fieldSymbol, semanticModel, typeDeclaration, cancellationToken, out identifier);
}

private bool CheckDescendants(
ISymbol symbol,
SemanticModel semanticModel,
SyntaxNode node,
CancellationToken cancellationToken,
[NotNullWhen(true)] out string? identifier)
{
var symbolName = symbol.Name;
var syntaxFacts = this.Info.SyntaxFacts;

foreach (var descendent in node.DescendantNodesAndSelf())
{
cancellationToken.ThrowIfCancellationRequested();

if (!syntaxFacts.IsIdentifierName(descendent))
continue;

var identifierToken = syntaxFacts.GetIdentifierOfIdentifierName(descendent);
if (identifierToken.ValueText != symbolName)
continue;

var otherSymbol = semanticModel.GetSymbolInfo(descendent, cancellationToken).GetAnySymbol();

// Only do a direct check here. We don't want to continually do indirect checks where a string literal
// is assigned to one local, assigned to another local, assigned to another local, and so on.
if (symbol.Equals(otherSymbol) &&
IsEmbeddedLanguageStringLiteralToken_Direct(identifierToken, semanticModel, cancellationToken, out identifier))
{
return true;
}
}

identifier = null;
return false;
}

Expand Down
Loading