Skip to content

Commit 24855dd

Browse files
authored
Merge pull request #15328 from michaelnebel/csharp/inlinearrays
C# 12: Inline array support.
2 parents cb53ca4 + 43350b0 commit 24855dd

File tree

115 files changed

+10847
-2251
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

115 files changed

+10847
-2251
lines changed

csharp/downgrades/f595d31422d7d462d2bee8c69b44341df8bdadb6/old.dbscheme

Lines changed: 2078 additions & 0 deletions
Large diffs are not rendered by default.

csharp/downgrades/f595d31422d7d462d2bee8c69b44341df8bdadb6/semmlecode.csharp.dbscheme

Lines changed: 2077 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Type extends @type {
2+
string toString() { none() }
3+
}
4+
5+
from Type t, int k, int kind, string name
6+
where
7+
types(t, k, name) and
8+
if k = 34 then kind = 15 else kind = k
9+
select t, kind, name
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
description: Remove support for inline arrays.
2+
compatibility: backwards
3+
types.rel: run types.qlo

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/Kinds/TypeKind.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
namespace Semmle.Extraction.Kinds // lgtm[cs/similar-file]
1+
namespace Semmle.Extraction.Kinds
22
{
33
/// <summary>
44
/// This enum has been auto-generated from the C# DB scheme - do not edit.
5+
/// Auto-generate command: `genkindenum.pl type`
56
/// </summary>
67
public enum TypeKind
78
{
@@ -35,6 +36,7 @@ public enum TypeKind
3536
ARGLIST = 30,
3637
UNKNOWN = 31,
3738
TUPLE = 32,
38-
FUNCTION_POINTER = 33
39+
FUNCTION_POINTER = 33,
40+
INLINE_ARRAY = 34,
3941
}
4042
}

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,17 @@ 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 =>
531+
attribute.AttributeClass is INamedTypeSymbol nt &&
532+
nt.Name == "InlineArrayAttribute" &&
533+
nt.ContainingNamespace.ToString() == "System.Runtime.CompilerServices"
534+
);
535+
return isInline;
536+
}
537+
527538
/// <summary>
528539
/// Holds if this type is of the form <code>System.ReadOnlySpan<byte></code>.
529540
/// </summary>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
category: minorAnalysis
3+
---
4+
* C# 12: Add extractor support and QL library support for inline arrays.
File renamed without changes.

0 commit comments

Comments
 (0)