Skip to content
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 @@ -152,8 +152,8 @@ static bool NameAndParametersValid(IMethodSymbol method) =>

private static void AnalyzeOperation(IOperation operation, OperationAnalysisContext context, PooledDictionary<string, int> knownPlatforms)
{
if (operation is IInvocationOperation invocation &&
methodNames.Contains(invocation.TargetMethod.Name) &&
var invocation = (IInvocationOperation)operation;
if (methodNames.Contains(invocation.TargetMethod.Name) &&
invocation.Arguments.Length > 0 &&
invocation.Arguments[0].Value is { } argument &&
argument.ConstantValue.HasValue &&
Expand Down
25 changes: 25 additions & 0 deletions src/Utilities/Compiler/Extensions/ISymbolExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,31 @@ public static IEnumerable<AttributeData> GetAttributes(this ISymbol symbol, para
return symbol.GetAttributes(attributesToMatch: attributeTypesToMatch);
}

#region "Overloads to avoid array allocations"
public static IEnumerable<AttributeData> GetAttributes(this ISymbol symbol, INamedTypeSymbol? attributeTypeToMatch1)
{
foreach (var attribute in symbol.GetAttributes())
{
if (SymbolEqualityComparer.Default.Equals(attribute.AttributeClass, attributeTypeToMatch1))
{
yield return attribute;
}
}
}

public static IEnumerable<AttributeData> GetAttributes(this ISymbol symbol, INamedTypeSymbol? attributeTypeToMatch1, INamedTypeSymbol? attributeTypeToMatch2)
Copy link
Member Author

Choose a reason for hiding this comment

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

Note: This is called for every named type, method, property, field, and event in the compilation. So using the params overload was likely inefficient.

An alternative is that the analyzer would pre-allocate a single array at compilation start, but I went with these overloads so that other analyzers could benefit as well.

{
foreach (var attribute in symbol.GetAttributes())
{
if (SymbolEqualityComparer.Default.Equals(attribute.AttributeClass, attributeTypeToMatch1) ||
SymbolEqualityComparer.Default.Equals(attribute.AttributeClass, attributeTypeToMatch2))
{
yield return attribute;
}
}
}
#endregion

public static bool HasAnyAttribute(this ISymbol symbol, IEnumerable<INamedTypeSymbol> attributesToMatch)
{
return symbol.GetAttributes(attributesToMatch).Any();
Expand Down