Skip to content

Commit

Permalink
Classify string fields with embedded languages if we can see their va…
Browse files Browse the repository at this point in the history
…lues passed to a StringSyntax api (#77199)

Fixes #77189
  • Loading branch information
CyrusNajmabadi authored Feb 13, 2025
2 parents ed675b4 + 4053035 commit 74f151e
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1312,4 +1312,70 @@ void Goo()
Namespace("RegularExpressions"),
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;

// 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))
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

0 comments on commit 74f151e

Please sign in to comment.