C#: Extract structs representing inline arrays as inline arrays.

This commit is contained in:
Michael Nebel
2024-01-15 15:57:02 +01:00
parent 5e692a882e
commit 83c16ae993
3 changed files with 20 additions and 4 deletions

View File

@@ -43,6 +43,9 @@ namespace Semmle.Extraction.CSharp.Entities.Expressions
public sealed override Microsoft.CodeAnalysis.Location? ReportingLocation => base.ReportingLocation;
private static bool IsArray(ITypeSymbol symbol) =>
symbol.TypeKind == Microsoft.CodeAnalysis.TypeKind.Array || symbol.IsInlineArray();
private static ExprKind GetKind(Context cx, ExpressionSyntax qualifier)
{
var qualifierType = cx.GetType(qualifier);
@@ -59,7 +62,7 @@ namespace Semmle.Extraction.CSharp.Entities.Expressions
return IsDynamic(cx, qualifier)
? ExprKind.DYNAMIC_ELEMENT_ACCESS
: qualifierType.Symbol.TypeKind == Microsoft.CodeAnalysis.TypeKind.Array
: IsArray(qualifierType.Symbol)
? ExprKind.ARRAY_ACCESS
: ExprKind.INDEXER_ACCESS;
}

View File

@@ -52,9 +52,15 @@ namespace Semmle.Extraction.CSharp.Entities
{
case TypeKind.Class: return Kinds.TypeKind.CLASS;
case TypeKind.Struct:
return ((INamedTypeSymbol)Symbol).IsTupleType && !constructUnderlyingTupleType
? Kinds.TypeKind.TUPLE
: Kinds.TypeKind.STRUCT;
{
if (((INamedTypeSymbol)Symbol).IsTupleType && !constructUnderlyingTupleType)
{
return Kinds.TypeKind.TUPLE;
}
return Symbol.IsInlineArray()
? Kinds.TypeKind.INLINE_ARRAY
: Kinds.TypeKind.STRUCT;
}
case TypeKind.Interface: return Kinds.TypeKind.INTERFACE;
case TypeKind.Array: return Kinds.TypeKind.ARRAY;
case TypeKind.Enum: return Kinds.TypeKind.ENUM;

View File

@@ -524,6 +524,13 @@ namespace Semmle.Extraction.CSharp
public static bool IsUnboundReadOnlySpan(this ITypeSymbol type) =>
type.ToString() == "System.ReadOnlySpan<T>";
public static bool IsInlineArray(this ITypeSymbol type)
{
var attributes = type.GetAttributes();
var isInline = attributes.Any(attribute => attribute.AttributeClass?.Name == "InlineArrayAttribute");
return isInline;
}
/// <summary>
/// Holds if this type is of the form <code>System.ReadOnlySpan<byte></code>.
/// </summary>