-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
CyrusNajmabadi
merged 3 commits into
dotnet:main
from
CyrusNajmabadi:fieldClassification
Feb 13, 2025
Merged
Changes from all commits
Commits
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 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 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 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 |
---|---|---|
|
@@ -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; | ||
|
@@ -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; | ||
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. 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)) | ||
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. 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; | ||
} | ||
|
||
|
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.
view with whitespace off.