Skip to content

Commit 83c16ae

Browse files
committed
C#: Extract structs representing inline arrays as inline arrays.
1 parent 5e692a8 commit 83c16ae

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/ElementAccess.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ protected override void PopulateExpression(TextWriter trapFile)
4343

4444
public sealed override Microsoft.CodeAnalysis.Location? ReportingLocation => base.ReportingLocation;
4545

46+
private static bool IsArray(ITypeSymbol symbol) =>
47+
symbol.TypeKind == Microsoft.CodeAnalysis.TypeKind.Array || symbol.IsInlineArray();
48+
4649
private static ExprKind GetKind(Context cx, ExpressionSyntax qualifier)
4750
{
4851
var qualifierType = cx.GetType(qualifier);
@@ -59,7 +62,7 @@ private static ExprKind GetKind(Context cx, ExpressionSyntax qualifier)
5962

6063
return IsDynamic(cx, qualifier)
6164
? ExprKind.DYNAMIC_ELEMENT_ACCESS
62-
: qualifierType.Symbol.TypeKind == Microsoft.CodeAnalysis.TypeKind.Array
65+
: IsArray(qualifierType.Symbol)
6366
? ExprKind.ARRAY_ACCESS
6467
: ExprKind.INDEXER_ACCESS;
6568
}

csharp/extractor/Semmle.Extraction.CSharp/Entities/Types/Type.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,15 @@ public Kinds.TypeKind GetTypeKind(Context cx, bool constructUnderlyingTupleType)
5252
{
5353
case TypeKind.Class: return Kinds.TypeKind.CLASS;
5454
case TypeKind.Struct:
55-
return ((INamedTypeSymbol)Symbol).IsTupleType && !constructUnderlyingTupleType
56-
? Kinds.TypeKind.TUPLE
57-
: Kinds.TypeKind.STRUCT;
55+
{
56+
if (((INamedTypeSymbol)Symbol).IsTupleType && !constructUnderlyingTupleType)
57+
{
58+
return Kinds.TypeKind.TUPLE;
59+
}
60+
return Symbol.IsInlineArray()
61+
? Kinds.TypeKind.INLINE_ARRAY
62+
: Kinds.TypeKind.STRUCT;
63+
}
5864
case TypeKind.Interface: return Kinds.TypeKind.INTERFACE;
5965
case TypeKind.Array: return Kinds.TypeKind.ARRAY;
6066
case TypeKind.Enum: return Kinds.TypeKind.ENUM;

csharp/extractor/Semmle.Extraction.CSharp/SymbolExtensions.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,13 @@ public static bool IsBoundSpan(this ITypeSymbol type) =>
524524
public static bool IsUnboundReadOnlySpan(this ITypeSymbol type) =>
525525
type.ToString() == "System.ReadOnlySpan<T>";
526526

527+
public static bool IsInlineArray(this ITypeSymbol type)
528+
{
529+
var attributes = type.GetAttributes();
530+
var isInline = attributes.Any(attribute => attribute.AttributeClass?.Name == "InlineArrayAttribute");
531+
return isInline;
532+
}
533+
527534
/// <summary>
528535
/// Holds if this type is of the form <code>System.ReadOnlySpan<byte></code>.
529536
/// </summary>

0 commit comments

Comments
 (0)